Home | History | Annotate | Line # | Download | only in include
      1 /*	$NetBSD: ldap_int_thread.h,v 1.4 2025/09/05 21:16:19 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-2024 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.  See libldap/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 #ifndef _LDAP_INT_THREAD_H
     29 #define _LDAP_INT_THREAD_H
     30 
     31 #if defined( HAVE_PTHREADS )
     32 /**********************************
     33  *                                *
     34  * definitions for POSIX Threads  *
     35  *                                *
     36  **********************************/
     37 
     38 #include <pthread.h>
     39 #ifdef HAVE_SCHED_H
     40 #include <sched.h>
     41 #endif
     42 
     43 LDAP_BEGIN_DECL
     44 
     45 typedef pthread_t		ldap_int_thread_t;
     46 typedef pthread_mutex_t		ldap_int_thread_mutex_t;
     47 typedef pthread_cond_t		ldap_int_thread_cond_t;
     48 typedef pthread_key_t		ldap_int_thread_key_t;
     49 
     50 #define ldap_int_thread_equal(a, b)	pthread_equal((a), (b))
     51 
     52 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
     53 	defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
     54 	defined( _POSIX_THREADSAFE_FUNCTIONS )
     55 #define HAVE_REENTRANT_FUNCTIONS 1
     56 #endif
     57 
     58 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
     59 	defined( HAVE_THR_GETCONCURRENCY )
     60 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
     61 #endif
     62 
     63 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
     64 	defined( HAVE_THR_SETCONCURRENCY )
     65 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
     66 #endif
     67 
     68 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
     69 #define LDAP_THREAD_HAVE_RDWR 1
     70 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
     71 #endif
     72 
     73 #ifndef LDAP_INT_MUTEX_NULL
     74 #define LDAP_INT_MUTEX_NULL	PTHREAD_MUTEX_INITIALIZER
     75 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
     76 #endif
     77 
     78 LDAP_END_DECL
     79 
     80 #elif defined( HAVE_GNU_PTH )
     81 /***********************************
     82  *                                 *
     83  * thread definitions for GNU Pth  *
     84  *                                 *
     85  ***********************************/
     86 
     87 #define PTH_SYSCALL_SOFT 1
     88 #include <pth.h>
     89 
     90 LDAP_BEGIN_DECL
     91 
     92 typedef pth_t		ldap_int_thread_t;
     93 typedef pth_mutex_t	ldap_int_thread_mutex_t;
     94 typedef pth_cond_t	ldap_int_thread_cond_t;
     95 typedef pth_key_t	ldap_int_thread_key_t;
     96 
     97 #if 0
     98 #define LDAP_THREAD_HAVE_RDWR 1
     99 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
    100 #endif
    101 
    102 #ifndef LDAP_INT_MUTEX_NULL
    103 #define LDAP_INT_MUTEX_NULL	PTH_MUTEX_INIT
    104 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
    105 #endif
    106 
    107 LDAP_END_DECL
    108 
    109 #elif defined( HAVE_THR )
    110 /********************************************
    111  *                                          *
    112  * thread definitions for Solaris LWP (THR) *
    113  *                                          *
    114  ********************************************/
    115 
    116 #include <thread.h>
    117 #include <synch.h>
    118 
    119 LDAP_BEGIN_DECL
    120 
    121 typedef thread_t		ldap_int_thread_t;
    122 typedef mutex_t			ldap_int_thread_mutex_t;
    123 typedef cond_t			ldap_int_thread_cond_t;
    124 typedef thread_key_t	ldap_int_thread_key_t;
    125 
    126 #define HAVE_REENTRANT_FUNCTIONS 1
    127 
    128 #ifdef HAVE_THR_GETCONCURRENCY
    129 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
    130 #endif
    131 #ifdef HAVE_THR_SETCONCURRENCY
    132 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
    133 #endif
    134 
    135 #ifndef LDAP_INT_MUTEX_NULL
    136 #define LDAP_INT_MUTEX_NULL	DEFAULTMUTEX
    137 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
    138 #endif
    139 
    140 #elif defined(HAVE_NT_THREADS)
    141 /*************************************
    142  *                                   *
    143  * thread definitions for NT threads *
    144  *                                   *
    145  *************************************/
    146 
    147 #include <process.h>
    148 #include <windows.h>
    149 
    150 LDAP_BEGIN_DECL
    151 
    152 typedef unsigned long	ldap_int_thread_t;
    153 typedef HANDLE	ldap_int_thread_mutex_t;
    154 typedef HANDLE	ldap_int_thread_cond_t;
    155 typedef DWORD	ldap_int_thread_key_t;
    156 
    157 LDAP_F( int )
    158 ldap_int_mutex_firstcreate LDAP_P(( ldap_int_thread_mutex_t *mutex ));
    159 
    160 #ifndef LDAP_INT_MUTEX_NULL
    161 #define LDAP_INT_MUTEX_NULL		((HANDLE)0)
    162 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
    163 		ldap_int_mutex_firstcreate(&(m))
    164 #endif
    165 
    166 LDAP_END_DECL
    167 
    168 #else
    169 /***********************************
    170  *                                 *
    171  * thread definitions for no       *
    172  * underlying library support      *
    173  *                                 *
    174  ***********************************/
    175 
    176 #ifndef NO_THREADS
    177 #define NO_THREADS 1
    178 #endif
    179 
    180 LDAP_BEGIN_DECL
    181 
    182 typedef int			ldap_int_thread_t;
    183 typedef int			ldap_int_thread_mutex_t;
    184 typedef int			ldap_int_thread_cond_t;
    185 typedef int			ldap_int_thread_key_t;
    186 
    187 #define LDAP_THREAD_HAVE_TPOOL 1
    188 typedef int			ldap_int_thread_pool_t;
    189 
    190 #ifndef LDAP_INT_MUTEX_NULL
    191 #define LDAP_INT_MUTEX_NULL				0
    192 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
    193 #endif
    194 
    195 LDAP_END_DECL
    196 
    197 #endif /* no threads support */
    198 
    199 
    200 LDAP_BEGIN_DECL
    201 
    202 #ifndef ldap_int_thread_equal
    203 #define ldap_int_thread_equal(a, b)	((a) == (b))
    204 #endif
    205 
    206 #ifndef LDAP_THREAD_HAVE_RDWR
    207 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
    208 #endif
    209 
    210 LDAP_F(int) ldap_int_thread_pool_startup ( void );
    211 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
    212 
    213 #ifndef LDAP_THREAD_HAVE_TPOOL
    214 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
    215 #endif
    216 LDAP_END_DECL
    217 
    218 
    219 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
    220 #define LDAP_THREAD_DEBUG_WRAP 1
    221 #endif
    222 
    223 #ifdef LDAP_THREAD_DEBUG_WRAP
    224 /**************************************
    225  *                                    *
    226  * definitions for type-wrapped debug *
    227  *                                    *
    228  **************************************/
    229 
    230 LDAP_BEGIN_DECL
    231 
    232 #ifndef LDAP_UINTPTR_T	/* May be configured in CPPFLAGS */
    233 #define LDAP_UINTPTR_T	unsigned long
    234 #endif
    235 
    236 typedef enum {
    237 	ldap_debug_magic =	-(int) (((unsigned)-1)/19)
    238 } ldap_debug_magic_t;
    239 
    240 typedef enum {
    241 	/* Could fill in "locked" etc here later */
    242 	ldap_debug_state_inited = (int) (((unsigned)-1)/11),
    243 	ldap_debug_state_destroyed
    244 } ldap_debug_state_t;
    245 
    246 typedef struct {
    247 	/* Enclosed in magic numbers in the hope of catching overwrites */
    248 	ldap_debug_magic_t	magic;	/* bit pattern to recognize usages  */
    249 	LDAP_UINTPTR_T		self;	/* ~(LDAP_UINTPTR_T)&(this struct) */
    250 	union ldap_debug_mem_u {	/* Dummy memory reference */
    251 		unsigned char	*ptr;
    252 		LDAP_UINTPTR_T	num;
    253 	} mem;
    254 	ldap_debug_state_t	state;	/* doubles as another magic number */
    255 } ldap_debug_usage_info_t;
    256 
    257 typedef struct {
    258 	ldap_int_thread_mutex_t	wrapped;
    259 	ldap_debug_usage_info_t	usage;
    260 	ldap_int_thread_t	owner;
    261 } ldap_debug_thread_mutex_t;
    262 
    263 #define	LDAP_DEBUG_MUTEX_NULL	{LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
    264 #define	LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
    265 	((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
    266 
    267 typedef struct {
    268 	ldap_int_thread_cond_t	wrapped;
    269 	ldap_debug_usage_info_t	usage;
    270 } ldap_debug_thread_cond_t;
    271 
    272 typedef struct {
    273 	ldap_int_thread_rdwr_t	wrapped;
    274 	ldap_debug_usage_info_t	usage;
    275 } ldap_debug_thread_rdwr_t;
    276 
    277 #ifndef NDEBUG
    278 #define	LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
    279 	ldap_debug_thread_assert_mutex_owner( \
    280 		__FILE__, __LINE__, "owns(" #mutex ")", mutex )
    281 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
    282 	LDAP_CONST char *file,
    283 	int line,
    284 	LDAP_CONST char *msg,
    285 	ldap_debug_thread_mutex_t *mutex ));
    286 #endif /* NDEBUG */
    287 
    288 LDAP_END_DECL
    289 
    290 #endif /* LDAP_THREAD_DEBUG_WRAP */
    291 
    292 #endif /* _LDAP_INT_THREAD_H */
    293