From d92b8a7d9a050eb9735e48a8d37a5833500242d3 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 22 Feb 2010 13:34:10 +1030 Subject: [PATCH] tdb: cleanup: tdb_have_extra_locks() helper 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 --- ccan/tdb/lock.c | 15 +++++++++++++-- ccan/tdb/open.c | 2 +- ccan/tdb/tdb_private.h | 1 + ccan/tdb/transaction.c | 4 ++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ccan/tdb/lock.c b/ccan/tdb/lock.c index 66cfcbde..4f8970fd 100644 --- a/ccan/tdb/lock.c +++ b/ccan/tdb/lock.c @@ -474,8 +474,8 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, 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; @@ -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); } + +bool tdb_have_extra_locks(struct tdb_context *tdb) +{ + if (tdb->allrecord_lock.count) { + return true; + } + if (tdb->num_lockrecs) { + return true; + } + return false; +} diff --git a/ccan/tdb/open.c b/ccan/tdb/open.c index d1a34bf1..b7aa6e74 100644 --- a/ccan/tdb/open.c +++ b/ccan/tdb/open.c @@ -461,7 +461,7 @@ static int tdb_reopen_internal(struct tdb_context *tdb, bool active_lock) 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; } diff --git a/ccan/tdb/tdb_private.h b/ccan/tdb/tdb_private.h index 33445fc5..ed88723c 100644 --- a/ccan/tdb/tdb_private.h +++ b/ccan/tdb/tdb_private.h @@ -266,6 +266,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); +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); diff --git a/ccan/tdb/transaction.c b/ccan/tdb/transaction.c index 3d34ac84..04e4bbcd 100644 --- a/ccan/tdb/transaction.c +++ b/ccan/tdb/transaction.c @@ -564,7 +564,7 @@ int tdb_transaction_start(struct tdb_context *tdb) 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 */ @@ -944,7 +944,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 (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); -- 2.39.2