diff options
author | Anatoly Galiulin <galiulin.anatoly@gmail.com> | 2016-12-29 11:22:50 +0700 |
---|---|---|
committer | Anatoly Galiulin <galiulin.anatoly@gmail.com> | 2016-12-29 17:27:03 +0700 |
commit | 8e71949b785ca2b2bc61d0f95c5ac88fa87621fe (patch) | |
tree | 15751fe701a510f50514142d60b6814519a7e533 /lib | |
parent | 46e4b4854e9cb564950e7802aaaaf1a2c8c8cab9 (diff) | |
download | Nim-8e71949b785ca2b2bc61d0f95c5ac88fa87621fe.tar.gz |
Add pending operations presence check function, fixes #5155
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/asyncdispatch.nim | 15 | ||||
-rw-r--r-- | lib/upcoming/asyncdispatch.nim | 12 |
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index 1367bc411..b72596060 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -254,8 +254,14 @@ when defined(windows) or defined(nimdoc): "Operation performed on a socket which has not been registered with" & " the dispatcher yet.") + proc hasPendingOperations*(): bool = + ## Returns `true` if the global dispatcher has pending operations. + let p = getGlobalDispatcher() + p.handles.len != 0 or p.timers.len != 0 or p.callbacks.len != 0 + proc poll*(timeout = 500) = - ## Waits for completion events and processes them. + ## Waits for completion events and processes them. Raises ``ValueError`` + ## if there are no pending operations. let p = getGlobalDispatcher() if p.handles.len == 0 and p.timers.len == 0 and p.callbacks.len == 0: raise newException(ValueError, @@ -1056,8 +1062,15 @@ else: newCBs.add(cb) callbacks = newCBs & callbacks + proc hasPendingOperations*(): bool = + let p = getGlobalDispatcher() + p.selector.len != 0 or p.timers.len != 0 or p.callbacks.len != 0 + proc poll*(timeout = 500) = let p = getGlobalDispatcher() + if p.selector.len == 0 and p.timers.len == 0 and p.callbacks.len == 0: + raise newException(ValueError, + "No handles or timers registered in dispatcher.") if p.selector.len > 0: for info in p.selector.select(p.adjustedTimeout(timeout)): diff --git a/lib/upcoming/asyncdispatch.nim b/lib/upcoming/asyncdispatch.nim index ca5c1f64c..78c7afffc 100644 --- a/lib/upcoming/asyncdispatch.nim +++ b/lib/upcoming/asyncdispatch.nim @@ -231,8 +231,14 @@ when defined(windows) or defined(nimdoc): "Operation performed on a socket which has not been registered with" & " the dispatcher yet.") + proc hasPendingOperations*(): bool = + ## Returns `true` if the global dispatcher has pending operations. + let p = getGlobalDispatcher() + p.handles.len != 0 or p.timers.len != 0 or p.callbacks.len != 0 + proc poll*(timeout = 500) = - ## Waits for completion events and processes them. + ## Waits for completion events and processes them. Raises ``ValueError`` + ## if there are no pending operations. let p = getGlobalDispatcher() if p.handles.len == 0 and p.timers.len == 0 and p.callbacks.len == 0: raise newException(ValueError, @@ -1182,6 +1188,10 @@ else: raise newException(ValueError, "File descriptor not registered.") p.selector.updateHandle(fd.SocketHandle, newEvents) + proc hasPendingOperations*(): bool = + let p = getGlobalDispatcher() + not p.selector.isEmpty() or p.timers.len != 0 or p.callbacks.len != 0 + proc poll*(timeout = 500) = var keys: array[64, ReadyKey[AsyncData]] |