]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/lock.c
tdb: cleanup: rename global_lock to allrecord_lock.
[ccan] / ccan / tdb / lock.c
index e6b900e419ea00a5b2610ec0ee939157ba665431..6b320f45fedf7a410bbed3ba5a14eff9f1fc3599 100644 (file)
@@ -33,17 +33,17 @@ void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
 }
 
 static int fcntl_lock(struct tdb_context *tdb,
-                     int rw, off_t off, off_t len, bool wait)
+                     int rw, off_t off, off_t len, bool waitflag)
 {
        struct flock fl;
-       
+
        fl.l_type = rw;
        fl.l_whence = SEEK_SET;
        fl.l_start = off;
        fl.l_len = len;
        fl.l_pid = 0;
 
-       if (wait)
+       if (waitflag)
                return fcntl(tdb->fd, F_SETLKW, &fl);
        else
                return fcntl(tdb->fd, F_SETLK, &fl);
@@ -158,13 +158,12 @@ int tdb_brlock(struct tdb_context *tdb,
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
+               tdb->ecode = TDB_ERR_LOCK;
                /* Generic lock error. errno set by fcntl.
                 * EAGAIN is an expected return from non-blocking
                 * locks. */
                if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
-                       /* Ensure error code is set for log fun to examine. */
-                       tdb->ecode = TDB_ERR_LOCK;
-                       TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n", 
+                       TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
                                 tdb->fd, offset, rw_type, flags, (int)len));
                }
                return -1;
@@ -186,7 +185,7 @@ int tdb_brunlock(struct tdb_context *tdb,
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n", 
+               TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
                         tdb->fd, offset, rw_type, (int)len));
        }
        return ret;
@@ -227,13 +226,13 @@ static int _tdb_lock(struct tdb_context *tdb, int list, int ltype,
        struct tdb_lock_type *new_lck;
        int i;
 
-       /* a global lock allows us to avoid per chain locks */
-       if (tdb->global_lock.count && 
-           (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
+       /* 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;
        }
 
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
@@ -319,12 +318,12 @@ static int _tdb_unlock(struct tdb_context *tdb, int list, int ltype,
        struct tdb_lock_type *lck = NULL;
 
        /* a global lock allows us to avoid per chain locks */
-       if (tdb->global_lock.count && 
-           (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
+       if (tdb->allrecord_lock.count &&
+           (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
                return 0;
        }
 
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
@@ -404,7 +403,7 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
  */
 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
 {
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                return 0;
        }
        if (tdb->transaction_lock_count > 0) {
@@ -427,7 +426,7 @@ int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
 {
        int ret;
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                return 0;
        }
        if (tdb->transaction_lock_count > 1) {
@@ -454,12 +453,12 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
-       if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
-               tdb->global_lock.count++;
+       if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
+               tdb->allrecord_lock.count++;
                return 0;
        }
 
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                /* a global lock of a different type exists */
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
@@ -480,8 +479,8 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
-       tdb->global_lock.count = 1;
-       tdb->global_lock.ltype = ltype;
+       tdb->allrecord_lock.count = 1;
+       tdb->allrecord_lock.ltype = ltype;
 
        return 0;
 }
@@ -497,25 +496,25 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
                return -1;
        }
 
-       if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
+       if (tdb->allrecord_lock.ltype != ltype || tdb->allrecord_lock.count == 0) {
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
 
-       if (tdb->global_lock.count > 1) {
-               tdb->global_lock.count--;
+       if (tdb->allrecord_lock.count > 1) {
+               tdb->allrecord_lock.count--;
                return 0;
        }
 
        if (!mark_lock &&
-           tdb->methods->brunlock(tdb, F_WRLCK,
+           tdb->methods->brunlock(tdb, ltype,
                                   FREELIST_TOP, 4*tdb->header.hash_size)) {
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
                return -1;
        }
 
-       tdb->global_lock.count = 0;
-       tdb->global_lock.ltype = 0;
+       tdb->allrecord_lock.count = 0;
+       tdb->allrecord_lock.ltype = 0;
 
        return 0;
 }
@@ -633,10 +632,12 @@ int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
        return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
 }
 
+
+
 /* record lock stops delete underneath */
 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
 {
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                return 0;
        }
        return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
@@ -667,7 +668,7 @@ int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
        struct tdb_traverse_lock *i;
        uint32_t count = 0;
 
-       if (tdb->global_lock.count) {
+       if (tdb->allrecord_lock.count) {
                return 0;
        }