Back to index

openldap  2.4.31
ldap_int_thread.h
Go to the documentation of this file.
00001 /* ldap_int_thread.h - ldap internal thread wrappers header file */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  * 
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 
00018 LDAP_BEGIN_DECL
00019 
00020 /* Can be done twice in libldap_r.  See libldap_r/ldap_thr_debug.h. */
00021 LDAP_F(int) ldap_int_thread_initialize LDAP_P(( void ));
00022 LDAP_F(int) ldap_int_thread_destroy    LDAP_P(( void ));
00023 
00024 LDAP_END_DECL
00025 
00026 
00027 #ifndef _LDAP_INT_THREAD_H
00028 #define _LDAP_INT_THREAD_H
00029 
00030 #if defined( HAVE_PTHREADS )
00031 /**********************************
00032  *                                *
00033  * definitions for POSIX Threads  *
00034  *                                *
00035  **********************************/
00036 
00037 #include <pthread.h>
00038 #ifdef HAVE_SCHED_H
00039 #include <sched.h>
00040 #endif
00041 
00042 LDAP_BEGIN_DECL
00043 
00044 typedef pthread_t           ldap_int_thread_t;
00045 typedef pthread_mutex_t            ldap_int_thread_mutex_t;
00046 typedef pthread_cond_t             ldap_int_thread_cond_t;
00047 typedef pthread_key_t              ldap_int_thread_key_t;
00048 
00049 #define ldap_int_thread_equal(a, b)       pthread_equal((a), (b))
00050 
00051 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
00052        defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
00053        defined( _POSIX_THREADSAFE_FUNCTIONS )
00054 #define HAVE_REENTRANT_FUNCTIONS 1
00055 #endif
00056 
00057 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
00058        defined( HAVE_THR_GETCONCURRENCY )
00059 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
00060 #endif
00061 
00062 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
00063        defined( HAVE_THR_SETCONCURRENCY )
00064 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
00065 #endif
00066 
00067 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
00068 #define LDAP_THREAD_HAVE_RDWR 1
00069 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
00070 #endif
00071 
00072 #ifndef LDAP_INT_MUTEX_NULL
00073 #define LDAP_INT_MUTEX_NULL PTHREAD_MUTEX_INITIALIZER
00074 #define LDAP_INT_MUTEX_FIRSTCREATE(m)     ((void) 0)
00075 #endif
00076 
00077 LDAP_END_DECL
00078 
00079 #elif defined ( HAVE_MACH_CTHREADS )
00080 /**********************************
00081  *                                *
00082  * definitions for Mach CThreads  *
00083  *                                *
00084  **********************************/
00085 
00086 #if defined( HAVE_MACH_CTHREADS_H )
00087 #      include <mach/cthreads.h>
00088 #elif defined( HAVE_CTHREADS_H )
00089 #      include <cthreads.h>
00090 #endif
00091 
00092 LDAP_BEGIN_DECL
00093 
00094 typedef cthread_t           ldap_int_thread_t;
00095 typedef struct mutex        ldap_int_thread_mutex_t;
00096 typedef struct condition    ldap_int_thread_cond_t;
00097 typedef cthread_key_t              ldap_int_thread_key_t;
00098 
00099 #ifndef LDAP_INT_MUTEX_NULL
00100 #define LDAP_INT_MUTEX_NULL MUTEX_INITIALIZER
00101 #define LDAP_INT_MUTEX_FIRSTCREATE(m)     ((void) 0)
00102 #endif
00103 
00104 LDAP_END_DECL
00105 
00106 #elif defined( HAVE_GNU_PTH )
00107 /***********************************
00108  *                                 *
00109  * thread definitions for GNU Pth  *
00110  *                                 *
00111  ***********************************/
00112 
00113 #define PTH_SYSCALL_SOFT 1
00114 #include <pth.h>
00115 
00116 LDAP_BEGIN_DECL
00117 
00118 typedef pth_t        ldap_int_thread_t;
00119 typedef pth_mutex_t  ldap_int_thread_mutex_t;
00120 typedef pth_cond_t   ldap_int_thread_cond_t;
00121 typedef pth_key_t    ldap_int_thread_key_t;
00122 
00123 #if 0
00124 #define LDAP_THREAD_HAVE_RDWR 1
00125 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
00126 #endif
00127 
00128 #ifndef LDAP_INT_MUTEX_NULL
00129 #define LDAP_INT_MUTEX_NULL PTH_MUTEX_INIT
00130 #define LDAP_INT_MUTEX_FIRSTCREATE(m)     ((void) 0)
00131 #endif
00132 
00133 LDAP_END_DECL
00134 
00135 #elif defined( HAVE_THR )
00136 /********************************************
00137  *                                          *
00138  * thread definitions for Solaris LWP (THR) *
00139  *                                          *
00140  ********************************************/
00141 
00142 #include <thread.h>
00143 #include <synch.h>
00144 
00145 LDAP_BEGIN_DECL
00146 
00147 typedef thread_t            ldap_int_thread_t;
00148 typedef mutex_t                    ldap_int_thread_mutex_t;
00149 typedef cond_t                     ldap_int_thread_cond_t;
00150 typedef thread_key_t ldap_int_thread_key_t;
00151 
00152 #define HAVE_REENTRANT_FUNCTIONS 1
00153 
00154 #ifdef HAVE_THR_GETCONCURRENCY
00155 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
00156 #endif
00157 #ifdef HAVE_THR_SETCONCURRENCY
00158 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
00159 #endif
00160 
00161 #ifndef LDAP_INT_MUTEX_NULL
00162 #define LDAP_INT_MUTEX_NULL DEFAULTMUTEX
00163 #define LDAP_INT_MUTEX_FIRSTCREATE(m)     ((void) 0)
00164 #endif
00165 
00166 #elif defined(HAVE_NT_THREADS)
00167 /*************************************
00168  *                                   *
00169  * thread definitions for NT threads *
00170  *                                   *
00171  *************************************/
00172 
00173 #include <process.h>
00174 #include <windows.h>
00175 
00176 LDAP_BEGIN_DECL
00177 
00178 typedef unsigned long       ldap_int_thread_t;
00179 typedef HANDLE       ldap_int_thread_mutex_t;
00180 typedef HANDLE       ldap_int_thread_cond_t;
00181 typedef DWORD ldap_int_thread_key_t;
00182 
00183 #ifndef LDAP_INT_MUTEX_NULL
00184 #define LDAP_INT_MUTEX_NULL        ((HANDLE)0)
00185 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
00186               ((void) ((m) || ldap_pvt_thread_mutex_init(&(m))))
00187 #endif
00188 
00189 LDAP_END_DECL
00190 
00191 #else
00192 /***********************************
00193  *                                 *
00194  * thread definitions for no       *
00195  * underlying library support      *
00196  *                                 *
00197  ***********************************/
00198 
00199 #ifndef NO_THREADS
00200 #define NO_THREADS 1
00201 #endif
00202 
00203 LDAP_BEGIN_DECL
00204 
00205 typedef int                 ldap_int_thread_t;
00206 typedef int                 ldap_int_thread_mutex_t;
00207 typedef int                 ldap_int_thread_cond_t;
00208 typedef int                 ldap_int_thread_key_t;
00209 
00210 #define LDAP_THREAD_HAVE_TPOOL 1
00211 typedef int                 ldap_int_thread_pool_t;
00212 
00213 #ifndef LDAP_INT_MUTEX_NULL
00214 #define LDAP_INT_MUTEX_NULL                      0
00215 #define LDAP_INT_MUTEX_FIRSTCREATE(m)     ((void) 0)
00216 #endif
00217 
00218 LDAP_END_DECL
00219 
00220 #endif /* no threads support */
00221 
00222 
00223 LDAP_BEGIN_DECL
00224 
00225 #ifndef ldap_int_thread_equal
00226 #define ldap_int_thread_equal(a, b)       ((a) == (b))
00227 #endif
00228 
00229 #ifndef LDAP_THREAD_HAVE_RDWR
00230 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
00231 #endif
00232 
00233 LDAP_F(int) ldap_int_thread_pool_startup ( void );
00234 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
00235 
00236 #ifndef LDAP_THREAD_HAVE_TPOOL
00237 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
00238 #endif
00239 
00240 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
00241 LDAP_END_DECL
00242 
00243 
00244 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
00245 #define LDAP_THREAD_DEBUG_WRAP 1
00246 #endif
00247 
00248 #ifdef LDAP_THREAD_DEBUG_WRAP
00249 /**************************************
00250  *                                    *
00251  * definitions for type-wrapped debug *
00252  *                                    *
00253  **************************************/
00254 
00255 LDAP_BEGIN_DECL
00256 
00257 #ifndef LDAP_UINTPTR_T      /* May be configured in CPPFLAGS */
00258 #define LDAP_UINTPTR_T      unsigned long
00259 #endif
00260 
00261 typedef enum {
00262        ldap_debug_magic =   -(int) (((unsigned)-1)/19)
00263 } ldap_debug_magic_t;
00264 
00265 typedef enum {
00266        /* Could fill in "locked" etc here later */
00267        ldap_debug_state_inited = (int) (((unsigned)-1)/11),
00268        ldap_debug_state_destroyed
00269 } ldap_debug_state_t;
00270 
00271 typedef struct {
00272        /* Enclosed in magic numbers in the hope of catching overwrites */
00273        ldap_debug_magic_t   magic; /* bit pattern to recognize usages  */
00274        LDAP_UINTPTR_T              self;  /* ~(LDAP_UINTPTR_T)&(this struct) */
00275        union ldap_debug_mem_u {    /* Dummy memory reference */
00276               unsigned char *ptr;
00277               LDAP_UINTPTR_T       num;
00278        } mem;
00279        ldap_debug_state_t   state; /* doubles as another magic number */
00280 } ldap_debug_usage_info_t;
00281 
00282 typedef struct {
00283        ldap_int_thread_mutex_t     wrapped;
00284        ldap_debug_usage_info_t     usage;
00285        ldap_int_thread_t    owner;
00286 } ldap_debug_thread_mutex_t;
00287 
00288 #define       LDAP_DEBUG_MUTEX_NULL       {LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
00289 #define       LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
00290        ((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
00291 
00292 typedef struct {
00293        ldap_int_thread_cond_t      wrapped;
00294        ldap_debug_usage_info_t     usage;
00295 } ldap_debug_thread_cond_t;
00296 
00297 typedef struct {
00298        ldap_int_thread_rdwr_t      wrapped;
00299        ldap_debug_usage_info_t     usage;
00300 } ldap_debug_thread_rdwr_t;
00301 
00302 #ifndef NDEBUG
00303 #define       LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
00304        ldap_debug_thread_assert_mutex_owner( \
00305               __FILE__, __LINE__, "owns(" #mutex ")", mutex )
00306 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
00307        LDAP_CONST char *file,
00308        int line,
00309        LDAP_CONST char *msg,
00310        ldap_debug_thread_mutex_t *mutex ));
00311 #endif /* NDEBUG */
00312 
00313 LDAP_END_DECL
00314 
00315 #endif /* LDAP_THREAD_DEBUG_WRAP */
00316 
00317 #endif /* _LDAP_INT_THREAD_H */