From 7d9a748fcdb97ace2c30fb8a8b65a3ac4a8661a0 Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Tue, 25 Aug 2015 16:33:44 +0200 Subject: [PATCH 1/3] removed coro prefixes from coro.nim --- lib/pure/coro.nim | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/pure/coro.nim b/lib/pure/coro.nim index 6ef5f6f54b..239700c3c1 100644 --- a/lib/pure/coro.nim +++ b/lib/pure/coro.nim @@ -15,7 +15,7 @@ import macros import arch import lists -const coroDefaultStackSize = 512 * 1024 +const defaultStackSize = 512 * 1024 type Coroutine = ref object @@ -38,8 +38,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 @@ -47,9 +46,10 @@ proc coroStart*(c: proc(), stacksize: int=coroDefaultStackSize) = coro.stack = alloc0(stacksize) coro.stacksize = stacksize coroutines.append(coro) +{.deprecated: [coroStart: start].} {.push stackTrace: off.} -proc coroYield*(sleepTime: float=0) = +proc sleep*(sleepTime: float=0) = ## Stops coroutine execution and resumes no sooner than after ``sleeptime`` seconds. ## Until then other coroutines are executed. var oldFrame = getFrame() @@ -61,8 +61,9 @@ proc coroYield*(sleepTime: float=0) = longjmp(mainCtx, 1) setFrame(oldFrame) {.pop.} +{.deprecated: [coroYield: sleep].} -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 @@ -107,19 +108,20 @@ proc coroRun*() = node = coroutines.head else: node = node.next +{.deprecated: [coroRun: run].} - -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 +{.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. - while coroAlive(c): - coroYield interval - + while alive(c): + sleep interval +{.deprecated: [coroWait: wait].} when isMainModule: var stackCheckValue = 1100220033 @@ -128,18 +130,18 @@ when isMainModule: proc c1() = for i in 0 .. 3: echo "c1" - coroYield 0.05 + sleep 0.05 echo "c1 exits" proc c2() = for i in 0 .. 3: echo "c2" - coroYield 0.025 - coroWait(c1) + sleep 0.025 + wait(c1) echo "c2 exits" - coroStart(c1) - coroStart(c2) - coroRun() + start(c1) + start(c2) + run() echo "done ", stackCheckValue From d876960b9d56144ff6ee9cdabd5af3686b02c920 Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Tue, 25 Aug 2015 23:52:14 +0200 Subject: [PATCH 2/3] removed coro deprecations --- lib/pure/coro.nim | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/pure/coro.nim b/lib/pure/coro.nim index 239700c3c1..f3780a24c4 100644 --- a/lib/pure/coro.nim +++ b/lib/pure/coro.nim @@ -17,7 +17,6 @@ import lists const defaultStackSize = 512 * 1024 - type Coroutine = ref object # prev: ptr Coroutine # next: ptr Coroutine @@ -46,7 +45,6 @@ proc start*(c: proc(), stacksize: int=defaultStackSize) = coro.stack = alloc0(stacksize) coro.stacksize = stacksize coroutines.append(coro) -{.deprecated: [coroStart: start].} {.push stackTrace: off.} proc sleep*(sleepTime: float=0) = @@ -61,7 +59,6 @@ proc sleep*(sleepTime: float=0) = longjmp(mainCtx, 1) setFrame(oldFrame) {.pop.} -{.deprecated: [coroYield: sleep].} proc run*() = ## Starts main event loop which exits when all coroutines exit. Calling this proc @@ -108,20 +105,17 @@ proc run*() = node = coroutines.head else: node = node.next -{.deprecated: [coroRun: run].} 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 -{.deprecated: [coroAlive: alive].} proc wait*(c: proc(), interval=0.01) = ## Returns only after coroutine ``c`` has returned. ``interval`` is time in seconds how often. while alive(c): sleep interval -{.deprecated: [coroWait: wait].} when isMainModule: var stackCheckValue = 1100220033 From 24655ecd52b9d68bfc9123f3883f7e2951db8aac Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Sun, 30 Aug 2015 10:24:13 +0200 Subject: [PATCH 3/3] coro.sleep -> suspend --- lib/pure/coro.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pure/coro.nim b/lib/pure/coro.nim index f3780a24c4..b2a7692bb3 100644 --- a/lib/pure/coro.nim +++ b/lib/pure/coro.nim @@ -47,13 +47,13 @@ proc start*(c: proc(), stacksize: int=defaultStackSize) = coroutines.append(coro) {.push stackTrace: off.} -proc sleep*(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. 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) @@ -115,7 +115,7 @@ proc alive*(c: proc()): bool = proc wait*(c: proc(), interval=0.01) = ## Returns only after coroutine ``c`` has returned. ``interval`` is time in seconds how often. while alive(c): - sleep interval + suspend interval when isMainModule: var stackCheckValue = 1100220033 @@ -124,14 +124,14 @@ when isMainModule: proc c1() = for i in 0 .. 3: echo "c1" - sleep 0.05 + suspend 0.05 echo "c1 exits" proc c2() = for i in 0 .. 3: echo "c2" - sleep 0.025 + suspend 0.025 wait(c1) echo "c2 exits"