Skip to content

Commit

Permalink
0.0.5: signals, wait, semaphores
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Jul 24, 2020
1 parent 405fdce commit a4ad640
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Key primitives and their implementation status, in order of priority:
- [x] sleep
- [x] yield
- [x] discard
- [ ] signal
- [ ] wait
- [x] signal
- [x] wait
- [ ] I/O
- [ ] fork
- [ ] thread
Expand Down
3 changes: 2 additions & 1 deletion cps.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.0.4"
version = "0.0.5"
author = "disruptek"
description = "continuation-passing style"
license = "MIT"
Expand All @@ -25,3 +25,4 @@ task test, "run tests for travis":
execTest("tests/test.nim")
execTest("tests/tock.nim")
execTest("tests/tyield.nim")
execTest("tests/tsignal.nim")
27 changes: 23 additions & 4 deletions cps/eventqueue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ proc len*(eq: EventQueue): int =
## The number of pending continuations.
result = len(eq.goto) + len(eq.yields) + len(eq.pending)

proc `[]=`(eq: var EventQueue; s: var Semaphore; id: Id) =
## put a semaphore into the queue with its registration
assert id != invalidId
assert id != wakeupId
assert not s.isReady
assert s.id.Id != invalidId
eq.pending[s] = id

proc `[]=`(eq: var EventQueue; id: Id; cont: Cont) =
## put a continuation into the queue according to its registration
assert id != invalidId
Expand Down Expand Up @@ -320,9 +328,9 @@ template signalImpl(s: Semaphore; body: untyped): untyped =
wakeUp()

proc cpsSignal*(s: var Semaphore): Cont {.cpsMagic.} =
## signal the given semaphore, causing the first waiting continuation
## Signal the given semaphore, causing the first waiting continuation
## to be queued for execution in the dispatcher; control remains in
## the calling procedure
## the calling procedure.
result = c
signal s
withReady s:
Expand All @@ -331,13 +339,24 @@ proc cpsSignal*(s: var Semaphore): Cont {.cpsMagic.} =
discard

proc cpsSignalAll*(s: var Semaphore): Cont {.cpsMagic.} =
## signal the given semaphore, causing all waiting continuations
## Signal the given semaphore, causing all waiting continuations
## to be queued for execution in the dispatcher; control remains in
## the calling procedure
## the calling procedure.
result = c
signal s
if s.isReady:
init()
while true:
signalImpl s:
break

proc cpsWait*(s: var Semaphore): Cont {.cpsMagic.} =
## Queue the current continuation pending readiness of the given
## Semaphore `s`.
let id = nextId()
if s.isReady:
addLast(eq.yields, c)
wakeUp()
else:
eq[s] = id
eq[id] = c
2 changes: 2 additions & 0 deletions cps/semaphore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type
cond: Cond
count: int

proc id*(s: Semaphore): int = s.id

proc hash*(s: Semaphore): Hash =
result = s.id.Hash

Expand Down
2 changes: 1 addition & 1 deletion docs/cps.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ <h1><a class="toc-backref" href="#17">Macros</a></h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 00:54:59 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 01:17:04 UTC</small>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/cps/environment.html
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ <h1><a class="toc-backref" href="#18">Templates</a></h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 00:54:59 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 01:17:04 UTC</small>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/theindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ <h1 class="title">Index</h1>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 00:54:59 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-24 01:17:04 UTC</small>
</div>
</div>
</div>
Expand Down
18 changes: 18 additions & 0 deletions tests/tsignal.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cps
import cps/eventqueue

var sem = newSemaphore()
var success = false

proc tick(ms: int): Cont {.cps.} =
cpsSleep ms
cpsSignal(sem)

proc tock(): Cont {.cps.} =
cpsWait sem
success = true

trampoline tick(10)
trampoline tock()

run()
5 changes: 5 additions & 0 deletions tests/tsignal.nim.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--path="$config/.."
--threads:on
--define:threadsafe
--gc:arc
--define:cpsDebug

0 comments on commit a4ad640

Please sign in to comment.