tdb: cleanup: tdb_have_extra_locks() helper
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 22 Apr 2010 04:23:51 +0000 (13:53 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 22 Apr 2010 04:23:51 +0000 (13:53 +0930)
In many places we check whether locks are held: add a helper to do this.

The _tdb_lockall() case has already checked for the allrecord lock, so
the extra work done by tdb_have_extra_locks() is merely redundant.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
(Imported from commit fba42f1fb4f81b8913cce5a23ca5350ba45f40e1)

lib/tdb/common/lock.c
lib/tdb/common/open.c
lib/tdb/common/tdb_private.h
lib/tdb/common/transaction.c

index 66cfcbdeea6eab9cd943f7b18a9dd8f66e4c9f8e..4f8970fd34bc4e7766e2748e2bbf077ad76a5f97 100644 (file)
@@ -474,8 +474,8 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype,
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
-       
-       if (tdb->num_locks != 0) {
+
+       if (tdb_have_extra_locks(tdb)) {
                /* can't combine global and chain locks */
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
                /* can't combine global and chain locks */
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
@@ -691,3 +691,14 @@ int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
                        count++;
        return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);
 }
                        count++;
        return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);
 }
+
+bool tdb_have_extra_locks(struct tdb_context *tdb)
+{
+       if (tdb->allrecord_lock.count) {
+               return true;
+       }
+       if (tdb->num_lockrecs) {
+               return true;
+       }
+       return false;
+}
index 9fbd5fdea92a9c4e15e165ca623c3447e0ab1e29..3fc98338639793c4b96975da2be84a71d80919c5 100644 (file)
@@ -465,7 +465,7 @@ static int tdb_reopen_internal(struct tdb_context *tdb, bool active_lock)
                return 0; /* Nothing to do. */
        }
 
                return 0; /* Nothing to do. */
        }
 
-       if (tdb->num_locks != 0 || tdb->allrecord_lock.count) {
+       if (tdb_have_extra_locks(tdb)) {
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
                goto fail;
        }
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
                goto fail;
        }
index 4e4816bd4aa7aadbbf7e1c412fd99d15ec267855..1af975de6e70b4dcac3a94006ba8e7c1789178cf 100644 (file)
@@ -234,6 +234,7 @@ int tdb_brlock(struct tdb_context *tdb,
               enum tdb_lock_flags flags);
 int tdb_brunlock(struct tdb_context *tdb,
                 int rw_type, tdb_off_t offset, size_t len);
               enum tdb_lock_flags flags);
 int tdb_brunlock(struct tdb_context *tdb,
                 int rw_type, tdb_off_t offset, size_t len);
+bool tdb_have_extra_locks(struct tdb_context *tdb);
 int tdb_transaction_lock(struct tdb_context *tdb, int ltype);
 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len);
 int tdb_transaction_lock(struct tdb_context *tdb, int ltype);
 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len);
index 374c597d8debf064908ce56f722290edd351d735..36b5c035b7f8280acaed3da6902749228590dd36 100644 (file)
@@ -463,7 +463,7 @@ int tdb_transaction_start(struct tdb_context *tdb)
                return 0;
        }
 
                return 0;
        }
 
-       if (tdb->num_locks != 0 || tdb->allrecord_lock.count) {
+       if (tdb_have_extra_locks(tdb)) {
                /* the caller must not have any locks when starting a
                   transaction as otherwise we'll be screwed by lack
                   of nested locks in posix */
                /* the caller must not have any locks when starting a
                   transaction as otherwise we'll be screwed by lack
                   of nested locks in posix */
@@ -950,7 +950,7 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
        
        /* if there are any locks pending then the caller has not
           nested their locks properly, so fail the transaction */
        
        /* if there are any locks pending then the caller has not
           nested their locks properly, so fail the transaction */
-       if (tdb->num_locks || tdb->allrecord_lock.count) {
+       if (tdb_have_extra_locks(tdb)) {
                tdb->ecode = TDB_ERR_LOCK;
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: locks pending on commit\n"));
                _tdb_transaction_cancel(tdb, F_RDLCK);
                tdb->ecode = TDB_ERR_LOCK;
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: locks pending on commit\n"));
                _tdb_transaction_cancel(tdb, F_RDLCK);