summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorMichał Zieliński <michal@zielinscy.org.pl>2017-07-06 13:24:33 +0200
committerMichał Zieliński <michal@zielinscy.org.pl>2017-07-06 13:27:42 +0200
commit6bdd00db0ed97595fb78930976312fc987d95397 (patch)
tree22c99b9941612d5274df691b9b44cb75034ee26e /lib
parent797690ba3ff415a457499ddf0edda24c31644b1d (diff)
downloadNim-6bdd00db0ed97595fb78930976312fc987d95397.tar.gz
asyncdispatch: add callSoon getter/setter, renames
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncdispatch.nim10
-rw-r--r--lib/pure/asyncfutures.nim17
-rw-r--r--lib/upcoming/asyncdispatch.nim10
3 files changed, 24 insertions, 13 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 2a412d967..0ce6115ad 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -191,9 +191,9 @@ proc adjustedTimeout(p: PDispatcherBase, timeout: int): int {.inline.} =
 
 proc callSoon(cbproc: proc ()) {.gcsafe.}
 
-proc initGlobalDispatcher =
-  if asyncfutures.callSoonProc == nil:
-    asyncfutures.callSoonProc = callSoon
+proc initCallSoonProc =
+  if asyncfutures.getCallSoonProc() == nil:
+    asyncfutures.setCallSoonProc(callSoon)
 
 when defined(windows) or defined(nimdoc):
   import winlean, sets, hashes
@@ -247,7 +247,7 @@ when defined(windows) or defined(nimdoc):
     if not gDisp.isNil:
       assert gDisp.callbacks.len == 0
     gDisp = disp
-    initGlobalDispatcher()
+    initCallSoonProc()
 
   proc getGlobalDispatcher*(): PDispatcher =
     if gDisp.isNil:
@@ -944,7 +944,7 @@ else:
     if not gDisp.isNil:
       assert gDisp.callbacks.len == 0
     gDisp = disp
-    initGlobalDispatcher()
+    initCallSoonProc()
 
   proc getGlobalDispatcher*(): PDispatcher =
     if gDisp.isNil:
diff --git a/lib/pure/asyncfutures.nim b/lib/pure/asyncfutures.nim
index 2d86d3d8d..8b27e7104 100644
--- a/lib/pure/asyncfutures.nim
+++ b/lib/pure/asyncfutures.nim
@@ -1,5 +1,3 @@
-include "system/inclrtl"
-
 import os, tables, strutils, times, heapqueue, options, deques
 
 # TODO: This shouldn't need to be included, but should ideally be exported.
@@ -34,9 +32,22 @@ type
 when not defined(release):
   var currentID = 0
 
-var callSoonProc* {.threadvar.}: (proc(cbproc: proc ()) {.gcsafe.})
+var callSoonProc {.threadvar.}: (proc(cbproc: proc ()) {.gcsafe.})
+
+proc getCallSoonProc*(): (proc(cbproc: proc ()) {.gcsafe.}) =
+  ## Get current implementation of ``callSoon``.
+  return callSoonProc
+
+proc setCallSoonProc*(p: (proc(cbproc: proc ()) {.gcsafe.})) =
+  ## Change current implementation of ``callSoon``. This is normally called when dispatcher from ``asyncdispatcher`` is initialized.
+  callSoonProc = p
 
 proc callSoon*(cbproc: proc ()) =
+  ## Call ``cbproc`` "soon".
+  ##
+  ## If async dispatcher is running, ``cbproc`` will be executed during next dispatcher tick.
+  ##
+  ## If async dispatcher is not running, ``cbproc`` will be executed immediately.
   if callSoonProc == nil:
     # Loop not initialized yet. Call the function directly to allow setup code to use futures.
     cbproc()
diff --git a/lib/upcoming/asyncdispatch.nim b/lib/upcoming/asyncdispatch.nim
index d90ca6534..9e9668aad 100644
--- a/lib/upcoming/asyncdispatch.nim
+++ b/lib/upcoming/asyncdispatch.nim
@@ -163,9 +163,9 @@ proc adjustedTimeout(p: PDispatcherBase, timeout: int): int {.inline.} =
 
 proc callSoon(cbproc: proc ()) {.gcsafe.}
 
-proc initGlobalDispatcher =
-  if asyncfutures.callSoonProc == nil:
-    asyncfutures.callSoonProc = callSoon
+proc initCallSoonProc =
+  if asyncfutures.getCallSoonProc() == nil:
+    asyncfutures.setCallSoonProc(callSoon)
 
 when defined(windows) or defined(nimdoc):
   import winlean, sets, hashes
@@ -225,7 +225,7 @@ when defined(windows) or defined(nimdoc):
     if not gDisp.isNil:
       assert gDisp.callbacks.len == 0
     gDisp = disp
-    initGlobalDispatcher()
+    initCallSoonProc()
 
   proc getGlobalDispatcher*(): PDispatcher =
     if gDisp.isNil:
@@ -1094,7 +1094,7 @@ else:
     if not gDisp.isNil:
       assert gDisp.callbacks.len == 0
     gDisp = disp
-    initGlobalDispatcher()
+    initCallSoonProc()
 
   proc getGlobalDispatcher*(): PDispatcher =
     if gDisp.isNil: