]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_debug.c
happy new year
[openldap] / libraries / libldap_r / thr_debug.c
1 /* thr_debug.c - wrapper around the chosen thread wrapper, for debugging. */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2007 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
17 /*
18  * This package provides three types of thread operation debugging:
19  *
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.
24  *
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.
30  *
31  * - Print a count of leaked thread resources after cleanup.
32  *
33  * Compile-time (./configure) setup:  Macros defined in CPPFLAGS.
34  *
35  *   LDAP_THREAD_DEBUG or LDAP_THREAD_DEBUG=2
36  *      Enables debugging, but value & 2 turns off type wrapping.
37  *
38  *   LDAP_UINTPTR_T=integer type to hold pointers, preferably unsigned.
39  *      Used by dummy memory option "scramble". Default = unsigned long.
40  *
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).
47  *
48  * Run-time configuration:  Environment variable LDAP_THREAD_DEBUG.
49  *
50  *   The variable may contain a comma- or space-separated option list.
51  *   Options:
52  *      off      - Disable this package.
53  *   Error checking:
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.
74  *   Tracing:
75  *      tracethreads - Report create/join/exit/kill of threads.
76  */
77
78 #include "portable.h"
79
80 #if defined( LDAP_THREAD_DEBUG )
81
82 #include <stdio.h>
83 #include <ac/errno.h>
84 #include <ac/stdlib.h>
85 #include <ac/string.h>
86
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 */
93
94
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;
101 #endif
102 static int tracethreads;
103
104 static int threading_enabled;
105
106 enum {
107         Idx_unexited_thread, Idx_unjoined_thread, Idx_locked_mutex,
108         Idx_mutex, Idx_cond, Idx_rdwr, Idx_tpool, Idx_max
109 };
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"
114 };
115 static ldap_int_thread_mutex_t resource_mutexes[Idx_max];
116
117
118 /*
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.
121  */
122 typedef struct ldap_debug_thread_s {
123         ldap_pvt_thread_t                       wrapped;
124         ldap_debug_usage_info_t         usage;
125         int                                                     detached;
126         int                                                     freeme, idx;
127 } ldap_debug_thread_t;
128
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;
132
133
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));}
138
139 #if 0
140 static void
141 warn( const char *file, int line, const char *msg, const char *var, int val )
142 {
143         fprintf( stderr, "%s:%d: %s warning: %s is %d\n",
144                 file, line, msg, var, val );
145 }
146 #endif
147
148 static void
149 error( const char *file, int line, const char *msg, const char *var, int val )
150 {
151         if( !noerror ) {
152                 fprintf( stderr, "%s:%d: %s error: %s is %d\n",
153                         file, line, msg, var, val );
154                 if( !noabort )
155                         abort();
156         }
157 }
158
159 static void
160 count_resource_leaks( void )
161 {
162         int i, j;
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" );
170 #endif
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] );
175                                 delim = ",";
176                         }
177                 }
178                 if( j )
179                         fprintf( stderr, "%s:%d: %s.\n", __FILE__, __LINE__, errbuf );
180         }
181 }
182
183 static void
184 get_options( void )
185 {
186         static const struct option_info_s {
187                 const char      *name;
188                 int             *var, val;
189         } option_info[] = {
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 },
200 #endif
201                 { "tracethreads", &tracethreads, 1 },
202                 { NULL, NULL, 0 }
203         };
204         const char *s = getenv( "LDAP_THREAD_DEBUG" );
205         if( s != NULL ) {
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' ) {
212                                                 *oi->var = oi->val;
213                                         } else {
214                                                 fprintf( stderr, "Unknown $%s option '%.*s'\n",
215                                                         "LDAP_THREAD_DEBUG", (int) optlen, s );
216                                         }
217                                         break;
218                                 }
219                         }
220                         s += optlen;
221                 }
222         }
223         if( nodebug ) {
224                 noabort = noerror = 1;
225                 tracethreads = dupinit = 0;
226                 count = Count_no;
227         }
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 )
236                 {
237                         fprintf( stderr, "Misconfigured for $%s %s.  Using %s.\n",
238                                 "LDAP_THREAD_DEBUG", "scramble", "adjptr" );
239                         wraptype = Wrap_adjptr;
240                 }
241         }
242         unwrap_offset = -(wrap_offset = (wraptype == Wrap_adjptr));
243 #endif
244         options_done = 1;
245 }
246
247
248 static char *
249 thread_name( char *buf, int bufsize, ldap_pvt_thread_t thread )
250 {
251         int i;
252         --bufsize;
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] );
257         return buf;
258 }
259
260 static void
261 exit_thread_message( const ldap_pvt_thread_t thread )
262 {
263         if( tracethreads ) {
264                 char buf[40];
265                 fprintf( stderr, "== Exiting thread %s ==\n",
266                         thread_name( buf, sizeof(buf), thread ) );
267         }
268 }
269
270
271 #ifndef LDAP_THREAD_DEBUG_WRAP
272
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)
277
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())
284
285 #else /* LDAP_THREAD_DEBUG_WRAP */
286
287 #define WRAPPED(ptr)                    (&(ptr)->wrapped)
288
289 #define INITED_VALUE            0x12345678UL
290 #define INITED_BYTE_VALUE       0xd5
291
292 static int
293 debug_already_initialized( const LDAP_UINTPTR_T *num )
294 {
295         /* Valid programs will access uninitialized memory if dupinit */
296         return dupinit && *num == INITED_VALUE;
297 }
298
299 static void
300 alloc_usage( ldap_debug_usage_info_t *usage, const char *msg )
301 {
302         if( !options_done )
303                 get_options();
304         if( wraptype == Wrap_noalloc ) {
305                 ERROR_IF( debug_already_initialized( &usage->num ), msg );
306                 usage->num = INITED_VALUE;
307         } else {
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 );
314                 } else {
315                         usage->ptr = dummy + wrap_offset;
316                 }
317         }
318 }
319
320 static void
321 check_usage( ldap_debug_usage_info_t *usage, const char *msg )
322 {
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 );
328         } else {
329                 ERROR_IF( !usage->ptr, msg );
330                 ERROR_IF( usage->ptr[unwrap_offset] != INITED_BYTE_VALUE, msg );
331         }
332 }
333
334 static void
335 free_usage( ldap_debug_usage_info_t *usage, const char *msg )
336 {
337         if( wraptype == Wrap_noalloc ) {
338                 usage->num = ~(LDAP_UINTPTR_T)INITED_VALUE;
339         } else {
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;
344                 free( dummy );
345         }
346 }
347
348 #define with_threads_lock(statement) { \
349         if( !nodebug ) { \
350                 int rc_wtl_ = ldap_int_thread_mutex_lock( &thread_info_mutex ); \
351                 assert( rc_wtl_ == 0 ); \
352         } \
353     statement; \
354         if( !nodebug ) { \
355                 int rc_wtl_ = ldap_int_thread_mutex_unlock( &thread_info_mutex ); \
356                 assert( rc_wtl_ == 0 ); \
357         } \
358 }
359
360 static ldap_debug_thread_t *
361 get_new_thread_info( const char *msg )
362 {
363         if( nodebug )
364                 return NULL;
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) );
369                 assert( t != NULL );
370                 t->freeme = 1;
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++;
376                 }
377         }
378         alloc_usage( &thread_info[thread_info_used]->usage, msg );
379         return thread_info[thread_info_used++];
380 }
381
382 static void
383 update_thread_info(
384         ldap_debug_thread_t *t,
385         const ldap_pvt_thread_t *thread,
386         int detached )
387 {
388         if( !nodebug ) {
389                 t->wrapped = *thread;
390                 t->detached = detached;
391         }
392 }
393
394 static void
395 remove_thread_info( ldap_debug_thread_t *t, const char *msg )
396 {
397         if( !nodebug ) {
398                 ldap_debug_thread_t *last;
399                 int idx;
400                 free_usage( &t->usage, msg );
401                 idx = t->idx;
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;
407         }
408 }
409
410 ldap_debug_thread_t *
411 get_thread_info( ldap_pvt_thread_t *thread, const char *msg )
412 {
413         unsigned int i;
414         ldap_debug_thread_t *t;
415         if( nodebug )
416                 return NULL;
417         for( i = 0; i < thread_info_used; i++ ) {
418                 if( ldap_pvt_thread_equal( *thread, thread_info[i]->wrapped ) )
419                         break;
420         }
421         ERROR_IF( i == thread_info_used, msg );
422         t = thread_info[i];
423         check_usage( &t->usage, msg );
424         return t;
425 }
426
427 static void
428 exiting_thread( const char *msg )
429 {
430         if( !nodebug ) {
431                 ldap_pvt_thread_t thread;
432                 thread = ldap_pvt_thread_self();
433                 exit_thread_message( thread );
434                 with_threads_lock({
435                         ldap_debug_thread_t *t = get_thread_info( &thread, msg );
436                         if( t->detached )
437                                 remove_thread_info( t, msg );
438                 });
439         }
440 }
441
442 #endif /* LDAP_THREAD_DEBUG_WRAP */
443
444
445 static void
446 adjust_count( int which, int adjust )
447 {
448         int rc;
449         switch( count ) {
450         case Count_no:
451                 break;
452         case Count_yes:
453                 rc = ldap_int_thread_mutex_lock( &resource_mutexes[which] );
454                 assert( rc == 0 );
455                 resource_counts[which] += adjust;
456                 rc = ldap_int_thread_mutex_unlock( &resource_mutexes[which] );
457                 assert( rc == 0 );
458                 break;
459         case Count_reported:
460                 fputs( "...more ldap_debug_thread activity after exit...\n", stderr );
461                 count = Count_reported_more;
462                 /* FALL THROUGH */
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;
467                 break;
468         }
469 }
470
471
472 /* Wrappers for LDAP_THREAD_IMPLEMENTATION: */
473
474 /* Used instead of ldap_int_thread_initialize by ldap_pvt_thread_initialize */
475 int
476 ldap_debug_thread_initialize( void )
477 {
478         int i, rc, rc2;
479         if( !options_done )
480                 get_options();
481         ERROR_IF( threading_enabled, "ldap_debug_thread_initialize" );
482         threading_enabled = 1;
483         rc = ldap_int_thread_initialize();
484         if( rc ) {
485                 ERROR( rc, "ldap_debug_thread_initialize:threads" );
486                 threading_enabled = 0;
487         } else {
488                 rc2 = ldap_int_thread_mutex_init( &thread_info_mutex );
489                 assert( rc2 == 0 );
490                 if( count != Count_no ) {
491                         for( i = rc2 = 0; i < Idx_max; i++ )
492                                 rc2 |= ldap_int_thread_mutex_init( &resource_mutexes[i] );
493                         assert( rc2 == 0 );
494                         /* FIXME: Only for static libldap_r as in init.c? If so, why? */
495                         atexit( count_resource_leaks );
496                 }
497         }
498         return rc;
499 }
500
501 /* Used instead of ldap_int_thread_destroy by ldap_pvt_thread_destroy */
502 int
503 ldap_debug_thread_destroy( void )
504 {
505         int rc;
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();
509         if( rc ) {
510                 ERROR( rc, "ldap_debug_thread_destroy:threads" );
511         } else {
512                 threading_enabled = 0;
513         }
514         return rc;
515 }
516
517 int
518 ldap_pvt_thread_set_concurrency( int n )
519 {
520         int rc;
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" );
524         return rc;
525 }
526
527 int
528 ldap_pvt_thread_get_concurrency( void )
529 {
530         int rc;
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" );
534         return rc;
535 }
536
537 unsigned int
538 ldap_pvt_thread_sleep( unsigned int interval )
539 {
540         int rc;
541         ERROR_IF( !threading_enabled, "ldap_pvt_thread_sleep" );
542         rc = ldap_int_thread_sleep( interval );
543         ERROR_IF( rc, "ldap_pvt_thread_sleep" );
544         return 0;
545 }
546
547 int
548 ldap_pvt_thread_create(
549         ldap_pvt_thread_t *thread,
550         int detach,
551         void *(*start_routine)( void * ),
552         void *arg )
553 {
554         int rc;
555         ERROR_IF( !threading_enabled, "ldap_pvt_thread_create" );
556         if( !options_done )
557                 get_options();
558         with_threads_lock({
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 );
562                 if( rc ) {
563                         ERROR( rc, "ldap_pvt_thread_create" );
564                         remove_thread_info( t, "ldap_pvt_thread_init" );
565                 } else {
566                         update_thread_info( t, thread, detach );
567                 }
568         });
569         if( rc == 0 ) {
570                 if( tracethreads ) {
571                         char buf[40];
572                         fprintf( stderr, "== Created thread %s%s ==\n",
573                                 thread_name( buf, sizeof(buf), *thread ),
574                                 detach ? " (detached)" : "" );
575                 }
576                 adjust_count( Idx_unexited_thread, +1 );
577                 if( !detach )
578                         adjust_count( Idx_unjoined_thread, +1 );
579         }
580         return rc;
581 }
582
583 void
584 ldap_pvt_thread_exit( void *retval )
585 {
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 );
590 }
591
592 int
593 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
594 {
595         int rc;
596         ldap_debug_thread_t *t;
597         ERROR_IF( !threading_enabled, "ldap_pvt_thread_join" );
598         if( tracethreads ) {
599                 char buf[40];
600                 fprintf( stderr, "== Joining thread %s ==\n",
601                         thread_name( buf, sizeof(buf), thread ) );
602         }
603         with_threads_lock(
604                 t = get_thread_info( &thread, "ldap_pvt_thread_join" ) );
605         rc = ldap_int_thread_join( thread, thread_return );
606         if( rc ) {
607                 ERROR( rc, "ldap_pvt_thread_join" );
608         } else {
609                 with_threads_lock(
610                         remove_thread_info( t, "ldap_pvt_thread_join" ) );
611                 adjust_count( Idx_unjoined_thread, -1 );
612         }
613         return rc;
614 }
615
616 int
617 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
618 {
619         int rc;
620         ERROR_IF( !threading_enabled, "ldap_pvt_thread_kill" );
621         if( tracethreads ) {
622                 char buf[40];
623                 fprintf( stderr, "== Killing thread %s (sig %i) ==\n",
624                         thread_name( buf, sizeof(buf), thread ), signo );
625         }
626         rc = ldap_int_thread_kill( thread, signo );
627         ERROR_IF( rc, "ldap_pvt_thread_kill" );
628         return rc;
629 }
630
631 int
632 ldap_pvt_thread_yield( void )
633 {
634         int rc;
635         ERROR_IF( !threading_enabled, "ldap_pvt_thread_yield" );
636         rc = ldap_int_thread_yield();
637         ERROR_IF( rc, "ldap_pvt_thread_yield" );
638         return rc;
639 }
640
641 ldap_pvt_thread_t
642 ldap_pvt_thread_self( void )
643 {
644 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
645         ERROR_IF( !threading_enabled, "ldap_pvt_thread_self" );
646 #endif
647         return ldap_int_thread_self();
648 }
649
650 int
651 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
652 {
653         int rc;
654         alloc_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
655         rc = ldap_int_thread_cond_init( WRAPPED( cond ) );
656         if( rc ) {
657                 ERROR( rc, "ldap_pvt_thread_cond_init" );
658                 free_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
659         } else {
660                 adjust_count( Idx_cond, +1 );
661         }
662         return rc;
663 }
664
665 int
666 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
667 {
668         int rc;
669         check_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
670         rc = ldap_int_thread_cond_destroy( WRAPPED( cond ) );
671         if( rc ) {
672                 ERROR( rc, "ldap_pvt_thread_cond_destroy" );
673         } else {
674                 free_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
675                 adjust_count( Idx_cond, -1 );
676         }
677         return rc;
678 }
679
680 int
681 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
682 {
683         int rc;
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" );
687         return rc;
688 }
689
690 int
691 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
692 {
693         int rc;
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" );
697         return rc;
698 }
699
700 int
701 ldap_pvt_thread_cond_wait(
702         ldap_pvt_thread_cond_t *cond,
703         ldap_pvt_thread_mutex_t *mutex )
704 {
705         int rc;
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" );
712         return rc;
713 }
714
715 int
716 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
717 {
718         int rc;
719         alloc_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
720         rc = ldap_int_thread_mutex_init( WRAPPED( mutex ) );
721         if( rc ) {
722                 ERROR( rc, "ldap_pvt_thread_mutex_init" );
723                 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
724         } else {
725                 adjust_count( Idx_mutex, +1 );
726         }
727         return rc;
728 }
729
730 int
731 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
732 {
733         int rc;
734         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
735         rc = ldap_int_thread_mutex_destroy( WRAPPED( mutex ) );
736         if( rc ) {
737                 ERROR( rc, "ldap_pvt_thread_mutex_destroy" );
738         } else {
739                 free_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
740                 adjust_count( Idx_mutex, -1 );
741         }
742         return rc;
743 }
744
745 int
746 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
747 {
748         int rc;
749         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_lock" );
750         rc = ldap_int_thread_mutex_lock( WRAPPED( mutex ) );
751         if( rc ) {
752                 ERROR_IF( rc, "ldap_pvt_thread_mutex_lock" );
753         } else {
754                 adjust_count( Idx_locked_mutex, +1 );
755         }
756         return rc;
757 }
758
759 int
760 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
761 {
762         int rc;
763         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_trylock" );
764         rc = ldap_int_thread_mutex_trylock( WRAPPED( mutex ) );
765         if( rc == 0 )
766                 adjust_count( Idx_locked_mutex, +1 );
767         return rc;
768 }
769
770 int
771 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
772 {
773         int rc;
774         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_unlock" );
775         rc = ldap_int_thread_mutex_unlock( WRAPPED( mutex ) );
776         if( rc ) {
777                 ERROR_IF( rc, "ldap_pvt_thread_mutex_unlock" );
778         } else {
779                 adjust_count( Idx_locked_mutex, -1 );
780         }
781         return rc;
782 }
783
784
785 /* Wrappers for LDAP_THREAD_RDWR_IMPLEMENTATION: */
786
787 int
788 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
789 {
790         int rc;
791         alloc_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
792         rc = ldap_int_thread_rdwr_init( WRAPPED( rwlock ) );
793         if( rc ) {
794                 ERROR( rc, "ldap_pvt_thread_rdwr_init" );
795                 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
796         } else {
797                 adjust_count( Idx_rdwr, +1 );
798         }
799         return rc;
800 }
801
802 int
803 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
804 {
805         int rc;
806         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
807         rc = ldap_int_thread_rdwr_destroy( WRAPPED( rwlock ) );
808         if( rc ) {
809                 ERROR( rc, "ldap_pvt_thread_rdwr_destroy" );
810         } else {
811                 free_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
812                 adjust_count( Idx_rdwr, -1 );
813         }
814         return rc;
815 }
816
817 int
818 ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
819 {
820         int rc;
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" );
824         return rc;
825 }
826
827 int
828 ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
829 {
830         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rtrylock" );
831         return ldap_int_thread_rdwr_rtrylock( WRAPPED( rwlock ) );
832 }
833
834 int
835 ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
836 {
837         int rc;
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" );
841         return rc;
842 }
843
844 int
845 ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
846 {
847         int rc;
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" );
851         return rc;
852 }
853
854 int
855 ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
856 {
857         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wtrylock" );
858         return ldap_int_thread_rdwr_wtrylock( WRAPPED( rwlock ) );
859 }
860
861 int
862 ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
863 {
864         int rc;
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" );
868         return rc;
869 }
870
871 #ifdef LDAP_RDWR_DEBUG
872
873 int
874 ldap_pvt_thread_rdwr_readers( ldap_pvt_thread_rdwr_t *rwlock )
875 {
876         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_readers" );
877         return ldap_int_thread_rdwr_readers( WRAPPED( rwlock ) );
878 }
879
880 int
881 ldap_pvt_thread_rdwr_writers( ldap_pvt_thread_rdwr_t *rwlock )
882 {
883         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_writers" );
884         return ldap_int_thread_rdwr_writers( WRAPPED( rwlock ) );
885 }
886
887 int
888 ldap_pvt_thread_rdwr_active( ldap_pvt_thread_rdwr_t *rwlock )
889 {
890         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_active" );
891         return ldap_int_thread_rdwr_active( WRAPPED( rwlock ) );
892 }
893
894 #endif /* LDAP_RDWR_DEBUG */
895
896
897 /* Some wrappers for LDAP_THREAD_POOL_IMPLEMENTATION: */
898 #ifdef LDAP_THREAD_POOL_IMPLEMENTATION
899
900 int
901 ldap_pvt_thread_pool_init(
902         ldap_pvt_thread_pool_t *tpool,
903         int max_threads,
904         int max_pending )
905 {
906         int rc;
907         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_init" );
908         rc = ldap_int_thread_pool_init( tpool, max_threads, max_pending );
909         if( rc ) {
910                 ERROR( rc, "ldap_pvt_thread_pool_init" );
911         } else {
912                 adjust_count( Idx_tpool, +1 );
913         }
914         return rc;
915 }
916
917 int
918 ldap_pvt_thread_pool_submit(
919         ldap_pvt_thread_pool_t *tpool,
920         ldap_pvt_thread_start_t *start_routine, void *arg )
921 {
922         int rc, has_pool;
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 );
926         if( has_pool )
927                 ERROR_IF( rc, "ldap_pvt_thread_pool_submit" );
928         return rc;
929 }
930
931 int
932 ldap_pvt_thread_pool_maxthreads(
933         ldap_pvt_thread_pool_t *tpool,
934         int max_threads )
935 {
936         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_maxthreads" );
937         return ldap_int_thread_pool_maxthreads( tpool, max_threads );
938 }
939
940 int
941 ldap_pvt_thread_pool_backload( ldap_pvt_thread_pool_t *tpool )
942 {
943         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_backload" );
944         return ldap_int_thread_pool_backload( tpool );
945 }
946
947 int
948 ldap_pvt_thread_pool_destroy( ldap_pvt_thread_pool_t *tpool, int run_pending )
949 {
950         int rc, has_pool;
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 );
954         if( has_pool ) {
955                 if( rc ) {
956                         ERROR( rc, "ldap_pvt_thread_pool_destroy" );
957                 } else {
958                         adjust_count( Idx_tpool, -1 );
959                 }
960         }
961         return rc;
962 }
963
964 int
965 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
966 {
967         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_pause" );
968         return ldap_int_thread_pool_pause( tpool );
969 }
970
971 int
972 ldap_pvt_thread_pool_resume( ldap_pvt_thread_pool_t *tpool )
973 {
974         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_resume" );
975         return ldap_int_thread_pool_resume( tpool );
976 }
977
978 int
979 ldap_pvt_thread_pool_getkey(
980         void *xctx,
981         void *key,
982         void **data,
983         ldap_pvt_thread_pool_keyfree_t **kfree )
984 {
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" );
987 #endif
988         return ldap_int_thread_pool_getkey( xctx, key, data, kfree );
989 }
990
991 int
992 ldap_pvt_thread_pool_setkey(
993         void *xctx,
994         void *key,
995         void *data,
996         ldap_pvt_thread_pool_keyfree_t *kfree )
997 {
998         int rc;
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" );
1002         return rc;
1003 }
1004
1005 void
1006 ldap_pvt_thread_pool_purgekey( void *key )
1007 {
1008         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_purgekey" );
1009         ldap_int_thread_pool_purgekey( key );
1010 }
1011
1012 void *
1013 ldap_pvt_thread_pool_context( void )
1014 {
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" );
1017 #endif
1018         return ldap_int_thread_pool_context();
1019 }
1020
1021 void
1022 ldap_pvt_thread_pool_context_reset( void *vctx )
1023 {
1024         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context_reset" );
1025         ldap_int_thread_pool_context_reset( vctx );
1026 }
1027
1028 #endif /* LDAP_THREAD_POOL_IMPLEMENTATION */
1029
1030 #endif /* LDAP_THREAD_DEBUG */