]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/dbcache.c
Code clean-up.
[openldap] / servers / slapd / back-ldbm / dbcache.c
index 56c259c8698d6f1a4b2130f443076acacdb983c3..7d044c34a8bd0dd61f549ebdad21871424d5905e 100644 (file)
@@ -1,24 +1,23 @@
 /* ldbmcache.c - maintain a cache of open ldbm files */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/param.h>
+
+#include <ac/errno.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include <sys/stat.h>
-#include <errno.h>
-#include "portable.h"
-#include "slap.h"
-#include "ldapconfig.h"
-#include "back-ldbm.h"
 
-#ifndef SYSERRLIST_IN_STDIO
-extern int             sys_nerr;
-extern char            *sys_errlist[];
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
 #endif
-extern time_t          currenttime;
-extern pthread_mutex_t currenttime_mutex;
+
+#include "ldapconfig.h"
+#include "slap.h"
+#include "back-ldbm.h"
 
 struct dbcache *
 ldbm_cache_open(
@@ -35,18 +34,18 @@ ldbm_cache_open(
        LDBM            db;
        struct stat     st;
 
-       sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
+       sprintf( buf, "%s%s%s%s", li->li_directory, DEFAULT_DIRSEP, name, suffix );
 
        Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
            flags, li->li_mode );
 
        lru = 0;
-       pthread_mutex_lock( &currenttime_mutex );
+       ldap_pvt_thread_mutex_lock( &currenttime_mutex );
        curtime = currenttime;
-       pthread_mutex_unlock( &currenttime_mutex );
+       ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
        oldtime = curtime;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
            i++ ) {
                /* already open - return it */
@@ -54,7 +53,7 @@ ldbm_cache_open(
                        li->li_dbcache[i].dbc_refcnt++;
                        Debug( LDAP_DEBUG_TRACE,
                            "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
-                       pthread_mutex_unlock( &li->li_dbcache_mutex );
+                       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
                        return( &li->li_dbcache[i] );
                }
 
@@ -75,7 +74,7 @@ ldbm_cache_open(
                            0, 0, 0 );
                        lru = -1;
                        while ( lru == -1 ) {
-                               pthread_cond_wait( &li->li_dbcache_cv,
+                               ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
                                    &li->li_dbcache_mutex );
                                for ( i = 0; i < MAXDBCACHE; i++ ) {
                                        if ( li->li_dbcache[i].dbc_refcnt
@@ -98,10 +97,10 @@ ldbm_cache_open(
                    "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
                    buf, errno, errno > -1 && errno < sys_nerr ?
                    sys_errlist[errno] : "unknown" );
-               pthread_mutex_unlock( &li->li_dbcache_mutex );
+               ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
                return( NULL );
        }
-       li->li_dbcache[i].dbc_name = strdup( buf );
+       li->li_dbcache[i].dbc_name = ch_strdup( buf );
        li->li_dbcache[i].dbc_refcnt = 1;
        li->li_dbcache[i].dbc_lastref = curtime;
        if ( stat( buf, &st ) == 0 ) {
@@ -110,16 +109,16 @@ ldbm_cache_open(
                li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
        }
        li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
-           sizeof(ID)) - 2;
+           sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
        li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
            li->li_dbcache[i].dbc_maxids) + 1;
 
        Debug( LDAP_DEBUG_ARGS,
-           "ldbm_cache_open (blksize %d) (maxids %d) (maxindirect %d)\n",
+           "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
            li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
            li->li_dbcache[i].dbc_maxindirect );
        Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
        return( &li->li_dbcache[i] );
 }
 
@@ -128,11 +127,11 @@ ldbm_cache_close( Backend *be, struct dbcache *db )
 {
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        if ( --db->dbc_refcnt == 0 ) {
-               pthread_cond_signal( &li->li_dbcache_cv );
+               ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 void
@@ -140,14 +139,14 @@ ldbm_cache_really_close( Backend *be, struct dbcache *db )
 {
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        if ( --db->dbc_refcnt == 0 ) {
-               pthread_cond_signal( &li->li_dbcache_cv );
+               ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
                ldbm_close( db->dbc_db );
                free( db->dbc_name );
                db->dbc_name = NULL;
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 void
@@ -156,17 +155,15 @@ ldbm_cache_flush_all( Backend *be )
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
        int             i;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        for ( i = 0; i < MAXDBCACHE; i++ ) {
                if ( li->li_dbcache[i].dbc_name != NULL ) {
                        Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
                            li->li_dbcache[i].dbc_name, 0, 0 );
-                       pthread_mutex_lock( &li->li_dbcache[i].dbc_mutex );
                        ldbm_sync( li->li_dbcache[i].dbc_db );
-                       pthread_mutex_unlock( &li->li_dbcache[i].dbc_mutex );
                }
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 Datum
@@ -176,28 +173,11 @@ ldbm_cache_fetch(
 )
 {
        Datum   data;
-#ifdef LDBM_USE_DB2
-       memset( &data, 0, sizeof( data ) );
-#endif
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* increment reader count */
-       db->dbc_readers++
-       pthread_mutex_unlock( &db->dbc_mutex );
-#endif
+       ldbm_datum_init( data );
 
        data = ldbm_fetch( db->dbc_db, key );
 
-#ifdef reentrant_database
-       pthread_mutex_lock( &db->dbc_mutex );
-       /* decrement reader count & signal any waiting writers */
-       if ( --db->dbc_readers == 0 ) {
-               pthread_cond_signal( &db->dbc_cv );
-       }
-#endif
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( data );
 }
 
@@ -211,14 +191,6 @@ ldbm_cache_store(
 {
        int     rc;
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* wait for reader count to drop to zero */
-       while ( db->dbc_readers > 0 ) {
-               pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
-       }
-#endif
-
 #ifdef LDBM_DEBUG
        Statslog( LDAP_DEBUG_STATS,
                "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
@@ -239,8 +211,6 @@ ldbm_cache_store(
 
        rc = ldbm_store( db->dbc_db, key, data, flags );
 
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( rc );
 }
 
@@ -252,17 +222,7 @@ ldbm_cache_delete(
 {
        int     rc;
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* wait for reader count to drop to zero - then write */
-       while ( db->dbc_readers > 0 ) {
-               pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
-       }
-#endif
-
        rc = ldbm_delete( db->dbc_db, key );
 
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( rc );
 }