mit-krb5 thread support: initialization and Windows

Ken Raeburn raeburn at MIT.EDU
Tue Apr 6 10:27:49 EDT 2004


I wrote:
> A second function or macro called at various possible "first" entry
> points which either calls pthread_once on the first function (POSIX),
> or checks some flag set by the first function (Windows, debug
> support), and possibly returns an error.  (In the non-threaded case, a
> simple flag can be used to avoid multiple invocations, and the mutexes
> don't need initialization anyways.)

The thread-system-specific bits of this shouldn't be spread around
into the various libraries.  Another small bit of support code would
help make the per-library code cleaner, something modelled on
pthread_once, probably something like:

   k5_once_t foo_once = K5_ONCE_INIT;
   void k5_once(k5_once_t *, void (*)(void));

   For POSIX: Map onto pthread_once facility.
   For Windows: More complicated, but Jeff Altman has given me some
     ideas and code.
   For non-threaded case: A simple flag.

With a wrapper around this, the real initialization function can be
invoked exactly once, and any resulting error code can be stored in an
additional variable.  Or perhaps the handling of such error codes
should also be part of the support code?

   /* POSIX version */
   typedef struct { pthread_once_t o; int err; } k5_once_t;
   /* k5_once returns int */
   #define k5_once(O,F) (pthread_once(&(O)->o,F), (O)->err)

   k5_once_t foo_once = K5_ONCE_INIT;
   void foo_initfn() { foo_once.err = ...; }

   #define init_foo_lib() (k5_once(&foo_once, foo_initfn))

I don't anticipate any cases where we'll want the "once" functionality
without being able to check for errors from whatever it is we're
supposed to have run, unless we make the initialization function
actually quit the program on any error, and I don't think we want
that.

The Windows versions of this would probably call the init function and
set a flag and error-code variable in the library initialization code,
and the k5_once calls in the main library code would simply check for
the error code and return it, or a specific error if the flag
indicating the initializer had run did not get set.

Ken


More information about the krbdev mailing list