Back to index

openldap  2.4.31
thr_pth.c
Go to the documentation of this file.
00001 /* thr_pth.c - wrappers around GNU Pth */
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_GNU_PTH )
00020 
00021 #include "ldap_pvt_thread.h" /* Get the thread interface */
00022 #define LDAP_THREAD_IMPLEMENTATION
00023 #define LDAP_THREAD_RDWR_IMPLEMENTATION
00024 #include "ldap_thr_debug.h"  /* May rename the symbols defined below */
00025 
00026 #include <errno.h>
00027 
00028 /*******************
00029  *                 *
00030  * GNU Pth Threads *
00031  *                 *
00032  *******************/
00033 
00034 static pth_attr_t detach_attr;
00035 static pth_attr_t joined_attr;
00036 
00037 int
00038 ldap_int_thread_initialize( void )
00039 {
00040        if( !pth_init() ) {
00041               return -1;
00042        }
00043        detach_attr = pth_attr_new();
00044        joined_attr = pth_attr_new();
00045 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
00046        pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
00047        pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
00048 #endif
00049        return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
00050 }
00051 
00052 int
00053 ldap_int_thread_destroy( void )
00054 {
00055        pth_attr_destroy(detach_attr);
00056        pth_kill();
00057        return 0;
00058 }
00059 
00060 int 
00061 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
00062        int detach,
00063        void *(*start_routine)( void *),
00064        void *arg)
00065 {
00066        *thread = pth_spawn( detach ? detach_attr : joined_attr,
00067               start_routine, arg );
00068 
00069        return *thread == NULL ? errno : 0;
00070 }
00071 
00072 void 
00073 ldap_pvt_thread_exit( void *retval )
00074 {
00075        pth_exit( retval );
00076 }
00077 
00078 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
00079 {
00080        return pth_join( thread, thread_return ) ? 0 : errno;
00081 }
00082 
00083 int 
00084 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
00085 {
00086        return pth_raise( thread, signo ) ? 0 : errno;
00087 }
00088        
00089 int 
00090 ldap_pvt_thread_yield( void )
00091 {
00092        return pth_yield(NULL) ? 0 : errno;
00093 }
00094 
00095 int 
00096 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
00097 {
00098        return( pth_cond_init( cond ) ? 0 : errno );
00099 }
00100 
00101 int 
00102 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
00103 {
00104        return( pth_cond_notify( cond, 0 ) ? 0 : errno );
00105 }
00106 
00107 int
00108 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
00109 {
00110        return( pth_cond_notify( cond, 1 ) ? 0 : errno );
00111 }
00112 
00113 int 
00114 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
00115        ldap_pvt_thread_mutex_t *mutex )
00116 {
00117        return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
00118 }
00119 
00120 int
00121 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
00122 {
00123        return 0;
00124 }
00125 
00126 int 
00127 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
00128 {
00129        return( pth_mutex_init( mutex ) ? 0 : errno );
00130 }
00131 
00132 int 
00133 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
00134 {
00135        return 0;
00136 }
00137 
00138 int 
00139 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
00140 {
00141        return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
00142 }
00143 
00144 int 
00145 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
00146 {
00147        return( pth_mutex_release( mutex ) ? 0 : errno );
00148 }
00149 
00150 int
00151 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
00152 {
00153        return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
00154 }
00155 
00156 ldap_pvt_thread_t
00157 ldap_pvt_thread_self( void )
00158 {
00159        return pth_self();
00160 }
00161 
00162 int
00163 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key )
00164 {
00165        return pth_key_create( key, NULL );
00166 }
00167 
00168 int
00169 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
00170 {
00171        return pth_key_delete( key );
00172 }
00173 
00174 int
00175 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
00176 {
00177        return pth_key_setdata( key, data );
00178 }
00179 
00180 int
00181 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
00182 {
00183        *data = pth_key_getdata( key );
00184        return 0;
00185 }
00186 
00187 #ifdef LDAP_THREAD_HAVE_RDWR
00188 int 
00189 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
00190 {
00191        return pth_rwlock_init( rw ) ? 0 : errno;
00192 }
00193 
00194 int 
00195 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
00196 {
00197        return 0;
00198 }
00199 
00200 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
00201 {
00202        return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
00203 }
00204 
00205 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
00206 {
00207        return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
00208 }
00209 
00210 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
00211 {
00212        return pth_rwlock_release( rw ) ? 0 : errno;
00213 }
00214 
00215 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
00216 {
00217        return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
00218 }
00219 
00220 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
00221 {
00222        return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
00223 }
00224 
00225 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
00226 {
00227        return pth_rwlock_release( rw ) ? 0 : errno;
00228 }
00229 
00230 #endif /* LDAP_THREAD_HAVE_RDWR */
00231 #endif /* HAVE_GNU_PTH */