Blame doc/threads.txt

Packit fd8b60
Thread safety in the MIT Kerberos libraries
Packit fd8b60
Packit fd8b60
The return value from krb5_cc_default_name is a handle on internal
Packit fd8b60
storage from the krb5_context.  It is valid only until
Packit fd8b60
krb5_cc_set_default_name or krb5_free_context is called.  If
Packit fd8b60
krb5_cc_set_default_name may be called, the calling code must ensure
Packit fd8b60
that the storage returned by krb5_cc_default_name is no longer in use
Packit fd8b60
by that time.
Packit fd8b60
Packit fd8b60
Any use of krb5_context must be confined to one thread at a time by
Packit fd8b60
the application code.
Packit fd8b60
Packit fd8b60
Uses of credentials caches, replay caches, and keytabs may happen in
Packit fd8b60
multiple threads simultaneously as long as none of them destroys the
Packit fd8b60
object while other threads may still be using it.  (Any internal data
Packit fd8b60
modification in those objects will be protected by mutexes or other
Packit fd8b60
means, within the krb5 library.)
Packit fd8b60
Packit fd8b60
The simple, exposed data structures in krb5.h like krb5_principal are
Packit fd8b60
not protected; they should not be used in one thread while another
Packit fd8b60
thread might be modifying them.  (TO DO: Build a list of which calls
Packit fd8b60
keep references to supplied data or return references to
Packit fd8b60
otherwise-referenced data, as opposed to everything making copies.)
Packit fd8b60
Packit fd8b60
Packit fd8b60
Packit fd8b60
[ This part is a little outdated already. ]
Packit fd8b60
Packit fd8b60
   // Between these two, we should be able to do pure compile-time
Packit fd8b60
   // and pure run-time initialization.
Packit fd8b60
   //   POSIX: partial initializer is PTHREAD_MUTEX_INITIALIZER,
Packit fd8b60
   //          finish does nothing
Packit fd8b60
   //   Windows: partial initializer is zero/empty,
Packit fd8b60
   //            finish does the actual work and runs at load time
Packit fd8b60
   //   debug: partial initializer sets one magic value,
Packit fd8b60
   //          finish verifies, sets a new magic value
Packit fd8b60
   k5_mutex_t foo_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
Packit fd8b60
   int k5_mutex_finish_init(k5_mutex_t *);
Packit fd8b60
   // for dynamic allocation
Packit fd8b60
   int k5_mutex_init(k5_mutex_t *);
Packit fd8b60
   // Must work for both kinds of allocation, even if it means adding
Packit fd8b60
   // a flag.
Packit fd8b60
   int k5_mutex_destroy(k5_mutex_t *);
Packit fd8b60
   //
Packit fd8b60
   // Per library, one function to finish the static mutex
Packit fd8b60
   // initialization.
Packit fd8b60
   //
Packit fd8b60
   // A second function called at various possible "first" entry
Packit fd8b60
   // points which either calls pthread_once on the first function
Packit fd8b60
   // (POSIX), or checks some flag set by the first function (Windows,
Packit fd8b60
   // debug support), and possibly returns an error.
Packit fd8b60
   //
Packit fd8b60
   // A third function for library termination calls mutex_destroy on
Packit fd8b60
   // each mutex for the library.
Packit fd8b60
   //
Packit fd8b60
   // 
Packit fd8b60
   int k5_mutex_lock(k5_mutex_t *);
Packit fd8b60
   int k5_mutex_unlock(k5_mutex_t *);
Packit fd8b60
   // Optional (always defined, but need not do anything):
Packit fd8b60
   void k5_mutex_assert_locked(k5_mutex_t *);
Packit fd8b60
   void k5_mutex_assert_unlocked(k5_mutex_t *);
Packit fd8b60
Packit fd8b60
Packit fd8b60
   k5_key_t key;
Packit fd8b60
   int k5_key_create(k5_key_t *, void (*destructor)(void *));
Packit fd8b60
   void *k5_getspecific(k5_key_t);
Packit fd8b60
   int k5_setspecific(k5_key_t, const void *);
Packit fd8b60
   ... stuff to signal library termination ...
Packit fd8b60
Packit fd8b60
This is **NOT** an exported interface, and is subject to change.
Packit fd8b60
Packit fd8b60
On many platforms with weak reference support, we can declare certain
Packit fd8b60
symbols to be weak, and test the addresses before calling them.  The
Packit fd8b60
references generally will be non-null if the application pulls in the
Packit fd8b60
pthread support.  Sometimes stubs are present in the C library for
Packit fd8b60
some of these routines, and sometimes they're not functional; if so,
Packit fd8b60
we need to figure out which ones, and check for the presence of some
Packit fd8b60
*other* routines.
Packit fd8b60
Packit fd8b60
AIX 4.3.3 doesn't support weak references.  However, it looks like
Packit fd8b60
calling dlsym(NULL) causes the pthread library to get loaded, so we're
Packit fd8b60
going to just go ahead and link against it anyways.
Packit fd8b60
Packit fd8b60
On Tru64 we also link against the thread library always.
Packit fd8b60
Packit fd8b60
Packit fd8b60
For now, the basic model is:
Packit fd8b60
Packit fd8b60
  If weak references are supported, use them.
Packit fd8b60
  Else, assume support is present; if that means explicitly pulling in
Packit fd8b60
  the thread library, so be it.
Packit fd8b60
Packit fd8b60
Packit fd8b60
Packit fd8b60
The locking described above may not be sufficient, at least for good
Packit fd8b60
performance.  At some point we may want to switch to read/write locks,
Packit fd8b60
so multiple threads can grovel over a data structure at once as long
Packit fd8b60
as they don't change it.
Packit fd8b60
Packit fd8b60
Packit fd8b60
See also notes in src/include/k5-thread.h.