1 /* thr_pth.c - wrappers around GNU Pth */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
19 #if defined( HAVE_GNU_PTH )
21 #include "ldap_pvt_thread.h" /* Get the thread interface */
22 #define LDAP_THREAD_IMPLEMENTATION
23 #define LDAP_THREAD_RDWR_IMPLEMENTATION
24 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
34 static pth_attr_t detach_attr;
35 static pth_attr_t joined_attr;
38 ldap_int_thread_initialize( void )
43 detach_attr = pth_attr_new();
44 joined_attr = pth_attr_new();
45 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
46 pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
47 pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
49 return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
53 ldap_int_thread_destroy( void )
55 pth_attr_destroy(detach_attr);
61 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
63 void *(*start_routine)( void *),
66 *thread = pth_spawn( detach ? detach_attr : joined_attr,
69 return *thread == NULL ? errno : 0;
73 ldap_pvt_thread_exit( void *retval )
78 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
80 return pth_join( thread, thread_return ) ? 0 : errno;
84 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
86 return pth_raise( thread, signo ) ? 0 : errno;
90 ldap_pvt_thread_yield( void )
92 return pth_yield(NULL) ? 0 : errno;
96 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
98 return( pth_cond_init( cond ) ? 0 : errno );
102 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
104 return( pth_cond_notify( cond, 0 ) ? 0 : errno );
108 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
110 return( pth_cond_notify( cond, 1 ) ? 0 : errno );
114 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
115 ldap_pvt_thread_mutex_t *mutex )
117 return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
121 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
127 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
129 return( pth_mutex_init( mutex ) ? 0 : errno );
133 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
139 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
141 return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
145 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
147 return( pth_mutex_release( mutex ) ? 0 : errno );
151 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
153 return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
157 ldap_pvt_thread_self( void )
162 #ifdef LDAP_THREAD_HAVE_RDWR
164 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
166 return pth_rwlock_init( rw ) ? 0 : errno;
170 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
175 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
177 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
180 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
182 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
185 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
187 return pth_rwlock_release( rw ) ? 0 : errno;
190 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
192 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
195 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
197 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
200 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
202 return pth_rwlock_release( rw ) ? 0 : errno;
205 #endif /* LDAP_THREAD_HAVE_RDWR */
206 #endif /* HAVE_GNU_PTH */