removed coro prefixes from coro.nim

This commit is contained in:
Simon Hafner 2015-08-25 16:33:44 +02:00
commit 7d9a748fcd

View file

@ -15,7 +15,7 @@ import macros
import arch import arch
import lists import lists
const coroDefaultStackSize = 512 * 1024 const defaultStackSize = 512 * 1024
type Coroutine = ref object type Coroutine = ref object
@ -38,8 +38,7 @@ proc GC_addStack(starts: pointer) {.cdecl, importc.}
proc GC_removeStack(starts: pointer) {.cdecl, importc.} proc GC_removeStack(starts: pointer) {.cdecl, importc.}
proc GC_setCurrentStack(starts, pos: pointer) {.cdecl, importc.} proc GC_setCurrentStack(starts, pos: pointer) {.cdecl, importc.}
proc start*(c: proc(), stacksize: int=defaultStackSize) =
proc coroStart*(c: proc(), stacksize: int=coroDefaultStackSize) =
## Adds coroutine to event loop. It does not run immediately. ## Adds coroutine to event loop. It does not run immediately.
var coro = Coroutine() var coro = Coroutine()
coro.fn = c coro.fn = c
@ -47,9 +46,10 @@ proc coroStart*(c: proc(), stacksize: int=coroDefaultStackSize) =
coro.stack = alloc0(stacksize) coro.stack = alloc0(stacksize)
coro.stacksize = stacksize coro.stacksize = stacksize
coroutines.append(coro) coroutines.append(coro)
{.deprecated: [coroStart: start].}
{.push stackTrace: off.} {.push stackTrace: off.}
proc coroYield*(sleepTime: float=0) = proc sleep*(sleepTime: float=0) =
## Stops coroutine execution and resumes no sooner than after ``sleeptime`` seconds. ## Stops coroutine execution and resumes no sooner than after ``sleeptime`` seconds.
## Until then other coroutines are executed. ## Until then other coroutines are executed.
var oldFrame = getFrame() var oldFrame = getFrame()
@ -61,8 +61,9 @@ proc coroYield*(sleepTime: float=0) =
longjmp(mainCtx, 1) longjmp(mainCtx, 1)
setFrame(oldFrame) setFrame(oldFrame)
{.pop.} {.pop.}
{.deprecated: [coroYield: sleep].}
proc coroRun*() = proc run*() =
## Starts main event loop which exits when all coroutines exit. Calling this proc ## Starts main event loop which exits when all coroutines exit. Calling this proc
## starts execution of first coroutine. ## starts execution of first coroutine.
var node = coroutines.head var node = coroutines.head
@ -107,19 +108,20 @@ proc coroRun*() =
node = coroutines.head node = coroutines.head
else: else:
node = node.next node = node.next
{.deprecated: [coroRun: run].}
proc alive*(c: proc()): bool =
proc coroAlive*(c: proc()): bool =
## Returns ``true`` if coroutine has not returned, ``false`` otherwise. ## Returns ``true`` if coroutine has not returned, ``false`` otherwise.
for coro in items(coroutines): for coro in items(coroutines):
if coro.fn == c: if coro.fn == c:
return true return true
{.deprecated: [coroAlive: alive].}
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. ## Returns only after coroutine ``c`` has returned. ``interval`` is time in seconds how often.
while coroAlive(c): while alive(c):
coroYield interval sleep interval
{.deprecated: [coroWait: wait].}
when isMainModule: when isMainModule:
var stackCheckValue = 1100220033 var stackCheckValue = 1100220033
@ -128,18 +130,18 @@ when isMainModule:
proc c1() = proc c1() =
for i in 0 .. 3: for i in 0 .. 3:
echo "c1" echo "c1"
coroYield 0.05 sleep 0.05
echo "c1 exits" echo "c1 exits"
proc c2() = proc c2() =
for i in 0 .. 3: for i in 0 .. 3:
echo "c2" echo "c2"
coroYield 0.025 sleep 0.025
coroWait(c1) wait(c1)
echo "c2 exits" echo "c2 exits"
coroStart(c1) start(c1)
coroStart(c2) start(c2)
coroRun() run()
echo "done ", stackCheckValue echo "done ", stackCheckValue