]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_debug.c
Fix adjust_count, missing break
[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-2006 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 several types of thread operation debugging:
19  *
20  * - Check the results of operations on threads, mutexes, condition
21  *   variables and read/write locks.  Also check some thread pool
22  *   operations, but not those for which failure can happen in normal
23  *   slapd operation.
24  *
25  * - Wrap those types except threads and pools in structs with state
26  *   information, and check that on all operations:
27  *
28  *   + Check that the resources are initialized and are only used at
29  *     their original address (i.e. not realloced or copied).
30  *
31  *   + Check the owner (thread ID) on mutex operations.
32  *
33  *   + Optionally allocate a reference to a byte of dummy memory.
34  *     This lets malloc debuggers see some incorrect use as memory
35  *     leaks, access to freed memory, etc.
36  *
37  * - Print an error message and by default abort() upon errors.
38  *
39  * - Print a count of leaked thread resources after cleanup.
40  *
41  * Compile-time (./configure) setup:  Macros defined in CPPFLAGS.
42  *
43  *   LDAP_THREAD_DEBUG or LDAP_THREAD_DEBUG=2
44  *      Enables debugging, but value & 2 turns off type wrapping.
45  *
46  *   LDAP_UINTPTR_T=integer type to hold pointers, preferably unsigned.
47  *      Used by dummy memory option "scramble". Default = unsigned long.
48  *
49  *   LDAP_DEBUG_THREAD_NONE = initializer for a "no thread" thread ID.
50  *
51  *   In addition, you may need to set up an implementation-specific way
52  *      to enable whatever error checking your thread library provides.
53  *      Currently only implemented for Posix threads (pthreads), where
54  *      you may need to define LDAP_INT_THREAD_MUTEXATTR.  The default
55  *      is PTHREAD_MUTEX_ERRORCHECK, or PTHREAD_MUTEX_ERRORCHECK_NP for
56  *      Linux threads.  See pthread_mutexattr_settype(3).
57  *
58  * Run-time configuration:
59  *
60  *  Setup of memory debugging tools:
61  *   Tools that report uninitialized memory accesses should disable
62  *   such warnings about the function debug_already_initialized().
63  *   Alternatively, include "noreinit" (below) in $LDAP_THREAD_DEBUG.
64  *
65  *  Environment variable $LDAP_THREAD_DEBUG:
66  *   The variable may contain a comma- or space-separated option list.
67  *   Options:
68  *      off      - Disable this package.  (It still slows things down).
69  *      tracethreads - Report create/join/exit/kill of threads.
70  *      noabort  - Do not abort() on errors.
71  *      noerror  - Do not report errors.  Implies noabort.
72  *      nocount  - Do not report counts of unreleased resources.
73  *      nosync   - Disable tests that use synchronizaion and thus
74  *                 clearly affect thread scheduling:
75  *                 Implies nocount, and cancels threadID if that is set.
76  *                 Note that if you turn on tracethreads or malloc
77  *                 debugging, these also use library calls which may
78  *                 affect thread scheduling (fprintf and malloc).
79  *   The following options do not apply if type wrapping is disabled:
80  *      nomem    - Do not check memory operations.
81  *                 Implies noreinit,noalloc.
82  *      noreinit - Do not catch reinitialization of existing resources.
83  *                 (That test accesses uninitialized memory).
84  *      threadID - Trace thread IDs.  Currently mostly useless.
85  *     Malloc debugging -- allocate dummy memory for initialized
86  *     resources, so malloc debuggers will report them as memory leaks:
87  *      noalloc  - Default.  Do not allocate dummy memory.
88  *      alloc    - Store a pointer to dummy memory.   However, leak
89  *                 detectors might not catch unreleased resources in
90  *                 global variables.
91  *      scramble - Store bitwise complement of dummy memory pointer.
92  *                 That never escapes memory leak detectors -
93  *                 but detection while the program is running will
94  *                 report active resources as leaks.  Do not
95  *                 use this if a garbage collector is in use:-)
96  *      adjptr   - Point to end of dummy memory.
97  *                 Purify reports these as "potential leaks" (PLK).
98  *                 I have not checked other malloc debuggers.
99  */
100
101 #include "portable.h"
102
103 #if defined( LDAP_THREAD_DEBUG )
104
105 #include <stdio.h>
106 #include <ac/errno.h>
107 #include <ac/stdlib.h>
108 #include <ac/string.h>
109
110 #include "ldap_pvt_thread.h" /* Get the thread interface */
111 #define LDAP_THREAD_IMPLEMENTATION
112 #define LDAP_THREAD_DEBUG_IMPLEMENTATION
113 #define LDAP_THREAD_RDWR_IMPLEMENTATION
114 #define LDAP_THREAD_POOL_IMPLEMENTATION
115 #include "ldap_thr_debug.h"  /* Get the underlying implementation */
116
117 #ifndef LDAP_THREAD_DEBUG_WRAP
118 #undef  LDAP_THREAD_DEBUG_THREAD_ID
119 #elif !defined LDAP_THREAD_DEBUG_THREAD_ID
120 #define LDAP_THREAD_DEBUG_THREAD_ID 1
121 #endif
122
123 /* Use native malloc - the OpenLDAP wrappers may defeat malloc debuggers */
124 #undef malloc
125 #undef calloc
126 #undef realloc
127 #undef free
128
129
130 /* Options from environment variable $LDAP_THREAD_DEBUG */
131 enum { Count_no = 0, Count_yes, Count_reported, Count_reported_more };
132 static int count = Count_yes;
133 #ifdef LDAP_THREAD_DEBUG_WRAP
134 enum { Wrap_noalloc, Wrap_alloc, Wrap_scramble, Wrap_adjptr };
135 static int wraptype = Wrap_noalloc, wrap_offset, unwrap_offset;
136 static int nomem, noreinit;
137 #endif
138 #if LDAP_THREAD_DEBUG_THREAD_ID +0
139 static int threadID;
140 #else
141 enum { threadID = 0 };
142 #endif
143 static int nodebug, noabort, noerror, nosync, tracethreads;
144 static int options_done;
145
146
147 /* ldap_pvt_thread_initialize() called, ldap_pvt_thread_destroy() not called */
148 static int threading_enabled;
149
150
151 /* Resource counts */
152 enum {
153         Idx_unexited_thread, Idx_unjoined_thread, Idx_locked_mutex,
154         Idx_mutex, Idx_cond, Idx_rdwr, Idx_tpool, Idx_max
155 };
156 static int resource_counts[Idx_max];
157 static const char *const resource_names[] = {
158         "unexited threads", "unjoined threads", "locked mutexes",
159         "mutexes", "conds", "rdwrs", "thread pools"
160 };
161 static ldap_int_thread_mutex_t resource_mutexes[Idx_max];
162
163
164 /* Hide pointers from malloc debuggers. */
165 #define SCRAMBLE(ptr) (~(LDAP_UINTPTR_T) (ptr))
166 #define UNSCRAMBLE_usagep(num) ((ldap_debug_usage_info_t *) ~(num))
167 #define UNSCRAMBLE_dummyp(num) ((unsigned char *) ~(num))
168
169
170 #define WARN(var, msg)   (warn (__FILE__, __LINE__, (msg), #var, (var)))
171 #define WARN_IF(rc, msg) {if (rc) warn (__FILE__, __LINE__, (msg), #rc, (rc));}
172
173 #define ERROR(var, msg) { \
174         if (!noerror) { \
175                 errmsg(__FILE__, __LINE__, (msg), #var, (var)); \
176                 if( !noabort ) abort(); \
177         } \
178 }
179
180 #define ERROR_IF(rc, msg) { \
181         if (!noerror) { \
182                 int rc_ = (rc); \
183                 if (rc_) { \
184                         errmsg(__FILE__, __LINE__, (msg), #rc, rc_); \
185                         if( !noabort ) abort(); \
186                 } \
187         } \
188 }
189
190 #ifdef LDAP_THREAD_DEBUG_WRAP
191 #define MEMERROR_IF(rc, msg, mem_act) { \
192         if (!noerror) { \
193                 int rc_ = (rc); \
194                 if (rc_) { \
195                         errmsg(__FILE__, __LINE__, (msg), #rc, rc_); \
196                         if( wraptype != Wrap_noalloc ) { mem_act; } \
197                         if( !noabort ) abort(); \
198                 } \
199         } \
200 }
201 #endif /* LDAP_THREAD_DEBUG_WRAP */
202
203 #if 0
204 static void
205 warn( const char *file, int line, const char *msg, const char *var, int val )
206 {
207         fprintf( stderr,
208                 (strpbrk( var, "!=" )
209                  ? "%s:%d: %s warning: %s\n"
210                  : "%s:%d: %s warning: %s is %d\n"),
211                 file, line, msg, var, val );
212 }
213 #endif
214
215 static void
216 errmsg( const char *file, int line, const char *msg, const char *var, int val )
217 {
218         fprintf( stderr,
219                 (strpbrk( var, "!=" )
220                  ? "%s:%d: %s error: %s\n"
221                  : "%s:%d: %s error: %s is %d\n"),
222                 file, line, msg, var, val );
223 }
224
225 static void
226 count_resource_leaks( void )
227 {
228         int i, j;
229         char errbuf[200];
230         if( count == Count_yes ) {
231                 count = Count_reported;
232 #if 0 /* Could break if there are still threads after atexit */
233                 for( i = j = 0; i < Idx_max; i++ )
234                         j |= ldap_int_thread_mutex_destroy( &resource_mutexes[i] );
235                 WARN_IF( j, "ldap_debug_thread_destroy:mutexes" );
236 #endif
237                 for( i = j = 0; i < Idx_max; i++ )
238                         if( resource_counts[i] )
239                                 j += sprintf( errbuf + j, ", %d %s",
240                                         resource_counts[i], resource_names[i] );
241                 if( j )
242                         fprintf( stderr, "== thr_debug: Leaked%s. ==\n", errbuf + 1 );
243         }
244 }
245
246 static void
247 get_options( void )
248 {
249         static const struct option_info_s {
250                 const char      *name;
251                 int             *var, val;
252         } option_info[] = {
253                 { "off",        &nodebug,  1 },
254                 { "noabort",    &noabort,  1 },
255                 { "noerror",    &noerror,  1 },
256                 { "nocount",    &count,    Count_no },
257                 { "nosync",     &nosync,   1 },
258 #if LDAP_THREAD_DEBUG_THREAD_ID +0
259                 { "threadID",   &threadID, 1 },
260 #endif
261 #ifdef LDAP_THREAD_DEBUG_WRAP
262                 { "nomem",      &nomem,    1 },
263                 { "noreinit",   &noreinit, 1 },
264                 { "noalloc",    &wraptype, Wrap_noalloc },
265                 { "alloc",      &wraptype, Wrap_alloc },
266                 { "adjptr",     &wraptype, Wrap_adjptr },
267                 { "scramble",   &wraptype, Wrap_scramble },
268 #endif
269                 { "tracethreads", &tracethreads, 1 },
270                 { NULL, NULL, 0 }
271         };
272         const char *s = getenv( "LDAP_THREAD_DEBUG" );
273         if( s != NULL ) {
274                 while( *(s += strspn( s, ", \t\r\n" )) != '\0' ) {
275                         size_t optlen = strcspn( s, ", \t\r\n" );
276                         const struct option_info_s *oi;
277                         for( oi = option_info; oi->name; oi++ ) {
278                                 if( strncasecmp( oi->name, s, optlen ) == 0 ) {
279                                         if( oi->name && oi->name[optlen] == '\0' ) {
280                                                 *oi->var = oi->val;
281                                         } else {
282                                                 fprintf( stderr,
283                                                         "== thr_debug: Unknown $%s option '%.*s' ==\n",
284                                                         "LDAP_THREAD_DEBUG", (int) optlen, s );
285                                         }
286                                         break;
287                                 }
288                         }
289                         s += optlen;
290                 }
291         }
292         if( nodebug ) {
293                 tracethreads = 0;
294                 nosync = noerror = 1;
295         }
296         if( nosync )
297                 count = Count_no;
298         if( noerror )
299                 noabort = 1;
300 #if LDAP_THREAD_DEBUG_THREAD_ID +0
301         if( nosync )
302                 threadID = 0;
303 #endif
304 #ifdef LDAP_THREAD_DEBUG_WRAP
305         if( noerror )
306                 nomem = 1;
307         if( !nomem ) {
308                 static const ldap_debug_usage_info_t usage;
309                 if( sizeof(LDAP_UINTPTR_T) < sizeof(unsigned char *)
310                         || sizeof(LDAP_UINTPTR_T) < sizeof(ldap_debug_usage_info_t *)
311                         || UNSCRAMBLE_usagep( SCRAMBLE( &usage ) ) != &usage
312                         || UNSCRAMBLE_dummyp( SCRAMBLE( (unsigned char *) 0 ) ) )
313                 {
314                         fputs( "== thr_debug: Memory checks unsupported, "
315                                 "adding nomem to $LDAP_THREAD_DEBUG ==\n", stderr );
316                         nomem = 1;
317                 }
318         }
319         if( nomem ) {
320                 noreinit = 1;
321                 wraptype = Wrap_noalloc;
322         }
323         unwrap_offset = -(wrap_offset = (wraptype == Wrap_adjptr));
324 #endif
325         options_done = 1;
326 }
327
328
329 #ifndef LDAP_THREAD_DEBUG_WRAP
330
331 #define WRAPPED(ptr)                    (ptr)
332 #define GET_OWNER(ptr)                  0
333 #define SET_OWNER(ptr, thread)  ((void) 0)
334 #define RESET_OWNER(ptr)                ((void) 0)
335 #define ASSERT_OWNER(ptr, msg)  ((void) 0)
336 #define ASSERT_NO_OWNER(ptr, msg) ((void) 0)
337
338 #define init_usage(ptr, msg)    ((void) 0)
339 #define check_usage(ptr, msg)   ((void) 0)
340 #define destroy_usage(ptr)              ((void) 0)
341
342 #else /* LDAP_THREAD_DEBUG_WRAP */
343
344 /* Specialize this if the initializer is not appropriate. */
345 /* The ASSERT_NO_OWNER() definition may also need an override. */
346 #ifndef LDAP_DEBUG_THREAD_NONE
347 #define LDAP_DEBUG_THREAD_NONE { -1 } /* "no thread" ldap_int_thread_t value */
348 #endif
349
350 static const ldap_int_thread_t ldap_debug_thread_none = LDAP_DEBUG_THREAD_NONE;
351
352 #define THREAD_MUTEX_OWNER(mutex) \
353         ldap_int_thread_equal( (mutex)->owner, ldap_int_thread_self() )
354
355 void
356 ldap_debug_thread_assert_mutex_owner(
357         const char *file,
358         int line,
359         const char *msg,
360         ldap_pvt_thread_mutex_t *mutex )
361 {
362         if( !(noerror || THREAD_MUTEX_OWNER( mutex )) ) {
363                 errmsg( file, line, msg, "ASSERT_MUTEX_OWNER", 0 );
364                 if( !noabort ) abort();
365         }
366 }
367
368 #define WRAPPED(ptr)                    (&(ptr)->wrapped)
369 #define GET_OWNER(ptr)                  ((ptr)->owner)
370 #define SET_OWNER(ptr, thread)  ((ptr)->owner = (thread))
371 #define RESET_OWNER(ptr)                ((ptr)->owner = ldap_debug_thread_none)
372 #define ASSERT_OWNER(ptr, msg)  ERROR_IF( !THREAD_MUTEX_OWNER( ptr ), msg )
373 #ifndef ASSERT_NO_OWNER
374 #define ASSERT_NO_OWNER(ptr, msg) ERROR_IF( \
375         !ldap_int_thread_equal( (ptr)->owner, ldap_debug_thread_none ), msg )
376 #endif
377
378 /* Try to provoke memory access error (for malloc debuggers) */
379 #define PEEK(mem) {if (-*(volatile const unsigned char *)(mem)) debug_noop();}
380
381 static void debug_noop( void );
382 static int debug_already_initialized( const ldap_debug_usage_info_t *usage );
383
384 /* Names used to give clearer error messages */
385 #define IS_COPY_OR_MOVED(usage) ((usage)->self != SCRAMBLE( usage ))
386 enum { Is_destroyed = 1 };
387
388 #define DUMMY_ADDR(usage) \
389         (wraptype == Wrap_scramble \
390          ? UNSCRAMBLE_dummyp( (usage)->mem.num ) \
391          : (usage)->mem.ptr + unwrap_offset)
392
393 /* Mark resource as initialized */
394 static void
395 init_usage( ldap_debug_usage_info_t *usage, const char *msg )
396 {
397         if( !options_done )
398                 get_options();
399         if( !nomem ) {
400                 if( !noreinit ) {
401                         MEMERROR_IF( debug_already_initialized( usage ), msg, {
402                                 /* Provoke malloc debuggers */
403                                 unsigned char *dummy = DUMMY_ADDR( usage );
404                                 PEEK( dummy );
405                                 free( dummy );
406                                 free( dummy );
407                         } );
408                 }
409                 usage->self = SCRAMBLE( usage );
410                 if( wraptype != Wrap_noalloc ) {
411                         unsigned char *dummy = malloc( 1 );
412                         assert( dummy != NULL );
413                         if( wraptype == Wrap_scramble ) {
414                                 usage->mem.num = SCRAMBLE( dummy );
415                                 assert( UNSCRAMBLE_dummyp( usage->mem.num ) == dummy );
416                         } else {
417                                 usage->mem.ptr = dummy + wrap_offset;
418                         }
419                 }
420         }
421         usage->magic = ldap_debug_magic;
422         usage->state = ldap_debug_state_inited;
423 }
424
425 /* Check that resource is initialized and not copied/realloced */
426 static void
427 check_usage( const ldap_debug_usage_info_t *usage, const char *msg )
428 {
429         if( usage->magic != ldap_debug_magic ) {
430                 ERROR( usage->magic, msg );
431                 return;
432         }
433         switch( usage->state ) {
434         case ldap_debug_state_destroyed:
435                 MEMERROR_IF( Is_destroyed, msg, {
436                         PEEK( DUMMY_ADDR( usage ) );
437                 } );
438                 break;
439         default:
440                 ERROR( usage->state, msg );
441                 break;
442         case ldap_debug_state_inited:
443                 if( !nomem ) {
444                         MEMERROR_IF( IS_COPY_OR_MOVED( usage ), msg, {
445                                 PEEK( DUMMY_ADDR( usage ) );
446                                 PEEK( UNSCRAMBLE_usagep( usage->self ) );
447                         } );
448                 }
449                 break;
450         }
451 }
452
453 /* Mark resource as destroyed. */
454 /* Does not check for errors, call check_usage()/init_usage() first. */
455 static void
456 destroy_usage( ldap_debug_usage_info_t *usage )
457 {
458         if( usage->state == ldap_debug_state_inited ) {
459                 if( wraptype != Wrap_noalloc ) {
460                         free( DUMMY_ADDR( usage ) );
461                         /* Do not reset the DUMMY_ADDR, leave it for malloc debuggers
462                          * in case the resource is used after it is freed. */
463                 }
464                 usage->state = ldap_debug_state_destroyed;
465         }
466 }
467
468 /* Define these after they are used, so they are hopefully not inlined */
469
470 static void
471 debug_noop( void )
472 {
473 }
474
475 /* Return true if the resource is initialized and not copied/realloced. */
476 /* Valid programs access uninitialized memory here unless "noreinit".   */
477 static int
478 debug_already_initialized( const ldap_debug_usage_info_t *usage )
479 {
480         return (usage->state == ldap_debug_state_inited &&
481                 !IS_COPY_OR_MOVED( usage ) &&
482                 usage->magic == ldap_debug_magic);
483 }
484
485 #endif /* LDAP_THREAD_DEBUG_WRAP */
486
487
488 #if !(LDAP_THREAD_DEBUG_THREAD_ID +0)
489
490 typedef int ldap_debug_thread_t;
491 #define init_thread_info()      {}
492 #define with_thread_info_lock(statements) { statements; }
493 #define thread_info_detached(t) 0
494 #define add_thread_info(msg, thr, det)  ((void) 0)
495 #define remove_thread_info(tinfo, msg)  ((void) 0)
496 #define get_thread_info(thread, msg)    NULL
497
498 #else /* LDAP_THREAD_DEBUG_THREAD_ID */
499
500 /*
501  * Thread ID tracking.  Currently acieves little.
502  * Should be either expanded or deleted.
503  */
504
505 /*
506  * Array of threads.  Used instead of making ldap_pvt_thread_t a wrapper
507  * around ldap_int_thread_t, which would slow down ldap_pvt_thread_self().
508  */
509 typedef struct {
510         ldap_pvt_thread_t           wrapped;
511         ldap_debug_usage_info_t     usage;
512         int                         detached;
513         int                         idx;
514 } ldap_debug_thread_t;
515
516 static ldap_debug_thread_t      **thread_info;
517 static unsigned int             thread_info_size, thread_info_used;
518 static ldap_int_thread_mutex_t  thread_info_mutex;
519
520 #define init_thread_info() { \
521         if( threadID ) { \
522                 int mutex_init_rc = ldap_int_thread_mutex_init( &thread_info_mutex ); \
523                 assert( mutex_init_rc == 0 ); \
524         } \
525 }
526
527 #define with_thread_info_lock(statements) { \
528         int rc_wtl_ = ldap_int_thread_mutex_lock( &thread_info_mutex ); \
529         assert( rc_wtl_ == 0 ); \
530         { statements; } \
531         rc_wtl_ = ldap_int_thread_mutex_unlock( &thread_info_mutex ); \
532         assert( rc_wtl_ == 0 ); \
533 }
534
535 #define thread_info_detached(t) ((t)->detached)
536
537 static void
538 add_thread_info(
539         const char *msg,
540         const ldap_pvt_thread_t *thread,
541         int detached )
542 {
543         ldap_debug_thread_t *t;
544         if( thread_info_used >= thread_info_size ) {
545                 unsigned int more = thread_info_size + 8;
546                 unsigned int new_size = thread_info_size + more;
547                 t = calloc( more, sizeof(ldap_debug_thread_t) );
548                 assert( t != NULL );
549                 thread_info = realloc( thread_info, new_size * sizeof(*thread_info) );
550                 assert( thread_info != NULL );
551                 while( thread_info_size < new_size ) {
552                         t->idx = thread_info_size;
553                         thread_info[thread_info_size++] = t++;
554                 }
555         }
556         t = thread_info[thread_info_used];
557         init_usage( &t->usage, msg );
558         t->wrapped = *thread;
559         t->detached = detached;
560         thread_info_used++;
561 }
562
563 static void
564 remove_thread_info( ldap_debug_thread_t *t, const char *msg )
565 {
566                 ldap_debug_thread_t *last;
567                 int idx;
568                 check_usage( &t->usage, msg );
569                 destroy_usage( &t->usage );
570                 idx = t->idx;
571                 assert( thread_info[idx] == t );
572                 last = thread_info[--thread_info_used];
573                 assert( last->idx == thread_info_used );
574                 (thread_info[idx]              = last)->idx = idx;
575                 (thread_info[thread_info_used] = t   )->idx = thread_info_used;
576 }
577
578 ldap_debug_thread_t *
579 get_thread_info( ldap_pvt_thread_t *thread, const char *msg )
580 {
581         unsigned int i;
582         ldap_debug_thread_t *t;
583         for( i = 0; i < thread_info_used; i++ ) {
584                 if( ldap_pvt_thread_equal( *thread, thread_info[i]->wrapped ) )
585                         break;
586         }
587         ERROR_IF( i == thread_info_used, msg );
588         t = thread_info[i];
589         check_usage( &t->usage, msg );
590         return t;
591 }
592
593 #endif /* LDAP_THREAD_DEBUG_THREAD_ID */
594
595
596 static char *
597 thread_name( char *buf, int bufsize, ldap_pvt_thread_t thread )
598 {
599         int i;
600         --bufsize;
601         if( bufsize > 2*sizeof(thread) )
602                 bufsize = 2*sizeof(thread);
603         for( i = 0; i < bufsize; i += 2 )
604                 snprintf( buf+i, 3, "%02x", ((unsigned char *)&thread)[i/2] );
605         return buf;
606 }
607
608
609 /* Add <adjust> (+/-1) to resource count <which> unless "nocount". */
610 static void
611 adjust_count( int which, int adjust )
612 {
613         int rc;
614         switch( count ) {
615         case Count_no:
616                 break;
617         case Count_yes:
618                 rc = ldap_int_thread_mutex_lock( &resource_mutexes[which] );
619                 assert( rc == 0 );
620                 resource_counts[which] += adjust;
621                 rc = ldap_int_thread_mutex_unlock( &resource_mutexes[which] );
622                 assert( rc == 0 );
623                 break;
624         case Count_reported:
625                 fputs( "== thr_debug: More thread activity after exit ==\n", stderr );
626                 count = Count_reported_more;
627                 /* FALL THROUGH */
628         case Count_reported_more:
629                 /* Not used, but result might be inspected with debugger */
630                 /* (Hopefully threading is disabled by now...) */
631                 resource_counts[which] += adjust;
632                 break;
633         }
634 }
635
636
637 /* Wrappers for LDAP_THREAD_IMPLEMENTATION: */
638
639 /* Used instead of ldap_int_thread_initialize by ldap_pvt_thread_initialize */
640 int
641 ldap_debug_thread_initialize( void )
642 {
643         int i, rc, rc2;
644         if( !options_done )
645                 get_options();
646         ERROR_IF( threading_enabled, "ldap_debug_thread_initialize" );
647         threading_enabled = 1;
648         rc = ldap_int_thread_initialize();
649         if( rc ) {
650                 ERROR( rc, "ldap_debug_thread_initialize:threads" );
651                 threading_enabled = 0;
652         } else {
653                 init_thread_info();
654                 if( count != Count_no ) {
655                         for( i = rc2 = 0; i < Idx_max; i++ )
656                                 rc2 |= ldap_int_thread_mutex_init( &resource_mutexes[i] );
657                         assert( rc2 == 0 );
658                         /* FIXME: Only for static libldap_r as in init.c? If so, why? */
659                         atexit( count_resource_leaks );
660                 }
661         }
662         return rc;
663 }
664
665 /* Used instead of ldap_int_thread_destroy by ldap_pvt_thread_destroy */
666 int
667 ldap_debug_thread_destroy( void )
668 {
669         int rc;
670         ERROR_IF( !threading_enabled, "ldap_debug_thread_destroy" );
671         /* sleep(1) -- need to wait for thread pool to finish? */
672         rc = ldap_int_thread_destroy();
673         if( rc ) {
674                 ERROR( rc, "ldap_debug_thread_destroy:threads" );
675         } else {
676                 threading_enabled = 0;
677         }
678         return rc;
679 }
680
681 int
682 ldap_pvt_thread_set_concurrency( int n )
683 {
684         int rc;
685         ERROR_IF( !threading_enabled, "ldap_pvt_thread_set_concurrency" );
686         rc = ldap_int_thread_set_concurrency( n );
687         ERROR_IF( rc, "ldap_pvt_thread_set_concurrency" );
688         return rc;
689 }
690
691 int
692 ldap_pvt_thread_get_concurrency( void )
693 {
694         int rc;
695         ERROR_IF( !threading_enabled, "ldap_pvt_thread_get_concurrency" );
696         rc = ldap_int_thread_get_concurrency();
697         ERROR_IF( rc, "ldap_pvt_thread_get_concurrency" );
698         return rc;
699 }
700
701 unsigned int
702 ldap_pvt_thread_sleep( unsigned int interval )
703 {
704         int rc;
705         ERROR_IF( !threading_enabled, "ldap_pvt_thread_sleep" );
706         rc = ldap_int_thread_sleep( interval );
707         ERROR_IF( rc, "ldap_pvt_thread_sleep" );
708         return 0;
709 }
710
711 int
712 ldap_pvt_thread_create(
713         ldap_pvt_thread_t *thread,
714         int detach,
715         void *(*start_routine)( void * ),
716         void *arg )
717 {
718         int rc;
719         if( !options_done )
720                 get_options();
721         ERROR_IF( !threading_enabled, "ldap_pvt_thread_create" );
722         if( threadID ) {
723                 with_thread_info_lock({
724                         rc = ldap_int_thread_create( thread, detach, start_routine, arg );
725                         if( rc == 0 )
726                                 add_thread_info( "ldap_pvt_thread_create", thread, detach );
727                 });
728         } else {
729                 rc = ldap_int_thread_create( thread, detach, start_routine, arg );
730         }
731         if( rc ) {
732                 ERROR( rc, "ldap_pvt_thread_create" );
733         } else {
734                 if( tracethreads ) {
735                         char buf[40], buf2[40];
736                         fprintf( stderr,
737                                 "== thr_debug: Created thread %s%s from thread %s ==\n",
738                                 thread_name( buf, sizeof(buf), *thread ),
739                                 detach ? " (detached)" : "",
740                                 thread_name( buf2, sizeof(buf2), ldap_pvt_thread_self() ) );
741                 }
742                 adjust_count( Idx_unexited_thread, +1 );
743                 if( !detach )
744                         adjust_count( Idx_unjoined_thread, +1 );
745         }
746         return rc;
747 }
748
749 void
750 ldap_pvt_thread_exit( void *retval )
751 {
752         ldap_pvt_thread_t thread;
753         ERROR_IF( !threading_enabled, "ldap_pvt_thread_exit" );
754         thread = ldap_pvt_thread_self();
755         if( tracethreads ) {
756                 char buf[40];
757                 fprintf( stderr, "== thr_debug: Exiting thread %s ==\n",
758                         thread_name( buf, sizeof(buf), thread ) );
759         }
760         if( threadID ) {
761                 with_thread_info_lock({
762                         ldap_debug_thread_t *t = get_thread_info(
763                                 &thread, "ldap_pvt_thread_exit" );
764                         if( thread_info_detached( t ) )
765                                 remove_thread_info( t, "ldap_pvt_thread_exit" );
766                 });
767         }
768         adjust_count( Idx_unexited_thread, -1 );
769         ldap_int_thread_exit( retval );
770 }
771
772 int
773 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
774 {
775         int rc;
776         ldap_debug_thread_t *t = NULL;
777         ERROR_IF( !threading_enabled, "ldap_pvt_thread_join" );
778         if( tracethreads ) {
779                 char buf[40], buf2[40];
780                 fprintf( stderr, "== thr_debug: Joining thread %s in thread %s ==\n",
781                         thread_name( buf, sizeof(buf), thread ),
782                         thread_name( buf2, sizeof(buf2), ldap_pvt_thread_self() ) );
783         }
784         if( threadID )
785                 with_thread_info_lock( {
786                         t = get_thread_info( &thread, "ldap_pvt_thread_join" );
787                         ERROR_IF( thread_info_detached( t ), "ldap_pvt_thread_join" );
788                 } );
789         rc = ldap_int_thread_join( thread, thread_return );
790         if( rc ) {
791                 ERROR( rc, "ldap_pvt_thread_join" );
792         } else {
793                 if( threadID )
794                         with_thread_info_lock(
795                                 remove_thread_info( t, "ldap_pvt_thread_join" ) );
796                 adjust_count( Idx_unjoined_thread, -1 );
797         }
798         return rc;
799 }
800
801 int
802 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
803 {
804         int rc;
805         ERROR_IF( !threading_enabled, "ldap_pvt_thread_kill" );
806         if( tracethreads ) {
807                 char buf[40], buf2[40];
808                 fprintf( stderr,
809                         "== thr_debug: Killing thread %s (sig %i) from thread %s ==\n",
810                         thread_name( buf, sizeof(buf), thread ), signo,
811                         thread_name( buf2, sizeof(buf2), ldap_pvt_thread_self() ) );
812         }
813         rc = ldap_int_thread_kill( thread, signo );
814         ERROR_IF( rc, "ldap_pvt_thread_kill" );
815         return rc;
816 }
817
818 int
819 ldap_pvt_thread_yield( void )
820 {
821         int rc;
822         ERROR_IF( !threading_enabled, "ldap_pvt_thread_yield" );
823         rc = ldap_int_thread_yield();
824         ERROR_IF( rc, "ldap_pvt_thread_yield" );
825         return rc;
826 }
827
828 ldap_pvt_thread_t
829 ldap_pvt_thread_self( void )
830 {
831 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
832         ERROR_IF( !threading_enabled, "ldap_pvt_thread_self" );
833 #endif
834         return ldap_int_thread_self();
835 }
836
837 int
838 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
839 {
840         int rc;
841         init_usage( &cond->usage, "ldap_pvt_thread_cond_init" );
842         rc = ldap_int_thread_cond_init( WRAPPED( cond ) );
843         if( rc ) {
844                 ERROR( rc, "ldap_pvt_thread_cond_init" );
845                 destroy_usage( &cond->usage );
846         } else {
847                 adjust_count( Idx_cond, +1 );
848         }
849         return rc;
850 }
851
852 int
853 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
854 {
855         int rc;
856         check_usage( &cond->usage, "ldap_pvt_thread_cond_destroy" );
857         rc = ldap_int_thread_cond_destroy( WRAPPED( cond ) );
858         if( rc ) {
859                 ERROR( rc, "ldap_pvt_thread_cond_destroy" );
860         } else {
861                 destroy_usage( &cond->usage );
862                 adjust_count( Idx_cond, -1 );
863         }
864         return rc;
865 }
866
867 int
868 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
869 {
870         int rc;
871         check_usage( &cond->usage, "ldap_pvt_thread_cond_signal" );
872         rc = ldap_int_thread_cond_signal( WRAPPED( cond ) );
873         ERROR_IF( rc, "ldap_pvt_thread_cond_signal" );
874         return rc;
875 }
876
877 int
878 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
879 {
880         int rc;
881         check_usage( &cond->usage, "ldap_pvt_thread_cond_broadcast" );
882         rc = ldap_int_thread_cond_broadcast( WRAPPED( cond ) );
883         ERROR_IF( rc, "ldap_pvt_thread_cond_broadcast" );
884         return rc;
885 }
886
887 int
888 ldap_pvt_thread_cond_wait(
889         ldap_pvt_thread_cond_t *cond,
890         ldap_pvt_thread_mutex_t *mutex )
891 {
892         int rc;
893         ldap_int_thread_t owner;
894         check_usage( &cond->usage, "ldap_pvt_thread_cond_wait:cond" );
895         check_usage( &mutex->usage, "ldap_pvt_thread_cond_wait:mutex" );
896         adjust_count( Idx_locked_mutex, -1 );
897         owner = GET_OWNER( mutex );
898         ASSERT_OWNER( mutex, "ldap_pvt_thread_cond_wait" );
899         RESET_OWNER( mutex );
900         rc = ldap_int_thread_cond_wait( WRAPPED( cond ), WRAPPED( mutex ) );
901         ASSERT_NO_OWNER( mutex, "ldap_pvt_thread_cond_wait" );
902         SET_OWNER( mutex, rc ? owner : ldap_int_thread_self() );
903         adjust_count( Idx_locked_mutex, +1 );
904         ERROR_IF( rc, "ldap_pvt_thread_cond_wait" );
905         return rc;
906 }
907
908 int
909 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
910 {
911         int rc;
912         init_usage( &mutex->usage, "ldap_pvt_thread_mutex_init" );
913         rc = ldap_int_thread_mutex_init( WRAPPED( mutex ) );
914         if( rc ) {
915                 ERROR( rc, "ldap_pvt_thread_mutex_init" );
916                 destroy_usage( &mutex->usage );
917         } else {
918                 RESET_OWNER( mutex );
919                 adjust_count( Idx_mutex, +1 );
920         }
921         return rc;
922 }
923
924 int
925 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
926 {
927         int rc;
928         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_destroy" );
929         ASSERT_NO_OWNER( mutex, "ldap_pvt_thread_mutex_destroy" );
930         rc = ldap_int_thread_mutex_destroy( WRAPPED( mutex ) );
931         if( rc ) {
932                 ERROR( rc, "ldap_pvt_thread_mutex_destroy" );
933         } else {
934                 destroy_usage( &mutex->usage );
935                 RESET_OWNER( mutex );
936                 adjust_count( Idx_mutex, -1 );
937         }
938         return rc;
939 }
940
941 int
942 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
943 {
944         int rc;
945         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_lock" );
946         rc = ldap_int_thread_mutex_lock( WRAPPED( mutex ) );
947         if( rc ) {
948                 ERROR_IF( rc, "ldap_pvt_thread_mutex_lock" );
949         } else {
950                 ASSERT_NO_OWNER( mutex, "ldap_pvt_thread_mutex_lock" );
951                 SET_OWNER( mutex, ldap_int_thread_self() );
952                 adjust_count( Idx_locked_mutex, +1 );
953         }
954         return rc;
955 }
956
957 int
958 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
959 {
960         int rc;
961         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_trylock" );
962         rc = ldap_int_thread_mutex_trylock( WRAPPED( mutex ) );
963         if( rc == 0 ) {
964                 ASSERT_NO_OWNER( mutex, "ldap_pvt_thread_mutex_trylock" );
965                 SET_OWNER( mutex, ldap_int_thread_self() );
966                 adjust_count( Idx_locked_mutex, +1 );
967         }
968         return rc;
969 }
970
971 int
972 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
973 {
974         int rc;
975         check_usage( &mutex->usage, "ldap_pvt_thread_mutex_unlock" );
976         ASSERT_OWNER( mutex, "ldap_pvt_thread_mutex_unlock" );
977         RESET_OWNER( mutex ); /* Breaks if this thread did not own the mutex */
978         rc = ldap_int_thread_mutex_unlock( WRAPPED( mutex ) );
979         if( rc ) {
980                 ERROR_IF( rc, "ldap_pvt_thread_mutex_unlock" );
981         } else {
982                 adjust_count( Idx_locked_mutex, -1 );
983         }
984         return rc;
985 }
986
987
988 /* Wrappers for LDAP_THREAD_RDWR_IMPLEMENTATION: */
989
990 int
991 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rwlock )
992 {
993         int rc;
994         init_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_init" );
995         rc = ldap_int_thread_rdwr_init( WRAPPED( rwlock ) );
996         if( rc ) {
997                 ERROR( rc, "ldap_pvt_thread_rdwr_init" );
998                 destroy_usage( &rwlock->usage );
999         } else {
1000                 adjust_count( Idx_rdwr, +1 );
1001         }
1002         return rc;
1003 }
1004
1005 int
1006 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rwlock )
1007 {
1008         int rc;
1009         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_destroy" );
1010         rc = ldap_int_thread_rdwr_destroy( WRAPPED( rwlock ) );
1011         if( rc ) {
1012                 ERROR( rc, "ldap_pvt_thread_rdwr_destroy" );
1013         } else {
1014                 destroy_usage( &rwlock->usage );
1015                 adjust_count( Idx_rdwr, -1 );
1016         }
1017         return rc;
1018 }
1019
1020 int
1021 ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rwlock )
1022 {
1023         int rc;
1024         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rlock" );
1025         rc = ldap_int_thread_rdwr_rlock( WRAPPED( rwlock ) );
1026         ERROR_IF( rc, "ldap_pvt_thread_rdwr_rlock" );
1027         return rc;
1028 }
1029
1030 int
1031 ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rwlock )
1032 {
1033         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_rtrylock" );
1034         return ldap_int_thread_rdwr_rtrylock( WRAPPED( rwlock ) );
1035 }
1036
1037 int
1038 ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rwlock )
1039 {
1040         int rc;
1041         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_runlock" );
1042         rc = ldap_int_thread_rdwr_runlock( WRAPPED( rwlock ) );
1043         ERROR_IF( rc, "ldap_pvt_thread_rdwr_runlock" );
1044         return rc;
1045 }
1046
1047 int
1048 ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rwlock )
1049 {
1050         int rc;
1051         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wlock" );
1052         rc = ldap_int_thread_rdwr_wlock( WRAPPED( rwlock ) );
1053         ERROR_IF( rc, "ldap_pvt_thread_rdwr_wlock" );
1054         return rc;
1055 }
1056
1057 int
1058 ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rwlock )
1059 {
1060         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wtrylock" );
1061         return ldap_int_thread_rdwr_wtrylock( WRAPPED( rwlock ) );
1062 }
1063
1064 int
1065 ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rwlock )
1066 {
1067         int rc;
1068         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_wunlock" );
1069         rc = ldap_int_thread_rdwr_wunlock( WRAPPED( rwlock ) );
1070         ERROR_IF( rc, "ldap_pvt_thread_rdwr_wunlock" );
1071         return rc;
1072 }
1073
1074 #if defined(LDAP_RDWR_DEBUG) && !defined(LDAP_THREAD_HAVE_RDWR)
1075
1076 int
1077 ldap_pvt_thread_rdwr_readers( ldap_pvt_thread_rdwr_t *rwlock )
1078 {
1079         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_readers" );
1080         return ldap_int_thread_rdwr_readers( WRAPPED( rwlock ) );
1081 }
1082
1083 int
1084 ldap_pvt_thread_rdwr_writers( ldap_pvt_thread_rdwr_t *rwlock )
1085 {
1086         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_writers" );
1087         return ldap_int_thread_rdwr_writers( WRAPPED( rwlock ) );
1088 }
1089
1090 int
1091 ldap_pvt_thread_rdwr_active( ldap_pvt_thread_rdwr_t *rwlock )
1092 {
1093         check_usage( &rwlock->usage, "ldap_pvt_thread_rdwr_active" );
1094         return ldap_int_thread_rdwr_active( WRAPPED( rwlock ) );
1095 }
1096
1097 #endif /* LDAP_RDWR_DEBUG && !LDAP_THREAD_HAVE_RDWR */
1098
1099
1100 /* Some wrappers for LDAP_THREAD_POOL_IMPLEMENTATION: */
1101 #ifdef LDAP_THREAD_POOL_IMPLEMENTATION
1102
1103 int
1104 ldap_pvt_thread_pool_init(
1105         ldap_pvt_thread_pool_t *tpool,
1106         int max_threads,
1107         int max_pending )
1108 {
1109         int rc;
1110         if( !options_done )
1111                 get_options();
1112         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_init" );
1113         rc = ldap_int_thread_pool_init( tpool, max_threads, max_pending );
1114         if( rc ) {
1115                 ERROR( rc, "ldap_pvt_thread_pool_init" );
1116         } else {
1117                 adjust_count( Idx_tpool, +1 );
1118         }
1119         return rc;
1120 }
1121
1122 int
1123 ldap_pvt_thread_pool_submit(
1124         ldap_pvt_thread_pool_t *tpool,
1125         ldap_pvt_thread_start_t *start_routine, void *arg )
1126 {
1127         int rc, has_pool;
1128         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_submit" );
1129         has_pool = (tpool != NULL && *tpool != NULL);
1130         rc = ldap_int_thread_pool_submit( tpool, start_routine, arg );
1131         if( has_pool )
1132                 ERROR_IF( rc, "ldap_pvt_thread_pool_submit" );
1133         return rc;
1134 }
1135
1136 int
1137 ldap_pvt_thread_pool_maxthreads(
1138         ldap_pvt_thread_pool_t *tpool,
1139         int max_threads )
1140 {
1141         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_maxthreads" );
1142         return ldap_int_thread_pool_maxthreads( tpool, max_threads );
1143 }
1144
1145 int
1146 ldap_pvt_thread_pool_backload( ldap_pvt_thread_pool_t *tpool )
1147 {
1148         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_backload" );
1149         return ldap_int_thread_pool_backload( tpool );
1150 }
1151
1152 int
1153 ldap_pvt_thread_pool_destroy( ldap_pvt_thread_pool_t *tpool, int run_pending )
1154 {
1155         int rc, has_pool;
1156         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_destroy" );
1157         has_pool = (tpool != NULL && *tpool != NULL);
1158         rc = ldap_int_thread_pool_destroy( tpool, run_pending );
1159         if( has_pool ) {
1160                 if( rc ) {
1161                         ERROR( rc, "ldap_pvt_thread_pool_destroy" );
1162                 } else {
1163                         adjust_count( Idx_tpool, -1 );
1164                 }
1165         }
1166         return rc;
1167 }
1168
1169 int
1170 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1171 {
1172         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_pause" );
1173         return ldap_int_thread_pool_pause( tpool );
1174 }
1175
1176 int
1177 ldap_pvt_thread_pool_resume( ldap_pvt_thread_pool_t *tpool )
1178 {
1179         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_resume" );
1180         return ldap_int_thread_pool_resume( tpool );
1181 }
1182
1183 int
1184 ldap_pvt_thread_pool_getkey(
1185         void *xctx,
1186         void *key,
1187         void **data,
1188         ldap_pvt_thread_pool_keyfree_t **kfree )
1189 {
1190 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
1191         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_getkey" );
1192 #endif
1193         return ldap_int_thread_pool_getkey( xctx, key, data, kfree );
1194 }
1195
1196 int
1197 ldap_pvt_thread_pool_setkey(
1198         void *xctx,
1199         void *key,
1200         void *data,
1201         ldap_pvt_thread_pool_keyfree_t *kfree )
1202 {
1203         int rc;
1204         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_setkey" );
1205         rc = ldap_int_thread_pool_setkey( xctx, key, data, kfree );
1206         ERROR_IF( rc, "ldap_pvt_thread_pool_setkey" );
1207         return rc;
1208 }
1209
1210 void
1211 ldap_pvt_thread_pool_purgekey( void *key )
1212 {
1213         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_purgekey" );
1214         ldap_int_thread_pool_purgekey( key );
1215 }
1216
1217 void *
1218 ldap_pvt_thread_pool_context( void )
1219 {
1220 #if 0 /* Function is used by ch_free() via slap_sl_contxt() in slapd */
1221         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context" );
1222 #endif
1223         return ldap_int_thread_pool_context();
1224 }
1225
1226 void
1227 ldap_pvt_thread_pool_context_reset( void *vctx )
1228 {
1229         ERROR_IF( !threading_enabled, "ldap_pvt_thread_pool_context_reset" );
1230         ldap_int_thread_pool_context_reset( vctx );
1231 }
1232
1233 #endif /* LDAP_THREAD_POOL_IMPLEMENTATION */
1234
1235 #endif /* LDAP_THREAD_DEBUG */