add files lock

This commit is contained in:
Jaremy Creechley 2023-09-28 19:17:43 -07:00
parent ebc93e8924
commit c85cf18af7
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300

View File

@ -16,12 +16,36 @@ push: {.upraises: [].}
import std/sharedtables
var keyTable: SharedTable[KeyId, int]
type
KeyLock = tuple[locked: bool]
var keyTable: SharedTable[KeyId, KeyLock]
keyTable.init()
template lockKeyImpl(key: KeyId, blk: untyped) =
var hasLock = false
try:
while not hasLock:
keyTable.withKey(key) do (k: KeyId, klock: var KeyLock, exists: var bool):
if not exists or not klock.locked:
klock.locked = true
exists = true
hasLock = klock.locked
os.sleep(1)
`blk`
finally:
if hasLock:
keyTable.withKey(key) do (k: KeyId, klock: var KeyLock, exists: var bool):
assert exists and klock.locked
klock.locked = false
exists = false
template withReadLock(key: KeyId, blk: untyped) =
`blk`
lockKeyImpl(key, blk)
template withWriteLock(key: KeyId, blk: untyped) =
`blk`
lockKeyImpl(key, blk)
type
FSBackend*[K, V] = object