X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Fhash.c;h=c0b34f8a42ab576da4981cbf6b224c69f541bc35;hb=6ce40d6ab4dbd63bd76979b8491351708c3df892;hp=3db1ac2ebc7c85b9b50006138953ba38aafc7584;hpb=f6067e4cbd7b7415571f12438aec00faec5657fb;p=ccan diff --git a/ccan/tdb2/hash.c b/ccan/tdb2/hash.c index 3db1ac2e..c0b34f8a 100644 --- a/ccan/tdb2/hash.c +++ b/ccan/tdb2/hash.c @@ -31,13 +31,12 @@ static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed, void tdb_hash_init(struct tdb_context *tdb) { - tdb->khash = jenkins_hash; - tdb->hash_priv = NULL; + tdb->hash_fn = jenkins_hash; } uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len) { - return tdb->khash(ptr, len, tdb->hash_seed, tdb->hash_priv); + return tdb->hash_fn(ptr, len, tdb->hash_seed, tdb->hash_data); } uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off) @@ -861,7 +860,7 @@ enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key) "tdb_chainlock"); } -enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) +void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) { uint64_t h = tdb_hash(tdb, key.dptr, key.dsize); tdb_off_t lockstart, locksize; @@ -873,6 +872,26 @@ enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key) lockstart = hlock_range(group, &locksize); tdb_trace_1rec(tdb, "tdb_chainunlock", key); - return tdb->last_error = tdb_unlock_hashes(tdb, lockstart, locksize, - F_WRLCK); + tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK); +} + +enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key) +{ + return tdb->last_error = chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT, + "tdb_chainlock_read"); +} + +void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key) +{ + uint64_t h = tdb_hash(tdb, key.dptr, key.dsize); + tdb_off_t lockstart, locksize; + unsigned int group, gbits; + + gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS; + group = bits_from(h, 64 - gbits, gbits); + + lockstart = hlock_range(group, &locksize); + + tdb_trace_1rec(tdb, "tdb_chainunlock_read", key); + tdb_unlock_hashes(tdb, lockstart, locksize, F_RDLCK); }