/* ldbmcache.c - maintain a cache of open ldbm files */
/* $OpenLDAP$ */
/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
+#include <ac/unistd.h>
#include <sys/stat.h>
#include "slap.h"
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
int i, lru, empty;
- time_t oldtime, curtime;
+ time_t oldtime;
char buf[MAXPATHLEN];
#ifdef HAVE_ST_BLKSIZE
struct stat st;
#endif
- curtime = slap_get_time();
empty = MAXDBCACHE;
ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
do {
lru = 0;
- oldtime = curtime;
+ oldtime = 1;
for ( i = 0; i < MAXDBCACHE; i++ ) {
/* see if this slot is free */
if ( li->li_dbcache[i].dbc_name == NULL) {
}
/* keep track of lru db */
- if ( li->li_dbcache[i].dbc_lastref < oldtime
- && li->li_dbcache[i].dbc_refcnt == 0 )
+ if (( li->li_dbcache[i].dbc_refcnt == 0 ) &&
+ (( oldtime == 1 ) ||
+ ( li->li_dbcache[i].dbc_lastref < oldtime )) )
{
lru = i;
oldtime = li->li_dbcache[i].dbc_lastref;
}
li->li_dbcache[i].dbc_name = ch_strdup( buf );
li->li_dbcache[i].dbc_refcnt = 1;
- li->li_dbcache[i].dbc_lastref = curtime;
+ li->li_dbcache[i].dbc_lastref = slap_get_time();
li->li_dbcache[i].dbc_flags = flags;
li->li_dbcache[i].dbc_dirty = 0;
#ifdef HAVE_ST_BLKSIZE
Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
#endif
+ ldap_pvt_thread_mutex_init( &li->li_dbcache[i].dbc_write_mutex );
+
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
return( &li->li_dbcache[i] );
}
ldbm_close( db->dbc_db );
free( db->dbc_name );
db->dbc_name = NULL;
+ ldap_pvt_thread_mutex_destroy( &db->dbc_write_mutex );
}
ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
}