tdb2: unify tdb1_chainlock et al. into tdb_chainlock
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
Switch on the TDB_VERSION1 flag.

ccan/tdb2/hash.c
ccan/tdb2/lock.c
ccan/tdb2/private.h
ccan/tdb2/tdb1.h
ccan/tdb2/tdb1_check.c
ccan/tdb2/tdb1_lock.c
ccan/tdb2/tdb1_open.c
ccan/tdb2/tdb1_private.h
ccan/tdb2/tdb1_summary.c
ccan/tdb2/tdb1_tdb.c
ccan/tdb2/test/run-tdb1-no-lock-during-traverse.c

index 56c5086e742552480a23e81a8e2a7baafe583d73..5eee41a88df0dcaf820ab00cc3cb01df87c4cdf4 100644 (file)
@@ -852,6 +852,11 @@ static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key,
    contention - it cannot guarantee how many records will be locked */
 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
 {
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_chainlock(tdb, key) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
        return tdb->last_error = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
                                           "tdb_chainlock");
 }
@@ -862,6 +867,11 @@ void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
        tdb_off_t lockstart, locksize;
        unsigned int group, gbits;
 
+       if (tdb->flags & TDB_VERSION1) {
+               tdb1_chainunlock(tdb, key);
+               return;
+       }
+
        gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
        group = bits_from(h, 64 - gbits, gbits);
 
@@ -873,6 +883,11 @@ void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
 
 enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
 {
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_chainlock_read(tdb, key) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
        return tdb->last_error = chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
                                           "tdb_chainlock_read");
 }
@@ -883,6 +898,10 @@ void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
        tdb_off_t lockstart, locksize;
        unsigned int group, gbits;
 
+       if (tdb->flags & TDB_VERSION1) {
+               tdb1_chainunlock_read(tdb, key);
+               return;
+       }
        gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
        group = bits_from(h, 64 - gbits, gbits);
 
index bd896a35e0144d5b2db619598cabd0bf4ae62690..7d4311c8e568b8125d72d2e91ec3dd0c8b08d4b9 100644 (file)
@@ -534,6 +534,12 @@ enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
        enum TDB_ERROR ecode;
        tdb_bool_err berr;
 
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_allrecord_lock(tdb, ltype, flags, upgradable) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
+
        if (tdb->flags & TDB_NOLOCK)
                return TDB_SUCCESS;
 
@@ -648,6 +654,11 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
 /* unlock entire db */
 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
 {
+       if (tdb->flags & TDB_VERSION1) {
+               tdb1_allrecord_unlock(tdb, ltype);
+               return;
+       }
+
        if (tdb->flags & TDB_NOLOCK)
                return;
 
@@ -862,12 +873,7 @@ void tdb_lock_cleanup(struct tdb_context *tdb)
 
        while (tdb->file->allrecord_lock.count
               && tdb->file->allrecord_lock.owner == tdb) {
-               if (tdb->flags & TDB_VERSION1)
-                       tdb1_allrecord_unlock(tdb,
-                                             tdb->file->allrecord_lock.ltype);
-               else
-                       tdb_allrecord_unlock(tdb,
-                                            tdb->file->allrecord_lock.ltype);
+               tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
        }
 
        for (i=0; i<tdb->file->num_lockrecs; i++) {
index 98e26c17336ac4ea1b3f94e9f9a74e400f576ca4..3acc06a9568e1e528f53905534a71646d48a6fb0 100644 (file)
@@ -640,8 +640,15 @@ enum TDB_ERROR tdb1_open(struct tdb_context *tdb);
 enum TDB_ERROR tdb1_probe_length(struct tdb_context *tdb);
 
 /* tdb1_lock.c: */
+int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
+                       enum tdb_lock_flags flags, bool upgradable);
 int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype);
 
+int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key);
+int tdb1_chainunlock(struct tdb_context *tdb, TDB_DATA key);
+int tdb1_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
+int tdb1_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
+
 /* tdb1_transaction.c: */
 int tdb1_transaction_recover(struct tdb_context *tdb);
 int tdb1_transaction_cancel(struct tdb_context *tdb);
index 4b0e454d1b7141c90a1481a4581c7a0850155b77..75cf39d7e16a75007215bee30ff16a319eb06fa2 100644 (file)
@@ -47,14 +47,6 @@ TDB_DATA tdb1_firstkey(struct tdb_context *tdb);
 
 TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA key);
 
-int tdb1_lockall(struct tdb_context *tdb);
-
-int tdb1_unlockall(struct tdb_context *tdb);
-
-int tdb1_lockall_read(struct tdb_context *tdb);
-
-int tdb1_unlockall_read(struct tdb_context *tdb);
-
 int tdb1_transaction_start(struct tdb_context *tdb);
 
 int tdb1_transaction_prepare_commit(struct tdb_context *tdb);
@@ -73,13 +65,6 @@ int tdb1_check(struct tdb_context *tdb,
 
 /* @} ******************************************************************/
 
-/* Low level locking functions: use with care */
-int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key);
-int tdb1_chainunlock(struct tdb_context *tdb, TDB_DATA key);
-int tdb1_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
-int tdb1_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
-
-
 /* wipe and repack */
 int tdb1_wipe_all(struct tdb_context *tdb);
 int tdb1_repack(struct tdb_context *tdb);
index 50e71d9783461a67442e99323d111dd208920215..c3c3c60f431cf494d78fcd5c28174b10345e9dc2 100644 (file)
@@ -338,7 +338,7 @@ int tdb1_check(struct tdb_context *tdb,
        if (tdb->file->allrecord_lock.count != 0) {
                locked = false;
        } else {
-               if (tdb1_lockall_read(tdb) == -1)
+               if (tdb_lockall_read(tdb) != TDB_SUCCESS)
                        return -1;
                locked = true;
        }
@@ -455,7 +455,7 @@ int tdb1_check(struct tdb_context *tdb,
 
        free(hashes);
        if (locked) {
-               tdb1_unlockall_read(tdb);
+               tdb_unlockall_read(tdb);
        }
        return 0;
 
@@ -463,7 +463,7 @@ free:
        free(hashes);
 unlock:
        if (locked) {
-               tdb1_unlockall_read(tdb);
+               tdb_unlockall_read(tdb);
        }
        return -1;
 }
index 7cc17d7a7e1777a3384c92ef442b90af39630e1d..f21a22cacd10457a0191fcf7bd9c8b9065ca8ae2 100644 (file)
@@ -306,7 +306,7 @@ int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
        }
 
        /* FIXME: Temporary cast. */
-       tdb->file->allrecord_lock.owner = (void *)(struct tdb1_context *)tdb;
+       tdb->file->allrecord_lock.owner = (void *)(struct tdb_context *)tdb;
        tdb->file->allrecord_lock.count = 1;
        /* If it's upgradable, it's actually exclusive so we can treat
         * it as a write lock. */
@@ -364,30 +364,6 @@ int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype)
        return 0;
 }
 
-/* lock entire database with write lock */
-int tdb1_lockall(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
-}
-
-/* unlock entire database with write lock */
-int tdb1_unlockall(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_unlock(tdb, F_WRLCK);
-}
-
-/* lock entire database with read lock */
-int tdb1_lockall_read(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
-}
-
-/* unlock entire database with read lock */
-int tdb1_unlockall_read(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_unlock(tdb, F_RDLCK);
-}
-
 /* lock/unlock one hash chain. This is meant to be used to reduce
    contention - it cannot guarantee how many records will be locked */
 int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key)
index 6e0c25db0556d8eff546e1b59907c781af532dc3..df46450c90b1d3596bf6dd84624b5b9ac80751c3 100644 (file)
@@ -42,7 +42,7 @@ void tdb1_header_hash(struct tdb_context *tdb,
                *magic1_hash = 1;
 }
 
-static void tdb1_context_init(struct tdb_context *tdb)
+static void tdb_context_init(struct tdb_context *tdb)
 {
        assert(tdb->flags & TDB_VERSION1);
 
@@ -71,7 +71,7 @@ enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb,
        int hash_size = TDB1_DEFAULT_HASH_SIZE;
        enum TDB_ERROR ret = TDB_ERR_IO;
 
-       tdb1_context_init(tdb);
+       tdb_context_init(tdb);
 
        /* Default TDB2 hash becomes default TDB1 hash. */
        if (tdb->hash_fn == tdb_jenkins_hash)
@@ -171,7 +171,7 @@ enum TDB_ERROR tdb1_open(struct tdb_context *tdb)
 
        tdb->flags |= TDB_VERSION1;
 
-       tdb1_context_init(tdb);
+       tdb_context_init(tdb);
 
        /* Default TDB2 hash becomes default TDB1 hash. */
        if (tdb->hash_fn == tdb_jenkins_hash) {
index 2e292ee0bc364144de59d11813c6423207e7e135..13c51df9f344c4acc49b3da7b14abaf05499fbe5 100644 (file)
@@ -142,8 +142,6 @@ int tdb1_recovery_area(struct tdb_context *tdb,
                      const struct tdb1_methods *methods,
                      tdb1_off_t *recovery_offset,
                      struct tdb1_record *rec);
-int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
-                      enum tdb_lock_flags flags, bool upgradable);
 int tdb1_allrecord_upgrade(struct tdb_context *tdb);
 int tdb1_write_lock_record(struct tdb_context *tdb, tdb1_off_t off);
 int tdb1_write_unlock_record(struct tdb_context *tdb, tdb1_off_t off);
index d7b498df897a3d1f559b2e2e8d39fbb6023cfa9c..b74b8f4474b87d8185aadde606f0635294cf2277 100644 (file)
@@ -98,7 +98,7 @@ char *tdb1_summary(struct tdb_context *tdb)
        if (tdb->file->allrecord_lock.count != 0) {
                locked = false;
        } else {
-               if (tdb1_lockall_read(tdb) == -1)
+               if (tdb_lockall_read(tdb) != TDB_SUCCESS)
                        return NULL;
                locked = true;
        }
@@ -196,7 +196,7 @@ char *tdb1_summary(struct tdb_context *tdb)
 
 unlock:
        if (locked) {
-               tdb1_unlockall_read(tdb);
+               tdb_unlockall_read(tdb);
        }
        return ret;
 }
index d7f518dce487c74a492c459b274d04d3fd9db803..0a4f8b5503a7841fe0420e523a27a3d3ae42fd31 100644 (file)
@@ -732,7 +732,7 @@ int tdb1_wipe_all(struct tdb_context *tdb)
        tdb1_off_t recovery_head;
        tdb1_len_t recovery_size = 0;
 
-       if (tdb1_lockall(tdb) != 0) {
+       if (tdb_lockall(tdb) != TDB_SUCCESS) {
                return -1;
        }
 
@@ -801,16 +801,11 @@ int tdb1_wipe_all(struct tdb_context *tdb)
                }
        }
 
-       if (tdb1_unlockall(tdb) != 0) {
-               tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
-                          "tdb1_wipe_all: failed to unlock");
-               goto failed;
-       }
-
+       tdb_unlockall(tdb);
        return 0;
 
 failed:
-       tdb1_unlockall(tdb);
+       tdb_unlockall(tdb);
        return -1;
 }
 
index 71404c2d7507077fb32d382d27f8e9315a6d2190..b2b7a781db602d690184035ee09f8ddddb07d049 100644 (file)
@@ -74,7 +74,7 @@ int main(int argc, char *argv[])
        hsize.base.next = &tap_log_attr;
        hsize.tdb1_hashsize.hsize = 1024;
 
-       plan_tests(43);
+       plan_tests(40);
        tdb = tdb_open("run-no-lock-during-traverse.tdb1",
                       TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR,
                       0600, &hsize);
@@ -82,28 +82,28 @@ int main(int argc, char *argv[])
        ok1(tdb);
        ok1(prepare_entries(tdb));
        ok1(locking_errors1 == 0);
-       ok1(tdb1_lockall(tdb) == 0);
+       ok1(tdb_lockall(tdb) == 0);
        ok1(locking_errors1 == 0);
        ok1(tdb_traverse(tdb, delete_other, &errors) >= 0);
        ok1(errors == 0);
        ok1(locking_errors1 == 0);
-       ok1(tdb1_unlockall(tdb) == 0);
+       tdb_unlockall(tdb);
 
        ok1(prepare_entries(tdb));
        ok1(locking_errors1 == 0);
-       ok1(tdb1_lockall(tdb) == 0);
+       ok1(tdb_lockall(tdb) == 0);
        ok1(locking_errors1 == 0);
        ok1(tdb_traverse(tdb, delete_self, NULL) == NUM_ENTRIES);
        ok1(locking_errors1 == 0);
-       ok1(tdb1_unlockall(tdb) == 0);
+       tdb_unlockall(tdb);
 
        ok1(prepare_entries(tdb));
        ok1(locking_errors1 == 0);
-       ok1(tdb1_lockall(tdb) == 0);
+       ok1(tdb_lockall(tdb) == 0);
        ok1(locking_errors1 == 0);
        delete_entries(tdb);
        ok1(locking_errors1 == 0);
-       ok1(tdb1_unlockall(tdb) == 0);
+       tdb_unlockall(tdb);
 
        ok1(tdb_close(tdb) == 0);