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 */
42 bdb_cache_entry_db_relock(
57 if ( !lock ) return 0;
60 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
62 list[0].op = DB_LOCK_PUT;
64 list[1].op = DB_LOCK_GET;
66 list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
67 list[1].obj = &lockobj;
68 rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
73 LDAP_LOG( CACHE, DETAIL1,
74 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
77 Debug( LDAP_DEBUG_TRACE,
78 "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
88 bdb_cache_entry_db_lock
89 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
98 if ( !lock ) return 0;
101 db_rw = DB_LOCK_WRITE;
103 db_rw = DB_LOCK_READ;
106 lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
108 rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
109 &lockobj, db_rw, lock);
112 LDAP_LOG( CACHE, DETAIL1,
113 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
114 ei->bei_id, rw, rc );
116 Debug( LDAP_DEBUG_TRACE,
117 "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
118 ei->bei_id, rw, rc );
122 #endif /* NO_THREADS */
126 bdb_cache_entry_db_unlock
127 ( DB_ENV *env, DB_LOCK *lock )
134 rc = LOCK_PUT ( env, lock );
140 bdb_cache_entryinfo_destroy( EntryInfo *e )
142 ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
143 free( e->bei_nrdn.bv_val );
145 free( e->bei_rdn.bv_val );
151 #define LRU_DELETE( cache, ei ) do { \
152 if ( (ei)->bei_lruprev != NULL ) { \
153 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
155 (cache)->c_lruhead = (ei)->bei_lrunext; \
157 if ( (ei)->bei_lrunext != NULL ) { \
158 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
160 (cache)->c_lrutail = (ei)->bei_lruprev; \
164 #define LRU_ADD( cache, ei ) do { \
165 (ei)->bei_lrunext = (cache)->c_lruhead; \
166 if ( (ei)->bei_lrunext != NULL ) { \
167 (ei)->bei_lrunext->bei_lruprev = (ei); \
169 (cache)->c_lruhead = (ei); \
170 (ei)->bei_lruprev = NULL; \
171 if ( (cache)->c_lrutail == NULL ) { \
172 (cache)->c_lrutail = (ei); \
176 /* Do a length-ordered sort on normalized RDNs */
178 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
180 const EntryInfo *e1 = v_e1, *e2 = v_e2;
181 int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
182 if (rc == 0) rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
183 e1->bei_nrdn.bv_len );
188 bdb_id_cmp( const void *v_e1, const void *v_e2 )
190 const EntryInfo *e1 = v_e1, *e2 = v_e2;
191 return e1->bei_id - e2->bei_id;
194 /* Create an entryinfo in the cache. Caller must release the locks later.
197 bdb_entryinfo_add_internal(
198 struct bdb_info *bdb,
204 Cache *cache = &bdb->bi_cache;
205 DB_ENV *env = bdb->bi_dbenv;
206 EntryInfo *ei2 = NULL;
214 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
215 bdb_cache_entryinfo_lock( ei->bei_parent );
217 /* if parent was previously considered a leaf node,
218 * it was on the LRU list. Now it's going to have
219 * kids, take it off the LRU list.
221 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
222 if ( ei->bei_parent->bei_id && !ei->bei_parent->bei_kids ) {
223 LRU_DELETE( cache, ei->bei_parent );
227 cache->c_cursize += incr;
229 /* See if we're above the cache size limit */
230 if ( cache->c_cursize > cache->c_maxsize ) {
231 EntryInfo *elru, *elprev;
234 /* Look for an unused entry to remove */
235 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
236 elprev = elru->bei_lruprev;
238 /* Too many probes, not enough idle, give up */
241 /* If we can successfully writelock it, then
242 * the object is idle.
244 if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
246 if ( !elru->bei_e ) {
247 bdb_cache_entry_db_unlock( env, &lock );
250 /* Need to lock parent to delete child */
251 if ( ldap_pvt_thread_mutex_trylock(
252 &elru->bei_parent->bei_kids_mutex )) {
253 bdb_cache_entry_db_unlock( env, &lock );
256 bdb_cache_delete_internal( cache, elru );
257 bdb_cache_entryinfo_unlock( elru->bei_parent );
258 elru->bei_e->e_private = NULL;
259 bdb_entry_return( elru->bei_e );
260 bdb_cache_entry_db_unlock( env, &lock );
262 bdb_cache_entryinfo_destroy( elru );
264 /* re-use this one */
265 ch_free(elru->bei_nrdn.bv_val);
266 elru->bei_nrdn.bv_val = NULL;
268 elru->bei_kids = NULL;
269 elru->bei_lrunext = NULL;
270 elru->bei_lruprev = NULL;
273 ch_free(elru->bei_rdn.bv_val);
274 elru->bei_rdn.bv_val = NULL;
275 elru->bei_modrdns = 0;
279 if (cache->c_cursize < cache->c_maxsize)
285 ei2 = bdb_cache_entryinfo_new();
287 ei2->bei_id = ei->bei_id;
288 ei2->bei_parent = ei->bei_parent;
290 ei2->bei_rdn = ei->bei_rdn;
293 /* Add to cache ID tree */
294 if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
296 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
297 bdb_cache_entryinfo_destroy( ei2 );
300 cache->c_cursize -= incr;
302 /* It got freed above because its value was
305 ei->bei_rdn.bv_val = NULL;
308 LRU_ADD( cache, ei2 );
309 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
313 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
317 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
323 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
324 * the info for its closest ancestor. *res should be NULL to process a
325 * complete DN starting from the tree root. Otherwise *res must be the
326 * immediate parent of the requested DN, and only the RDN will be searched.
327 * The EntryInfo is locked upon return and must be unlocked by the caller.
338 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
339 EntryInfo ei, *eip, *ei2;
343 /* this function is always called with normalized DN */
345 /* we're doing a onelevel search for an RDN */
346 ei.bei_nrdn.bv_val = ndn->bv_val;
347 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
350 /* we're searching a full DN from the root */
351 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
352 ei.bei_nrdn.bv_val = ptr;
353 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
354 eip = &bdb->bi_cache.c_dntree;
357 for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
359 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
361 int len = ei.bei_nrdn.bv_len;
363 ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
364 bdb_cache_entryinfo_unlock( eip );
366 rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
368 bdb_cache_entryinfo_lock( eip );
373 /* DN exists but needs to be added to cache */
374 ei.bei_nrdn.bv_len = len;
375 rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
377 /* add_internal left eip and c_rwlock locked */
378 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
383 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
384 /* In the midst of deleting? Give it a chance to
387 bdb_cache_entryinfo_unlock( eip );
388 ldap_pvt_thread_yield();
389 bdb_cache_entryinfo_lock( eip );
393 bdb_cache_entryinfo_unlock( eip );
394 bdb_cache_entryinfo_lock( ei2 );
398 /* Advance to next lower RDN */
399 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
400 && !DN_SEPARATOR(*ptr); ptr--);
401 if ( ptr >= ndn->bv_val ) {
402 if (DN_SEPARATOR(*ptr)) ptr++;
403 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
404 ei.bei_nrdn.bv_val = ptr;
406 if ( ptr < ndn->bv_val ) {
416 /* Walk up the tree from a child node, looking for an ID that's already
417 * been linked into the cache.
420 hdb_cache_find_parent(
427 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
428 EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
436 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
439 /* Save the previous node, if any */
442 /* Create a new node for the current ID */
443 ein = bdb_cache_entryinfo_new();
444 ein->bei_id = ei.bei_id;
445 ein->bei_kids = ei.bei_kids;
446 ein->bei_nrdn = ei.bei_nrdn;
447 ein->bei_rdn = ei.bei_rdn;
449 /* This node is not fully connected yet */
450 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
452 /* Insert this node into the ID tree */
453 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
454 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
455 bdb_id_cmp, avl_dup_error ) ) {
457 /* Hm, can this really happen? */
458 bdb_cache_entryinfo_destroy( ein );
459 ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
460 (caddr_t) &ei, bdb_id_cmp );
462 bdb_cache_entryinfo_lock( ein );
463 avl_insert( &ein->bei_kids, (caddr_t)ei2,
464 bdb_rdn_cmp, avl_dup_error );
465 bdb_cache_entryinfo_unlock( ein );
469 /* If this is the first time, save this node
470 * to be returned later.
472 if ( eir == NULL ) eir = ein;
474 /* If there was a previous node, link it to this one */
475 if ( ei2 ) ei2->bei_parent = ein;
478 ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
479 (caddr_t) &eip, bdb_id_cmp );
481 ei2 = &bdb->bi_cache.c_dntree;
485 ein->bei_parent = ei2;
486 bdb_cache_entryinfo_lock( ei2 );
487 avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
489 bdb_cache_entryinfo_unlock( ei2 );
491 bdb_cache_entryinfo_lock( eir );
493 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
495 /* Found a link. Reset all the state info */
496 for (ein = eir; ein != ei2; ein=ein->bei_parent)
497 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
501 ei.bei_id = eip.bei_id;
502 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
510 * cache_find_id - find an entry in the cache, given id.
511 * The entry is locked for Read upon return. Call with islocked TRUE if
512 * the supplied *eip was already locked.
526 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
533 /* If we weren't given any info, see if we have it already cached */
535 again: ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
536 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
537 (caddr_t) &ei, bdb_id_cmp );
539 if ( ldap_pvt_thread_mutex_trylock(
540 &(*eip)->bei_kids_mutex )) {
541 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
542 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
545 ldap_pvt_thread_yield();
550 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
553 /* See if the ID exists in the database; add it to the cache if so */
556 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
558 rc = bdb_cache_find_ndn( op, tid,
559 &ep->e_nname, eip, locker );
563 bdb_entry_return( ep );
568 rc = hdb_cache_find_parent(op, tid, id, eip );
569 if ( rc == 0 && *eip )
574 /* Ok, we found the info, do we have the entry? */
575 if ( *eip && rc == 0 ) {
576 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
579 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
581 if ( !(*eip)->bei_e ) {
583 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
586 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
588 /* Make sure no other modifier beat us to it */
589 if ( (*eip)->bei_e ) {
590 bdb_entry_return( ep );
593 ep->e_private = *eip;
599 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
605 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
607 bdb_cache_entry_db_relock( bdb->bi_dbenv,
608 locker, *eip, 1, 0, lock );
609 /* check again in case other modifier did it already */
610 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
611 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
612 bdb_cache_entry_db_relock( bdb->bi_dbenv,
613 locker, *eip, 0, 0, lock );
619 if ( rc == 0 && (*eip)->bei_kids == NULL ) {
621 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
622 LRU_DELETE( &bdb->bi_cache, *eip );
623 LRU_ADD( &bdb->bi_cache, *eip );
624 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
628 bdb_cache_entryinfo_unlock( *eip );
642 if ( BEI(e)->bei_kids ) {
645 if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
648 rc = bdb_dn2id_children( op, txn, e );
649 if ( rc == DB_NOTFOUND ) {
650 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
655 /* Update the cache after a successful database Add. */
658 struct bdb_info *bdb,
666 struct berval rdn = e->e_name;
673 if ( nrdn->bv_len != e->e_nname.bv_len ) {
674 char *ptr = strchr( rdn.bv_val, ',' );
675 rdn.bv_len = ptr - rdn.bv_val;
677 ber_dupbv( &ei.bei_rdn, &rdn );
679 rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
682 new->bei_state = CACHE_ENTRY_NO_KIDS;
683 eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
684 bdb_cache_entryinfo_unlock( eip );
685 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
698 EntryInfo *ei = BEI(e);
700 /* Get write lock on data */
701 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
703 /* If we've done repeated mods on a cached entry, then e_attrs
704 * is no longer contiguous with the entry, and must be freed.
706 if ( (void *)e->e_attrs != (void *)(e+1) ) {
707 attrs_free( e->e_attrs );
709 e->e_attrs = newAttrs;
715 * Change the rdn in the entryinfo. Also move to a new parent if needed.
728 EntryInfo *ei = BEI(e), *pei;
732 /* Get write lock on data */
733 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
735 /* If we've done repeated mods on a cached entry, then e_attrs
736 * is no longer contiguous with the entry, and must be freed.
738 if ( (void *)e->e_attrs != (void *)(e+1) ) {
739 attrs_free( e->e_attrs );
741 e->e_attrs = new->e_attrs;
742 if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
743 e->e_bv.bv_val + e->e_bv.bv_len ) {
744 ch_free(e->e_name.bv_val);
745 ch_free(e->e_nname.bv_val);
747 e->e_name = new->e_name;
748 e->e_nname = new->e_nname;
750 /* Lock the parent's kids AVL tree */
751 pei = ei->bei_parent;
752 bdb_cache_entryinfo_lock( pei );
753 avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
754 free( ei->bei_nrdn.bv_val );
755 ber_dupbv( &ei->bei_nrdn, nrdn );
757 free( ei->bei_rdn.bv_val );
760 if ( nrdn->bv_len != e->e_nname.bv_len ) {
761 char *ptr = strchr(rdn.bv_val, ',');
762 rdn.bv_len = ptr - rdn.bv_val;
764 ber_dupbv( &ei->bei_rdn, &rdn );
768 ein = ei->bei_parent;
770 ei->bei_parent = ein;
771 bdb_cache_entryinfo_unlock( pei );
772 bdb_cache_entryinfo_lock( ein );
775 { int max = ei->bei_modrdns;
776 /* Record the generation number of this change */
777 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
778 if ( pei->bei_modrdns > max )
779 max = pei->bei_modrdns;
781 ei->bei_modrdns = max + 1;
784 avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
785 bdb_cache_entryinfo_unlock( ein );
789 * cache_delete - delete the entry e from the cache.
791 * returns: 0 e was deleted ok
792 * 1 e was not in the cache
793 * -1 something bad happened
804 EntryInfo *ei = BEI(e);
807 assert( e->e_private );
809 /* Set this early, warn off any queriers */
810 ei->bei_state |= CACHE_ENTRY_DELETED;
812 /* Lock the entry's info */
813 bdb_cache_entryinfo_lock( ei );
815 /* Get write lock on the data */
816 bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
818 /* set cache write lock */
819 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
821 /* Lock the parent's kids tree */
822 bdb_cache_entryinfo_lock( ei->bei_parent );
825 LDAP_LOG( CACHE, ENTRY,
826 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
828 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
833 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
834 rc = bdb_cache_delete_internal( cache, e->e_private );
836 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
838 /* free cache write lock */
839 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
840 bdb_cache_entryinfo_unlock( ei->bei_parent );
842 /* Leave entry info locked */
848 bdb_cache_delete_cleanup(
852 bdb_cache_entryinfo_unlock( BEI(e) );
853 bdb_cache_entryinfo_destroy( e->e_private );
855 bdb_entry_return( e );
859 bdb_cache_delete_internal(
864 int rc = 0; /* return code */
867 if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
872 /* If parent has no more kids, put in on LRU list */
873 if ( e->bei_parent->bei_kids == NULL ) {
874 LRU_ADD( cache, e->bei_parent );
879 if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
889 LRU_DELETE( cache, e );
893 * flag entry to be freed later by a call to cache_return_entry()
895 e->bei_state |= CACHE_ENTRY_DELETED;
901 bdb_entryinfo_release( void *data )
903 EntryInfo *ei = (EntryInfo *)data;
904 if ( ei->bei_kids ) {
905 avl_free( ei->bei_kids, NULL );
908 ei->bei_e->e_private = NULL;
909 bdb_entry_return( ei->bei_e );
911 bdb_cache_entryinfo_destroy( ei );
915 bdb_cache_release_all( Cache *cache )
917 /* set cache write lock */
918 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
920 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
923 LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
925 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
928 avl_free( cache->c_dntree.bei_kids, NULL );
929 avl_free( cache->c_idtree, bdb_entryinfo_release );
930 cache->c_lruhead = NULL;
931 cache->c_lrutail = NULL;
934 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
935 /* free cache write lock */
936 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
941 bdb_lru_print( Cache *cache )
945 fprintf( stderr, "LRU queue (head to tail):\n" );
946 for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
947 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
948 e->bei_nrdn.bv_val, e->bei_id );
950 fprintf( stderr, "LRU queue (tail to head):\n" );
951 for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
952 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
953 e->bei_nrdn.bv_val, e->bei_id );
958 #ifdef BDB_REUSE_LOCKERS
960 bdb_locker_id_free( void *key, void *data )
963 int lockid = (int) data;
967 rc = XLOCK_ID_FREE( env, lockid );
968 if ( rc == EINVAL ) {
971 LDAP_LOG( BACK_BDB, ERR,
972 "bdb_locker_id_free: %d err %s(%d)\n",
973 lockid, db_strerror(rc), rc );
975 Debug( LDAP_DEBUG_ANY,
976 "bdb_locker_id_free: %d err %s(%d)\n",
977 lockid, db_strerror(rc), rc );
979 memset( &lr, 0, sizeof(lr) );
981 /* release all locks held by this locker. */
982 lr.op = DB_LOCK_PUT_ALL;
983 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
984 XLOCK_ID_FREE( env, lockid );
989 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
995 if ( !env || !locker ) return -1;
997 /* If no op was provided, try to find the ctx anyway... */
999 ctx = op->o_threadctx;
1001 ctx = ldap_pvt_thread_pool_context();
1004 /* Shouldn't happen unless we're single-threaded */
1010 if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1011 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1012 rc = XLOCK_ID( env, &lockid );
1013 if (rc) ldap_pvt_thread_yield();
1018 data = (void *)lockid;
1019 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1020 data, bdb_locker_id_free ) ) ) {
1021 XLOCK_ID_FREE( env, lockid );
1023 LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1024 db_strerror(rc), rc, 0 );
1026 Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1027 db_strerror(rc), rc, 0 );