openidec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 5093df59407c8ce1a41e52c2967ccdf8ecfd2b63
parent 0cf1f11706f6ea1db32c3d143a49be010c0986ca
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Fri, 11 Sep 2020 17:49:30 +0100

db.Lock is now recursive

Diffstat:
Mii/db.go | 8++++++++
1 file changed, 8 insertions(+), 0 deletions(-)

diff --git a/ii/db.go b/ii/db.go @@ -13,6 +13,7 @@ import ( "sort" "strings" "sync" + "sync/atomic" "time" ) @@ -37,6 +38,7 @@ type DB struct { Sync sync.RWMutex IdxSync sync.RWMutex Name string + LockDepth int32 } func append_file(fn string, text string) error { @@ -52,6 +54,9 @@ func append_file(fn string, text string) error { } func (db *DB) Lock() bool { + if atomic.AddInt32(&db.LockDepth, 1) > 1 { + return true + } try := 16 for try > 0 { if err := os.Mkdir(db.LockPath(), 0777); err == nil { @@ -65,6 +70,9 @@ func (db *DB) Lock() bool { } func (db *DB) Unlock() { + if atomic.AddInt32(&db.LockDepth, -1) > 0 { + return + } os.Remove(db.LockPath()) }