diff options
author | Greg Hudson <ghudson@mit.edu> | 2009-11-14 18:56:43 +0000 |
---|---|---|
committer | Greg Hudson <ghudson@mit.edu> | 2009-11-14 18:56:43 +0000 |
commit | be4e9d973c2717872663010c9c02f8bc75cb18a5 (patch) | |
tree | 7e9080437aebd7807d08a27ac454a3144ef5ec1d /src/include/k5-thread.h | |
parent | 8852050c9361822bd182634ee2ce833d4d9f14e8 (diff) | |
download | krb5-be4e9d973c2717872663010c9c02f8bc75cb18a5.tar.gz krb5-be4e9d973c2717872663010c9c02f8bc75cb18a5.tar.xz krb5-be4e9d973c2717872663010c9c02f8bc75cb18a5.zip |
Reindent include directory, reformatting prototypes as necessary.
Exclude include/gssrpc due to its Sun origin and k5-platform.h due to
macros too hairy for emacs c-mode to handle.
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@23180 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/include/k5-thread.h')
-rw-r--r-- | src/include/k5-thread.h | 327 |
1 files changed, 164 insertions, 163 deletions
diff --git a/src/include/k5-thread.h b/src/include/k5-thread.h index 069b51c74..5f0325c5e 100644 --- a/src/include/k5-thread.h +++ b/src/include/k5-thread.h @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * include/k5-thread.h * @@ -40,101 +41,101 @@ /* Interface (tentative): - Mutex support: - - // Between these two, we should be able to do pure compile-time - // and pure run-time initialization. - // POSIX: partial initializer is PTHREAD_MUTEX_INITIALIZER, - // finish does nothing - // Windows: partial initializer is an invalid handle, - // finish does the real initialization work - k5_mutex_t foo_mutex = K5_MUTEX_PARTIAL_INITIALIZER; - int k5_mutex_finish_init(k5_mutex_t *); - // for dynamic allocation - int k5_mutex_init(k5_mutex_t *); - // Must work for both kinds of alloc, even if it means adding flags. - int k5_mutex_destroy(k5_mutex_t *); - - // As before. - int k5_mutex_lock(k5_mutex_t *); - int k5_mutex_unlock(k5_mutex_t *); - - In each library, one new function to finish the static mutex init, - and any other library-wide initialization that might be desired. - On POSIX, this function would be called via the second support - function (see below). On Windows, it would be called at library - load time. These functions, or functions they calls, should be the - only places that k5_mutex_finish_init gets called. - - 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), - 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 run-time initialization anyways.) - - A third function for library termination calls mutex_destroy on - each mutex for the library. This function would be called - automatically at library unload time. If it turns out to be needed - at exit time for libraries that don't get unloaded, perhaps we - should also use atexit(). Any static mutexes should be cleaned up - with k5_mutex_destroy here. - - How does that second support function invoke the first support - function only once? Through something modelled on pthread_once - that I haven't written up yet. Probably: - - k5_once_t foo_once = K5_ONCE_INIT; - k5_once(k5_once_t *, void (*)(void)); - - For POSIX: Map onto pthread_once facility. - For non-threaded case: A simple flag. - For Windows: Not needed; library init code takes care of it. - - XXX: A general k5_once mechanism isn't possible for Windows, - without faking it through named mutexes or mutexes initialized at - startup. I was only using it in one place outside these headers, - so I'm dropping the general scheme. Eventually the existing uses - in k5-thread.h and k5-platform.h will be converted to pthread_once - or static variables. - - - Thread-specific data: - - // TSD keys are limited in number in gssapi/krb5/com_err; enumerate - // them all. This allows support code init to allocate the - // necessary storage for pointers all at once, and avoids any - // possible error in key creation. - enum { ... } k5_key_t; - // Register destructor function. Called in library init code. - int k5_key_register(k5_key_t, void (*destructor)(void *)); - // Returns NULL or data. - void *k5_getspecific(k5_key_t); - // Returns error if key out of bounds, or the pointer table can't - // be allocated. A call to k5_key_register must have happened first. - // This may trigger the calling of pthread_setspecific on POSIX. - int k5_setspecific(k5_key_t, void *); - // Called in library termination code. - // Trashes data in all threads, calling the registered destructor - // (but calling it from the current thread). - int k5_key_delete(k5_key_t); - - For the non-threaded version, the support code will have a static - array indexed by k5_key_t values, and get/setspecific simply access - the array elements. - - The TSD destructor table is global state, protected by a mutex if - threads are enabled. - - - Any actual external symbols will use the krb5int_ prefix. The k5_ - names will be simple macros or inline functions to rename the - external symbols, or slightly more complex ones to expand the - implementation inline (e.g., map to POSIX versions and/or debug - code using __FILE__ and the like). - - - More to be added, perhaps. */ + Mutex support: + + // Between these two, we should be able to do pure compile-time + // and pure run-time initialization. + // POSIX: partial initializer is PTHREAD_MUTEX_INITIALIZER, + // finish does nothing + // Windows: partial initializer is an invalid handle, + // finish does the real initialization work + k5_mutex_t foo_mutex = K5_MUTEX_PARTIAL_INITIALIZER; + int k5_mutex_finish_init(k5_mutex_t *); + // for dynamic allocation + int k5_mutex_init(k5_mutex_t *); + // Must work for both kinds of alloc, even if it means adding flags. + int k5_mutex_destroy(k5_mutex_t *); + + // As before. + int k5_mutex_lock(k5_mutex_t *); + int k5_mutex_unlock(k5_mutex_t *); + + In each library, one new function to finish the static mutex init, + and any other library-wide initialization that might be desired. + On POSIX, this function would be called via the second support + function (see below). On Windows, it would be called at library + load time. These functions, or functions they calls, should be the + only places that k5_mutex_finish_init gets called. + + 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), + 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 run-time initialization anyways.) + + A third function for library termination calls mutex_destroy on + each mutex for the library. This function would be called + automatically at library unload time. If it turns out to be needed + at exit time for libraries that don't get unloaded, perhaps we + should also use atexit(). Any static mutexes should be cleaned up + with k5_mutex_destroy here. + + How does that second support function invoke the first support + function only once? Through something modelled on pthread_once + that I haven't written up yet. Probably: + + k5_once_t foo_once = K5_ONCE_INIT; + k5_once(k5_once_t *, void (*)(void)); + + For POSIX: Map onto pthread_once facility. + For non-threaded case: A simple flag. + For Windows: Not needed; library init code takes care of it. + + XXX: A general k5_once mechanism isn't possible for Windows, + without faking it through named mutexes or mutexes initialized at + startup. I was only using it in one place outside these headers, + so I'm dropping the general scheme. Eventually the existing uses + in k5-thread.h and k5-platform.h will be converted to pthread_once + or static variables. + + + Thread-specific data: + + // TSD keys are limited in number in gssapi/krb5/com_err; enumerate + // them all. This allows support code init to allocate the + // necessary storage for pointers all at once, and avoids any + // possible error in key creation. + enum { ... } k5_key_t; + // Register destructor function. Called in library init code. + int k5_key_register(k5_key_t, void (*destructor)(void *)); + // Returns NULL or data. + void *k5_getspecific(k5_key_t); + // Returns error if key out of bounds, or the pointer table can't + // be allocated. A call to k5_key_register must have happened first. + // This may trigger the calling of pthread_setspecific on POSIX. + int k5_setspecific(k5_key_t, void *); + // Called in library termination code. + // Trashes data in all threads, calling the registered destructor + // (but calling it from the current thread). + int k5_key_delete(k5_key_t); + + For the non-threaded version, the support code will have a static + array indexed by k5_key_t values, and get/setspecific simply access + the array elements. + + The TSD destructor table is global state, protected by a mutex if + threads are enabled. + + + Any actual external symbols will use the krb5int_ prefix. The k5_ + names will be simple macros or inline functions to rename the + external symbols, or slightly more complex ones to expand the + implementation inline (e.g., map to POSIX versions and/or debug + code using __FILE__ and the like). + + + More to be added, perhaps. */ #include <assert.h> @@ -161,7 +162,7 @@ /* Define the OS mutex bit. */ typedef char k5_os_nothread_mutex; -# define K5_OS_NOTHREAD_MUTEX_PARTIAL_INITIALIZER 0 +# define K5_OS_NOTHREAD_MUTEX_PARTIAL_INITIALIZER 0 /* Empty inline functions avoid the "statement with no effect" warnings, and do better type-checking than functions that don't use their arguments. */ @@ -186,28 +187,28 @@ static inline int k5_os_nothread_mutex_unlock(k5_os_nothread_mutex *m) { 3 - function has been run 4 - function is being run -- deadlock detected */ typedef unsigned char k5_os_nothread_once_t; -# define K5_OS_NOTHREAD_ONCE_INIT 2 -# define k5_os_nothread_once(O,F) \ - (*(O) == 3 ? 0 \ - : *(O) == 2 ? (*(O) = 4, (F)(), *(O) = 3, 0) \ - : (assert(*(O) != 4), assert(*(O) == 2 || *(O) == 3), 0)) +# define K5_OS_NOTHREAD_ONCE_INIT 2 +# define k5_os_nothread_once(O,F) \ + (*(O) == 3 ? 0 \ + : *(O) == 2 ? (*(O) = 4, (F)(), *(O) = 3, 0) \ + : (assert(*(O) != 4), assert(*(O) == 2 || *(O) == 3), 0)) #ifndef ENABLE_THREADS typedef k5_os_nothread_mutex k5_os_mutex; -# define K5_OS_MUTEX_PARTIAL_INITIALIZER \ - K5_OS_NOTHREAD_MUTEX_PARTIAL_INITIALIZER -# define k5_os_mutex_finish_init k5_os_nothread_mutex_finish_init -# define k5_os_mutex_init k5_os_nothread_mutex_init -# define k5_os_mutex_destroy k5_os_nothread_mutex_destroy -# define k5_os_mutex_lock k5_os_nothread_mutex_lock -# define k5_os_mutex_unlock k5_os_nothread_mutex_unlock - -# define k5_once_t k5_os_nothread_once_t -# define K5_ONCE_INIT K5_OS_NOTHREAD_ONCE_INIT -# define k5_once k5_os_nothread_once +# define K5_OS_MUTEX_PARTIAL_INITIALIZER \ + K5_OS_NOTHREAD_MUTEX_PARTIAL_INITIALIZER +# define k5_os_mutex_finish_init k5_os_nothread_mutex_finish_init +# define k5_os_mutex_init k5_os_nothread_mutex_init +# define k5_os_mutex_destroy k5_os_nothread_mutex_destroy +# define k5_os_mutex_lock k5_os_nothread_mutex_lock +# define k5_os_mutex_unlock k5_os_nothread_mutex_unlock + +# define k5_once_t k5_os_nothread_once_t +# define K5_ONCE_INIT K5_OS_NOTHREAD_ONCE_INIT +# define k5_once k5_os_nothread_once #elif HAVE_PTHREAD @@ -247,10 +248,10 @@ typedef k5_os_nothread_mutex k5_os_mutex; symbol tables of the current process. */ extern int krb5int_pthread_loaded(void) #ifdef __GNUC__ - /* We should always get the same answer for the life of the process. */ - __attribute__((const)) +/* We should always get the same answer for the life of the process. */ + __attribute__((const)) #endif - ; + ; #if defined(HAVE_PRAGMA_WEAK_REF) && !defined(NO_WEAK_PTHREADS) # pragma weak pthread_once # pragma weak pthread_mutex_lock @@ -259,7 +260,7 @@ extern int krb5int_pthread_loaded(void) # pragma weak pthread_mutex_init # pragma weak pthread_self # pragma weak pthread_equal -# define K5_PTHREADS_LOADED (krb5int_pthread_loaded()) +# define K5_PTHREADS_LOADED (krb5int_pthread_loaded()) # define USE_PTHREAD_LOCK_ONLY_IF_LOADED /* Can't rely on useful stubs -- see above regarding Solaris. */ @@ -267,19 +268,19 @@ typedef struct { pthread_once_t o; k5_os_nothread_once_t n; } k5_once_t; -# define K5_ONCE_INIT { PTHREAD_ONCE_INIT, K5_OS_NOTHREAD_ONCE_INIT } -# define k5_once(O,F) (K5_PTHREADS_LOADED \ - ? pthread_once(&(O)->o,F) \ - : k5_os_nothread_once(&(O)->n,F)) +# define K5_ONCE_INIT { PTHREAD_ONCE_INIT, K5_OS_NOTHREAD_ONCE_INIT } +# define k5_once(O,F) (K5_PTHREADS_LOADED \ + ? pthread_once(&(O)->o,F) \ + : k5_os_nothread_once(&(O)->n,F)) #else /* no pragma weak support */ -# define K5_PTHREADS_LOADED (1) +# define K5_PTHREADS_LOADED (1) typedef pthread_once_t k5_once_t; -# define K5_ONCE_INIT PTHREAD_ONCE_INIT -# define k5_once pthread_once +# define K5_ONCE_INIT PTHREAD_ONCE_INIT +# define k5_once pthread_once #endif @@ -294,28 +295,28 @@ typedef pthread_once_t k5_once_t; #endif typedef pthread_mutex_t k5_os_mutex; -# define K5_OS_MUTEX_PARTIAL_INITIALIZER \ - PTHREAD_MUTEX_INITIALIZER +# define K5_OS_MUTEX_PARTIAL_INITIALIZER \ + PTHREAD_MUTEX_INITIALIZER #ifdef USE_PTHREAD_LOCK_ONLY_IF_LOADED -# define k5_os_mutex_finish_init(M) (0) -# define k5_os_mutex_init(M) \ - (K5_PTHREADS_LOADED ? pthread_mutex_init((M), 0) : 0) -# define k5_os_mutex_destroy(M) \ - (K5_PTHREADS_LOADED ? pthread_mutex_destroy((M)) : 0) -# define k5_os_mutex_lock(M) \ - (K5_PTHREADS_LOADED ? pthread_mutex_lock(M) : 0) -# define k5_os_mutex_unlock(M) \ - (K5_PTHREADS_LOADED ? pthread_mutex_unlock(M) : 0) +# define k5_os_mutex_finish_init(M) (0) +# define k5_os_mutex_init(M) \ + (K5_PTHREADS_LOADED ? pthread_mutex_init((M), 0) : 0) +# define k5_os_mutex_destroy(M) \ + (K5_PTHREADS_LOADED ? pthread_mutex_destroy((M)) : 0) +# define k5_os_mutex_lock(M) \ + (K5_PTHREADS_LOADED ? pthread_mutex_lock(M) : 0) +# define k5_os_mutex_unlock(M) \ + (K5_PTHREADS_LOADED ? pthread_mutex_unlock(M) : 0) #else static inline int k5_os_mutex_finish_init(k5_os_mutex *m) { return 0; } -# define k5_os_mutex_init(M) pthread_mutex_init((M), 0) -# define k5_os_mutex_destroy(M) pthread_mutex_destroy((M)) -# define k5_os_mutex_lock(M) pthread_mutex_lock(M) -# define k5_os_mutex_unlock(M) pthread_mutex_unlock(M) +# define k5_os_mutex_init(M) pthread_mutex_init((M), 0) +# define k5_os_mutex_destroy(M) pthread_mutex_destroy((M)) +# define k5_os_mutex_lock(M) pthread_mutex_lock(M) +# define k5_os_mutex_unlock(M) pthread_mutex_unlock(M) #endif /* is pthreads always available? */ @@ -328,21 +329,21 @@ typedef struct { # define K5_OS_MUTEX_PARTIAL_INITIALIZER { INVALID_HANDLE_VALUE, 0 } -# define k5_os_mutex_finish_init(M) \ - (assert((M)->h == INVALID_HANDLE_VALUE), \ - ((M)->h = CreateMutex(NULL, FALSE, NULL)) ? 0 : GetLastError()) -# define k5_os_mutex_init(M) \ - ((M)->is_locked = 0, \ - ((M)->h = CreateMutex(NULL, FALSE, NULL)) ? 0 : GetLastError()) -# define k5_os_mutex_destroy(M) \ - (CloseHandle((M)->h) ? ((M)->h = 0, 0) : GetLastError()) +# define k5_os_mutex_finish_init(M) \ + (assert((M)->h == INVALID_HANDLE_VALUE), \ + ((M)->h = CreateMutex(NULL, FALSE, NULL)) ? 0 : GetLastError()) +# define k5_os_mutex_init(M) \ + ((M)->is_locked = 0, \ + ((M)->h = CreateMutex(NULL, FALSE, NULL)) ? 0 : GetLastError()) +# define k5_os_mutex_destroy(M) \ + (CloseHandle((M)->h) ? ((M)->h = 0, 0) : GetLastError()) static inline int k5_os_mutex_lock(k5_os_mutex *m) { DWORD res; res = WaitForSingleObject(m->h, INFINITE); if (res == WAIT_FAILED) - return GetLastError(); + return GetLastError(); /* Eventually these should be turned into some reasonable error code. */ assert(res != WAIT_TIMEOUT); @@ -354,10 +355,10 @@ static inline int k5_os_mutex_lock(k5_os_mutex *m) return 0; } -# define k5_os_mutex_unlock(M) \ - (assert((M)->is_locked == 1), \ - (M)->is_locked = 0, \ - ReleaseMutex((M)->h) ? 0 : GetLastError()) +# define k5_os_mutex_unlock(M) \ + (assert((M)->is_locked == 1), \ + (M)->is_locked = 0, \ + ReleaseMutex((M)->h) ? 0 : GetLastError()) #else @@ -369,7 +370,7 @@ static inline int k5_os_mutex_lock(k5_os_mutex *m) typedef k5_os_mutex k5_mutex_t; -#define K5_MUTEX_PARTIAL_INITIALIZER K5_OS_MUTEX_PARTIAL_INITIALIZER +#define K5_MUTEX_PARTIAL_INITIALIZER K5_OS_MUTEX_PARTIAL_INITIALIZER static inline int k5_mutex_init(k5_mutex_t *m) { return k5_os_mutex_init(m); @@ -378,7 +379,7 @@ static inline int k5_mutex_finish_init(k5_mutex_t *m) { return k5_os_mutex_finish_init(m); } -#define k5_mutex_destroy(M) \ +#define k5_mutex_destroy(M) \ (k5_os_mutex_destroy(M)) #if __GNUC__ >= 4 @@ -390,13 +391,13 @@ static inline int k5_mutex_lock(k5_mutex_t *m) return k5_os_mutex_lock(m); } -#define k5_mutex_unlock(M) \ - (k5_os_mutex_unlock(M)) +#define k5_mutex_unlock(M) \ + (k5_os_mutex_unlock(M)) -#define k5_mutex_assert_locked(M) ((void)(M)) -#define k5_mutex_assert_unlocked(M) ((void)(M)) -#define k5_assert_locked k5_mutex_assert_locked -#define k5_assert_unlocked k5_mutex_assert_unlocked +#define k5_mutex_assert_locked(M) ((void)(M)) +#define k5_mutex_assert_unlocked(M) ((void)(M)) +#define k5_assert_locked k5_mutex_assert_locked +#define k5_assert_unlocked k5_mutex_assert_unlocked /* Thread-specific data; implemented in a support file, because we'll @@ -417,10 +418,10 @@ typedef enum { K5_KEY_MAX } k5_key_t; /* rename shorthand symbols for export */ -#define k5_key_register krb5int_key_register -#define k5_getspecific krb5int_getspecific -#define k5_setspecific krb5int_setspecific -#define k5_key_delete krb5int_key_delete +#define k5_key_register krb5int_key_register +#define k5_getspecific krb5int_getspecific +#define k5_setspecific krb5int_setspecific +#define k5_key_delete krb5int_key_delete extern int k5_key_register(k5_key_t, void (*)(void *)); extern void *k5_getspecific(k5_key_t); extern int k5_setspecific(k5_key_t, void *); |