1 /* thr_debug.c - wrapper around the chosen thread wrapper, for debugging. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2007 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>.
18 * This package provides three types of thread operation debugging:
20 * - Print error messages and abort() when thread operations fail:
21 * Operations on threads, mutexes, condition variables, rdwr locks.
22 * Some thread pool operations are also checked, but not those for
23 * which failure can happen in normal slapd operation.
25 * - Wrap those types except threads and pools in structs that
26 * contain a state variable or a pointer to dummy allocated memory,
27 * and check that on all operations. The dummy memory variant lets
28 * malloc debuggers see some incorrect use as memory leaks, access
29 * to freed memory, etc.
31 * - Print a count of leaked thread resources after cleanup.
33 * Compile-time (./configure) setup: Macros defined in CPPFLAGS.
35 * LDAP_THREAD_DEBUG or LDAP_THREAD_DEBUG=2
36 * Enables debugging, but value & 2 turns off type wrapping.
38 * LDAP_UINTPTR_T=integer type to hold pointers, preferably unsigned.
39 * Used by dummy memory option "scramble". Default = unsigned long.
41 * In addition, you may need to set up an implementation-specific way
42 * to enable whatever error checking your thread library provides.
43 * Currently only implemented for Posix threads (pthreads), where
44 * you may need to define LDAP_INT_THREAD_MUTEXATTR. The default
45 * is PTHREAD_MUTEX_ERRORCHECK, or PTHREAD_MUTEX_ERRORCHECK_NP for
46 * Linux threads. See pthread_mutexattr_settype(3).
48 * Run-time configuration: Environment variable LDAP_THREAD_DEBUG.
50 * The variable may contain a comma- or space-separated option list.
52 * off - Disable this package.
54 * noabort - Do not abort() on errors.
55 * noerror - Do not report errors. Implies noabort.
56 * nocount - Do not report counts of unreleased resources.
57 * State variable/dummy memory, unless type wrapping is disabled:
58 * noalloc - Default. Use a state variable, not dummy memory.
59 * dupinit - Implies noalloc. Check if resources that have
60 * not been destroyed are reinitialized. Tools that
61 * report uninitialized memory access should disable
62 * such warnings about debug_already_initialized().
63 * alloc - Allocate dummy memory and store pointers as-is.
64 * Malloc debuggers might not notice unreleased
65 * resources in global variables as memory leaks.
66 * scramble - Store bitwise complement of dummy memory pointer.
67 * That never escapes memory leak detectors -
68 * but detection while the program is running will
69 * report active resources as leaks. Do not
70 * use this if a garbage collector is in use:-)
71 * adjptr - Point to end of dummy memory.
72 * Purify reports these as "potential leaks" (PLK).
73 * I have not checked other malloc debuggers.
75 * tracethreads - Report create/join/exit/kill of threads.
80 #if defined( LDAP_THREAD_DEBUG )
84 #include <ac/stdlib.h>
85 #include <ac/string.h>
87 #include "ldap_pvt_thread.h" /* Get the thread interface */
88 #define LDAP_THREAD_IMPLEMENTATION
89 #define LDAP_THREAD_DEBUG_IMPLEMENTATION
90 #define LDAP_THREAD_RDWR_IMPLEMENTATION
91 #define LDAP_THREAD_POOL_IMPLEMENTATION
92 #include "ldap_thr_debug.h" /* Get the underlying implementation */
95 /* Options from environment variable $LDAP_THREAD_DEBUG */
96 enum { Count_no = 0, Count_yes, Count_reported, Count_reported_more };
97 static int nodebug, noabort, noerror, count = Count_yes, options_done;
98 #ifdef LDAP_THREAD_DEBUG_WRAP
99 enum { Wrap_noalloc, Wrap_alloc, Wrap_scramble, Wrap_adjptr };
100 static int dupinit, wraptype = Wrap_noalloc, wrap_offset, unwrap_offset;
102 static int tracethreads;
104 static int threading_enabled;
107 Idx_unexited_thread, Idx_unjoined_thread, Idx_locked_mutex,
108 Idx_mutex, Idx_cond, Idx_rdwr, Idx_tpool, Idx_max
110 static int resource_counts[Idx_max];
111 static const char *const resource_names[] = {
112 "unexited threads", "unjoined threads", "locked mutexes",
113 "mutexes", "conds", "rdwrs", "thread pools"
115 static ldap_int_thread_mutex_t resource_mutexes[Idx_max];
119 * Making ldap_pvt_thread_t a wrapper around ldap_int_thread_t would
120 * slow down ldap_pvt_thread_self(), so keep a list of threads instead.
122 typedef struct ldap_debug_thread_s {
123 ldap_pvt_thread_t wrapped;
124 ldap_debug_usage_info_t usage;
127 } ldap_debug_thread_t;
129 static ldap_debug_thread_t **thread_info;
130 static unsigned int thread_info_size, thread_info_used;
131 static ldap_int_thread_mutex_t thread_info_mutex;
134 #define WARN(var, msg) (warn (__FILE__, __LINE__, (msg), #var, (var)))
135 #define ERROR(var,msg) (error(__FILE__, __LINE__, (msg), #var, (var)))
136 #define WARN_IF(rc, msg) {if (rc) warn (__FILE__, __LINE__, (msg), #rc, (rc));}
137 #define ERROR_IF(rc,msg) {if (rc) error(__FILE__, __LINE__, (msg), #rc, (rc));}
141 warn( const char *file, int line, const char *msg, const char *var, int val )
143 fprintf( stderr, "%s:%d: %s warning: %s is %d\n",
144 file, line, msg, var, val );
149 error( const char *file, int line, const char *msg, const char *var, int val )
152 fprintf( stderr, "%s:%d: %s error: %s is %d\n",
153 file, line, msg, var, val );
160 count_resource_leaks( void )
163 char errbuf[200], *delim = "Leaked";
164 if( count == Count_yes ) {
165 count = Count_reported;
166 #if 0 /* Could break if there are still threads after atexit */
167 for( i = j = 0; i < Idx_max; i++ )
168 j |= ldap_int_thread_mutex_destroy( &resource_mutexes[i] );
169 WARN_IF( j, "ldap_debug_thread_destroy:mutexes" );
171 for( i = j = 0; i < Idx_max; i++ ) {
172 if( resource_counts[i] ) {
173 j += sprintf( errbuf + j, "%s %d %s",
174 delim, resource_counts[i], resource_names[i] );
179 fprintf( stderr, "%s:%d: %s.\n", __FILE__, __LINE__, errbuf );
186 static const struct option_info_s {
190 { "off", &nodebug, 1 },
191 { "noabort", &noabort, 1 },
192 { "noerror", &noerror, 1 },
193 { "nocount", &count, Count_no },
194 #ifdef LDAP_THREAD_DEBUG_WRAP
195 { "noalloc", &wraptype, Wrap_noalloc },
196 { "dupinit", &dupinit, 1 },
197 { "alloc", &wraptype, Wrap_alloc },
198 { "adjptr", &wraptype, Wrap_adjptr },
199 { "scramble", &wraptype, Wrap_scramble },
201 { "tracethreads", &tracethreads, 1 },
204 const char *s = getenv( "LDAP_THREAD_DEBUG" );
206 while( *(s += strspn( s, ", \t\r\n" )) != '\0' ) {
207 size_t optlen = strcspn( s, ", \t\r\n" );
208 const struct option_info_s *oi;
209 for( oi = option_info; oi->name; oi++ ) {
210 if( strncasecmp( oi->name, s, optlen ) == 0 ) {
211 if( oi->name && oi->name[optlen] == '\0' ) {
214 fprintf( stderr, "Unknown $%s option '%.*s'\n",
215 "LDAP_THREAD_DEBUG", (int) optlen, s );
224 noabort = noerror = 1;
225 tracethreads = dupinit = 0;
228 #ifdef LDAP_THREAD_DEBUG_WRAP
229 if( nodebug || dupinit ) {
230 wraptype = Wrap_noalloc;
231 } else if( wraptype == Wrap_scramble ) {
232 const unsigned char *dummy = (const unsigned char *)&option_info;
233 if( sizeof(LDAP_UINTPTR_T) < sizeof(void *)
234 || (unsigned char *)~~(LDAP_UINTPTR_T) dummy != dummy
235 || (unsigned char *)~~(LDAP_UINTPTR_T) (unsigned char *)0 )
237 fprintf( stderr, "Misconfigured for $%s %s. Using %s.\n",
238 "LDAP_THREAD_DEBUG", "scramble", "adjptr" );
239 wraptype = Wrap_adjptr;
242 unwrap_offset = -(wrap_offset = (wraptype == Wrap_adjptr));
249 thread_name( char *buf, int bufsize, ldap_pvt_thread_t thread )
253 if( bufsize > 2*sizeof(thread) )
254 bufsize = 2*sizeof(thread);
255 for( i = 0; i < bufsize; i += 2 )
256 snprintf( buf+i, 3, "%02x", ((unsigned char *)&thread)[i/2] );
261 exit_thread_message( const ldap_pvt_thread_t thread )
265 fprintf( stderr, "== Exiting thread %s ==\n",
266 thread_name( buf, sizeof(buf), thread ) );
271 #ifndef LDAP_THREAD_DEBUG_WRAP
273 #define WRAPPED(ptr) (ptr)
274 #define alloc_usage(ptr, msg) ((void) 0)
275 #define check_usage(ptr, msg) ((void) 0)
276 #define free_usage(ptr, msg) ((void) 0)
278 #define with_threads_lock(statement) { statement; }
279 #define get_new_thread_info(msg) NULL
280 #define update_thread_info(ti, th, det) {}
281 #define remove_thread_info(ti, msg) ((void)0)
282 #define get_thread_info(thread, msg) NULL
283 #define exiting_thread(msg) exit_thread_message(ldap_pvt_thread_self())
285 #else /* LDAP_THREAD_DEBUG_WRAP */
287 #define WRAPPED(ptr) (&(ptr)->wrapped)
289 #define INITED_VALUE 0x12345678UL
290 #define INITED_BYTE_VALUE 0xd5
293 debug_already_initialized( const LDAP_UINTPTR_T *num )
295 /* Valid programs will access uninitialized memory if dupinit */
296 return dupinit && *num == INITED_VALUE;
300 alloc_usage( ldap_debug_usage_info_t *usage, const char *msg )
304 if( wraptype == Wrap_noalloc ) {
305 ERROR_IF( debug_already_initialized( &usage->num ), msg );
306 usage->num = INITED_VALUE;
308 unsigned char *dummy = malloc( 1 );
309 assert( dummy != NULL );
310 *dummy = INITED_BYTE_VALUE;
311 if( wraptype == Wrap_scramble ) {
312 usage->num = ~(LDAP_UINTPTR_T) dummy;
313 assert( (unsigned char *)~usage->num == dummy );
315 usage->ptr = dummy + wrap_offset;
321 check_usage( ldap_debug_usage_info_t *usage, const char *msg )
323 if( wraptype == Wrap_noalloc ) {
324 ERROR_IF( usage->num != INITED_VALUE, msg );
325 } else if( wraptype == Wrap_scramble ) {
326 ERROR_IF( !usage->num, msg );
327 ERROR_IF( *(unsigned char *)~usage->num != INITED_BYTE_VALUE, msg );
329 ERROR_IF( !usage->ptr, msg );
330 ERROR_IF( usage->ptr[unwrap_offset] != INITED_BYTE_VALUE, msg );
335 free_usage( ldap_debug_usage_info_t *usage, const char *msg )
337 if( wraptype == Wrap_noalloc ) {
338 usage->num = ~(LDAP_UINTPTR_T)INITED_VALUE;
340 unsigned char *dummy = (wraptype == Wrap_scramble
341 ? (unsigned char *)~usage->num
342 : usage->ptr + unwrap_offset);
343 *(volatile unsigned char *)dummy = (unsigned char)-1;
348 #define with_threads_lock(statement) { \
350 int rc_wtl_ = ldap_int_thread_mutex_lock( &thread_info_mutex ); \
351 assert( rc_wtl_ == 0 ); \
355 int rc_wtl_ = ldap_int_thread_mutex_unlock( &thread_info_mutex ); \
356 assert( rc_wtl_ == 0 ); \
360 static ldap_debug_thread_t *
361 get_new_thread_info( const char *msg )
365 if( thread_info_used >= thread_info_size ) {
366 unsigned int more = thread_info_size + 1; /* debug value. increase. */
367 unsigned int new_size = thread_info_size + more;
368 ldap_debug_thread_t *t = calloc( more, sizeof(ldap_debug_thread_t) );
371 thread_info = realloc( thread_info, new_size * sizeof(*thread_info) );
372 assert( thread_info != NULL );
373 while( thread_info_size < new_size ) {
374 t->idx = thread_info_size;
375 thread_info[thread_info_size++] = t++;
378 alloc_usage( &thread_info[thread_info_used]->usage, msg );
379 return thread_info[thread_info_used++];
384 ldap_debug_thread_t *t,
385 const ldap_pvt_thread_t *thread,
389 t->wrapped = *thread;
390 t->detached = detached;
395 remove_thread_info( ldap_debug_thread_t *t, const char *msg )
398 ldap_debug_thread_t *last;
400 free_usage( &t->usage, msg );
402 assert( thread_info[idx] == t );
403 last = thread_info[--thread_info_used];
404 assert( last->idx == thread_info_used );
405 (thread_info[idx] = last)->idx = idx;
406 (thread_info[thread_info_used] = t )->idx = thread_info_used;
410 ldap_debug_thread_t *
411 get_thread_info( ldap_pvt_thread_t *thread, const char *msg )
414 ldap_debug_thread_t *t;
417 for( i = 0; i < thread_info_used; i++ ) {
418 if( ldap_pvt_thread_equal( *thread, thread_info[i]->wrapped ) )
421 ERROR_IF( i == thread_info_used, msg );
423 check_usage( &t->usage, msg );
428 exiting_thread( const char *msg )
431 ldap_pvt_thread_t thread;
432 thread = ldap_pvt_thread_self();
433 exit_thread_message( thread );
435 ldap_debug_thread_t *t = get_thread_info( &thread, msg );
437 remove_thread_info( t, msg );
442 #endif /* LDAP_THREAD_DEBUG_WRAP */
446 adjust_count( int which, int adjust )
453 rc = ldap_int_thread_mutex_lock( &resource_mutexes[which] );
455 resource_counts[which] += adjust;
456 rc = ldap_int_thread_mutex_unlock( &resource_mutexes[which] );
460 fputs( "...more ldap_debug_thread activity after exit...\n", stderr );
461 count = Count_reported_more;
463 case Count_reported_more:
464 /* Not used, but result might be inspected with debugger */
465 /* (Hopefully threading is disabled by now...) */
466 resource_counts[which] += adjust;
472 /* Wrappers for LDAP_THREAD_IMPLEMENTATION: */
474 /* Used instead of ldap_int_thread_initialize by ldap_pvt_thread_initialize */
476 ldap_debug_thread_initialize( void )
481 ERROR_IF( threading_enabled, "ldap_debug_thread_initialize" );
482 threading_enabled = 1;
483 rc = ldap_int_thread_initialize();
485 ERROR( rc, "ldap_debug_thread_initialize:threads" );
486 threading_enabled = 0;
488 rc2 = ldap_int_thread_mutex_init( &thread_info_mutex );
490 if( count != Count_no ) {
491 for( i = rc2 = 0; i < Idx_max; i++ )
492 rc2 |= ldap_int_thread_mutex_init( &resource_mutexes[i] );
494 /* FIXME: Only for static libldap_r as in init.c? If so, why? */
495 atexit( count_resource_leaks );
501 /* Used instead of ldap_int_thread_destroy by ldap_pvt_thread_destroy */
503 ldap_debug_thread_destroy( void )
506 ERROR_IF( !threading_enabled, "ldap_debug_thread_destroy" );
507 /* sleep(1) -- need to wait for thread pool to finish? */
508 rc = ldap_int_thread_destroy();
510 ERROR( rc, "ldap_debug_thread_destroy:threads" );
512 threading_enabled = 0;
518 ldap_pvt_thread_set_concurrency( int n )
521 ERROR_IF( !threading_enabled, "ldap_pvt_thread_set_concurrency" );
522 rc = ldap_int_thread_set_concurrency( n );
523 ERROR_IF( rc, "ldap_pvt_thread_set_concurrency" );
528 ldap_pvt_thread_get_concurrency( void )
531 ERROR_IF( !threading_enabled, "ldap_pvt_thread_get_concurrency" );
532 rc = ldap_int_thread_get_concurrency();
533 ERROR_IF( rc, "ldap_pvt_thread_get_concurrency" );
538 ldap_pvt_thread_sleep( unsigned int interval )
541 ERROR_IF( !threading_enabled, "ldap_pvt_thread_sleep" );
542 rc = ldap_int_thread_sleep( interval );
543 ERROR_IF( rc, "ldap_pvt_thread_sleep" );
548 ldap_pvt_thread_create(
549 ldap_pvt_thread_t *thread,
551 void *(*start_routine)( void * ),
555 ERROR_IF( !threading_enabled, "ldap_pvt_thread_create" );
559 ldap_debug_thread_t *t;
560 t = get_new_thread_info( "ldap_pvt_thread_create" );
561 rc = ldap_int_thread_create( thread, detach, start_routine, arg );
563 ERROR( rc, "ldap_pvt_thread_create" );
564 remove_thread_info( t, "ldap_pvt_thread_init" );
566 update_thread_info( t, thread, detach );
572 fprintf( stderr, "== Created thread %s%s ==\n",
573 thread_name( buf, sizeof(buf), *thread ),
574 detach ? " (detached)" : "" );
576 adjust_count( Idx_unexited_thread, +1 );
578 adjust_count( Idx_unjoined_thread, +1 );
584 ldap_pvt_thread_exit( void *retval )
586 ERROR_IF( !threading_enabled, "ldap_pvt_thread_exit" );
587 adjust_count( Idx_unexited_thread, -1 );
588 exiting_thread( "ldap_pvt_thread_exit" );
589 ldap_int_thread_exit( retval );
593 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
596 ldap_debug_thread_t *t;
597 ERROR_IF( !threading_enabled, "ldap_pvt_thread_join" );
600 fprintf( stderr, "== Joining thread %s ==\n",
601 thread_name( buf, sizeof(buf), thread ) );
604 t = get_thread_info( &thread, "ldap_pvt_thread_join" ) );
605 rc = ldap_int_thread_join( thread, thread_return );
607 ERROR( rc, "ldap_pvt_thread_join" );
610 remove_thread_info( t, "ldap_pvt_thread_join" ) );
611 adjust_count( Idx_unjoined_thread, -1 );
617 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
620 ERROR_IF( !threading_enabled, "ldap_pvt_thread_kill" );
623 fprintf( stderr, "== Killing thread %s (sig %i) ==\n",
624 thread_name( buf, sizeof(buf), thread ), signo );
626 rc = ldap_int_thread_kill( thread, signo );
627 ERROR_IF( rc, "ldap_pvt_thread_kill" );
632 ldap_pvt_thread_yield( void )
635 ERROR_IF( !threading_enabled, "ldap_pvt_thread_yield" );
636 rc = ldap_int_thread_yield();
637 ERROR_IF( rc, "ldap_pvt_thread_yield" );
642 ldap_pvt_thread_self( void )
644 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
645 ERROR_IF( !threading_enabled, "ldap_pvt_thread_self" );
647 return ldap_int_thread_self();
651 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
654 alloc_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
655 rc = ldap_int_thread_cond_init( WRAPPED( cond ) );
657 ERROR( rc, "ldap_pvt_thread_cond_init" );
658 free_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
660 adjust_count( Idx_cond, +1 );
666 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
669 check_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
670 rc = ldap_int_thread_cond_destroy( WRAPPED( cond ) );
672 ERROR( rc, "ldap_pvt_thread_cond_destroy" );
674 free_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
675 adjust_count( Idx_cond, -1 );
681 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
684 check_usage( &cond->usage, "ldap_pvt_thread_cond_signal" );
685 rc = ldap_int_thread_cond_signal( WRAPPED( cond ) );
686 ERROR_IF( rc, "ldap_pvt_thread_cond_signal" );
691 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
694 check_usage( &cond->usage, "ldap_pvt_thread_cond_broadcast" );
695 rc = ldap_int_thread_cond_broadcast( WRAPPED( cond ) );
696 ERROR_IF( rc, "ldap_pvt_thread_cond_broadcast" );
701 ldap_pvt_thread_cond_wait(
702 ldap_pvt_thread_cond_t *cond,
703 ldap_pvt_thread_mutex_t *mutex )
706 check_usage( &cond->usage, "ldap_pvt_thread_cond_wait:cond" );
707 check_usage( &mutex->usage, "ldap_pvt_thread_cond_wait:mutex" );
708 adjust_count( Idx_locked_mutex, -1 );
709 rc = ldap_int_thread_cond_wait( WRAPPED( cond ), WRAPPED( mutex ) );
710 adjust_count( Idx_locked_mutex, +1 );
711 ERROR_IF( rc, "ldap_pvt_thread_cond_wait" );
716 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
719 alloc_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
720 rc = ldap_int_thread_mutex_init( WRAPPED( mutex ) );
722 ERROR( rc, "ldap_pvt_thread_mutex_init" );
723 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
725 adjust_count( Idx_mutex, +1 );
731 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
734 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
735 rc = ldap_int_thread_mutex_destroy( WRAPPED( mutex ) );
737 ERROR( rc, "ldap_pvt_thread_mutex_destroy" );
739 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
740 adjust_count( Idx_mutex, -1 );
746 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
749 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_lock" );
750 rc = ldap_int_thread_mutex_lock( WRAPPED( mutex ) );
752 ERROR_IF( rc, "ldap_pvt_thread_mutex_lock" );
754 adjust_count( Idx_locked_mutex, +1 );
760 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
763 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_trylock" );
764 rc = ldap_int_thread_mutex_trylock( WRAPPED( mutex ) );
766 adjust_count( Idx_locked_mutex, +1 );
771 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
774 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_unlock" );
775 rc = ldap_int_thread_mutex_unlock( WRAPPED( mutex ) );
777 ERROR_IF( rc, "ldap_pvt_thread_mutex_unlock" );
779 adjust_count( Idx_locked_mutex, -1 );
785 /* Wrappers for LDAP_THREAD_RDWR_IMPLEMENTATION: */
788 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
791 alloc_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
792 rc = ldap_int_thread_rdwr_init( WRAPPED( rwlock ) );
794 ERROR( rc, "ldap_pvt_thread_rdwr_init" );
795 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
797 adjust_count( Idx_rdwr, +1 );
803 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
806 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
807 rc = ldap_int_thread_rdwr_destroy( WRAPPED( rwlock ) );
809 ERROR( rc, "ldap_pvt_thread_rdwr_destroy" );
811 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
812 adjust_count( Idx_rdwr, -1 );
818 ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
821 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rlock" );
822 rc = ldap_int_thread_rdwr_rlock( WRAPPED( rwlock ) );
823 ERROR_IF( rc, "ldap_pvt_thread_rdwr_rlock" );
828 ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
830 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rtrylock" );
831 return ldap_int_thread_rdwr_rtrylock( WRAPPED( rwlock ) );
835 ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
838 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_runlock" );
839 rc = ldap_int_thread_rdwr_runlock( WRAPPED( rwlock ) );
840 ERROR_IF( rc, "ldap_pvt_thread_rdwr_runlock" );
845 ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
848 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wlock" );
849 rc = ldap_int_thread_rdwr_wlock( WRAPPED( rwlock ) );
850 ERROR_IF( rc, "ldap_pvt_thread_rdwr_wlock" );
855 ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
857 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wtrylock" );
858 return ldap_int_thread_rdwr_wtrylock( WRAPPED( rwlock ) );
862 ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
865 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wunlock" );
866 rc = ldap_int_thread_rdwr_wunlock( WRAPPED( rwlock ) );
867 ERROR_IF( rc, "ldap_pvt_thread_rdwr_wunlock" );
871 #ifdef LDAP_RDWR_DEBUG
874 ldap_pvt_thread_rdwr_readers( ldap_pvt_thread_rdwr_t *rwlock )
876 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_readers" );
877 return ldap_int_thread_rdwr_readers( WRAPPED( rwlock ) );
881 ldap_pvt_thread_rdwr_writers( ldap_pvt_thread_rdwr_t *rwlock )
883 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_writers" );
884 return ldap_int_thread_rdwr_writers( WRAPPED( rwlock ) );
888 ldap_pvt_thread_rdwr_active( ldap_pvt_thread_rdwr_t *rwlock )
890 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_active" );
891 return ldap_int_thread_rdwr_active( WRAPPED( rwlock ) );
894 #endif /* LDAP_RDWR_DEBUG */
897 /* Some wrappers for LDAP_THREAD_POOL_IMPLEMENTATION: */
898 #ifdef LDAP_THREAD_POOL_IMPLEMENTATION
901 ldap_pvt_thread_pool_init(
902 ldap_pvt_thread_pool_t *tpool,
907 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_init" );
908 rc = ldap_int_thread_pool_init( tpool, max_threads, max_pending );
910 ERROR( rc, "ldap_pvt_thread_pool_init" );
912 adjust_count( Idx_tpool, +1 );
918 ldap_pvt_thread_pool_submit(
919 ldap_pvt_thread_pool_t *tpool,
920 ldap_pvt_thread_start_t *start_routine, void *arg )
923 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_submit" );
924 has_pool = (tpool != NULL && *tpool != NULL);
925 rc = ldap_int_thread_pool_submit( tpool, start_routine, arg );
927 ERROR_IF( rc, "ldap_pvt_thread_pool_submit" );
932 ldap_pvt_thread_pool_maxthreads(
933 ldap_pvt_thread_pool_t *tpool,
936 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_maxthreads" );
937 return ldap_int_thread_pool_maxthreads( tpool, max_threads );
941 ldap_pvt_thread_pool_backload( ldap_pvt_thread_pool_t *tpool )
943 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_backload" );
944 return ldap_int_thread_pool_backload( tpool );
948 ldap_pvt_thread_pool_destroy( ldap_pvt_thread_pool_t *tpool, int run_pending )
951 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_destroy" );
952 has_pool = (tpool != NULL && *tpool != NULL);
953 rc = ldap_int_thread_pool_destroy( tpool, run_pending );
956 ERROR( rc, "ldap_pvt_thread_pool_destroy" );
958 adjust_count( Idx_tpool, -1 );
965 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
967 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_pause" );
968 return ldap_int_thread_pool_pause( tpool );
972 ldap_pvt_thread_pool_resume( ldap_pvt_thread_pool_t *tpool )
974 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_resume" );
975 return ldap_int_thread_pool_resume( tpool );
979 ldap_pvt_thread_pool_getkey(
983 ldap_pvt_thread_pool_keyfree_t **kfree )
985 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
986 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_getkey" );
988 return ldap_int_thread_pool_getkey( xctx, key, data, kfree );
992 ldap_pvt_thread_pool_setkey(
996 ldap_pvt_thread_pool_keyfree_t *kfree )
999 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_setkey" );
1000 rc = ldap_int_thread_pool_setkey( xctx, key, data, kfree );
1001 ERROR_IF( rc, "ldap_pvt_thread_pool_setkey" );
1006 ldap_pvt_thread_pool_purgekey( void *key )
1008 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_purgekey" );
1009 ldap_int_thread_pool_purgekey( key );
1013 ldap_pvt_thread_pool_context( void )
1015 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
1016 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context" );
1018 return ldap_int_thread_pool_context();
1022 ldap_pvt_thread_pool_context_reset( void *vctx )
1024 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context_reset" );
1025 ldap_int_thread_pool_context_reset( vctx );
1028 #endif /* LDAP_THREAD_POOL_IMPLEMENTATION */
1030 #endif /* LDAP_THREAD_DEBUG */