Skip to content

Commit

Permalink
Add timeout to poll
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Oct 5, 2024
1 parent d4b3fa4 commit 663817a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
1 change: 1 addition & 0 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ patchFile("stdlib", "excpt", "patches/excpt")
patchFile("stdlib", "jsonutils", "src/misc/myjsonutils")
patchFile("stdlib", "tables", "patches/tables") # Patch tables.nim to remove exceptions
patchFile("chronos", "asyncengine", "patches/asyncengine") # Patch this to enable 0 timeout poll
patchFile("npeg", "codegen", "patches/codegen") # Patch this for proper gcsafety

# switches for debugging
# switch("d", "wasm3EnableStrace2")
Expand Down
15 changes: 7 additions & 8 deletions patches/asyncengine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getAsyncTimestamp*(a: Duration): auto {.inline.} =
result = cast[int32](res)
result += min(1, cast[int32](mid))

template processTimersGetTimeout(loop, timeout: untyped) =
template processTimersGetTimeout(loop: untyped) =
var lastFinish = curTime
while loop.timers.len > 0:
if loop.timers[0].function.function.isNil:
Expand Down Expand Up @@ -570,11 +570,11 @@ elif defined(windows):
if res.isErr():
raise newException(ValueError, osErrorMsg(res.error()))

proc poll*() =
proc poll*(timeout: int = 0) =
let loop = getThreadDispatcher()
var
curTime = Moment.now()
curTimeout = DWORD(0)
curTimeout = DWORD(timeout)
events: array[MaxEventsCount, osdefs.OVERLAPPED_ENTRY]

# On reentrant `poll` calls from `processCallbacks`, e.g., `waitFor`,
Expand All @@ -583,7 +583,7 @@ elif defined(windows):
processCallbacks(loop)

# Moving expired timers to `loop.callbacks` and calculate timeout
loop.processTimersGetTimeout(curTimeout)
loop.processTimersGetTimeout()

let networkEventsCount =
if isNil(loop.getQueuedCompletionStatusEx):
Expand Down Expand Up @@ -998,24 +998,23 @@ elif defined(macosx) or defined(freebsd) or defined(netbsd) or
## Remove process' watching using process' descriptor ``procHandle``.
removeProcess2(procHandle).tryGet()

proc poll*() {.gcsafe.} =
proc poll*(timeout: int = 0) {.gcsafe.} =
## Perform single asynchronous step.
let loop = getThreadDispatcher()
var curTime = Moment.now()
var curTimeout = 0

# On reentrant `poll` calls from `processCallbacks`, e.g., `waitFor`,
# complete pending work of the outer `processCallbacks` call.
# On non-reentrant `poll` calls, this only removes sentinel element.
processCallbacks(loop)

# Moving expired timers to `loop.callbacks` and calculate timeout.
loop.processTimersGetTimeout(curTimeout)
loop.processTimersGetTimeout()

# Processing IO descriptors and all hardware events.
let count =
block:
let res = loop.selector.selectInto2(curTimeout, loop.keys)
let res = loop.selector.selectInto2(timeout, loop.keys)
if res.isErr():
raiseOsDefect(res.error(), "poll(): Unable to get OS events")
res.get()
Expand Down
2 changes: 1 addition & 1 deletion src/text/language/lsp_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1174,4 +1174,4 @@ proc lspClientRunner*(client: LSPClient) {.thread, nimcall.} =

# todo: cleanup
while true:
poll()
poll(10)

0 comments on commit 663817a

Please sign in to comment.