1 /* $NetBSD: ldap_int_thread.h,v 1.2 2020/08/11 13:15:37 christos Exp $ */ 2 3 /* ldap_int_thread.h - ldap internal thread wrappers header file */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2020 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 19 20 LDAP_BEGIN_DECL 21 22 /* Can be done twice in libldap_r. See libldap_r/ldap_thr_debug.h. */ 23 LDAP_F(int) ldap_int_thread_initialize LDAP_P(( void )); 24 LDAP_F(int) ldap_int_thread_destroy LDAP_P(( void )); 25 26 LDAP_END_DECL 27 28 29 #ifndef _LDAP_INT_THREAD_H 30 #define _LDAP_INT_THREAD_H 31 32 #if defined( HAVE_PTHREADS ) 33 /********************************** 34 * * 35 * definitions for POSIX Threads * 36 * * 37 **********************************/ 38 39 #include <pthread.h> 40 #ifdef HAVE_SCHED_H 41 #include <sched.h> 42 #endif 43 44 LDAP_BEGIN_DECL 45 46 typedef pthread_t ldap_int_thread_t; 47 typedef pthread_mutex_t ldap_int_thread_mutex_t; 48 typedef pthread_cond_t ldap_int_thread_cond_t; 49 typedef pthread_key_t ldap_int_thread_key_t; 50 51 #define ldap_int_thread_equal(a, b) pthread_equal((a), (b)) 52 53 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \ 54 defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \ 55 defined( _POSIX_THREADSAFE_FUNCTIONS ) 56 #define HAVE_REENTRANT_FUNCTIONS 1 57 #endif 58 59 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \ 60 defined( HAVE_THR_GETCONCURRENCY ) 61 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1 62 #endif 63 64 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \ 65 defined( HAVE_THR_SETCONCURRENCY ) 66 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1 67 #endif 68 69 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY ) 70 #define LDAP_THREAD_HAVE_RDWR 1 71 typedef pthread_rwlock_t ldap_int_thread_rdwr_t; 72 #endif 73 74 #ifndef LDAP_INT_MUTEX_NULL 75 #define LDAP_INT_MUTEX_NULL PTHREAD_MUTEX_INITIALIZER 76 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0) 77 #endif 78 79 LDAP_END_DECL 80 81 #elif defined ( HAVE_MACH_CTHREADS ) 82 /********************************** 83 * * 84 * definitions for Mach CThreads * 85 * * 86 **********************************/ 87 88 #if defined( HAVE_MACH_CTHREADS_H ) 89 # include <mach/cthreads.h> 90 #elif defined( HAVE_CTHREADS_H ) 91 # include <cthreads.h> 92 #endif 93 94 LDAP_BEGIN_DECL 95 96 typedef cthread_t ldap_int_thread_t; 97 typedef struct mutex ldap_int_thread_mutex_t; 98 typedef struct condition ldap_int_thread_cond_t; 99 typedef cthread_key_t ldap_int_thread_key_t; 100 101 #ifndef LDAP_INT_MUTEX_NULL 102 #define LDAP_INT_MUTEX_NULL MUTEX_INITIALIZER 103 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0) 104 #endif 105 106 LDAP_END_DECL 107 108 #elif defined( HAVE_GNU_PTH ) 109 /*********************************** 110 * * 111 * thread definitions for GNU Pth * 112 * * 113 ***********************************/ 114 115 #define PTH_SYSCALL_SOFT 1 116 #include <pth.h> 117 118 LDAP_BEGIN_DECL 119 120 typedef pth_t ldap_int_thread_t; 121 typedef pth_mutex_t ldap_int_thread_mutex_t; 122 typedef pth_cond_t ldap_int_thread_cond_t; 123 typedef pth_key_t ldap_int_thread_key_t; 124 125 #if 0 126 #define LDAP_THREAD_HAVE_RDWR 1 127 typedef pth_rwlock_t ldap_int_thread_rdwr_t; 128 #endif 129 130 #ifndef LDAP_INT_MUTEX_NULL 131 #define LDAP_INT_MUTEX_NULL PTH_MUTEX_INIT 132 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0) 133 #endif 134 135 LDAP_END_DECL 136 137 #elif defined( HAVE_THR ) 138 /******************************************** 139 * * 140 * thread definitions for Solaris LWP (THR) * 141 * * 142 ********************************************/ 143 144 #include <thread.h> 145 #include <synch.h> 146 147 LDAP_BEGIN_DECL 148 149 typedef thread_t ldap_int_thread_t; 150 typedef mutex_t ldap_int_thread_mutex_t; 151 typedef cond_t ldap_int_thread_cond_t; 152 typedef thread_key_t ldap_int_thread_key_t; 153 154 #define HAVE_REENTRANT_FUNCTIONS 1 155 156 #ifdef HAVE_THR_GETCONCURRENCY 157 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1 158 #endif 159 #ifdef HAVE_THR_SETCONCURRENCY 160 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1 161 #endif 162 163 #ifndef LDAP_INT_MUTEX_NULL 164 #define LDAP_INT_MUTEX_NULL DEFAULTMUTEX 165 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0) 166 #endif 167 168 #elif defined(HAVE_NT_THREADS) 169 /************************************* 170 * * 171 * thread definitions for NT threads * 172 * * 173 *************************************/ 174 175 #include <process.h> 176 #include <windows.h> 177 178 LDAP_BEGIN_DECL 179 180 typedef unsigned long ldap_int_thread_t; 181 typedef HANDLE ldap_int_thread_mutex_t; 182 typedef HANDLE ldap_int_thread_cond_t; 183 typedef DWORD ldap_int_thread_key_t; 184 185 LDAP_F( int ) 186 ldap_int_mutex_firstcreate LDAP_P(( ldap_int_thread_mutex_t *mutex )); 187 188 #ifndef LDAP_INT_MUTEX_NULL 189 #define LDAP_INT_MUTEX_NULL ((HANDLE)0) 190 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \ 191 ldap_int_mutex_firstcreate(&(m)) 192 #endif 193 194 LDAP_END_DECL 195 196 #else 197 /*********************************** 198 * * 199 * thread definitions for no * 200 * underlying library support * 201 * * 202 ***********************************/ 203 204 #ifndef NO_THREADS 205 #define NO_THREADS 1 206 #endif 207 208 LDAP_BEGIN_DECL 209 210 typedef int ldap_int_thread_t; 211 typedef int ldap_int_thread_mutex_t; 212 typedef int ldap_int_thread_cond_t; 213 typedef int ldap_int_thread_key_t; 214 215 #define LDAP_THREAD_HAVE_TPOOL 1 216 typedef int ldap_int_thread_pool_t; 217 218 #ifndef LDAP_INT_MUTEX_NULL 219 #define LDAP_INT_MUTEX_NULL 0 220 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0) 221 #endif 222 223 LDAP_END_DECL 224 225 #endif /* no threads support */ 226 227 228 LDAP_BEGIN_DECL 229 230 #ifndef ldap_int_thread_equal 231 #define ldap_int_thread_equal(a, b) ((a) == (b)) 232 #endif 233 234 #ifndef LDAP_THREAD_HAVE_RDWR 235 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t; 236 #endif 237 238 LDAP_F(int) ldap_int_thread_pool_startup ( void ); 239 LDAP_F(int) ldap_int_thread_pool_shutdown ( void ); 240 241 #ifndef LDAP_THREAD_HAVE_TPOOL 242 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t; 243 #endif 244 245 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t; 246 LDAP_END_DECL 247 248 249 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U) 250 #define LDAP_THREAD_DEBUG_WRAP 1 251 #endif 252 253 #ifdef LDAP_THREAD_DEBUG_WRAP 254 /************************************** 255 * * 256 * definitions for type-wrapped debug * 257 * * 258 **************************************/ 259 260 LDAP_BEGIN_DECL 261 262 #ifndef LDAP_UINTPTR_T /* May be configured in CPPFLAGS */ 263 #define LDAP_UINTPTR_T unsigned long 264 #endif 265 266 typedef enum { 267 ldap_debug_magic = -(int) (((unsigned)-1)/19) 268 } ldap_debug_magic_t; 269 270 typedef enum { 271 /* Could fill in "locked" etc here later */ 272 ldap_debug_state_inited = (int) (((unsigned)-1)/11), 273 ldap_debug_state_destroyed 274 } ldap_debug_state_t; 275 276 typedef struct { 277 /* Enclosed in magic numbers in the hope of catching overwrites */ 278 ldap_debug_magic_t magic; /* bit pattern to recognize usages */ 279 LDAP_UINTPTR_T self; /* ~(LDAP_UINTPTR_T)&(this struct) */ 280 union ldap_debug_mem_u { /* Dummy memory reference */ 281 unsigned char *ptr; 282 LDAP_UINTPTR_T num; 283 } mem; 284 ldap_debug_state_t state; /* doubles as another magic number */ 285 } ldap_debug_usage_info_t; 286 287 typedef struct { 288 ldap_int_thread_mutex_t wrapped; 289 ldap_debug_usage_info_t usage; 290 ldap_int_thread_t owner; 291 } ldap_debug_thread_mutex_t; 292 293 #define LDAP_DEBUG_MUTEX_NULL {LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/} 294 #define LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \ 295 ((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m)))) 296 297 typedef struct { 298 ldap_int_thread_cond_t wrapped; 299 ldap_debug_usage_info_t usage; 300 } ldap_debug_thread_cond_t; 301 302 typedef struct { 303 ldap_int_thread_rdwr_t wrapped; 304 ldap_debug_usage_info_t usage; 305 } ldap_debug_thread_rdwr_t; 306 307 #ifndef NDEBUG 308 #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \ 309 ldap_debug_thread_assert_mutex_owner( \ 310 __FILE__, __LINE__, "owns(" #mutex ")", mutex ) 311 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P(( 312 LDAP_CONST char *file, 313 int line, 314 LDAP_CONST char *msg, 315 ldap_debug_thread_mutex_t *mutex )); 316 #endif /* NDEBUG */ 317 318 LDAP_END_DECL 319 320 #endif /* LDAP_THREAD_DEBUG_WRAP */ 321 322 #endif /* _LDAP_INT_THREAD_H */ 323