0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark AndrewsCopyright (C) 1999-2001, 2004, 2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark AndrewsThis Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark AndrewsLicense, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrewsfile, You can obtain one at http://mozilla.org/MPL/2.0/.
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews$Id: results,v 1.7 2004/03/05 05:04:50 marka Exp $
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyResult Codes
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyThe use of global variables or a GetLastError() function to return results
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleydoesn't work well in a multithreaded application. The global variable has
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyobvious problems, as does a global GetLastError(). A per-object GetLastError()
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyseems more promising, e.g.
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley sometype_t s;
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley sometype_dosomething(s, buffer);
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley if (sometype_error(s)) {
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley /* ... */
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley }
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyIf 's' is shared however this approach doesn't work unless the locking is
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleydone by the caller, e.g.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley sometype_lock();
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley sometype_dosomething(s, buffer);
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley if (sometype_error(s)) {
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley /* ... */
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley }
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley sometype_unlock();
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyThose ISC and DNS libraries which have locks almost universally put the
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleylocking inside of the called routines, since it's more convenient for
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleythe calling programmer, makes for a cleaner API, and puts the burden
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyof locking on the library programmer, who should know best what the
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleylocking needs of the routine are.
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyBecause of this locking style the ISC and DNS libraries typically provide
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyresult information as the return value of the function. E.g.
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley isc_result_t result;
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley result = isc_task_send(task, &event);
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyNote that an explicit result type is used, instead of mixing the error result
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleytype with the normal result type. E.g. the C library routine getc() can
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyreturn a character or EOF, but the BIND 9 style keeps the types of the
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyfunction's return values separate.
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley char c;
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley result = isc_io_getc(stream, &c);
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley if (result == ISC_R_SUCCESS) {
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley /* Do something with 'c'. */
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley } else if (result == ISC_R_EOF) {
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley /* EOF. */
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley } else {
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley /* Some other error. */
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley }
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halley
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob HalleyFunctions which cannot fail (assuming the caller has provided valid
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyarguments) need not return a result type. For example, dns_name_issubdomain()
767d29c43d98bae8ea95f0ccd2b9653cbcd43310Bob Halleyreturns an isc_boolean_t, because it cannot fail.