X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Flock.c;h=49313a5419cb7b83b7f672aaff32f5942ddf079b;hp=2878eaeee5e98a9a034005a9dd0e626fa733ffc0;hb=f6900d2358ca0ec2f2dc776b9e42e4fca7bffbdf;hpb=7fe3218488f1bd25a4e50c27964752a18a64fb5f diff --git a/ccan/tdb2/lock.c b/ccan/tdb2/lock.c index 2878eaee..49313a54 100644 --- a/ccan/tdb2/lock.c +++ b/ccan/tdb2/lock.c @@ -37,7 +37,7 @@ static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call) call); } -/* If we fork, we no longer really own locks: preserves errno */ +/* If we fork, we no longer really own locks. */ static bool check_lock_pid(struct tdb_context *tdb, const char *call, bool log) { @@ -104,9 +104,9 @@ static int lock(struct tdb_context *tdb, tdb->file->locker = getpid(); } - add_stat(tdb, lock_lowlevel, 1); + tdb->stats.lock_lowlevel++; if (!waitflag) - add_stat(tdb, lock_nonblock, 1); + tdb->stats.lock_nonblock++; return tdb->lock_fn(tdb->file->fd, rw, off, len, waitflag, tdb->lock_data); } @@ -224,16 +224,14 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb, int rw_type, tdb_off_t offset, size_t len) { - int ret; - if (tdb->flags & TDB_NOLOCK) { return TDB_SUCCESS; } - ret = unlock(tdb, rw_type, offset, len); + if (!check_lock_pid(tdb, "tdb_brunlock", true)) + return TDB_ERR_LOCK; - /* If we fail, *then* we verify that we owned the lock. If not, ok. */ - if (ret == -1 && check_lock_pid(tdb, "tdb_brunlock", false)) { + if (unlock(tdb, rw_type, offset, len) == -1) { return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, "tdb_brunlock failed (fd=%d) at offset %zu" " rw_type=%d len=%zu: %s", @@ -359,7 +357,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, return TDB_ERR_LOCK; } - add_stat(tdb, locks, 1); + tdb->stats.locks++; new_lck = find_nestlock(tdb, offset, NULL); if (new_lck) { @@ -378,12 +376,14 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, return TDB_SUCCESS; } +#if 0 if (tdb->file->num_lockrecs && offset >= TDB_HASH_LOCK_START && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) { return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, "tdb_nest_lock: already have a hash lock?"); } +#endif new_lck = (struct tdb_lock *)realloc( tdb->file->lockrecs, @@ -567,7 +567,7 @@ enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype, " can't upgrade a write lock"); } - add_stat(tdb, locks, 1); + tdb->stats.locks++; again: /* Lock hashes, gradually. */ ecode = tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START, @@ -849,6 +849,10 @@ void tdb_lock_cleanup(struct tdb_context *tdb) { unsigned int i; + /* We don't want to warn: they're allowed to close tdb after fork. */ + if (!check_lock_pid(tdb, "tdb_close", false)) + return; + while (tdb->file->allrecord_lock.count && tdb->file->allrecord_lock.owner == tdb) { tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);