Back to index

openldap  2.4.31
thr_posix.c
Go to the documentation of this file.
00001 /* thr_posix.c - wrapper around posix and posixish thread implementations.  */
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 #include "portable.h"
00018 
00019 #if defined( HAVE_PTHREADS )
00020 
00021 #include <ac/errno.h>
00022 
00023 #ifdef REPLACE_BROKEN_YIELD
00024 #ifndef HAVE_NANOSLEEP
00025 #include <ac/socket.h>
00026 #endif
00027 #include <ac/time.h>
00028 #endif
00029 
00030 #include "ldap_pvt_thread.h" /* Get the thread interface */
00031 #define LDAP_THREAD_IMPLEMENTATION
00032 #define LDAP_THREAD_RDWR_IMPLEMENTATION
00033 #include "ldap_thr_debug.h"  /* May rename the symbols defined below */
00034 #include <signal.h>                 /* For pthread_kill() */
00035 
00036 #if HAVE_PTHREADS < 6
00037 #  define LDAP_INT_THREAD_ATTR_DEFAULT           pthread_attr_default
00038 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT       pthread_condattr_default
00039 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT      pthread_mutexattr_default
00040 #else
00041 #  define LDAP_INT_THREAD_ATTR_DEFAULT           NULL
00042 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT       NULL
00043 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
00044 #endif
00045 
00046 #ifdef LDAP_THREAD_DEBUG
00047 #  if defined LDAP_INT_THREAD_MUTEXATTR   /* May be defined in CPPFLAGS */
00048 #  elif defined HAVE_PTHREAD_KILL_OTHER_THREADS_NP
00049         /* LinuxThreads hack */
00050 #    define LDAP_INT_THREAD_MUTEXATTR     PTHREAD_MUTEX_ERRORCHECK_NP
00051 #  else
00052 #    define LDAP_INT_THREAD_MUTEXATTR     PTHREAD_MUTEX_ERRORCHECK
00053 #  endif
00054 static pthread_mutexattr_t mutex_attr;
00055 #  undef  LDAP_INT_THREAD_MUTEXATTR_DEFAULT
00056 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
00057 #endif
00058 
00059 #if HAVE_PTHREADS < 7
00060 #define ERRVAL(val)  ((val) < 0 ? errno : 0)
00061 #else
00062 #define ERRVAL(val)  (val)
00063 #endif
00064 
00065 int
00066 ldap_int_thread_initialize( void )
00067 {
00068 #ifdef LDAP_INT_THREAD_MUTEXATTR
00069        pthread_mutexattr_init( &mutex_attr );
00070        pthread_mutexattr_settype( &mutex_attr, LDAP_INT_THREAD_MUTEXATTR );
00071 #endif
00072        return 0;
00073 }
00074 
00075 int
00076 ldap_int_thread_destroy( void )
00077 {
00078 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
00079        /* LinuxThreads: kill clones */
00080        pthread_kill_other_threads_np();
00081 #endif
00082 #ifdef LDAP_INT_THREAD_MUTEXATTR
00083        pthread_mutexattr_destroy( &mutex_attr );
00084 #endif
00085        return 0;
00086 }
00087 
00088 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
00089 int
00090 ldap_pvt_thread_set_concurrency(int n)
00091 {
00092 #ifdef HAVE_PTHREAD_SETCONCURRENCY
00093        return pthread_setconcurrency( n );
00094 #elif defined(HAVE_THR_SETCONCURRENCY)
00095        return thr_setconcurrency( n );
00096 #else
00097        return 0;
00098 #endif
00099 }
00100 #endif
00101 
00102 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
00103 int
00104 ldap_pvt_thread_get_concurrency(void)
00105 {
00106 #ifdef HAVE_PTHREAD_GETCONCURRENCY
00107        return pthread_getconcurrency();
00108 #elif defined(HAVE_THR_GETCONCURRENCY)
00109        return thr_getconcurrency();
00110 #else
00111        return 0;
00112 #endif
00113 }
00114 #endif
00115 
00116 /* detachstate appeared in Draft 6, but without manifest constants.
00117  * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
00118  * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
00119  */
00120 #ifndef PTHREAD_CREATE_JOINABLE
00121 #ifdef PTHREAD_CREATE_UNDETACHED
00122 #define       PTHREAD_CREATE_JOINABLE     PTHREAD_CREATE_UNDETACHED
00123 #else
00124 #define       PTHREAD_CREATE_JOINABLE     0
00125 #endif
00126 #endif
00127 
00128 #ifndef PTHREAD_CREATE_DETACHED
00129 #define       PTHREAD_CREATE_DETACHED     1
00130 #endif
00131 
00132 int 
00133 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
00134        int detach,
00135        void *(*start_routine)( void * ),
00136        void *arg)
00137 {
00138        int rtn;
00139        pthread_attr_t attr;
00140 
00141 /* Always create the thread attrs, so we can set stacksize if we need to */
00142 #if HAVE_PTHREADS > 5
00143        pthread_attr_init(&attr);
00144 #else
00145        pthread_attr_create(&attr);
00146 #endif
00147 
00148 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
00149        /* this should be tunable */
00150        pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
00151 #endif
00152 
00153 #if HAVE_PTHREADS > 5
00154        detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
00155 #if HAVE_PTHREADS == 6
00156        pthread_attr_setdetachstate(&attr, &detach);
00157 #else
00158        pthread_attr_setdetachstate(&attr, detach);
00159 #endif
00160 #endif
00161 
00162 #if HAVE_PTHREADS < 5
00163        rtn = pthread_create( thread, attr, start_routine, arg );
00164 #else
00165        rtn = pthread_create( thread, &attr, start_routine, arg );
00166 #endif
00167 
00168 #if HAVE_PTHREADS > 5
00169        pthread_attr_destroy(&attr);
00170 #else
00171        pthread_attr_delete(&attr);
00172        if( detach ) {
00173               pthread_detach( thread );
00174        }
00175 #endif
00176 
00177 #if HAVE_PTHREADS < 7
00178        if ( rtn < 0 ) rtn = errno;
00179 #endif
00180        return rtn;
00181 }
00182 
00183 void 
00184 ldap_pvt_thread_exit( void *retval )
00185 {
00186        pthread_exit( retval );
00187 }
00188 
00189 int 
00190 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
00191 {
00192 #if HAVE_PTHREADS < 7
00193        void *dummy;
00194        if (thread_return==NULL)
00195          thread_return=&dummy;
00196 #endif
00197        return ERRVAL( pthread_join( thread, thread_return ) );
00198 }
00199 
00200 int 
00201 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
00202 {
00203 #if defined(HAVE_PTHREAD_KILL) && HAVE_PTHREADS > 4
00204        /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
00205        return ERRVAL( pthread_kill( thread, signo ) );
00206 #else
00207        /* pthread package with DCE */
00208        if (kill( getpid(), signo )<0)
00209               return errno;
00210        return 0;
00211 #endif
00212 }
00213 
00214 int 
00215 ldap_pvt_thread_yield( void )
00216 {
00217 #ifdef REPLACE_BROKEN_YIELD
00218 #ifdef HAVE_NANOSLEEP
00219        struct timespec t = { 0, 0 };
00220        nanosleep(&t, NULL);
00221 #else
00222        struct timeval tv = {0,0};
00223        select( 0, NULL, NULL, NULL, &tv );
00224 #endif
00225        return 0;
00226 
00227 #elif defined(HAVE_THR_YIELD)
00228        thr_yield();
00229        return 0;
00230 
00231 #elif HAVE_PTHREADS == 10
00232        return sched_yield();
00233 
00234 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
00235        sched_yield();
00236        return 0;
00237 
00238 #elif HAVE_PTHREADS == 6
00239        pthread_yield(NULL);
00240        return 0;
00241 
00242 #else
00243        pthread_yield();
00244        return 0;
00245 #endif
00246 }
00247 
00248 int 
00249 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
00250 {
00251        return ERRVAL( pthread_cond_init(
00252               cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) );
00253 }
00254 
00255 int 
00256 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
00257 {
00258        return ERRVAL( pthread_cond_destroy( cond ) );
00259 }
00260        
00261 int 
00262 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
00263 {
00264        return ERRVAL( pthread_cond_signal( cond ) );
00265 }
00266 
00267 int
00268 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
00269 {
00270        return ERRVAL( pthread_cond_broadcast( cond ) );
00271 }
00272 
00273 int 
00274 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
00275                     ldap_pvt_thread_mutex_t *mutex )
00276 {
00277        return ERRVAL( pthread_cond_wait( cond, mutex ) );
00278 }
00279 
00280 int 
00281 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
00282 {
00283        return ERRVAL( pthread_mutex_init(
00284               mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT ) );
00285 }
00286 
00287 int 
00288 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
00289 {
00290        return ERRVAL( pthread_mutex_destroy( mutex ) );
00291 }
00292 
00293 int 
00294 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
00295 {
00296        return ERRVAL( pthread_mutex_lock( mutex ) );
00297 }
00298 
00299 int 
00300 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
00301 {
00302        return ERRVAL( pthread_mutex_trylock( mutex ) );
00303 }
00304 
00305 int 
00306 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
00307 {
00308        return ERRVAL( pthread_mutex_unlock( mutex ) );
00309 }
00310 
00311 ldap_pvt_thread_t ldap_pvt_thread_self( void )
00312 {
00313        return pthread_self();
00314 }
00315 
00316 int
00317 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key )
00318 {
00319        return pthread_key_create( key, NULL );
00320 }
00321 
00322 int
00323 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
00324 {
00325        return pthread_key_delete( key );
00326 }
00327 
00328 int
00329 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
00330 {
00331        return pthread_setspecific( key, data );
00332 }
00333 
00334 int
00335 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
00336 {
00337        *data = pthread_getspecific( key );
00338        return 0;
00339 }
00340 
00341 #ifdef LDAP_THREAD_HAVE_RDWR
00342 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
00343 int 
00344 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
00345 {
00346        return ERRVAL( pthread_rwlock_init( rw, NULL ) );
00347 }
00348 
00349 int 
00350 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
00351 {
00352        return ERRVAL( pthread_rwlock_destroy( rw ) );
00353 }
00354 
00355 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
00356 {
00357        return ERRVAL( pthread_rwlock_rdlock( rw ) );
00358 }
00359 
00360 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
00361 {
00362        return ERRVAL( pthread_rwlock_tryrdlock( rw ) );
00363 }
00364 
00365 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
00366 {
00367        return ERRVAL( pthread_rwlock_unlock( rw ) );
00368 }
00369 
00370 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
00371 {
00372        return ERRVAL( pthread_rwlock_wrlock( rw ) );
00373 }
00374 
00375 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
00376 {
00377        return ERRVAL( pthread_rwlock_trywrlock( rw ) );
00378 }
00379 
00380 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
00381 {
00382        return ERRVAL( pthread_rwlock_unlock( rw ) );
00383 }
00384 
00385 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
00386 #endif /* LDAP_THREAD_HAVE_RDWR */
00387 #endif /* HAVE_PTHREADS */
00388