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 if ( !elru->bei_e ) {
243 bdb_cache_entry_db_unlock( env, &lock );
246 /* Need to lock parent to delete child */
247 if ( ldap_pvt_thread_mutex_trylock(
248 &elru->bei_parent->bei_kids_mutex )) {
249 bdb_cache_entry_db_unlock( env, &lock );
252 bdb_cache_delete_internal( cache, elru );
253 bdb_cache_entryinfo_unlock( elru->bei_parent );
254 elru->bei_e->e_private = NULL;
255 bdb_entry_return( elru->bei_e );
256 bdb_cache_entry_db_unlock( env, &lock );
258 bdb_cache_entryinfo_destroy( elru );
260 /* re-use this one */
261 ch_free(elru->bei_nrdn.bv_val);
262 elru->bei_nrdn.bv_val = NULL;
264 elru->bei_kids = NULL;
265 elru->bei_lrunext = NULL;
266 elru->bei_lruprev = NULL;
269 ch_free(elru->bei_rdn.bv_val);
270 elru->bei_rdn.bv_val = NULL;
271 elru->bei_modrdns = 0;
275 if (cache->c_cursize < cache->c_maxsize)
281 ei2 = bdb_cache_entryinfo_new();
283 ei2->bei_id = ei->bei_id;
284 ei2->bei_parent = ei->bei_parent;
286 ei2->bei_rdn = ei->bei_rdn;
289 /* Add to cache ID tree */
290 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
292 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
293 bdb_cache_entryinfo_destroy( ei2 );
296 cache->c_cursize -= incr;
298 /* It got freed above because its value was
301 ei->bei_rdn.bv_val = NULL;
304 LRU_ADD( cache, ei2 );
305 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
309 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
313 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
319 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
320 * the info for its closest ancestor. *res should be NULL to process a
321 * complete DN starting from the tree root. Otherwise *res must be the
322 * immediate parent of the requested DN, and only the RDN will be searched.
323 * The EntryInfo is locked upon return and must be unlocked by the caller.
334 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
335 EntryInfo ei, *eip, *ei2;
339 /* this function is always called with normalized DN */
341 /* we're doing a onelevel search for an RDN */
342 ei.bei_nrdn.bv_val = ndn->bv_val;
343 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
346 /* we're searching a full DN from the root */
347 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
348 ei.bei_nrdn.bv_val = ptr;
349 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
350 eip = &bdb->bi_cache.c_dntree;
353 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
355 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
357 int len = ei.bei_nrdn.bv_len;
359 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
360 bdb_cache_entryinfo_unlock( eip );
362 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
364 bdb_cache_entryinfo_lock( eip );
369 /* DN exists but needs to be added to cache */
370 ei.bei_nrdn.bv_len = len;
371 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
373 /* add_internal left eip and c_rwlock locked */
374 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
379 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
380 /* In the midst of deleting? Give it a chance to
383 bdb_cache_entryinfo_unlock( eip );
384 ldap_pvt_thread_yield();
385 bdb_cache_entryinfo_lock( eip );
389 bdb_cache_entryinfo_unlock( eip );
390 bdb_cache_entryinfo_lock( ei2 );
394 /* Advance to next lower RDN */
395 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
396 && !DN_SEPARATOR(*ptr); ptr--);
397 if ( ptr >= ndn->bv_val ) {
398 if (DN_SEPARATOR(*ptr)) ptr++;
399 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
400 ei.bei_nrdn.bv_val = ptr;
402 if ( ptr < ndn->bv_val ) {
412 /* Walk up the tree from a child node, looking for an ID that's already
413 * been linked into the cache.
416 hdb_cache_find_parent(
423 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
424 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
432 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
435 /* Save the previous node, if any */
438 /* Create a new node for the current ID */
439 ein = bdb_cache_entryinfo_new();
440 ein->bei_id = ei.bei_id;
441 ein->bei_kids = ei.bei_kids;
442 ein->bei_nrdn = ei.bei_nrdn;
443 ein->bei_rdn = ei.bei_rdn;
445 /* This node is not fully connected yet */
446 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
448 /* Insert this node into the ID tree */
449 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
450 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
451 bdb_id_cmp, avl_dup_error ) ) {
453 /* Hm, can this really happen? */
454 bdb_cache_entryinfo_destroy( ein );
455 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
456 (caddr_t) &ei, bdb_id_cmp );
458 bdb_cache_entryinfo_lock( ein );
459 avl_insert( &ein->bei_kids, (caddr_t)ei2,
460 bdb_rdn_cmp, avl_dup_error );
461 bdb_cache_entryinfo_unlock( ein );
465 /* If this is the first time, save this node
466 * to be returned later.
468 if ( eir == NULL ) eir = ein;
470 /* If there was a previous node, link it to this one */
471 if ( ei2 ) ei2->bei_parent = ein;
474 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
475 (caddr_t) &eip, bdb_id_cmp );
477 ei2 = &bdb->bi_cache.c_dntree;
481 ein->bei_parent = ei2;
482 bdb_cache_entryinfo_lock( ei2 );
483 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
485 bdb_cache_entryinfo_unlock( ei2 );
487 bdb_cache_entryinfo_lock( eir );
489 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
491 /* Found a link. Reset all the state info */
492 for (ein = eir; ein != ei2; ein=ein->bei_parent)
493 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
497 ei.bei_id = eip.bei_id;
498 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
506 * cache_find_id - find an entry in the cache, given id.
507 * The entry is locked for Read upon return. Call with islocked TRUE if
508 * the supplied *eip was already locked.
522 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
529 /* If we weren't given any info, see if we have it already cached */
531 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
532 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
533 (caddr_t) &ei, bdb_id_cmp );
535 if ( ldap_pvt_thread_mutex_trylock(
536 &(*eip)->bei_kids_mutex )) {
537 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
538 ldap_pvt_thread_yield();
543 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
546 /* See if the ID exists in the database; add it to the cache if so */
549 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
551 rc = bdb_cache_find_ndn( op, tid,
552 &ep->e_nname, eip, locker );
556 bdb_entry_return( ep );
561 rc = hdb_cache_find_parent(op, tid, id, eip );
562 if ( rc == 0 && *eip )
567 /* Ok, we found the info, do we have the entry? */
568 if ( *eip && rc == 0 ) {
569 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
571 } else if ( !(*eip)->bei_e ) {
573 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
576 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
578 ep->e_private = *eip;
583 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
588 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
590 bdb_cache_entry_db_lock( bdb->bi_dbenv,
591 locker, *eip, 1, 0, lock );
592 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
593 bdb_cache_entry_db_relock( bdb->bi_dbenv,
594 locker, *eip, 0, 0, lock );
596 bdb_cache_entry_db_lock( bdb->bi_dbenv,
597 locker, *eip, 0, 0, lock );
600 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
605 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
607 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
608 LRU_DELETE( &bdb->bi_cache, *eip );
609 LRU_ADD( &bdb->bi_cache, *eip );
610 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
614 bdb_cache_entryinfo_unlock( *eip );
628 if ( BEI(e)->bei_kids ) {
631 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
634 rc = bdb_dn2id_children( op, txn, e );
635 if ( rc == DB_NOTFOUND ) {
636 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
641 /* Update the cache after a successful database Add. */
644 struct bdb_info *bdb,
652 struct berval rdn = e->e_name;
659 if ( nrdn->bv_len != e->e_nname.bv_len ) {
660 char *ptr = strchr( rdn.bv_val, ',' );
661 rdn.bv_len = ptr - rdn.bv_val;
663 ber_dupbv( &ei.bei_rdn, &rdn );
665 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
668 new->bei_state = CACHE_ENTRY_NO_KIDS;
669 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
670 bdb_cache_entryinfo_unlock( eip );
671 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
684 EntryInfo *ei = BEI(e);
686 /* Get write lock on data */
687 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
689 /* If we've done repeated mods on a cached entry, then e_attrs
690 * is no longer contiguous with the entry, and must be freed.
692 if ( (void *)e->e_attrs != (void *)(e+1) ) {
693 attrs_free( e->e_attrs );
695 e->e_attrs = newAttrs;
701 * Change the rdn in the entryinfo. Also move to a new parent if needed.
714 EntryInfo *ei = BEI(e), *pei;
718 /* Get write lock on data */
719 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
721 /* If we've done repeated mods on a cached entry, then e_attrs
722 * is no longer contiguous with the entry, and must be freed.
724 if ( (void *)e->e_attrs != (void *)(e+1) ) {
725 attrs_free( e->e_attrs );
727 e->e_attrs = new->e_attrs;
728 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
729 e->e_bv.bv_val + e->e_bv.bv_len ) {
730 ch_free(e->e_name.bv_val);
731 ch_free(e->e_nname.bv_val);
733 e->e_name = new->e_name;
734 e->e_nname = new->e_nname;
736 /* Lock the parent's kids AVL tree */
737 pei = ei->bei_parent;
738 bdb_cache_entryinfo_lock( pei );
739 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
740 free( ei->bei_nrdn.bv_val );
741 ber_dupbv( &ei->bei_nrdn, nrdn );
743 free( ei->bei_rdn.bv_val );
746 if ( nrdn->bv_len != e->e_nname.bv_len ) {
747 char *ptr = strchr(rdn.bv_val, ',');
748 rdn.bv_len = ptr - rdn.bv_val;
750 ber_dupbv( &ei->bei_rdn, &rdn );
754 ein = ei->bei_parent;
756 ei->bei_parent = ein;
757 bdb_cache_entryinfo_unlock( pei );
758 bdb_cache_entryinfo_lock( ein );
761 { int max = ei->bei_modrdns;
762 /* Record the generation number of this change */
763 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
764 if ( pei->bei_modrdns > max )
765 max = pei->bei_modrdns;
767 ei->bei_modrdns = max + 1;
770 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
771 bdb_cache_entryinfo_unlock( ein );
775 * cache_delete - delete the entry e from the cache.
777 * returns: 0 e was deleted ok
778 * 1 e was not in the cache
779 * -1 something bad happened
790 EntryInfo *ei = BEI(e);
793 assert( e->e_private );
795 /* Set this early, warn off any queriers */
796 ei->bei_state |= CACHE_ENTRY_DELETED;
798 /* Get write lock on the data */
799 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
801 /* set cache write lock */
802 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
804 /* Lock the parent's kids tree */
805 bdb_cache_entryinfo_lock( ei->bei_parent );
808 LDAP_LOG( CACHE, ENTRY,
809 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
811 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
816 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
817 rc = bdb_cache_delete_internal( cache, e->e_private );
819 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
821 /* free cache write lock */
822 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
823 bdb_cache_entryinfo_unlock( ei->bei_parent );
828 bdb_cache_delete_cleanup(
832 bdb_cache_entryinfo_destroy( e->e_private );
834 bdb_entry_return( e );
838 bdb_cache_delete_internal(
843 int rc = 0; /* return code */
846 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
851 /* If parent has no more kids, put in on LRU list */
852 if ( e->bei_parent->bei_kids == NULL ) {
853 LRU_ADD( cache, e->bei_parent );
858 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
868 LRU_DELETE( cache, e );
872 * flag entry to be freed later by a call to cache_return_entry()
874 e->bei_state |= CACHE_ENTRY_DELETED;
880 bdb_entryinfo_release( void *data )
882 EntryInfo *ei = (EntryInfo *)data;
883 if ( ei->bei_kids ) {
884 avl_free( ei->bei_kids, NULL );
887 ei->bei_e->e_private = NULL;
888 bdb_entry_return( ei->bei_e );
890 bdb_cache_entryinfo_destroy( ei );
894 bdb_cache_release_all( Cache *cache )
896 /* set cache write lock */
897 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
899 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
902 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
904 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
907 avl_free( cache->c_dntree.bei_kids, NULL );
908 avl_free( cache->c_idtree, bdb_entryinfo_release );
909 cache->c_lruhead = NULL;
910 cache->c_lrutail = NULL;
913 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
914 /* free cache write lock */
915 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
920 bdb_lru_print( Cache *cache )
924 fprintf( stderr, "LRU queue (head to tail):\n" );
925 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
926 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
927 e->bei_nrdn.bv_val, e->bei_id );
929 fprintf( stderr, "LRU queue (tail to head):\n" );
930 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
931 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
932 e->bei_nrdn.bv_val, e->bei_id );
937 #ifdef BDB_REUSE_LOCKERS
939 bdb_locker_id_free( void *key, void *data )
942 int lockid = (int) data;
946 rc = XLOCK_ID_FREE( env, lockid );
947 if ( rc == EINVAL ) {
950 LDAP_LOG( BACK_BDB, ERR,
951 "bdb_locker_id_free: %d err %s(%d)\n",
952 lockid, db_strerror(rc), rc );
954 Debug( LDAP_DEBUG_ANY,
955 "bdb_locker_id_free: %d err %s(%d)\n",
956 lockid, db_strerror(rc), rc );
958 memset( &lr, 0, sizeof(lr) );
960 /* release all locks held by this locker. */
961 lr.op = DB_LOCK_PUT_ALL;
962 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
963 XLOCK_ID_FREE( env, lockid );
968 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
974 if ( !env || !locker ) return -1;
976 /* If no op was provided, try to find the ctx anyway... */
978 ctx = op->o_threadctx;
980 ctx = ldap_pvt_thread_pool_context();
983 /* Shouldn't happen unless we're single-threaded */
989 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
990 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
991 rc = XLOCK_ID( env, &lockid );
992 if (rc) ldap_pvt_thread_yield();
997 data = (void *)lockid;
998 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
999 data, bdb_locker_id_free ) ) ) {
1000 XLOCK_ID_FREE( env, lockid );
1002 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1003 db_strerror(rc), rc, 0 );
1005 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1006 db_strerror(rc), rc, 0 );