summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2015-08-30 13:11:47 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2015-08-30 13:11:47 +0100
commit0e2a798af4ea210288cd85cde0373a3f13cc8025 (patch)
tree9b781e3fae03a3aab489e5d46d5f8205527fe5a6 /lib/pure
parent50fa1759aaea500d9e2f7a4fe7093b9197a3e6ac (diff)
parentc60728326ed926255800fe3064f818a4aeaff8c5 (diff)
downloadNim-0e2a798af4ea210288cd85cde0373a3f13cc8025.tar.gz
Merge branch 'reactormonk-coro-remove-prefix' into devel
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/coro.nim36
1 files changed, 17 insertions, 19 deletions
diff --git a/lib/pure/coro.nim b/lib/pure/coro.nim
index 6ef5f6f54..8fa529474 100644
--- a/lib/pure/coro.nim
+++ b/lib/pure/coro.nim
@@ -15,8 +15,7 @@ import macros
 import arch
 import lists
 
-const coroDefaultStackSize = 512 * 1024
-
+const defaultStackSize = 512 * 1024
 
 type Coroutine = ref object
   # prev: ptr Coroutine
@@ -38,8 +37,7 @@ proc GC_addStack(starts: pointer) {.cdecl, importc.}
 proc GC_removeStack(starts: pointer) {.cdecl, importc.}
 proc GC_setCurrentStack(starts, pos: pointer) {.cdecl, importc.}
 
-
-proc coroStart*(c: proc(), stacksize: int=coroDefaultStackSize) =
+proc start*(c: proc(), stacksize: int=defaultStackSize) =
   ## Adds coroutine to event loop. It does not run immediately.
   var coro = Coroutine()
   coro.fn = c
@@ -49,20 +47,22 @@ proc coroStart*(c: proc(), stacksize: int=coroDefaultStackSize) =
   coroutines.append(coro)
 
 {.push stackTrace: off.}
-proc coroYield*(sleepTime: float=0) =
+proc suspend*(sleepTime: float=0) =
   ## Stops coroutine execution and resumes no sooner than after ``sleeptime`` seconds.
   ## Until then other coroutines are executed.
+  ##
+  ## This is similar to a `yield`:idx:, or a `yieldFrom`:idx in Python.
   var oldFrame = getFrame()
   var sp {.volatile.}: pointer
   GC_setCurrentStack(current.stack, cast[pointer](addr sp))
-  current.sleepTime = sleep_time
+  current.sleepTime = sleepTime
   current.lastRun = epochTime()
   if setjmp(current.ctx) == 0:
     longjmp(mainCtx, 1)
   setFrame(oldFrame)
 {.pop.}
 
-proc coroRun*() =
+proc run*() =
   ## Starts main event loop which exits when all coroutines exit. Calling this proc
   ## starts execution of first coroutine.
   var node = coroutines.head
@@ -108,18 +108,16 @@ proc coroRun*() =
     else:
       node = node.next
 
-
-proc coroAlive*(c: proc()): bool =
+proc alive*(c: proc()): bool =
   ## Returns ``true`` if coroutine has not returned, ``false`` otherwise.
   for coro in items(coroutines):
     if coro.fn == c:
       return true
 
-proc coroWait*(c: proc(), interval=0.01) =
+proc wait*(c: proc(), interval=0.01) =
   ## Returns only after coroutine ``c`` has returned. ``interval`` is time in seconds how often.
-  while coroAlive(c):
-    coroYield interval
-
+  while alive(c):
+    suspend interval
 
 when isMainModule:
   var stackCheckValue = 1100220033
@@ -128,18 +126,18 @@ when isMainModule:
   proc c1() =
     for i in 0 .. 3:
       echo "c1"
-      coroYield 0.05
+      suspend 0.05
     echo "c1 exits"
 
 
   proc c2() =
     for i in 0 .. 3:
       echo "c2"
-      coroYield 0.025
-    coroWait(c1)
+      suspend 0.025
+    wait(c1)
     echo "c2 exits"
 
-  coroStart(c1)
-  coroStart(c2)
-  coroRun()
+  start(c1)
+  start(c2)
+  run()
   echo "done ", stackCheckValue