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 = option_info;
210 (strncasecmp( oi->name, s, optlen ) || oi->name[optlen]) )
215 fprintf( stderr, "Unknown $%s option '%.*s'\n",
216 "LDAP_THREAD_DEBUG", (int) optlen, s );
221 noabort = noerror = 1;
222 tracethreads = dupinit = 0;
225 #ifdef LDAP_THREAD_DEBUG_WRAP
226 if( nodebug || dupinit ) {
227 wraptype = Wrap_noalloc;
228 } else if( wraptype == Wrap_scramble ) {
229 const unsigned char *dummy = (const unsigned char *)&option_info;
230 if( sizeof(LDAP_UINTPTR_T) < sizeof(void *)
231 || (unsigned char *)~~(LDAP_UINTPTR_T) dummy != dummy
232 || (unsigned char *)~~(LDAP_UINTPTR_T) (unsigned char *)0 )
234 fprintf( stderr, "Misconfigured for $%s %s. Using %s.\n",
235 "LDAP_THREAD_DEBUG", "scramble", "adjptr" );
236 wraptype = Wrap_adjptr;
239 unwrap_offset = -(wrap_offset = (wraptype == Wrap_adjptr));
246 thread_name( char *buf, int bufsize, ldap_pvt_thread_t thread )
250 if( bufsize > 2*sizeof(thread) )
251 bufsize = 2*sizeof(thread);
252 for( i = 0; i < bufsize; i += 2 )
253 snprintf( buf+i, 3, "%02x", ((unsigned char *)&thread)[i/2] );
258 exit_thread_message( ldap_pvt_thread_t thread )
262 fprintf( stderr, "== Exiting thread %s ==\n",
263 thread_name( buf, sizeof(buf), thread ) );
268 #ifndef LDAP_THREAD_DEBUG_WRAP
270 #define WRAPPED(ptr) (ptr)
271 #define alloc_usage(ptr, msg) ((void) 0)
272 #define check_usage(ptr, msg) ((void) 0)
273 #define free_usage(ptr, msg) ((void) 0)
275 #define with_threads_lock(statement) { statement; }
276 #define get_new_thread_info(msg) NULL
277 #define update_thread_info(ti, th, det) {}
278 #define remove_thread_info(ti, msg) ((void)0)
279 #define get_thread_info(thread, msg) NULL
280 #define exiting_thread(msg) exit_thread_message(ldap_pvt_thread_self())
282 #else /* LDAP_THREAD_DEBUG_WRAP */
284 #define WRAPPED(ptr) (&(ptr)->wrapped)
286 #define INITED_VALUE 0x12345678UL
287 #define INITED_BYTE_VALUE 0xd5
289 /* Valid programs will access uninitialized memory here if dupinit. */
291 debug_already_initialized( const LDAP_UINTPTR_T *num )
294 * 'ret' keeps the Valgrind warning "Conditional jump or move
295 * depends on uninitialised value(s)" _inside_ this function.
297 volatile int ret = 0;
298 if( dupinit && *num == INITED_VALUE )
304 alloc_usage( ldap_debug_usage_info_t *usage, const char *msg )
308 if( wraptype == Wrap_noalloc ) {
309 ERROR_IF( debug_already_initialized( &usage->num ), msg );
310 usage->num = INITED_VALUE;
312 unsigned char *dummy = malloc( 1 );
313 assert( dummy != NULL );
314 *dummy = INITED_BYTE_VALUE;
315 if( wraptype == Wrap_scramble ) {
316 usage->num = ~(LDAP_UINTPTR_T) dummy;
317 /* Check that ptr<->integer casts work on this host */
318 assert( (unsigned char *)~usage->num == dummy );
320 usage->ptr = dummy + wrap_offset;
326 check_usage( ldap_debug_usage_info_t *usage, const char *msg )
328 if( wraptype == Wrap_noalloc ) {
329 ERROR_IF( usage->num != INITED_VALUE, msg );
330 } else if( wraptype == Wrap_scramble ) {
331 ERROR_IF( !usage->num, msg );
332 ERROR_IF( *(unsigned char *)~usage->num != INITED_BYTE_VALUE, msg );
334 ERROR_IF( !usage->ptr, msg );
335 ERROR_IF( usage->ptr[unwrap_offset] != INITED_BYTE_VALUE, msg );
340 free_usage( ldap_debug_usage_info_t *usage, const char *msg )
342 if( wraptype == Wrap_noalloc ) {
343 usage->num = ~(LDAP_UINTPTR_T)INITED_VALUE;
345 unsigned char *dummy = (wraptype == Wrap_scramble
346 ? (unsigned char *)~usage->num
347 : usage->ptr + unwrap_offset);
348 *(volatile unsigned char *)dummy = (unsigned char)-1;
353 #define with_threads_lock(statement) { \
355 int rc_wtl_ = ldap_int_thread_mutex_lock( &thread_info_mutex ); \
356 assert( rc_wtl_ == 0 ); \
360 int rc_wtl_ = ldap_int_thread_mutex_unlock( &thread_info_mutex ); \
361 assert( rc_wtl_ == 0 ); \
365 static ldap_debug_thread_t *
366 get_new_thread_info( const char *msg )
370 if( thread_info_used >= thread_info_size ) {
371 unsigned int more = thread_info_size + 1; /* debug value. increase. */
372 unsigned int new_size = thread_info_size + more;
373 ldap_debug_thread_t *t = calloc( more, sizeof(ldap_debug_thread_t) );
376 thread_info = realloc( thread_info, new_size * sizeof(*thread_info) );
377 assert( thread_info != NULL );
378 while( thread_info_size < new_size ) {
379 t->idx = thread_info_size;
380 thread_info[thread_info_size++] = t++;
383 alloc_usage( &thread_info[thread_info_used]->usage, msg );
384 return thread_info[thread_info_used++];
389 ldap_debug_thread_t *t,
390 const ldap_pvt_thread_t *thread,
394 t->wrapped = *thread;
395 t->detached = detached;
400 remove_thread_info( ldap_debug_thread_t *t, const char *msg )
403 ldap_debug_thread_t *last;
405 free_usage( &t->usage, msg );
407 assert( thread_info[idx] == t );
408 last = thread_info[--thread_info_used];
409 assert( last->idx == thread_info_used );
410 (thread_info[idx] = last)->idx = idx;
411 (thread_info[thread_info_used] = t )->idx = thread_info_used;
415 static ldap_debug_thread_t *
416 get_thread_info( ldap_pvt_thread_t thread, const char *msg )
419 ldap_debug_thread_t *t;
422 for( i = 0; i < thread_info_used; i++ ) {
423 if( ldap_pvt_thread_equal( thread, thread_info[i]->wrapped ) )
426 ERROR_IF( i == thread_info_used, msg );
428 check_usage( &t->usage, msg );
433 exiting_thread( const char *msg )
436 ldap_pvt_thread_t thread;
437 thread = ldap_pvt_thread_self();
438 exit_thread_message( thread );
440 ldap_debug_thread_t *t = get_thread_info( thread, msg );
442 remove_thread_info( t, msg );
447 #endif /* LDAP_THREAD_DEBUG_WRAP */
451 adjust_count( int which, int adjust )
458 rc = ldap_int_thread_mutex_lock( &resource_mutexes[which] );
460 resource_counts[which] += adjust;
461 rc = ldap_int_thread_mutex_unlock( &resource_mutexes[which] );
465 fputs( "...more ldap_debug_thread activity after exit...\n", stderr );
466 count = Count_reported_more;
468 case Count_reported_more:
469 /* Not used, but result might be inspected with debugger */
470 /* (Hopefully threading is disabled by now...) */
471 resource_counts[which] += adjust;
477 /* Wrappers for LDAP_THREAD_IMPLEMENTATION: */
479 /* Used instead of ldap_int_thread_initialize by ldap_pvt_thread_initialize */
481 ldap_debug_thread_initialize( void )
486 ERROR_IF( threading_enabled, "ldap_debug_thread_initialize" );
487 threading_enabled = 1;
488 rc = ldap_int_thread_initialize();
490 ERROR( rc, "ldap_debug_thread_initialize:threads" );
491 threading_enabled = 0;
493 rc2 = ldap_int_thread_mutex_init( &thread_info_mutex );
495 if( count != Count_no ) {
496 for( i = rc2 = 0; i < Idx_max; i++ )
497 rc2 |= ldap_int_thread_mutex_init( &resource_mutexes[i] );
499 /* FIXME: Only for static libldap_r as in init.c? If so, why? */
500 atexit( count_resource_leaks );
506 /* Used instead of ldap_int_thread_destroy by ldap_pvt_thread_destroy */
508 ldap_debug_thread_destroy( void )
511 ERROR_IF( !threading_enabled, "ldap_debug_thread_destroy" );
512 /* sleep(1) -- need to wait for thread pool to finish? */
513 rc = ldap_int_thread_destroy();
515 ERROR( rc, "ldap_debug_thread_destroy:threads" );
517 threading_enabled = 0;
523 ldap_pvt_thread_set_concurrency( int n )
526 ERROR_IF( !threading_enabled, "ldap_pvt_thread_set_concurrency" );
527 rc = ldap_int_thread_set_concurrency( n );
528 ERROR_IF( rc, "ldap_pvt_thread_set_concurrency" );
533 ldap_pvt_thread_get_concurrency( void )
536 ERROR_IF( !threading_enabled, "ldap_pvt_thread_get_concurrency" );
537 rc = ldap_int_thread_get_concurrency();
538 ERROR_IF( rc, "ldap_pvt_thread_get_concurrency" );
543 ldap_pvt_thread_sleep( unsigned int interval )
546 ERROR_IF( !threading_enabled, "ldap_pvt_thread_sleep" );
547 rc = ldap_int_thread_sleep( interval );
548 ERROR_IF( rc, "ldap_pvt_thread_sleep" );
553 ldap_pvt_thread_create(
554 ldap_pvt_thread_t *thread,
556 void *(*start_routine)( void * ),
560 ERROR_IF( !threading_enabled, "ldap_pvt_thread_create" );
564 ldap_debug_thread_t *t;
565 t = get_new_thread_info( "ldap_pvt_thread_create" );
566 rc = ldap_int_thread_create( thread, detach, start_routine, arg );
568 ERROR( rc, "ldap_pvt_thread_create" );
569 remove_thread_info( t, "ldap_pvt_thread_init" );
571 update_thread_info( t, thread, detach );
577 fprintf( stderr, "== Created thread %s%s ==\n",
578 thread_name( buf, sizeof(buf), *thread ),
579 detach ? " (detached)" : "" );
581 adjust_count( Idx_unexited_thread, +1 );
583 adjust_count( Idx_unjoined_thread, +1 );
589 ldap_pvt_thread_exit( void *retval )
591 #if 0 /* Detached threads may exit after ldap_debug_thread_destroy(). */
592 ERROR_IF( !threading_enabled, "ldap_pvt_thread_exit" );
594 adjust_count( Idx_unexited_thread, -1 );
595 exiting_thread( "ldap_pvt_thread_exit" );
596 ldap_int_thread_exit( retval );
600 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
603 ldap_debug_thread_t *t;
604 ERROR_IF( !threading_enabled, "ldap_pvt_thread_join" );
607 fprintf( stderr, "== Joining thread %s ==\n",
608 thread_name( buf, sizeof(buf), thread ) );
611 t = get_thread_info( thread, "ldap_pvt_thread_join" ) );
612 rc = ldap_int_thread_join( thread, thread_return );
614 ERROR( rc, "ldap_pvt_thread_join" );
617 remove_thread_info( t, "ldap_pvt_thread_join" ) );
618 adjust_count( Idx_unjoined_thread, -1 );
624 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
627 ERROR_IF( !threading_enabled, "ldap_pvt_thread_kill" );
630 fprintf( stderr, "== Killing thread %s (sig %i) ==\n",
631 thread_name( buf, sizeof(buf), thread ), signo );
633 rc = ldap_int_thread_kill( thread, signo );
634 ERROR_IF( rc, "ldap_pvt_thread_kill" );
639 ldap_pvt_thread_yield( void )
642 ERROR_IF( !threading_enabled, "ldap_pvt_thread_yield" );
643 rc = ldap_int_thread_yield();
644 ERROR_IF( rc, "ldap_pvt_thread_yield" );
649 ldap_pvt_thread_self( void )
651 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
652 ERROR_IF( !threading_enabled, "ldap_pvt_thread_self" );
654 return ldap_int_thread_self();
658 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
661 alloc_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
662 rc = ldap_int_thread_cond_init( WRAPPED( cond ) );
664 ERROR( rc, "ldap_pvt_thread_cond_init" );
665 free_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
667 adjust_count( Idx_cond, +1 );
673 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
676 check_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
677 rc = ldap_int_thread_cond_destroy( WRAPPED( cond ) );
679 ERROR( rc, "ldap_pvt_thread_cond_destroy" );
681 free_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
682 adjust_count( Idx_cond, -1 );
688 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
691 check_usage( &cond->usage, "ldap_pvt_thread_cond_signal" );
692 rc = ldap_int_thread_cond_signal( WRAPPED( cond ) );
693 ERROR_IF( rc, "ldap_pvt_thread_cond_signal" );
698 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
701 check_usage( &cond->usage, "ldap_pvt_thread_cond_broadcast" );
702 rc = ldap_int_thread_cond_broadcast( WRAPPED( cond ) );
703 ERROR_IF( rc, "ldap_pvt_thread_cond_broadcast" );
708 ldap_pvt_thread_cond_wait(
709 ldap_pvt_thread_cond_t *cond,
710 ldap_pvt_thread_mutex_t *mutex )
713 check_usage( &cond->usage, "ldap_pvt_thread_cond_wait:cond" );
714 check_usage( &mutex->usage, "ldap_pvt_thread_cond_wait:mutex" );
715 adjust_count( Idx_locked_mutex, -1 );
716 rc = ldap_int_thread_cond_wait( WRAPPED( cond ), WRAPPED( mutex ) );
717 adjust_count( Idx_locked_mutex, +1 );
718 ERROR_IF( rc, "ldap_pvt_thread_cond_wait" );
723 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
726 alloc_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
727 rc = ldap_int_thread_mutex_init( WRAPPED( mutex ) );
729 ERROR( rc, "ldap_pvt_thread_mutex_init" );
730 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
732 adjust_count( Idx_mutex, +1 );
738 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
741 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
742 rc = ldap_int_thread_mutex_destroy( WRAPPED( mutex ) );
744 ERROR( rc, "ldap_pvt_thread_mutex_destroy" );
746 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
747 adjust_count( Idx_mutex, -1 );
753 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
756 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_lock" );
757 rc = ldap_int_thread_mutex_lock( WRAPPED( mutex ) );
759 ERROR_IF( rc, "ldap_pvt_thread_mutex_lock" );
761 adjust_count( Idx_locked_mutex, +1 );
767 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
770 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_trylock" );
771 rc = ldap_int_thread_mutex_trylock( WRAPPED( mutex ) );
773 adjust_count( Idx_locked_mutex, +1 );
778 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
781 check_usage( &mutex->usage, "ldap_pvt_thread_mutex_unlock" );
782 rc = ldap_int_thread_mutex_unlock( WRAPPED( mutex ) );
784 ERROR_IF( rc, "ldap_pvt_thread_mutex_unlock" );
786 adjust_count( Idx_locked_mutex, -1 );
792 /* Wrappers for LDAP_THREAD_RDWR_IMPLEMENTATION: */
795 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
798 alloc_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
799 rc = ldap_int_thread_rdwr_init( WRAPPED( rwlock ) );
801 ERROR( rc, "ldap_pvt_thread_rdwr_init" );
802 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
804 adjust_count( Idx_rdwr, +1 );
810 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
813 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
814 rc = ldap_int_thread_rdwr_destroy( WRAPPED( rwlock ) );
816 ERROR( rc, "ldap_pvt_thread_rdwr_destroy" );
818 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
819 adjust_count( Idx_rdwr, -1 );
825 ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
828 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rlock" );
829 rc = ldap_int_thread_rdwr_rlock( WRAPPED( rwlock ) );
830 ERROR_IF( rc, "ldap_pvt_thread_rdwr_rlock" );
835 ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
837 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rtrylock" );
838 return ldap_int_thread_rdwr_rtrylock( WRAPPED( rwlock ) );
842 ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
845 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_runlock" );
846 rc = ldap_int_thread_rdwr_runlock( WRAPPED( rwlock ) );
847 ERROR_IF( rc, "ldap_pvt_thread_rdwr_runlock" );
852 ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
855 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wlock" );
856 rc = ldap_int_thread_rdwr_wlock( WRAPPED( rwlock ) );
857 ERROR_IF( rc, "ldap_pvt_thread_rdwr_wlock" );
862 ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
864 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wtrylock" );
865 return ldap_int_thread_rdwr_wtrylock( WRAPPED( rwlock ) );
869 ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
872 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wunlock" );
873 rc = ldap_int_thread_rdwr_wunlock( WRAPPED( rwlock ) );
874 ERROR_IF( rc, "ldap_pvt_thread_rdwr_wunlock" );
878 #ifdef LDAP_RDWR_DEBUG
881 ldap_pvt_thread_rdwr_readers( ldap_pvt_thread_rdwr_t *rwlock )
883 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_readers" );
884 return ldap_int_thread_rdwr_readers( WRAPPED( rwlock ) );
888 ldap_pvt_thread_rdwr_writers( ldap_pvt_thread_rdwr_t *rwlock )
890 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_writers" );
891 return ldap_int_thread_rdwr_writers( WRAPPED( rwlock ) );
895 ldap_pvt_thread_rdwr_active( ldap_pvt_thread_rdwr_t *rwlock )
897 check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_active" );
898 return ldap_int_thread_rdwr_active( WRAPPED( rwlock ) );
901 #endif /* LDAP_RDWR_DEBUG */
904 /* Some wrappers for LDAP_THREAD_POOL_IMPLEMENTATION: */
905 #ifdef LDAP_THREAD_POOL_IMPLEMENTATION
908 ldap_pvt_thread_pool_init(
909 ldap_pvt_thread_pool_t *tpool,
914 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_init" );
915 rc = ldap_int_thread_pool_init( tpool, max_threads, max_pending );
917 ERROR( rc, "ldap_pvt_thread_pool_init" );
919 adjust_count( Idx_tpool, +1 );
925 ldap_pvt_thread_pool_submit(
926 ldap_pvt_thread_pool_t *tpool,
927 ldap_pvt_thread_start_t *start_routine, void *arg )
930 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_submit" );
931 has_pool = (tpool && *tpool);
932 rc = ldap_int_thread_pool_submit( tpool, start_routine, arg );
934 ERROR_IF( rc, "ldap_pvt_thread_pool_submit" );
939 ldap_pvt_thread_pool_maxthreads(
940 ldap_pvt_thread_pool_t *tpool,
943 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_maxthreads" );
944 return ldap_int_thread_pool_maxthreads( tpool, max_threads );
948 ldap_pvt_thread_pool_backload( ldap_pvt_thread_pool_t *tpool )
950 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_backload" );
951 return ldap_int_thread_pool_backload( tpool );
955 ldap_pvt_thread_pool_destroy( ldap_pvt_thread_pool_t *tpool, int run_pending )
958 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_destroy" );
959 has_pool = (tpool && *tpool);
960 rc = ldap_int_thread_pool_destroy( tpool, run_pending );
963 ERROR( rc, "ldap_pvt_thread_pool_destroy" );
965 adjust_count( Idx_tpool, -1 );
972 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
974 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_pause" );
975 return ldap_int_thread_pool_pause( tpool );
979 ldap_pvt_thread_pool_resume( ldap_pvt_thread_pool_t *tpool )
981 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_resume" );
982 return ldap_int_thread_pool_resume( tpool );
986 ldap_pvt_thread_pool_getkey(
990 ldap_pvt_thread_pool_keyfree_t **kfree )
992 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
993 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_getkey" );
995 return ldap_int_thread_pool_getkey( xctx, key, data, kfree );
999 ldap_pvt_thread_pool_setkey(
1003 ldap_pvt_thread_pool_keyfree_t *kfree )
1006 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_setkey" );
1007 rc = ldap_int_thread_pool_setkey( xctx, key, data, kfree );
1008 ERROR_IF( rc, "ldap_pvt_thread_pool_setkey" );
1013 ldap_pvt_thread_pool_purgekey( void *key )
1015 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_purgekey" );
1016 ldap_int_thread_pool_purgekey( key );
1020 ldap_pvt_thread_pool_context( void )
1022 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
1023 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context" );
1025 return ldap_int_thread_pool_context();
1029 ldap_pvt_thread_pool_context_reset( void *vctx )
1031 ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context_reset" );
1032 ldap_int_thread_pool_context_reset( vctx );
1035 #endif /* LDAP_THREAD_POOL_IMPLEMENTATION */
1037 #endif /* LDAP_THREAD_DEBUG */