1 /* cache.c - routines to maintain an in-core cache of entries */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
20 /* BDB backend specific entry info -- visible only to the cache */
21 typedef struct bdb_entry_info {
22 ldap_pvt_thread_rdwr_t bei_rdwr; /* reader/writer lock */
25 * remaining fields require backend cache lock to access
26 * These items are specific to the BDB backend and should
29 int bei_state; /* for the cache */
30 #define CACHE_ENTRY_UNDEFINED 0
31 #define CACHE_ENTRY_CREATING 1
32 #define CACHE_ENTRY_READY 2
33 #define CACHE_ENTRY_DELETED 3
34 #define CACHE_ENTRY_COMMITTED 4
36 int bei_refcnt; /* # threads ref'ing this entry */
37 Entry *bei_lrunext; /* for cache lru list */
41 #define BEI(e) ((EntryInfo *) ((e)->e_private))
43 static int bdb_cache_delete_entry_internal(Cache *cache, Entry *e);
45 static void bdb_lru_print(Cache *cache);
49 bdb_cache_entry_rdwr_lock(Entry *e, int rw)
52 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
53 "bdb_cache_entry_rdwr_lock: %s lock on ID %ld\n",
54 rw ? "w" : "r", e->e_id ));
56 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
57 rw ? "w" : "r", e->e_id, 0);
61 return ldap_pvt_thread_rdwr_wlock(&BEI(e)->bei_rdwr);
63 return ldap_pvt_thread_rdwr_rlock(&BEI(e)->bei_rdwr);
67 bdb_cache_entry_rdwr_trylock(Entry *e, int rw)
70 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
71 "bdb_cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
72 rw ? "w" : "r", e->e_id ));
74 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
75 rw ? "w" : "r", e->e_id, 0);
79 return ldap_pvt_thread_rdwr_wtrylock(&BEI(e)->bei_rdwr);
81 return ldap_pvt_thread_rdwr_rtrylock(&BEI(e)->bei_rdwr);
85 bdb_cache_entry_rdwr_unlock(Entry *e, int rw)
88 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
89 "bdb_cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
90 rw ? "w" : "r", e->e_id ));
92 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
93 rw ? "w" : "r", e->e_id, 0);
97 return ldap_pvt_thread_rdwr_wunlock(&BEI(e)->bei_rdwr);
99 return ldap_pvt_thread_rdwr_runlock(&BEI(e)->bei_rdwr);
103 bdb_cache_entry_rdwr_init(Entry *e)
105 return ldap_pvt_thread_rdwr_init( &BEI(e)->bei_rdwr );
109 bdb_cache_entry_rdwr_destroy(Entry *e)
111 return ldap_pvt_thread_rdwr_destroy( &BEI(e)->bei_rdwr );
115 bdb_cache_entry_private_init( Entry *e )
117 assert( e->e_private == NULL );
119 if( e->e_private != NULL ) {
120 /* this should never happen */
124 e->e_private = ch_calloc(1, sizeof(struct bdb_entry_info));
126 if( bdb_cache_entry_rdwr_init( e ) != 0 ) {
136 bdb_cache_entry_db_lock
137 ( DB_ENV *env, u_int32_t locker, Entry *e, int rw, u_int32_t flags, DB_LOCK *lock )
144 db_rw = DB_LOCK_WRITE;
146 db_rw = DB_LOCK_READ;
148 lockobj.data = e->e_nname.bv_val;
149 lockobj.size = e->e_nname.bv_len;
150 rc = LOCK_GET(env, locker, flags | DB_LOCK_NOWAIT,
151 &lockobj, db_rw, lock);
156 bdb_cache_entry_db_unlock
157 ( DB_ENV *env, DB_LOCK *lock )
161 rc = LOCK_PUT ( env, lock );
166 * marks an entry in CREATING state as committed, so it is really returned
167 * to the cache. Otherwise an entry in CREATING state is removed.
168 * Makes e_private be destroyed at the following cache_return_entry_w,
169 * but lets the entry untouched (owned by someone else)
172 bdb_cache_entry_commit( Entry *e )
175 assert( e->e_private );
176 assert( BEI(e)->bei_state == CACHE_ENTRY_CREATING );
177 /* assert( BEI(e)->bei_refcnt == 1 ); */
179 BEI(e)->bei_state = CACHE_ENTRY_COMMITTED;
183 bdb_cache_entry_private_destroy( Entry *e )
185 assert( e->e_private );
187 bdb_cache_entry_rdwr_destroy( e );
189 free( e->e_private );
195 bdb_unlocked_cache_return_entry_rw( Cache *cache, Entry *e, int rw )
199 int refcnt, freeit = 1;
201 /* set cache write lock */
202 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
204 assert( e->e_private );
207 bdb_cache_entry_rdwr_unlock(e, rw);
211 refcnt = --BEI(e)->bei_refcnt;
214 * if the entry is returned when in CREATING state, it is deleted
215 * but not freed because it may belong to someone else (do_add,
218 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
220 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
221 bdb_cache_delete_entry_internal( cache, e );
223 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
225 /* now the entry is in DELETED state */
228 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
229 BEI(e)->bei_state = CACHE_ENTRY_READY;
231 /* free cache write lock */
232 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
235 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
236 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
237 id, rw ? "w" : "r", refcnt ));
239 Debug( LDAP_DEBUG_TRACE,
240 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
241 rw ? "w" : "r", id, refcnt );
245 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
247 /* free cache write lock */
248 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
251 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
252 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
255 Debug( LDAP_DEBUG_TRACE,
256 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
257 rw ? "w" : "r", id, refcnt );
261 bdb_cache_entry_private_destroy( e );
263 bdb_entry_return( e );
266 /* free cache write lock */
267 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
270 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
271 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
274 Debug( LDAP_DEBUG_TRACE,
275 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
276 rw ? "w" : "r", id, refcnt );
281 /* free cache write lock */
282 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
285 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
286 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
287 id, rw ? "w": "r", refcnt ));
289 Debug( LDAP_DEBUG_TRACE,
290 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
291 rw ? "w" : "r", id, refcnt);
297 bdb_cache_return_entry_rw
298 ( DB_ENV *env, Cache *cache, Entry *e, int rw, DB_LOCK *lock )
301 int refcnt, freeit = 1;
303 /* set cache write lock */
304 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
306 assert( e->e_private );
308 bdb_cache_entry_db_unlock( env, lock );
310 bdb_cache_entry_rdwr_unlock(e, rw);
314 refcnt = --BEI(e)->bei_refcnt;
317 * if the entry is returned when in CREATING state, it is deleted
318 * but not freed because it may belong to someone else (do_add,
321 if ( BEI(e)->bei_state == CACHE_ENTRY_CREATING ) {
323 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
324 bdb_cache_delete_entry_internal( cache, e );
326 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
328 /* now the entry is in DELETED state */
331 if ( BEI(e)->bei_state == CACHE_ENTRY_COMMITTED ) {
332 BEI(e)->bei_state = CACHE_ENTRY_READY;
334 /* free cache write lock */
335 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
338 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
339 "bdb_cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
340 id, rw ? "w" : "r", refcnt ));
342 Debug( LDAP_DEBUG_TRACE,
343 "====> bdb_cache_return_entry_%s( %ld ): created (%d)\n",
344 rw ? "w" : "r", id, refcnt );
348 } else if ( BEI(e)->bei_state == CACHE_ENTRY_DELETED ) {
350 /* free cache write lock */
351 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
354 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
355 "bdb_cache_return_entry_rw: %ld, delete pending (%d).\n",
358 Debug( LDAP_DEBUG_TRACE,
359 "====> bdb_cache_return_entry_%s( %ld ): delete pending (%d)\n",
360 rw ? "w" : "r", id, refcnt );
364 bdb_cache_entry_private_destroy( e );
366 bdb_entry_return( e );
369 /* free cache write lock */
370 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
373 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
374 "bdb_cache_return_entry_rw: (%ld): deleted (%d)\n",
377 Debug( LDAP_DEBUG_TRACE,
378 "====> bdb_cache_return_entry_%s( %ld ): deleted (%d)\n",
379 rw ? "w" : "r", id, refcnt );
384 /* free cache write lock */
385 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
388 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
389 "bdb_cache_return_entry_rw: ID %ld:%s returned (%d)\n",
390 id, rw ? "w": "r", refcnt ));
392 Debug( LDAP_DEBUG_TRACE,
393 "====> bdb_cache_return_entry_%s( %ld ): returned (%d)\n",
394 rw ? "w" : "r", id, refcnt);
399 #define LRU_DELETE( cache, e ) do { \
400 if ( BEI(e)->bei_lruprev != NULL ) { \
401 BEI(BEI(e)->bei_lruprev)->bei_lrunext = BEI(e)->bei_lrunext; \
403 (cache)->c_lruhead = BEI(e)->bei_lrunext; \
405 if ( BEI(e)->bei_lrunext != NULL ) { \
406 BEI(BEI(e)->bei_lrunext)->bei_lruprev = BEI(e)->bei_lruprev; \
408 (cache)->c_lrutail = BEI(e)->bei_lruprev; \
412 #define LRU_ADD( cache, e ) do { \
413 BEI(e)->bei_lrunext = (cache)->c_lruhead; \
414 if ( BEI(e)->bei_lrunext != NULL ) { \
415 BEI(BEI(e)->bei_lrunext)->bei_lruprev = (e); \
417 (cache)->c_lruhead = (e); \
418 BEI(e)->bei_lruprev = NULL; \
419 if ( (cache)->c_lrutail == NULL ) { \
420 (cache)->c_lrutail = (e); \
425 * cache_add_entry_rw - create and lock an entry in the cache
426 * returns: 0 entry has been created and locked
427 * 1 entry already existed
428 * -1 something bad happened
429 * other Berkeley DB locking error code
432 bdb_cache_add_entry_rw(
445 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
446 "bdb_cache_add_entry_rw: add (%s):%s to cache\n",
447 e->e_dn, rw ? "w" : "r" ));
449 /* set cache write lock */
450 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
452 assert( e->e_private == NULL );
454 if( bdb_cache_entry_private_init(e) != 0 ) {
455 /* free cache write lock */
456 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
459 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
460 "bdb_cache_add_entry_rw: add (%s):%ld private init failed!\n",
463 Debug( LDAP_DEBUG_ANY,
464 "====> bdb_cache_add_entry( %ld ): \"%s\": private init failed!\n",
465 e->e_id, e->e_dn, 0 );
472 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
473 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
475 /* free cache write lock */
476 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
479 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
480 "bdb_cache_add_entry: (%s):%ld already in cache.\n",
483 Debug( LDAP_DEBUG_TRACE,
484 "====> bdb_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
485 e->e_id, e->e_dn, 0 );
488 bdb_cache_entry_private_destroy(e);
494 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
495 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
498 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
499 "bdb_cache_add_entry: (%s):%ls already in cache.\n",
502 Debug( LDAP_DEBUG_ANY,
503 "====> bdb_cache_add_entry( %ld ): \"%s\": already in id cache\n",
504 e->e_id, e->e_dn, 0 );
507 /* delete from dn tree inserted above */
508 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
509 (AVL_CMP) entry_dn_cmp ) == NULL )
512 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
513 "bdb_cache_add_entry: can't delete (%s) from cache.\n",
516 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
521 bdb_cache_entry_private_destroy(e);
523 /* free cache write lock */
524 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
528 rc = bdb_cache_entry_db_lock( env, locker, e, rw, 0, lock );
532 case DB_LOCK_DEADLOCK :
533 case DB_LOCK_NOTGRANTED :
534 /* undo avl changes immediately */
535 if ( avl_delete( &cache->c_idtree, (caddr_t) e,
536 (AVL_CMP) entry_id_cmp ) == NULL ) {
538 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
539 "bdb_cache_add_entry: can't delete (%s) from cache.\n", e->e_dn ));
541 Debug( LDAP_DEBUG_ANY, "====> can't delete from id cache\n", 0, 0, 0 );
544 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
545 (AVL_CMP) entry_dn_cmp ) == NULL ) {
547 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
548 "bdb_cache_add_entry: can't delete (%s) from cache.\n", e->e_dn ));
550 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n", 0, 0, 0 );
555 bdb_cache_entry_private_destroy(e);
556 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
560 /* put the entry into 'CREATING' state */
561 /* will be marked after when entry is returned */
562 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
563 BEI(e)->bei_refcnt = 1;
566 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
569 if ( ++cache->c_cursize > cache->c_maxsize ) {
571 * find the lru entry not currently in use and delete it.
572 * in case a lot of entries are in use, only look at the
573 * first 10 on the tail of the list.
576 while ( cache->c_lrutail != NULL &&
577 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
580 /* move this in-use entry to the front of the q */
581 ee = cache->c_lrutail;
582 LRU_DELETE( cache, ee );
583 LRU_ADD( cache, ee );
588 * found at least one to delete - try to get back under
589 * the max cache size.
591 while ( cache->c_lrutail != NULL &&
592 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
593 cache->c_cursize > cache->c_maxsize )
595 e = cache->c_lrutail;
597 /* delete from cache and lru q */
598 /* XXX do we need rc ? */
599 rc = bdb_cache_delete_entry_internal( cache, e );
600 bdb_cache_entry_private_destroy( e );
601 bdb_entry_return( e );
606 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
607 /* free cache write lock */
608 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
613 * cache_update_entry - update a LOCKED entry which has been deleted.
614 * returns: 0 entry has been created and locked
615 * 1 entry already existed
616 * -1 something bad happened
619 bdb_cache_update_entry(
627 /* set cache write lock */
628 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
630 assert( e->e_private );
632 if ( avl_insert( &cache->c_dntree, (caddr_t) e,
633 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
636 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
637 "bdb_cache_update_entry: (%s):%ld already in dn cache\n",
640 Debug( LDAP_DEBUG_TRACE,
641 "====> bdb_cache_update_entry( %ld ): \"%s\": already in dn cache\n",
642 e->e_id, e->e_dn, 0 );
645 /* free cache write lock */
646 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
651 if ( avl_insert( &cache->c_idtree, (caddr_t) e,
652 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
655 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
656 "bdb_cache_update_entry: (%s)%ld already in id cache\n",
659 Debug( LDAP_DEBUG_ANY,
660 "====> bdb_cache_update_entry( %ld ): \"%s\": already in id cache\n",
661 e->e_id, e->e_dn, 0 );
664 /* delete from dn tree inserted above */
665 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
666 (AVL_CMP) entry_dn_cmp ) == NULL )
669 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
670 "bdb_cache_update_entry: can't delete (%s)%ld from dn cache.\n",
673 Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
678 /* free cache write lock */
679 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
684 /* put the entry into 'CREATING' state */
685 /* will be marked after when entry is returned */
686 BEI(e)->bei_state = CACHE_ENTRY_CREATING;
689 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
692 if ( ++cache->c_cursize > cache->c_maxsize ) {
694 * find the lru entry not currently in use and delete it.
695 * in case a lot of entries are in use, only look at the
696 * first 10 on the tail of the list.
699 while ( cache->c_lrutail != NULL &&
700 BEI(cache->c_lrutail)->bei_refcnt != 0 &&
703 /* move this in-use entry to the front of the q */
704 ee = cache->c_lrutail;
705 LRU_DELETE( cache, ee );
706 LRU_ADD( cache, ee );
711 * found at least one to delete - try to get back under
712 * the max cache size.
714 while ( cache->c_lrutail != NULL &&
715 BEI(cache->c_lrutail)->bei_refcnt == 0 &&
716 cache->c_cursize > cache->c_maxsize )
718 e = cache->c_lrutail;
720 /* delete from cache and lru q */
721 /* XXX do we need rc ? */
722 rc = bdb_cache_delete_entry_internal( cache, e );
723 bdb_cache_entry_private_destroy( e );
724 bdb_entry_return( e );
729 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
730 /* free cache write lock */
731 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
736 bdb_cache_find_entry_ndn2id(
746 /* this function is always called with normalized DN */
750 /* set cache read lock */
751 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
753 if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
754 (AVL_CMP) entry_dn_cmp )) != NULL )
760 * ep now points to an unlocked entry
761 * we do not need to lock the entry if we only
762 * check the state, refcnt, LRU, and id.
765 assert( ep->e_private );
769 state = BEI(ep)->bei_state;
772 * entry is deleted or not fully created yet
774 if ( state != CACHE_ENTRY_READY ) {
775 assert(state != CACHE_ENTRY_UNDEFINED);
777 /* free cache read lock */
778 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
781 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
782 "bdb_cache_find_entry_dn2id: (%s) %ld not ready: %d\n",
783 ndn->bv_val, id, state ));
785 Debug(LDAP_DEBUG_TRACE,
786 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
787 ndn->bv_val, id, state);
791 ldap_pvt_thread_yield();
795 /* free cache read lock */
796 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
799 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
802 LRU_DELETE( cache, ep );
803 LRU_ADD( cache, ep );
806 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
809 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
810 "bdb_cache_find_entry_dn2id: (%s): %ld %d tries\n",
811 ndn->bv_val, id, count ));
813 Debug(LDAP_DEBUG_TRACE,
814 "====> bdb_cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
815 ndn->bv_val, id, count);
819 /* free cache read lock */
820 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
829 * cache_find_entry_id - find an entry in the cache, given id
833 bdb_cache_find_entry_id(
850 /* set cache read lock */
851 ldap_pvt_thread_rdwr_rlock( &cache->c_rwlock );
853 if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
854 (AVL_CMP) entry_id_cmp )) != NULL )
861 assert( ep->e_private );
864 state = BEI(ep)->bei_state;
867 * entry is deleted or not fully created yet
869 if ( state != CACHE_ENTRY_READY ) {
871 assert(state != CACHE_ENTRY_UNDEFINED);
873 /* free cache read lock */
874 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
877 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
878 "bdb_cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
882 Debug(LDAP_DEBUG_TRACE,
883 "====> bdb_cache_find_entry_id( %ld ): %ld (not ready) %d\n",
887 ldap_pvt_thread_yield();
891 /* acquire reader lock */
892 rc = bdb_cache_entry_db_lock ( env, locker, ep, rw, 0, lock );
895 if ( bdb_cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
898 if ( rc ) { /* will be changed to retry beyond threshold */
899 /* could not acquire entry lock...
900 * owner cannot free as we have the cache locked.
901 * so, unlock the cache, yield, and try again.
904 /* free cache read lock */
905 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
908 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
909 "bdb_cache_find_entry_id: %ld -> %ld (busy) %d.\n",
912 Debug(LDAP_DEBUG_TRACE,
913 "====> bdb_cache_find_entry_id( %ld ): %ld (busy) %d\n",
915 Debug(LDAP_DEBUG_TRACE,
920 ldap_pvt_thread_yield();
924 /* free cache read lock */
925 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
927 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
929 LRU_DELETE( cache, ep );
930 LRU_ADD( cache, ep );
932 BEI(ep)->bei_refcnt++;
935 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
938 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
939 "bdb_cache_find_entry_id: %ld -> %s found %d tries.\n",
940 ep_id, ep->e_dn, count ));
942 Debug(LDAP_DEBUG_TRACE,
943 "====> bdb_cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
944 ep_id, ep->e_dn, count);
951 /* free cache read lock */
952 ldap_pvt_thread_rdwr_runlock( &cache->c_rwlock );
958 * cache_delete_entry - delete the entry e from the cache. the caller
959 * should have obtained e (increasing its ref count) via a call to one
960 * of the cache_find_* routines. the caller should *not* call the
961 * cache_return_entry() routine prior to calling cache_delete_entry().
962 * it performs this function.
964 * returns: 0 e was deleted ok
965 * 1 e was not in the cache
966 * -1 something bad happened
969 bdb_cache_delete_entry(
976 /* set cache write lock */
977 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
979 assert( e->e_private );
982 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
983 "bdb_cache_delete_entry: delete %ld.\n", e->e_id ));
985 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
990 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
991 rc = bdb_cache_delete_entry_internal( cache, e );
993 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
995 /* free cache write lock */
996 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1001 bdb_cache_delete_entry_internal(
1006 int rc = 0; /* return code */
1009 if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
1016 if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
1027 LRU_DELETE( cache, e );
1031 * flag entry to be freed later by a call to cache_return_entry()
1033 BEI(e)->bei_state = CACHE_ENTRY_DELETED;
1039 bdb_cache_release_all( Cache *cache )
1044 /* set cache write lock */
1045 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1047 ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1050 LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
1051 "bdb_cache_release_all: enter\n" ));
1053 Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1056 while ( (e = cache->c_lrutail) != NULL && BEI(e)->bei_refcnt == 0 ) {
1057 #ifdef LDAP_RDWR_DEBUG
1058 assert(!ldap_pvt_thread_rdwr_active(&BEI(e)->bei_rdwr));
1061 /* delete from cache and lru q */
1062 /* XXX do we need rc ? */
1063 rc = bdb_cache_delete_entry_internal( cache, e );
1064 bdb_cache_entry_private_destroy( e );
1065 bdb_entry_return( e );
1068 if ( cache->c_cursize ) {
1070 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
1071 "bdb_cache_release_all: Entry cache could not be emptied.\n" ));
1073 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
1078 /* free lru mutex */
1079 ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1080 /* free cache write lock */
1081 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1086 bdb_lru_print( Cache *cache )
1090 fprintf( stderr, "LRU queue (head to tail):\n" );
1091 for ( e = cache->c_lruhead; e != NULL; e = BEI(e)->bei_lrunext ) {
1092 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1093 e->e_dn, e->e_id, BEI(e)->bei_refcnt );
1095 fprintf( stderr, "LRU queue (tail to head):\n" );
1096 for ( e = cache->c_lrutail; e != NULL; e = BEI(e)->bei_lruprev ) {
1097 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
1098 e->e_dn, e->e_id, BEI(e)->bei_refcnt );