]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/lock.c
Don't fail mysteriously on existing trace file.
[ccan] / ccan / tdb / lock.c
index 6809ce37d87513986cc54fab82251004379557cb..326d38e70713443f86aa45e821b20e883d3df44a 100644 (file)
@@ -301,17 +301,21 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
  */
 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
 {
-       if (tdb->have_transaction_lock || tdb->global_lock.count) {
-               tdb->have_transaction_lock++;
+       if (tdb->global_lock.count) {
+               return 0;
+       }
+       if (tdb->transaction_lock_count > 0) {
+               tdb->transaction_lock_count++;
                return 0;
        }
+
        if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype, 
                                     F_SETLKW, 0, 1) == -1) {
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
-       tdb->have_transaction_lock++;
+       tdb->transaction_lock_count++;
        return 0;
 }
 
@@ -320,10 +324,19 @@ int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
  */
 int tdb_transaction_unlock(struct tdb_context *tdb)
 {
-       if (--tdb->have_transaction_lock) {
+       int ret;
+       if (tdb->global_lock.count) {
                return 0;
        }
-       return tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+       if (tdb->transaction_lock_count > 1) {
+               tdb->transaction_lock_count--;
+               return 0;
+       }
+       ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+       if (ret == 0) {
+               tdb->transaction_lock_count = 0;
+       }
+       return ret;
 }
 
 
@@ -500,7 +513,7 @@ int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
 
 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
 {
-       tdb_trace_1rec(tdb, "tdb_chainlock", key);
+       tdb_trace_1rec(tdb, "tdb_chainunlock", key);
        return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
 }