]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/lock.c
tdb2: Add stats attribute.
[ccan] / ccan / tdb2 / lock.c
index a4cfd26c8c05da87540e0294aa2d9380ca7c4b5b..65e9dc25a6b743c1d5a50a75134d144104837011 100644 (file)
@@ -40,10 +40,13 @@ static int fcntl_lock(struct tdb_context *tdb,
        fl.l_len = len;
        fl.l_pid = 0;
 
+       add_stat(tdb, lock_lowlevel, 1);
        if (waitflag)
                return fcntl(tdb->fd, F_SETLKW, &fl);
-       else
+       else {
+               add_stat(tdb, lock_nonblock, 1);
                return fcntl(tdb->fd, F_SETLK, &fl);
+       }
 }
 
 static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
@@ -286,6 +289,8 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
        if (tdb->flags & TDB_NOLOCK)
                return 0;
 
+       add_stat(tdb, locks, 1);
+
        new_lck = find_nestlock(tdb, offset);
        if (new_lck) {
                if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
@@ -486,6 +491,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
+       add_stat(tdb, locks, 1);
 again:
        /* Lock hashes, gradually. */
        if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
@@ -614,52 +620,6 @@ bool tdb_has_hash_locks(struct tdb_context *tdb)
        return false;
 }
 
-#if 0
-/* lock entire database with write lock */
-int tdb_lockall(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_lockall");
-       return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
-}
-
-/* lock entire database with write lock - nonblocking varient */
-int tdb_lockall_nonblock(struct tdb_context *tdb)
-{
-       int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
-       tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
-       return ret;
-}
-
-/* unlock entire database with write lock */
-int tdb_unlockall(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_unlockall");
-       return tdb_allrecord_unlock(tdb, F_WRLCK);
-}
-
-/* lock entire database with read lock */
-int tdb_lockall_read(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_lockall_read");
-       return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
-}
-
-/* lock entire database with read lock - nonblock varient */
-int tdb_lockall_read_nonblock(struct tdb_context *tdb)
-{
-       int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
-       tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
-       return ret;
-}
-
-/* unlock entire database with read lock */
-int tdb_unlockall_read(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_unlockall_read");
-       return tdb_allrecord_unlock(tdb, F_RDLCK);
-}
-#endif
-
 static bool tdb_has_free_lock(struct tdb_context *tdb)
 {
        unsigned int i;
@@ -781,117 +741,6 @@ void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
        tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
 }
 
-#if 0
-/* lock/unlock one hash chain, non-blocking. This is meant to be used
-   to reduce contention - it cannot guarantee how many records will be
-   locked */
-int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
-{
-       return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT,
-                        "tdb_chainlock_nonblock");
-}
-
-int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
-{
-       return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
-                        "tdb_chainlock_read");
-}
-
-int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
-{
-       uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
-       tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
-       return tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1),
-                              F_RDLCK);
-}
-
-/* record lock stops delete underneath */
-int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-       return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
-}
-
-/*
-  Write locks override our own fcntl readlocks, so check it here.
-  Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
-  an error to fail to get the lock here.
-*/
-int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       struct tdb_traverse_lock *i;
-       for (i = &tdb->travlocks; i; i = i->next)
-               if (i->off == off)
-                       return -1;
-       if (tdb->allrecord_lock.count) {
-               if (tdb->allrecord_lock.ltype == F_WRLCK) {
-                       return 0;
-               }
-               return -1;
-       }
-       return tdb_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
-}
-
-int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-       return tdb_brunlock(tdb, F_WRLCK, off, 1);
-}
-
-/* fcntl locks don't stack: avoid unlocking someone else's */
-int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       struct tdb_traverse_lock *i;
-       uint32_t count = 0;
-
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-
-       if (off == 0)
-               return 0;
-       for (i = &tdb->travlocks; i; i = i->next)
-               if (i->off == off)
-                       count++;
-       return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
-}
-
-/* The transaction code uses this to remove all locks. */
-void tdb_release_transaction_locks(struct tdb_context *tdb)
-{
-       unsigned int i;
-
-       if (tdb->allrecord_lock.count != 0) {
-               tdb_off_t hash_size, free_size;
-
-               hash_size = (1ULL << tdb->header.v.hash_bits)
-                       * sizeof(tdb_off_t);
-               free_size = tdb->header.v.free_zones 
-                       * (tdb->header.v.free_buckets + 1) * sizeof(tdb_off_t);
-
-               tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
-                            tdb->header.v.hash_off, hash_size);
-               tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
-                            tdb->header.v.free_off, free_size);
-               tdb->allrecord_lock.count = 0;
-               tdb->allrecord_lock.ltype = 0;
-       }
-
-       for (i = 0; i<tdb->num_lockrecs; i++) {
-               struct tdb_lock_type *lck = &tdb->lockrecs[i];
-
-               tdb_brunlock(tdb, lck->ltype, lck->off, 1);
-       }
-       tdb->num_lockrecs = 0;
-       SAFE_FREE(tdb->lockrecs);
-       tdb->header_uptodate = false;
-}
-#endif
-
 void tdb_lock_init(struct tdb_context *tdb)
 {
        tdb->num_lockrecs = 0;