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 EntryInfo *ei2 = NULL;
204 ei2 = bdb_cache_entryinfo_new();
206 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
207 bdb_cache_entryinfo_lock( ei->bei_parent );
209 ei2->bei_id = ei->bei_id;
210 ei2->bei_parent = ei->bei_parent;
212 ei2->bei_rdn = ei->bei_rdn;
215 /* Add to cache ID tree */
216 if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
218 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
219 bdb_cache_entryinfo_destroy( ei2 );
222 /* It got freed above because its value was
225 ei->bei_rdn.bv_val = NULL;
228 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
229 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
237 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
238 * the info for its closest ancestor. *res should be NULL to process a
239 * complete DN starting from the tree root. Otherwise *res must be the
240 * immediate parent of the requested DN, and only the RDN will be searched.
241 * The EntryInfo is locked upon return and must be unlocked by the caller.
252 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
253 EntryInfo ei, *eip, *ei2;
257 /* this function is always called with normalized DN */
259 /* we're doing a onelevel search for an RDN */
260 ei.bei_nrdn.bv_val = ndn->bv_val;
261 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
264 /* we're searching a full DN from the root */
265 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
266 ei.bei_nrdn.bv_val = ptr;
267 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
268 eip = &bdb->bi_cache.c_dntree;
271 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
273 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
275 int len = ei.bei_nrdn.bv_len;
277 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
278 bdb_cache_entryinfo_unlock( eip );
280 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
282 bdb_cache_entryinfo_lock( eip );
287 /* DN exists but needs to be added to cache */
288 ei.bei_nrdn.bv_len = len;
289 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
291 /* add_internal left eip and c_rwlock locked */
292 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
297 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
298 /* In the midst of deleting? Give it a chance to
301 bdb_cache_entryinfo_unlock( eip );
302 ldap_pvt_thread_yield();
303 bdb_cache_entryinfo_lock( eip );
307 bdb_cache_entryinfo_unlock( eip );
308 bdb_cache_entryinfo_lock( ei2 );
312 /* Advance to next lower RDN */
313 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
314 && !DN_SEPARATOR(*ptr); ptr--);
315 if ( ptr >= ndn->bv_val ) {
316 if (DN_SEPARATOR(*ptr)) ptr++;
317 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
318 ei.bei_nrdn.bv_val = ptr;
320 if ( ptr < ndn->bv_val ) {
330 /* Walk up the tree from a child node, looking for an ID that's already
331 * been linked into the cache.
334 hdb_cache_find_parent(
341 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
342 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
343 char ndn[SLAP_LDAPDN_MAXLEN];
352 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
355 /* Save the previous node, if any */
358 /* Create a new node for the current ID */
359 ein = bdb_cache_entryinfo_new();
360 ein->bei_id = ei.bei_id;
361 ein->bei_kids = ei.bei_kids;
362 ein->bei_nrdn = ei.bei_nrdn;
363 ein->bei_rdn = ei.bei_rdn;
365 /* This node is not fully connected yet */
366 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
368 /* Insert this node into the ID tree */
369 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
370 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
371 bdb_id_cmp, avl_dup_error ) ) {
373 /* Someone else created this node just before us.
374 * Free our new copy and use the existing one.
376 bdb_cache_entryinfo_destroy( ein );
377 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
378 (caddr_t) &ei, bdb_id_cmp );
380 /* Link in any kids we've already processed */
382 bdb_cache_entryinfo_lock( ein );
383 avl_insert( &ein->bei_kids, (caddr_t)ei2,
384 bdb_rdn_cmp, avl_dup_error );
385 bdb_cache_entryinfo_unlock( ein );
393 /* If this is the first time, save this node
394 * to be returned later.
396 if ( eir == NULL ) eir = ein;
398 /* If there was a previous node, link it to this one */
399 if ( ei2 ) ei2->bei_parent = ein;
401 /* Look for this node's parent */
403 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
404 (caddr_t) &eip, bdb_id_cmp );
406 ei2 = &bdb->bi_cache.c_dntree;
408 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
410 /* Got the parent, link in and we're done. */
412 bdb_cache_entryinfo_lock( ei2 );
413 ein->bei_parent = ei2;
414 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
416 bdb_cache_entryinfo_unlock( ei2 );
417 bdb_cache_entryinfo_lock( eir );
419 /* Reset all the state info */
420 for (ein = eir; ein != ei2; ein=ein->bei_parent)
421 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
426 ei.bei_id = eip.bei_id;
427 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
434 /* caller must have lru_mutex locked. mutex
435 * will be unlocked on return.
439 struct bdb_info *bdb,
446 /* See if we're above the cache size limit */
447 if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
448 EntryInfo *elru, *elprev;
451 /* Look for an unused entry to remove */
452 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
453 elprev = elru->bei_lruprev;
455 /* Too many probes, not enough idle, give up */
458 /* If we can successfully writelock it, then
459 * the object is idle.
461 if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, elru, 1, 1,
463 /* If there's no entry, or this node is in
464 * the process of linking into the cache,
467 if ( !elru->bei_e || (elru->bei_state & CACHE_ENTRY_NOT_LINKED) ) {
468 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
471 LRU_DELETE( &bdb->bi_cache, elru );
472 elru->bei_e->e_private = NULL;
473 bdb_entry_return( elru->bei_e );
475 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &lock );
476 --bdb->bi_cache.c_cursize;
477 if (bdb->bi_cache.c_cursize < bdb->bi_cache.c_maxsize)
482 LRU_ADD( &bdb->bi_cache, ei );
483 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
487 * cache_find_id - find an entry in the cache, given id.
488 * The entry is locked for Read upon return. Call with islocked TRUE if
489 * the supplied *eip was already locked.
503 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
511 /* If we weren't given any info, see if we have it already cached */
513 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
514 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
515 (caddr_t) &ei, bdb_id_cmp );
517 /* If the lock attempt fails, the info is in use */
518 if ( ldap_pvt_thread_mutex_trylock(
519 &(*eip)->bei_kids_mutex )) {
520 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
521 /* If this node is being deleted, treat
522 * as if the delete has already finished
524 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
527 /* otherwise, wait for the info to free up */
528 ldap_pvt_thread_yield();
531 /* If this info isn't hooked up to its parent yet,
532 * unlock and wait for it to be fully initialized
534 if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
535 bdb_cache_entryinfo_unlock( *eip );
536 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
537 ldap_pvt_thread_yield();
542 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
545 /* See if the ID exists in the database; add it to the cache if so */
548 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
550 rc = bdb_cache_find_ndn( op, tid,
551 &ep->e_nname, eip, locker );
555 bdb_entry_return( ep );
560 rc = hdb_cache_find_parent(op, tid, id, eip );
561 if ( rc == 0 && *eip )
566 /* Ok, we found the info, do we have the entry? */
567 if ( *eip && rc == 0 ) {
568 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
571 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
573 if ( !(*eip)->bei_e ) {
575 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
578 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
580 /* Make sure no other modifier beat us to it */
581 if ( (*eip)->bei_e ) {
582 bdb_entry_return( ep );
585 ep->e_private = *eip;
591 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
595 /* If we had the entry already, this item
596 * is on the LRU list.
600 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
602 bdb_cache_entry_db_relock( bdb->bi_dbenv,
603 locker, *eip, 1, 0, lock );
604 /* check again in case other modifier did it already */
605 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
606 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
607 bdb_cache_entry_db_relock( bdb->bi_dbenv,
608 locker, *eip, 0, 0, lock );
616 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
617 /* if entry is old, remove from old spot on LRU list */
619 LRU_DELETE( &bdb->bi_cache, *eip );
621 /* if entry is new, bump cache size */
622 bdb->bi_cache.c_cursize++;
624 /* lru_mutex is unlocked for us */
625 bdb_cache_lru_add( bdb, locker, *eip );
629 bdb_cache_entryinfo_unlock( *eip );
643 if ( BEI(e)->bei_kids ) {
646 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
649 rc = bdb_dn2id_children( op, txn, e );
650 if ( rc == DB_NOTFOUND ) {
651 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
656 /* Update the cache after a successful database Add. */
659 struct bdb_info *bdb,
667 struct berval rdn = e->e_name;
674 if ( nrdn->bv_len != e->e_nname.bv_len ) {
675 char *ptr = strchr( rdn.bv_val, ',' );
676 rdn.bv_len = ptr - rdn.bv_val;
678 ber_dupbv( &ei.bei_rdn, &rdn );
680 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
683 new->bei_state = CACHE_ENTRY_NO_KIDS;
684 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
687 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
688 ++bdb->bi_cache.c_cursize;
689 /* lru_mutex is unlocked for us */
690 bdb_cache_lru_add( bdb, locker, new );
692 bdb_cache_entryinfo_unlock( eip );
693 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
706 EntryInfo *ei = BEI(e);
708 /* Get write lock on data */
709 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
711 /* If we've done repeated mods on a cached entry, then e_attrs
712 * is no longer contiguous with the entry, and must be freed.
714 if ( (void *)e->e_attrs != (void *)(e+1) ) {
715 attrs_free( e->e_attrs );
717 e->e_attrs = newAttrs;
723 * Change the rdn in the entryinfo. Also move to a new parent if needed.
736 EntryInfo *ei = BEI(e), *pei;
740 /* Get write lock on data */
741 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
743 /* If we've done repeated mods on a cached entry, then e_attrs
744 * is no longer contiguous with the entry, and must be freed.
746 if ( (void *)e->e_attrs != (void *)(e+1) ) {
747 attrs_free( e->e_attrs );
749 e->e_attrs = new->e_attrs;
750 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
751 e->e_bv.bv_val + e->e_bv.bv_len ) {
752 ch_free(e->e_name.bv_val);
753 ch_free(e->e_nname.bv_val);
755 e->e_name = new->e_name;
756 e->e_nname = new->e_nname;
758 /* Lock the parent's kids AVL tree */
759 pei = ei->bei_parent;
760 bdb_cache_entryinfo_lock( pei );
761 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
762 free( ei->bei_nrdn.bv_val );
763 ber_dupbv( &ei->bei_nrdn, nrdn );
765 free( ei->bei_rdn.bv_val );
768 if ( nrdn->bv_len != e->e_nname.bv_len ) {
769 char *ptr = strchr(rdn.bv_val, ',');
770 rdn.bv_len = ptr - rdn.bv_val;
772 ber_dupbv( &ei->bei_rdn, &rdn );
776 ein = ei->bei_parent;
778 ei->bei_parent = ein;
779 bdb_cache_entryinfo_unlock( pei );
780 bdb_cache_entryinfo_lock( ein );
783 { int max = ei->bei_modrdns;
784 /* Record the generation number of this change */
785 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
786 if ( pei->bei_modrdns > max )
787 max = pei->bei_modrdns;
789 ei->bei_modrdns = max + 1;
792 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
793 bdb_cache_entryinfo_unlock( ein );
797 * cache_delete - delete the entry e from the cache.
799 * returns: 0 e was deleted ok
800 * 1 e was not in the cache
801 * -1 something bad happened
812 EntryInfo *ei = BEI(e);
815 assert( e->e_private );
817 /* Set this early, warn off any queriers */
818 ei->bei_state |= CACHE_ENTRY_DELETED;
820 /* Lock the entry's info */
821 bdb_cache_entryinfo_lock( ei );
823 /* Get write lock on the data */
824 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
826 /* set cache write lock */
827 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
829 /* Lock the parent's kids tree */
830 bdb_cache_entryinfo_lock( ei->bei_parent );
833 LDAP_LOG( CACHE, ENTRY,
834 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
836 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
841 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
842 rc = bdb_cache_delete_internal( cache, e->e_private );
844 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
846 /* free cache write lock */
847 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
848 bdb_cache_entryinfo_unlock( ei->bei_parent );
850 /* Leave entry info locked */
856 bdb_cache_delete_cleanup(
860 bdb_cache_entryinfo_unlock( BEI(e) );
861 bdb_cache_entryinfo_destroy( e->e_private );
863 bdb_entry_return( e );
867 bdb_cache_delete_internal(
872 int rc = 0; /* return code */
875 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
881 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
891 LRU_DELETE( cache, e );
895 * flag entry to be freed later by a call to cache_return_entry()
897 e->bei_state |= CACHE_ENTRY_DELETED;
903 bdb_entryinfo_release( void *data )
905 EntryInfo *ei = (EntryInfo *)data;
906 if ( ei->bei_kids ) {
907 avl_free( ei->bei_kids, NULL );
910 ei->bei_e->e_private = NULL;
911 bdb_entry_return( ei->bei_e );
913 bdb_cache_entryinfo_destroy( ei );
917 bdb_cache_release_all( Cache *cache )
919 /* set cache write lock */
920 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
922 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
925 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
927 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
930 avl_free( cache->c_dntree.bei_kids, NULL );
931 avl_free( cache->c_idtree, bdb_entryinfo_release );
932 cache->c_lruhead = NULL;
933 cache->c_lrutail = NULL;
936 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
937 /* free cache write lock */
938 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
943 bdb_lru_print( Cache *cache )
947 fprintf( stderr, "LRU queue (head to tail):\n" );
948 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
949 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
950 e->bei_nrdn.bv_val, e->bei_id );
952 fprintf( stderr, "LRU queue (tail to head):\n" );
953 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
954 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
955 e->bei_nrdn.bv_val, e->bei_id );
960 #ifdef BDB_REUSE_LOCKERS
962 bdb_locker_id_free( void *key, void *data )
965 int lockid = (int) data;
969 rc = XLOCK_ID_FREE( env, lockid );
970 if ( rc == EINVAL ) {
973 LDAP_LOG( BACK_BDB, ERR,
974 "bdb_locker_id_free: %d err %s(%d)\n",
975 lockid, db_strerror(rc), rc );
977 Debug( LDAP_DEBUG_ANY,
978 "bdb_locker_id_free: %d err %s(%d)\n",
979 lockid, db_strerror(rc), rc );
981 memset( &lr, 0, sizeof(lr) );
983 /* release all locks held by this locker. */
984 lr.op = DB_LOCK_PUT_ALL;
985 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
986 XLOCK_ID_FREE( env, lockid );
991 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
997 if ( !env || !locker ) return -1;
999 /* If no op was provided, try to find the ctx anyway... */
1001 ctx = op->o_threadctx;
1003 ctx = ldap_pvt_thread_pool_context();
1006 /* Shouldn't happen unless we're single-threaded */
1012 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1013 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1014 rc = XLOCK_ID( env, &lockid );
1015 if (rc) ldap_pvt_thread_yield();
1020 data = (void *)lockid;
1021 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1022 data, bdb_locker_id_free ) ) ) {
1023 XLOCK_ID_FREE( env, lockid );
1025 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1026 db_strerror(rc), rc, 0 );
1028 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1029 db_strerror(rc), rc, 0 );