thr_pth.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* thr_pth.c - wrappers around GNU Pth */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  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>.
  15. */
  16. #include "portable.h"
  17. #if defined( HAVE_GNU_PTH )
  18. #include "ldap_pvt_thread.h" /* Get the thread interface */
  19. #define LDAP_THREAD_IMPLEMENTATION
  20. #define LDAP_THREAD_RDWR_IMPLEMENTATION
  21. #include "ldap_thr_debug.h" /* May rename the symbols defined below */
  22. #include <errno.h>
  23. /*******************
  24. * *
  25. * GNU Pth Threads *
  26. * *
  27. *******************/
  28. static pth_attr_t detach_attr;
  29. static pth_attr_t joined_attr;
  30. int
  31. ldap_int_thread_initialize( void )
  32. {
  33. if( !pth_init() ) {
  34. return -1;
  35. }
  36. detach_attr = pth_attr_new();
  37. joined_attr = pth_attr_new();
  38. #ifdef LDAP_PVT_THREAD_SET_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 );
  41. #endif
  42. return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
  43. }
  44. int
  45. ldap_int_thread_destroy( void )
  46. {
  47. pth_attr_destroy(detach_attr);
  48. pth_kill();
  49. return 0;
  50. }
  51. int
  52. ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
  53. int detach,
  54. void *(*start_routine)( void *),
  55. void *arg)
  56. {
  57. *thread = pth_spawn( detach ? detach_attr : joined_attr,
  58. start_routine, arg );
  59. return *thread == NULL ? errno : 0;
  60. }
  61. void
  62. ldap_pvt_thread_exit( void *retval )
  63. {
  64. pth_exit( retval );
  65. }
  66. int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
  67. {
  68. return pth_join( thread, thread_return ) ? 0 : errno;
  69. }
  70. int
  71. ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
  72. {
  73. return pth_raise( thread, signo ) ? 0 : errno;
  74. }
  75. int
  76. ldap_pvt_thread_yield( void )
  77. {
  78. return pth_yield(NULL) ? 0 : errno;
  79. }
  80. int
  81. ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
  82. {
  83. return( pth_cond_init( cond ) ? 0 : errno );
  84. }
  85. int
  86. ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
  87. {
  88. return( pth_cond_notify( cond, 0 ) ? 0 : errno );
  89. }
  90. int
  91. ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
  92. {
  93. return( pth_cond_notify( cond, 1 ) ? 0 : errno );
  94. }
  95. int
  96. ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
  97. ldap_pvt_thread_mutex_t *mutex )
  98. {
  99. return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
  100. }
  101. int
  102. ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
  103. {
  104. return 0;
  105. }
  106. int
  107. ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
  108. {
  109. return( pth_mutex_init( mutex ) ? 0 : errno );
  110. }
  111. int
  112. ldap_pvt_thread_mutex_recursive_init( ldap_pvt_thread_mutex_t *mutex )
  113. {
  114. /* All pth mutexes are recursive */
  115. return ldap_pvt_thread_mutex_init( mutex );
  116. }
  117. int
  118. ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
  119. {
  120. return 0;
  121. }
  122. int
  123. ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
  124. {
  125. return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
  126. }
  127. int
  128. ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
  129. {
  130. return( pth_mutex_release( mutex ) ? 0 : errno );
  131. }
  132. int
  133. ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
  134. {
  135. return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
  136. }
  137. ldap_pvt_thread_t
  138. ldap_pvt_thread_self( void )
  139. {
  140. return pth_self();
  141. }
  142. int
  143. ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key )
  144. {
  145. return pth_key_create( key, NULL );
  146. }
  147. int
  148. ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
  149. {
  150. return pth_key_delete( key );
  151. }
  152. int
  153. ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
  154. {
  155. return pth_key_setdata( key, data );
  156. }
  157. int
  158. ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
  159. {
  160. *data = pth_key_getdata( key );
  161. return 0;
  162. }
  163. #ifdef LDAP_THREAD_HAVE_RDWR
  164. int
  165. ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
  166. {
  167. return pth_rwlock_init( rw ) ? 0 : errno;
  168. }
  169. int
  170. ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
  171. {
  172. return 0;
  173. }
  174. int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
  175. {
  176. return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
  177. }
  178. int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
  179. {
  180. return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
  181. }
  182. int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
  183. {
  184. return pth_rwlock_release( rw ) ? 0 : errno;
  185. }
  186. int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
  187. {
  188. return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
  189. }
  190. int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
  191. {
  192. return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
  193. }
  194. int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
  195. {
  196. return pth_rwlock_release( rw ) ? 0 : errno;
  197. }
  198. #endif /* LDAP_THREAD_HAVE_RDWR */
  199. #endif /* HAVE_GNU_PTH */