X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Flock.c;h=2b00584580810ad9aca0ae24dd6f880bd58d0796;hb=2c56e4d9ec516aceb9c5b26ceebd6d99361855c5;hp=c274c11051cc416bec0977319dec5f9ed712f1ec;hpb=ebdd6451e2d7aa185e62a59fa2c72ffe36772d9a;p=ccan diff --git a/ccan/tdb2/lock.c b/ccan/tdb2/lock.c index c274c110..2b005845 100644 --- a/ccan/tdb2/lock.c +++ b/ccan/tdb2/lock.c @@ -485,8 +485,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, /* Lock all the hash buckets. */ again: hash_size = (1ULL << tdb->header.v.hash_bits); - if (tdb_lock_gradual(tdb, ltype, TDB_HASH_LOCK_START, - 1ULL << tdb->header.v.hash_bits, flags)) { + if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START, + hash_size)) { if (!(flags & TDB_LOCK_PROBE)) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_lockall hashes failed (%s)\n", @@ -502,7 +502,7 @@ again: tdb->allrecord_lock.off = upgradable; /* Now we re-check header, holding lock. */ - if (unlikely(update_header(tdb))) { + if (unlikely(header_changed(tdb))) { tdb_allrecord_unlock(tdb, ltype); goto again; } @@ -567,6 +567,7 @@ int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) tdb->allrecord_lock.count = 0; tdb->allrecord_lock.ltype = 0; + tdb->header_uptodate = false; hash_size = (1ULL << tdb->header.v.hash_bits); @@ -624,13 +625,18 @@ int tdb_unlockall_read(struct tdb_context *tdb) } #endif -int tdb_lock_list(struct tdb_context *tdb, tdb_off_t list, - int ltype, enum tdb_lock_flags waitflag) +/* Returns the list we actually locked. */ +tdb_off_t tdb_lock_list(struct tdb_context *tdb, uint64_t hash, + int ltype, enum tdb_lock_flags waitflag) { + tdb_off_t list = hash & ((1ULL << tdb->header.v.hash_bits) - 1); + /* Header can change ONLY if we had no locks before. */ + bool can_change = tdb->num_lockrecs == 0; + /* a allrecord lock allows us to avoid per chain locks */ if (tdb->allrecord_lock.count && (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) { - return 0; + return list; } if (tdb->allrecord_lock.count) { @@ -639,15 +645,28 @@ int tdb_lock_list(struct tdb_context *tdb, tdb_off_t list, "tdb_lock_list: have %s allrecordlock\n", tdb->allrecord_lock.ltype == F_RDLCK ? "read" : "write"); - return -1; + return TDB_OFF_ERR; } - /* FIXME: Should we do header_uptodate and return retry here? */ - return tdb_nest_lock(tdb, TDB_HASH_LOCK_START + list, ltype, waitflag); +again: + if (tdb_nest_lock(tdb, TDB_HASH_LOCK_START + list, ltype, waitflag)) + return TDB_OFF_ERR; + + if (can_change && unlikely(header_changed(tdb))) { + tdb_off_t new = hash & ((1ULL << tdb->header.v.hash_bits) - 1); + if (new != list) { + tdb_nest_unlock(tdb, TDB_HASH_LOCK_START+list, ltype); + list = new; + goto again; + } + } + return list; } int tdb_unlock_list(struct tdb_context *tdb, tdb_off_t list, int ltype) { + list &= ((1ULL << tdb->header.v.hash_bits) - 1); + /* a allrecord lock allows us to avoid per chain locks */ if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.ltype == F_RDLCK @@ -668,7 +687,7 @@ int tdb_lock_free_list(struct tdb_context *tdb, tdb_off_t flist, enum tdb_lock_flags waitflag) { /* You're supposed to have a hash lock first! */ - if (!tdb_has_locks(tdb)) { + if (!(tdb->flags & TDB_NOLOCK) && !tdb_has_locks(tdb)) { tdb->ecode = TDB_ERR_LOCK; tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_lock_free_list without lock!\n"); @@ -701,23 +720,14 @@ void tdb_unlock_free_list(struct tdb_context *tdb, tdb_off_t flist) } #if 0 -static int chainlock_loop(struct tdb_context *tdb, const TDB_DATA *key, - int ltype, enum tdb_lock_flags waitflag, - const char *func) +static int chainlock(struct tdb_context *tdb, const TDB_DATA *key, + int ltype, enum tdb_lock_flags waitflag, + const char *func) { int ret; uint64_t h = tdb_hash(tdb, key->dptr, key->dsize); -again: - ret = tdb_lock_list(tdb, - h & ((1ULL << tdb->header.v.hash_bits) - 1), - ltype, waitflag); - if (likely(ret == 0) && unlikely(update_header(tdb))) { - tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1), - ltype); - goto again; - } - + ret = tdb_lock_list(tdb, h, ltype, waitflag) == TDB_OFF_ERR ? -1 : 0; tdb_trace_1rec(tdb, func, *key); return ret; } @@ -726,8 +736,7 @@ again: contention - it cannot guarantee how many records will be locked */ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key) { - return chainlock_loop(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, - "tdb_chainlock"); + return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock"); } /* lock/unlock one hash chain, non-blocking. This is meant to be used @@ -735,8 +744,8 @@ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key) locked */ int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key) { - return chainlock_loop(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT, - "tdb_chainlock_nonblock"); + return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT, + "tdb_chainlock_nonblock"); } int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) @@ -749,8 +758,8 @@ int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key) { - return chainlock_loop(tdb, &key, F_RDLCK, TDB_LOCK_WAIT, - "tdb_chainlock_read"); + return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT, + "tdb_chainlock_read"); } int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key) @@ -847,3 +856,10 @@ void tdb_release_transaction_locks(struct tdb_context *tdb) tdb->header_uptodate = false; } #endif + +void tdb_lock_init(struct tdb_context *tdb) +{ + tdb->num_lockrecs = 0; + tdb->lockrecs = NULL; + tdb->allrecord_lock.count = 0; +}