Skip to content

Commit

Permalink
0.5.0; api simplification and boilerplate cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shayanhabibi committed Dec 22, 2021
1 parent 5e61369 commit fbb7e06
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 359 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ if lock.wAcquire():
let lock = initWRFLock()
if lock.wAcquire():
while not lock.wTimeWait(1_000):
while not lock.wWait(1_000):
# Do other things if the lock times out waiting for its action
# Do write things here
lock.wRelease()
Expand All @@ -102,6 +102,22 @@ if lock.wAcquire():
> locks. Yield TimeWaits will always complete after the allotted time. Blocking TimeWaits can actually wait longer than the allotted time. It is guaranteed to
> either eventually succeed or time out.
---

### Alternative API

```nim
let lock = initWRFLock()
if lock.acquire(Write):
while not lock.wait(Write, 1_000):
# Do other things if the lock times out waiting for its action
# Do write things here
lock.release(Write)
```

---

By default, the Wait operations for all 3 actions (write, read, free) are blocking
using a futex. You can pass flags to change this to just have the thread yield
to the scheduler for any of the actions.
Expand Down
6 changes: 3 additions & 3 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ var counter {.global.}: Atomic[int]
proc writeLock() {.thread.} =
sleep(1000)
doassert lock.wAcquire()
lock.wWait()
discard lock.wWait()
discard counter.fetchAdd(1)
doassert lock.wRelease()

proc readLock() {.thread.} =
sleep(200)
lock.withRLock:
lock.withLock(Read):
try:
check counter.load == 1
except Exception:
Expand All @@ -44,7 +44,7 @@ proc readLock() {.thread.} =
proc freeLock() {.thread.} =
sleep(500)
doassert lock.fAcquire()
if not lock.fTimeWait(1_000):
if not lock.fWait(1_000):
return
counter.store(-10000)
doassert lock.fRelease()
Expand Down
Loading

0 comments on commit fbb7e06

Please sign in to comment.