1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 static int bdb_cache_delete_internal(Cache *cache, EntryInfo *e);
22 static void bdb_lru_print(Cache *cache);
26 bdb_cache_entryinfo_new( )
30 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
31 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
36 /* Atomically release and reacquire a lock */
38 bdb_cache_entry_db_relock(
53 if ( !lock ) return 0;
56 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
58 list[0].op = DB_LOCK_PUT;
60 list[1].op = DB_LOCK_GET;
62 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
63 list[1].obj = &lockobj;
64 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
69 LDAP_LOG( CACHE, DETAIL1,
70 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
73 Debug( LDAP_DEBUG_TRACE,
74 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
84 bdb_cache_entry_db_lock
85 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
94 if ( !lock ) return 0;
97 db_rw = DB_LOCK_WRITE;
102 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
104 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
105 &lockobj, db_rw, lock);
108 LDAP_LOG( CACHE, DETAIL1,
109 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
110 ei->bei_id, rw, rc );
112 Debug( LDAP_DEBUG_TRACE,
113 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
114 ei->bei_id, rw, rc );
118 #endif /* NO_THREADS */
122 bdb_cache_entry_db_unlock
123 ( DB_ENV *env, DB_LOCK *lock )
130 rc = LOCK_PUT ( env, lock );
136 bdb_cache_entryinfo_destroy( EntryInfo *e )
138 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
139 free( e->bei_nrdn.bv_val );
141 free( e->bei_rdn.bv_val );
147 #define LRU_DELETE( cache, ei ) do { \
148 if ( (ei)->bei_lruprev != NULL ) { \
149 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
151 (cache)->c_lruhead = (ei)->bei_lrunext; \
153 if ( (ei)->bei_lrunext != NULL ) { \
154 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
156 (cache)->c_lrutail = (ei)->bei_lruprev; \
160 #define LRU_ADD( cache, ei ) do { \
161 (ei)->bei_lrunext = (cache)->c_lruhead; \
162 if ( (ei)->bei_lrunext != NULL ) { \
163 (ei)->bei_lrunext->bei_lruprev = (ei); \
165 (cache)->c_lruhead = (ei); \
166 (ei)->bei_lruprev = NULL; \
167 if ( (cache)->c_lrutail == NULL ) { \
168 (cache)->c_lrutail = (ei); \
172 /* Do a length-ordered sort on normalized RDNs */
174 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
176 const EntryInfo *e1 = v_e1, *e2 = v_e2;
177 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
178 if (rc == 0) rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
179 e1->bei_nrdn.bv_len );
184 bdb_id_cmp( const void *v_e1, const void *v_e2 )
186 const EntryInfo *e1 = v_e1, *e2 = v_e2;
187 return e1->bei_id - e2->bei_id;
190 /* Create an entryinfo in the cache. Caller must release the locks later.
193 bdb_entryinfo_add_internal(
194 struct bdb_info *bdb,
200 Cache *cache = &bdb->bi_cache;
201 DB_ENV *env = bdb->bi_dbenv;
202 EntryInfo *ei2 = NULL;
210 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
211 bdb_cache_entryinfo_lock( ei->bei_parent );
213 /* if parent was previously considered a leaf node,
214 * it was on the LRU list. Now it's going to have
215 * kids, take it off the LRU list.
217 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
218 if ( ei->bei_parent->bei_id && !ei->bei_parent->bei_kids ) {
219 LRU_DELETE( cache, ei->bei_parent );
223 cache->c_cursize += incr;
225 /* See if we're above the cache size limit */
226 if ( cache->c_cursize > cache->c_maxsize ) {
227 EntryInfo *elru, *elprev;
230 /* Look for an unused entry to remove */
231 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
232 elprev = elru->bei_lruprev;
234 /* Too many probes, not enough idle, give up */
237 /* If we can successfully writelock it, then
238 * the object is idle.
240 if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
242 /* Need to lock parent to delete child */
243 if ( ldap_pvt_thread_mutex_trylock(
244 &elru->bei_parent->bei_kids_mutex )) {
245 bdb_cache_entry_db_unlock( env, &lock );
248 bdb_cache_delete_internal( cache, elru );
249 bdb_cache_entryinfo_unlock( elru->bei_parent );
250 elru->bei_e->e_private = NULL;
251 bdb_entry_return( elru->bei_e );
252 bdb_cache_entry_db_unlock( env, &lock );
254 bdb_cache_entryinfo_destroy( elru );
256 /* re-use this one */
257 ch_free(elru->bei_nrdn.bv_val);
258 elru->bei_nrdn.bv_val = NULL;
260 elru->bei_kids = NULL;
261 elru->bei_lrunext = NULL;
262 elru->bei_lruprev = NULL;
265 ch_free(elru->bei_rdn.bv_val);
266 elru->bei_rdn.bv_val = NULL;
267 elru->bei_modrdns = 0;
271 if (cache->c_cursize < cache->c_maxsize)
277 ei2 = bdb_cache_entryinfo_new();
279 ei2->bei_id = ei->bei_id;
280 ei2->bei_parent = ei->bei_parent;
282 ei2->bei_rdn = ei->bei_rdn;
285 /* Add to cache ID tree */
286 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
288 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
289 bdb_cache_entryinfo_destroy( ei2 );
292 cache->c_cursize -= incr;
294 /* It got freed above because its value was
297 ei->bei_rdn.bv_val = NULL;
300 LRU_ADD( cache, ei2 );
301 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
305 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
309 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
315 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
316 * the info for its closest ancestor. *res should be NULL to process a
317 * complete DN starting from the tree root. Otherwise *res must be the
318 * immediate parent of the requested DN, and only the RDN will be searched.
319 * The EntryInfo is locked upon return and must be unlocked by the caller.
330 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
331 EntryInfo ei, *eip, *ei2;
335 /* this function is always called with normalized DN */
337 /* we're doing a onelevel search for an RDN */
338 ei.bei_nrdn.bv_val = ndn->bv_val;
339 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
342 /* we're searching a full DN from the root */
343 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
344 ei.bei_nrdn.bv_val = ptr;
345 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
346 eip = &bdb->bi_cache.c_dntree;
349 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
351 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
353 int len = ei.bei_nrdn.bv_len;
355 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
356 bdb_cache_entryinfo_unlock( eip );
358 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
360 bdb_cache_entryinfo_lock( eip );
365 /* DN exists but needs to be added to cache */
366 ei.bei_nrdn.bv_len = len;
367 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
369 /* add_internal left eip and c_rwlock locked */
370 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
375 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
376 /* In the midst of deleting? Give it a chance to
379 bdb_cache_entryinfo_unlock( eip );
380 ldap_pvt_thread_yield();
381 bdb_cache_entryinfo_lock( eip );
385 bdb_cache_entryinfo_unlock( eip );
386 bdb_cache_entryinfo_lock( ei2 );
390 /* Advance to next lower RDN */
391 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
392 && !DN_SEPARATOR(*ptr); ptr--);
393 if ( ptr >= ndn->bv_val ) {
394 if (DN_SEPARATOR(*ptr)) ptr++;
395 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
396 ei.bei_nrdn.bv_val = ptr;
398 if ( ptr < ndn->bv_val ) {
408 /* Walk up the tree from a child node, looking for an ID that's already
409 * been linked into the cache.
412 hdb_cache_find_parent(
419 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
420 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
428 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
431 /* Save the previous node, if any */
434 /* Create a new node for the current ID */
435 ein = bdb_cache_entryinfo_new();
436 ein->bei_id = ei.bei_id;
437 ein->bei_kids = ei.bei_kids;
438 ein->bei_nrdn = ei.bei_nrdn;
439 ein->bei_rdn = ei.bei_rdn;
441 /* This node is not fully connected yet */
442 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
444 /* Insert this node into the ID tree */
445 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
446 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
447 bdb_id_cmp, avl_dup_error ) ) {
449 /* Hm, can this really happen? */
450 bdb_cache_entryinfo_destroy( ein );
451 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
452 (caddr_t) &ei, bdb_id_cmp );
454 bdb_cache_entryinfo_lock( ein );
455 avl_insert( &ein->bei_kids, (caddr_t)ei2,
456 bdb_rdn_cmp, avl_dup_error );
457 bdb_cache_entryinfo_unlock( ein );
461 /* If this is the first time, save this node
462 * to be returned later.
464 if ( eir == NULL ) eir = ein;
466 /* If there was a previous node, link it to this one */
467 if ( ei2 ) ei2->bei_parent = ein;
470 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
471 (caddr_t) &eip, bdb_id_cmp );
473 ei2 = &bdb->bi_cache.c_dntree;
477 ein->bei_parent = ei2;
478 bdb_cache_entryinfo_lock( ei2 );
479 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
481 bdb_cache_entryinfo_unlock( ei2 );
483 bdb_cache_entryinfo_lock( eir );
485 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
487 /* Found a link. Reset all the state info */
488 for (ein = eir; ein != ei2; ein=ein->bei_parent)
489 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
493 ei.bei_id = eip.bei_id;
494 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
502 * cache_find_id - find an entry in the cache, given id.
503 * The entry is locked for Read upon return. Call with islocked TRUE if
504 * the supplied *eip was already locked.
518 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
525 /* If we weren't given any info, see if we have it already cached */
527 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
528 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
529 (caddr_t) &ei, bdb_id_cmp );
531 bdb_cache_entryinfo_lock( *eip );
534 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
537 /* See if the ID exists in the database; add it to the cache if so */
540 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
542 rc = bdb_cache_find_ndn( op, tid,
543 &ep->e_nname, eip, locker );
547 bdb_entry_return( ep );
552 rc = hdb_cache_find_parent(op, tid, id, eip );
553 if ( rc == 0 && *eip )
558 /* Ok, we found the info, do we have the entry? */
559 if ( *eip && rc == 0 ) {
560 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
562 } else if (!(*eip)->bei_e ) {
564 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
567 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
569 ep->e_private = *eip;
574 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
579 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
581 bdb_cache_entry_db_lock( bdb->bi_dbenv,
582 locker, *eip, 1, 0, lock );
583 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
584 bdb_cache_entry_db_relock( bdb->bi_dbenv,
585 locker, *eip, 0, 0, lock );
587 bdb_cache_entry_db_lock( bdb->bi_dbenv,
588 locker, *eip, 0, 0, lock );
591 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
596 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
598 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
599 LRU_DELETE( &bdb->bi_cache, *eip );
600 LRU_ADD( &bdb->bi_cache, *eip );
601 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
605 bdb_cache_entryinfo_unlock( *eip );
619 if ( BEI(e)->bei_kids ) {
622 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
625 rc = bdb_dn2id_children( op, txn, e );
626 if ( rc == DB_NOTFOUND ) {
627 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
632 /* Update the cache after a successful database Add. */
635 struct bdb_info *bdb,
643 struct berval rdn = e->e_name;
650 if ( nrdn->bv_len != e->e_nname.bv_len ) {
651 char *ptr = strchr( rdn.bv_val, ',' );
652 rdn.bv_len = ptr - rdn.bv_val;
654 ber_dupbv( &ei.bei_rdn, &rdn );
656 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
659 new->bei_state = CACHE_ENTRY_NO_KIDS;
660 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
661 bdb_cache_entryinfo_unlock( eip );
662 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
675 EntryInfo *ei = BEI(e);
677 /* Get write lock on data */
678 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
680 /* If we've done repeated mods on a cached entry, then e_attrs
681 * is no longer contiguous with the entry, and must be freed.
683 if ( (void *)e->e_attrs != (void *)(e+1) ) {
684 attrs_free( e->e_attrs );
686 e->e_attrs = newAttrs;
692 * Change the rdn in the entryinfo. Also move to a new parent if needed.
705 EntryInfo *ei = BEI(e), *pei;
709 /* Get write lock on data */
710 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
712 /* If we've done repeated mods on a cached entry, then e_attrs
713 * is no longer contiguous with the entry, and must be freed.
715 if ( (void *)e->e_attrs != (void *)(e+1) ) {
716 attrs_free( e->e_attrs );
718 e->e_attrs = new->e_attrs;
719 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
720 e->e_bv.bv_val + e->e_bv.bv_len ) {
721 ch_free(e->e_name.bv_val);
722 ch_free(e->e_nname.bv_val);
724 e->e_name = new->e_name;
725 e->e_nname = new->e_nname;
727 /* Lock the parent's kids AVL tree */
728 pei = ei->bei_parent;
729 bdb_cache_entryinfo_lock( pei );
730 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
731 free( ei->bei_nrdn.bv_val );
732 ber_dupbv( &ei->bei_nrdn, nrdn );
734 free( ei->bei_rdn.bv_val );
737 if ( nrdn->bv_len != e->e_nname.bv_len ) {
738 char *ptr = strchr(rdn.bv_val, ',');
739 rdn.bv_len = ptr - rdn.bv_val;
741 ber_dupbv( &ei->bei_rdn, &rdn );
745 ein = ei->bei_parent;
747 ei->bei_parent = ein;
748 bdb_cache_entryinfo_unlock( pei );
749 bdb_cache_entryinfo_lock( ein );
752 { int max = ei->bei_modrdns;
753 /* Record the generation number of this change */
754 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
755 if ( pei->bei_modrdns > max )
756 max = pei->bei_modrdns;
758 ei->bei_modrdns = max + 1;
761 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
762 bdb_cache_entryinfo_unlock( ein );
766 * cache_delete - delete the entry e from the cache.
768 * returns: 0 e was deleted ok
769 * 1 e was not in the cache
770 * -1 something bad happened
781 EntryInfo *ei = BEI(e);
784 assert( e->e_private );
786 /* Set this early, warn off any queriers */
787 ei->bei_state |= CACHE_ENTRY_DELETED;
789 /* Get write lock on the data */
790 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
792 /* set cache write lock */
793 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
795 /* Lock the parent's kids tree */
796 bdb_cache_entryinfo_lock( ei->bei_parent );
799 LDAP_LOG( CACHE, ENTRY,
800 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
802 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
807 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
808 rc = bdb_cache_delete_internal( cache, e->e_private );
810 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
812 /* free cache write lock */
813 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
814 bdb_cache_entryinfo_unlock( ei->bei_parent );
819 bdb_cache_delete_cleanup(
823 bdb_cache_entryinfo_destroy( e->e_private );
825 bdb_entry_return( e );
829 bdb_cache_delete_internal(
834 int rc = 0; /* return code */
837 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
842 /* If parent has no more kids, put in on LRU list */
843 if ( e->bei_parent->bei_kids == NULL ) {
844 LRU_ADD( cache, e->bei_parent );
849 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
859 LRU_DELETE( cache, e );
863 * flag entry to be freed later by a call to cache_return_entry()
865 e->bei_state |= CACHE_ENTRY_DELETED;
871 bdb_entryinfo_release( void *data )
873 EntryInfo *ei = (EntryInfo *)data;
874 if ( ei->bei_kids ) {
875 avl_free( ei->bei_kids, NULL );
878 ei->bei_e->e_private = NULL;
879 bdb_entry_return( ei->bei_e );
881 bdb_cache_entryinfo_destroy( ei );
885 bdb_cache_release_all( Cache *cache )
887 /* set cache write lock */
888 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
890 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
893 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
895 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
898 avl_free( cache->c_dntree.bei_kids, NULL );
899 avl_free( cache->c_idtree, bdb_entryinfo_release );
900 cache->c_lruhead = NULL;
901 cache->c_lrutail = NULL;
904 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
905 /* free cache write lock */
906 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
911 bdb_lru_print( Cache *cache )
915 fprintf( stderr, "LRU queue (head to tail):\n" );
916 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
917 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
918 e->bei_nrdn.bv_val, e->bei_id );
920 fprintf( stderr, "LRU queue (tail to head):\n" );
921 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
922 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
923 e->bei_nrdn.bv_val, e->bei_id );
928 #ifdef BDB_REUSE_LOCKERS
930 bdb_locker_id_free( void *key, void *data )
933 int lockid = (int) data;
937 rc = XLOCK_ID_FREE( env, lockid );
938 if ( rc == EINVAL ) {
941 LDAP_LOG( BACK_BDB, ERR,
942 "bdb_locker_id_free: %d err %s(%d)\n",
943 lockid, db_strerror(rc), rc );
945 Debug( LDAP_DEBUG_ANY,
946 "bdb_locker_id_free: %d err %s(%d)\n",
947 lockid, db_strerror(rc), rc );
949 memset( &lr, 0, sizeof(lr) );
951 /* release all locks held by this locker. */
952 lr.op = DB_LOCK_PUT_ALL;
953 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
954 XLOCK_ID_FREE( env, lockid );
959 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
965 if ( !env || !locker ) return -1;
967 /* If no op was provided, try to find the ctx anyway... */
969 ctx = op->o_threadctx;
971 ctx = ldap_pvt_thread_pool_context();
974 /* Shouldn't happen unless we're single-threaded */
980 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
981 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
982 rc = XLOCK_ID( env, &lockid );
983 if (rc) ldap_pvt_thread_yield();
988 data = (void *)lockid;
989 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
990 data, bdb_locker_id_free ) ) ) {
991 XLOCK_ID_FREE( env, lockid );
993 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
994 db_strerror(rc), rc, 0 );
996 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
997 db_strerror(rc), rc, 0 );