-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4364a57
commit 304a0cd
Showing
5 changed files
with
154 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,84 @@ | ||
import std/osproc | ||
import std/strutils | ||
import std/logging | ||
import std/atomics | ||
import std/os | ||
import std/macros | ||
|
||
import balls | ||
|
||
import wrflock | ||
|
||
let v = initWRFLock() | ||
echo v.facquire() | ||
echo v.wacquire() | ||
echo v.wrelease() | ||
echo v.racquire() | ||
echo v.racquire() | ||
echo v.racquire() | ||
const threadCount = 6 | ||
|
||
addHandler newConsoleLogger() | ||
setLogFilter: | ||
when defined(danger): | ||
lvlNotice | ||
elif defined(release): | ||
lvlInfo | ||
else: | ||
lvlDebug | ||
|
||
var lock {.global.} = initWRFLock([wWaitYield, fWaitYield, rWaitYield]) | ||
var counter {.global.}: Atomic[int] | ||
|
||
proc writeLock() {.thread.} = | ||
sleep(1000) | ||
doassert lock.wAcquire() | ||
lock.wWait() | ||
discard counter.fetchAdd(1) | ||
doassert lock.wRelease() | ||
|
||
proc readLock() {.thread.} = | ||
sleep(200) | ||
doassert lock.rAcquire() | ||
lock.rWait() | ||
doassert counter.load() == 1, "lock allowed read before it was written to" | ||
echo counter.load() | ||
doassert lock.rRelease() | ||
|
||
proc freeLock() {.thread.} = | ||
sleep(500) | ||
doassert lock.fAcquire() | ||
lock.fWait() | ||
counter.store(-10000) | ||
doassert lock.fRelease() | ||
|
||
# try to delay a reasonable amount of time despite platform | ||
|
||
template expectCounter(n: int): untyped = | ||
## convenience | ||
try: | ||
check counter.load == n | ||
except Exception: | ||
checkpoint " counter: ", load counter | ||
checkpoint "expected: ", n | ||
raise | ||
|
||
suite "wrflock": | ||
block: | ||
## See if it works | ||
|
||
var threads: seq[Thread[void]] | ||
newSeq(threads, threadCount) | ||
|
||
counter.store 0 | ||
|
||
var i: int | ||
for thread in threads.mitems: | ||
if i == 0: | ||
createThread(thread, writeLock) | ||
elif i == threadCount - 1: | ||
createThread(thread, freeLock) | ||
else: | ||
createThread(thread, readLock) | ||
inc i | ||
checkpoint "created $# threads" % [ $threadCount ] | ||
|
||
for thread in threads.mitems: | ||
joinThread thread | ||
checkpoint "joined $# threads" % [ $threadCount ] | ||
|
||
|
||
expectCounter -10000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version = "0.0.0" | ||
version = "0.1.0" | ||
author = "Shayan Habibi" | ||
description = "" | ||
description = "Write, Read, Free lock primitive" | ||
license = "MIT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters