ldap_int_thread.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* ldap_int_thread.h - ldap internal thread wrappers header file */
  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. LDAP_BEGIN_DECL
  17. /* Can be done twice. See libldap/ldap_thr_debug.h. */
  18. LDAP_F(int) ldap_int_thread_initialize LDAP_P(( void ));
  19. LDAP_F(int) ldap_int_thread_destroy LDAP_P(( void ));
  20. LDAP_END_DECL
  21. #ifndef _LDAP_INT_THREAD_H
  22. #define _LDAP_INT_THREAD_H
  23. #if defined( HAVE_PTHREADS )
  24. /**********************************
  25. * *
  26. * definitions for POSIX Threads *
  27. * *
  28. **********************************/
  29. #include <pthread.h>
  30. #ifdef HAVE_SCHED_H
  31. #include <sched.h>
  32. #endif
  33. LDAP_BEGIN_DECL
  34. typedef pthread_t ldap_int_thread_t;
  35. typedef pthread_mutex_t ldap_int_thread_mutex_t;
  36. typedef pthread_cond_t ldap_int_thread_cond_t;
  37. typedef pthread_key_t ldap_int_thread_key_t;
  38. #define ldap_int_thread_equal(a, b) pthread_equal((a), (b))
  39. #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
  40. defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
  41. defined( _POSIX_THREADSAFE_FUNCTIONS )
  42. #define HAVE_REENTRANT_FUNCTIONS 1
  43. #endif
  44. #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
  45. defined( HAVE_THR_GETCONCURRENCY )
  46. #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
  47. #endif
  48. #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
  49. defined( HAVE_THR_SETCONCURRENCY )
  50. #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
  51. #endif
  52. #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
  53. #define LDAP_THREAD_HAVE_RDWR 1
  54. typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
  55. #endif
  56. #ifndef LDAP_INT_MUTEX_NULL
  57. #define LDAP_INT_MUTEX_NULL PTHREAD_MUTEX_INITIALIZER
  58. #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
  59. #endif
  60. LDAP_END_DECL
  61. #elif defined( HAVE_GNU_PTH )
  62. /***********************************
  63. * *
  64. * thread definitions for GNU Pth *
  65. * *
  66. ***********************************/
  67. #define PTH_SYSCALL_SOFT 1
  68. #include <pth.h>
  69. LDAP_BEGIN_DECL
  70. typedef pth_t ldap_int_thread_t;
  71. typedef pth_mutex_t ldap_int_thread_mutex_t;
  72. typedef pth_cond_t ldap_int_thread_cond_t;
  73. typedef pth_key_t ldap_int_thread_key_t;
  74. #if 0
  75. #define LDAP_THREAD_HAVE_RDWR 1
  76. typedef pth_rwlock_t ldap_int_thread_rdwr_t;
  77. #endif
  78. #ifndef LDAP_INT_MUTEX_NULL
  79. #define LDAP_INT_MUTEX_NULL PTH_MUTEX_INIT
  80. #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
  81. #endif
  82. LDAP_END_DECL
  83. #elif defined( HAVE_THR )
  84. /********************************************
  85. * *
  86. * thread definitions for Solaris LWP (THR) *
  87. * *
  88. ********************************************/
  89. #include <thread.h>
  90. #include <synch.h>
  91. LDAP_BEGIN_DECL
  92. typedef thread_t ldap_int_thread_t;
  93. typedef mutex_t ldap_int_thread_mutex_t;
  94. typedef cond_t ldap_int_thread_cond_t;
  95. typedef thread_key_t ldap_int_thread_key_t;
  96. #define HAVE_REENTRANT_FUNCTIONS 1
  97. #ifdef HAVE_THR_GETCONCURRENCY
  98. #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
  99. #endif
  100. #ifdef HAVE_THR_SETCONCURRENCY
  101. #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
  102. #endif
  103. #ifndef LDAP_INT_MUTEX_NULL
  104. #define LDAP_INT_MUTEX_NULL DEFAULTMUTEX
  105. #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
  106. #endif
  107. #elif defined(HAVE_NT_THREADS)
  108. /*************************************
  109. * *
  110. * thread definitions for NT threads *
  111. * *
  112. *************************************/
  113. #include <process.h>
  114. #include <windows.h>
  115. LDAP_BEGIN_DECL
  116. typedef unsigned long ldap_int_thread_t;
  117. typedef HANDLE ldap_int_thread_mutex_t;
  118. typedef HANDLE ldap_int_thread_cond_t;
  119. typedef DWORD ldap_int_thread_key_t;
  120. LDAP_F( int )
  121. ldap_int_mutex_firstcreate LDAP_P(( ldap_int_thread_mutex_t *mutex ));
  122. #ifndef LDAP_INT_MUTEX_NULL
  123. #define LDAP_INT_MUTEX_NULL ((HANDLE)0)
  124. #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
  125. ldap_int_mutex_firstcreate(&(m))
  126. #endif
  127. LDAP_END_DECL
  128. #else
  129. /***********************************
  130. * *
  131. * thread definitions for no *
  132. * underlying library support *
  133. * *
  134. ***********************************/
  135. #ifndef NO_THREADS
  136. #define NO_THREADS 1
  137. #endif
  138. LDAP_BEGIN_DECL
  139. typedef int ldap_int_thread_t;
  140. typedef int ldap_int_thread_mutex_t;
  141. typedef int ldap_int_thread_cond_t;
  142. typedef int ldap_int_thread_key_t;
  143. #define LDAP_THREAD_HAVE_TPOOL 1
  144. typedef int ldap_int_thread_pool_t;
  145. #ifndef LDAP_INT_MUTEX_NULL
  146. #define LDAP_INT_MUTEX_NULL 0
  147. #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
  148. #endif
  149. LDAP_END_DECL
  150. #endif /* no threads support */
  151. LDAP_BEGIN_DECL
  152. #ifndef ldap_int_thread_equal
  153. #define ldap_int_thread_equal(a, b) ((a) == (b))
  154. #endif
  155. #ifndef LDAP_THREAD_HAVE_RDWR
  156. typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
  157. #endif
  158. LDAP_F(int) ldap_int_thread_pool_startup ( void );
  159. LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
  160. #ifndef LDAP_THREAD_HAVE_TPOOL
  161. typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
  162. #endif
  163. LDAP_END_DECL
  164. #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
  165. #define LDAP_THREAD_DEBUG_WRAP 1
  166. #endif
  167. #ifdef LDAP_THREAD_DEBUG_WRAP
  168. /**************************************
  169. * *
  170. * definitions for type-wrapped debug *
  171. * *
  172. **************************************/
  173. LDAP_BEGIN_DECL
  174. #ifndef LDAP_UINTPTR_T /* May be configured in CPPFLAGS */
  175. #define LDAP_UINTPTR_T unsigned long
  176. #endif
  177. typedef enum {
  178. ldap_debug_magic = -(int) (((unsigned)-1)/19)
  179. } ldap_debug_magic_t;
  180. typedef enum {
  181. /* Could fill in "locked" etc here later */
  182. ldap_debug_state_inited = (int) (((unsigned)-1)/11),
  183. ldap_debug_state_destroyed
  184. } ldap_debug_state_t;
  185. typedef struct {
  186. /* Enclosed in magic numbers in the hope of catching overwrites */
  187. ldap_debug_magic_t magic; /* bit pattern to recognize usages */
  188. LDAP_UINTPTR_T self; /* ~(LDAP_UINTPTR_T)&(this struct) */
  189. union ldap_debug_mem_u { /* Dummy memory reference */
  190. unsigned char *ptr;
  191. LDAP_UINTPTR_T num;
  192. } mem;
  193. ldap_debug_state_t state; /* doubles as another magic number */
  194. } ldap_debug_usage_info_t;
  195. typedef struct {
  196. ldap_int_thread_mutex_t wrapped;
  197. ldap_debug_usage_info_t usage;
  198. ldap_int_thread_t owner;
  199. } ldap_debug_thread_mutex_t;
  200. #define LDAP_DEBUG_MUTEX_NULL {LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
  201. #define LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
  202. ((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
  203. typedef struct {
  204. ldap_int_thread_cond_t wrapped;
  205. ldap_debug_usage_info_t usage;
  206. } ldap_debug_thread_cond_t;
  207. typedef struct {
  208. ldap_int_thread_rdwr_t wrapped;
  209. ldap_debug_usage_info_t usage;
  210. } ldap_debug_thread_rdwr_t;
  211. #ifndef NDEBUG
  212. #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
  213. ldap_debug_thread_assert_mutex_owner( \
  214. __FILE__, __LINE__, "owns(" #mutex ")", mutex )
  215. LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
  216. LDAP_CONST char *file,
  217. int line,
  218. LDAP_CONST char *msg,
  219. ldap_debug_thread_mutex_t *mutex ));
  220. #endif /* NDEBUG */
  221. LDAP_END_DECL
  222. #endif /* LDAP_THREAD_DEBUG_WRAP */
  223. #endif /* _LDAP_INT_THREAD_H */