Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1e5a071

Browse files
committedOct 14, 2019
Changes based on code review from @dom96
- changed deInitVirtualEventDispatcher(p: PDispatcher) to close(vd: VirtualEventDispatcher) - changed VirtualEventDispatcher to a ref object and cleaned up some code related to this change
1 parent 264bfee commit 1e5a071

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed
 

‎lib/pure/asyncdispatch.nim

+20-18
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ when defined(windows) or defined(nimdoc):
258258
handles: HashSet[AsyncFD]
259259
vd: VirtualEventDispatcher
260260

261-
VirtualEventDispatcher = object
261+
VirtualEventDispatcher = ref object
262262
virtualHandles: Table[VirtualFD, VirtualAsyncEvent] # pseudo handles for custom AsyncEvents.
263263
nextVirtualHandle: VirtualFD
264264
virtualMuxHandle: AsyncEvent # all the virtual handles get multiplexed through a single real handle.
@@ -289,7 +289,7 @@ when defined(windows) or defined(nimdoc):
289289
triggered: bool
290290
when compileOption("threads"):
291291
eventLock: RLock
292-
p: PDispatcher
292+
vd: VirtualEventDispatcher
293293
vFD: VirtualFD
294294
cb: Callback
295295
VirtualAsyncEvent* = ptr VirtualAsyncEventImpl
@@ -311,6 +311,7 @@ when defined(windows) or defined(nimdoc):
311311
result.handles = initSet[AsyncFD]()
312312
result.timers.newHeapQueue()
313313
result.callbacks = initDeque[proc ()](64)
314+
result.vd = new VirtualEventDispatcher
314315
result.vd.virtualHandles = initTable[VirtualFD, VirtualAsyncEvent]()
315316

316317
var gDisp{.threadvar.}: owned PDispatcher ## Global dispatcher
@@ -1140,7 +1141,7 @@ else:
11401141
triggered: bool
11411142
when compileOption("threads"):
11421143
eventLock: RLock
1143-
p: PDispatcher
1144+
vd: VirtualEventDispatcher
11441145
vFD: VirtualFD
11451146
cb: Callback
11461147
VirtualAsyncEvent* = ptr VirtualAsyncEventImpl
@@ -1149,7 +1150,7 @@ else:
11491150
selector: Selector[AsyncData]
11501151
vd: VirtualEventDispatcher
11511152

1152-
VirtualEventDispatcher = object
1153+
VirtualEventDispatcher = ref object
11531154
virtualHandles: Table[VirtualFD, VirtualAsyncEvent] # pseudo handles for custom AsyncEvents.
11541155
nextVirtualHandle: VirtualFD
11551156
virtualMuxHandle: AsyncEvent # all the virtual handles get multiplexed through a single real handle.
@@ -1173,6 +1174,7 @@ else:
11731174
result.selector = newSelector[AsyncData]()
11741175
result.timers.newHeapQueue()
11751176
result.callbacks = initDeque[proc ()](InitDelayedCallbackListSize)
1177+
result.vd = new VirtualEventDispatcher
11761178
result.vd.virtualHandles = initTable[VirtualFD, VirtualAsyncEvent]()
11771179

11781180
var gDisp{.threadvar.}: owned PDispatcher ## Global dispatcher
@@ -1907,30 +1909,30 @@ proc send*(socket: AsyncFD, data: string,
19071909

19081910
return retFuture
19091911

1910-
proc deInitVirtualEventDispatcher(p: PDispatcher) =
1911-
assert p.vd.virtualHandles.len == 0, "Cannot de-Init Virtual Event Dispacter. There are still Pending Events."
1912-
p.vd.virtualMuxHandle.unregister()
1913-
p.vd.virtualMuxHandle.close()
1914-
p.vd.virtualMuxHandle = nil
1915-
p.vd.nextVirtualHandle = 0.VirtualFD
1912+
proc close(vd: VirtualEventDispatcher) =
1913+
assert vd.virtualHandles.len == 0, "Cannot de-Init Virtual Event Dispacter. There are still Pending Events."
1914+
vd.virtualMuxHandle.unregister()
1915+
vd.virtualMuxHandle.close()
1916+
vd.virtualMuxHandle = nil
1917+
vd.nextVirtualHandle = 0.VirtualFD
19161918

19171919
proc unregister*(ev: VirtualAsyncEvent) =
19181920
## Unregisters event ``ev``.
19191921
doAssert(ev.vFD != InvalidVirtualFD, "Event is not registered in the queue!")
19201922

1921-
let oldP = ev.p
1923+
let vd = ev.vd
19221924
withLockIfThreads ev.eventLock:
1923-
ev.p.vd.virtualHandles.del ev.vFD
1924-
ev.p = nil
1925+
ev.vd.virtualHandles.del ev.vFD
1926+
ev.vd = nil
19251927
ev.cb = nil
19261928
ev.vFD = InvalidVirtualFD
19271929

1928-
if oldP.vd.virtualHandles.len == 0:
1929-
# lazy de-init the physical event with the Dispatcher
1930+
if vd.virtualHandles.len == 0:
1931+
# lazy close the physical event with the Dispatcher
19301932
# The main reason we need this is to make
19311933
# hasPendingOperations still work
19321934
# without modifying the ioselector code.
1933-
deInitVirtualEventDispatcher(oldP)
1935+
vd.close
19341936

19351937
proc initVirtualEventDispatcher(p: PDispatcher) =
19361938
p.vd.virtualMuxHandle = newAsyncEvent()
@@ -1978,7 +1980,7 @@ proc trigger*(ev: VirtualAsyncEvent) =
19781980
ev.triggered = true
19791981

19801982
# send the signal to wake up the dispatcher thread.
1981-
trigger(ev.p.vd.virtualMuxHandle)
1983+
trigger(ev.vd.virtualMuxHandle)
19821984

19831985
proc addEvent*(ev: VirtualAsyncEvent, cb: Callback) =
19841986
## Registers callback ``cb`` to be called when ``ev`` will be signaled
@@ -1997,7 +1999,7 @@ proc addEvent*(ev: VirtualAsyncEvent, cb: Callback) =
19971999
ev.vFD = p.vd.nextVirtualHandle
19982000
p.vd.nextVirtualHandle = VirtualFD(int(p.vd.nextVirtualHandle) + 1)
19992001
p.vd.virtualHandles[ev.vFD] = ev
2000-
ev.p = p
2002+
ev.vd = p.vd
20012003

20022004
proc close*(ev: VirtualAsyncEvent) =
20032005
## Closes event ``ev``.

0 commit comments

Comments
 (0)
Please sign in to comment.