1 /* thr_pth.c - wrappers around GNU Pth */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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"
30 static pth_attr_t detach_attr;
31 static pth_attr_t joined_attr;
34 ldap_int_thread_initialize( void )
39 detach_attr = pth_attr_new();
40 joined_attr = pth_attr_new();
41 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
42 pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
43 pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
45 return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
49 ldap_int_thread_destroy( void )
51 pth_attr_destroy(detach_attr);
57 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
59 void *(*start_routine)( void *),
62 *thread = pth_spawn( detach ? detach_attr : joined_attr,
65 return *thread == NULL ? errno : 0;
69 ldap_pvt_thread_exit( void *retval )
74 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
76 return pth_join( thread, thread_return ) ? 0 : errno;
80 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
82 return pth_raise( thread, signo ) ? 0 : errno;
86 ldap_pvt_thread_yield( void )
88 return pth_yield(NULL) ? 0 : errno;
92 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
94 return( pth_cond_init( cond ) ? 0 : errno );
98 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
100 return( pth_cond_notify( cond, 0 ) ? 0 : errno );
104 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
106 return( pth_cond_notify( cond, 1 ) ? 0 : errno );
110 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
111 ldap_pvt_thread_mutex_t *mutex )
113 return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
117 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
123 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
125 return( pth_mutex_init( mutex ) ? 0 : errno );
129 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
135 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
137 return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
141 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
143 return( pth_mutex_release( mutex ) ? 0 : errno );
147 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
149 return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
153 ldap_pvt_thread_self( void )
158 #ifdef LDAP_THREAD_HAVE_RDWR
160 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
162 return pth_rwlock_init( rw ) ? 0 : errno;
166 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
171 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
173 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
176 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
178 return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
181 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
183 return pth_rwlock_release( rw ) ? 0 : errno;
186 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
188 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
191 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
193 return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
196 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
198 return pth_rwlock_release( rw ) ? 0 : errno;
201 #endif /* LDAP_THREAD_HAVE_RDWR */
202 #endif /* HAVE_GNU_PTH */