3 * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
12 /* thr_pth.c - wrappers around GNU Pth */
16 #if defined( HAVE_GNU_PTH )
18 #include "ldap_pvt_thread.h"
27 static pth_attr_t detach_attr;
28 static pth_attr_t joined_attr;
31 ldap_int_thread_initialize( void )
36 detach_attr = pth_attr_new();
37 joined_attr = pth_attr_new();
38 #if LDAP_PVT_THREAD_STACK_SIZE
39 pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
40 pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
42 return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
46 ldap_int_thread_destroy( void )
48 pth_attr_destroy(detach_attr);
54 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
56 void *(*start_routine)( void *),
59 *thread = pth_spawn( detach ? detach_attr : joined_attr,
62 return *thread == NULL ? errno : 0;
66 ldap_pvt_thread_exit( void *retval )
71 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
73 return pth_join( thread, thread_return ) ? 0 : errno;
77 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
79 return pth_raise( thread, signo ) ? 0 : errno;
83 ldap_pvt_thread_yield( void )
85 return pth_yield(NULL) ? 0 : errno;
89 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
91 return( pth_cond_init( cond ) ? 0 : errno );
95 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
97 return( pth_cond_notify( cond, 0 ) ? 0 : errno );
101 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
103 return( pth_cond_notify( cond, 1 ) ? 0 : errno );
107 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
108 ldap_pvt_thread_mutex_t *mutex )
110 return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
114 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
120 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
122 return( pth_mutex_init( mutex ) ? 0 : errno );
126 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
132 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
134 return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
138 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
140 return( pth_mutex_release( mutex ) ? 0 : errno );
144 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
146 return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
149 #ifdef LDAP_THREAD_HAVE_RDWR
151 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
153 return pth_rwlock_init( rw ) ? 0 : errno;
157 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
162 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
164 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
167 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
169 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
172 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
174 return pth_rwlock_release( rw ) ? 0 : errno;
177 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
179 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
182 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
184 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
187 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
189 return pth_rwlock_release( rw ) ? 0 : errno;
192 #endif /* LDAP_THREAD_HAVE_RDWR */
193 #endif /* HAVE_GNU_PTH */