Skip to content

Commit

Permalink
Workaround for the high cpu usage issue in coroutines on linux (#5186)
Browse files Browse the repository at this point in the history
Fixes high cpu usage when all coroutines are asleep
  • Loading branch information
xomachine authored and Araq committed Jan 11, 2017
1 parent d04ca6e commit d356c37
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/pure/coro.nim
Expand Up @@ -66,14 +66,14 @@ 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
var minDelay: float = 0 var minDelay: int = 0 # in milliseconds
var frame: PFrame var frame: PFrame
while node != nil: while node != nil:
var coro = node.value var coro = node.value
current = coro current = coro
os.sleep(int(minDelay * 1000)) os.sleep(minDelay)


var remaining = coro.sleepTime - (epochTime() - coro.lastRun); var remaining = int((coro.sleepTime - (epochTime() - coro.lastRun)) * 1000)
if remaining <= 0: if remaining <= 0:
remaining = 0 remaining = 0
let res = setjmp(mainCtx) let res = setjmp(mainCtx)
Expand Down

0 comments on commit d356c37

Please sign in to comment.