@@ -258,7 +258,7 @@ when defined(windows) or defined(nimdoc):
258
258
handles: HashSet [AsyncFD ]
259
259
vd: VirtualEventDispatcher
260
260
261
- VirtualEventDispatcher = object
261
+ VirtualEventDispatcher = ref object
262
262
virtualHandles: Table [VirtualFD , VirtualAsyncEvent ] # pseudo handles for custom AsyncEvents.
263
263
nextVirtualHandle: VirtualFD
264
264
virtualMuxHandle: AsyncEvent # all the virtual handles get multiplexed through a single real handle.
@@ -289,7 +289,7 @@ when defined(windows) or defined(nimdoc):
289
289
triggered: bool
290
290
when compileOption (" threads" ):
291
291
eventLock: RLock
292
- p: PDispatcher
292
+ vd: VirtualEventDispatcher
293
293
vFD: VirtualFD
294
294
cb: Callback
295
295
VirtualAsyncEvent * = ptr VirtualAsyncEventImpl
@@ -311,6 +311,7 @@ when defined(windows) or defined(nimdoc):
311
311
result .handles = initSet [AsyncFD ]()
312
312
result .timers.newHeapQueue ()
313
313
result .callbacks = initDeque [proc ()](64 )
314
+ result .vd = new VirtualEventDispatcher
314
315
result .vd.virtualHandles = initTable [VirtualFD , VirtualAsyncEvent ]()
315
316
316
317
var gDisp{.threadvar .}: owned PDispatcher # # Global dispatcher
@@ -1140,7 +1141,7 @@ else:
1140
1141
triggered: bool
1141
1142
when compileOption (" threads" ):
1142
1143
eventLock: RLock
1143
- p: PDispatcher
1144
+ vd: VirtualEventDispatcher
1144
1145
vFD: VirtualFD
1145
1146
cb: Callback
1146
1147
VirtualAsyncEvent * = ptr VirtualAsyncEventImpl
@@ -1149,7 +1150,7 @@ else:
1149
1150
selector: Selector [AsyncData ]
1150
1151
vd: VirtualEventDispatcher
1151
1152
1152
- VirtualEventDispatcher = object
1153
+ VirtualEventDispatcher = ref object
1153
1154
virtualHandles: Table [VirtualFD , VirtualAsyncEvent ] # pseudo handles for custom AsyncEvents.
1154
1155
nextVirtualHandle: VirtualFD
1155
1156
virtualMuxHandle: AsyncEvent # all the virtual handles get multiplexed through a single real handle.
@@ -1173,6 +1174,7 @@ else:
1173
1174
result .selector = newSelector [AsyncData ]()
1174
1175
result .timers.newHeapQueue ()
1175
1176
result .callbacks = initDeque [proc ()](InitDelayedCallbackListSize )
1177
+ result .vd = new VirtualEventDispatcher
1176
1178
result .vd.virtualHandles = initTable [VirtualFD , VirtualAsyncEvent ]()
1177
1179
1178
1180
var gDisp{.threadvar .}: owned PDispatcher # # Global dispatcher
@@ -1907,30 +1909,30 @@ proc send*(socket: AsyncFD, data: string,
1907
1909
1908
1910
return retFuture
1909
1911
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
1916
1918
1917
1919
proc unregister * (ev: VirtualAsyncEvent ) =
1918
1920
# # Unregisters event ``ev``.
1919
1921
doAssert (ev.vFD != InvalidVirtualFD , " Event is not registered in the queue!" )
1920
1922
1921
- let oldP = ev.p
1923
+ let vd = ev.vd
1922
1924
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
1925
1927
ev.cb = nil
1926
1928
ev.vFD = InvalidVirtualFD
1927
1929
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
1930
1932
# The main reason we need this is to make
1931
1933
# hasPendingOperations still work
1932
1934
# without modifying the ioselector code.
1933
- deInitVirtualEventDispatcher (oldP)
1935
+ vd.close
1934
1936
1935
1937
proc initVirtualEventDispatcher (p: PDispatcher ) =
1936
1938
p.vd.virtualMuxHandle = newAsyncEvent ()
@@ -1978,7 +1980,7 @@ proc trigger*(ev: VirtualAsyncEvent) =
1978
1980
ev.triggered = true
1979
1981
1980
1982
# send the signal to wake up the dispatcher thread.
1981
- trigger (ev.p. vd.virtualMuxHandle)
1983
+ trigger (ev.vd.virtualMuxHandle)
1982
1984
1983
1985
proc addEvent * (ev: VirtualAsyncEvent , cb: Callback ) =
1984
1986
# # Registers callback ``cb`` to be called when ``ev`` will be signaled
@@ -1997,7 +1999,7 @@ proc addEvent*(ev: VirtualAsyncEvent, cb: Callback) =
1997
1999
ev.vFD = p.vd.nextVirtualHandle
1998
2000
p.vd.nextVirtualHandle = VirtualFD (int (p.vd.nextVirtualHandle) + 1 )
1999
2001
p.vd.virtualHandles[ev.vFD] = ev
2000
- ev.p = p
2002
+ ev.vd = p.vd
2001
2003
2002
2004
proc close * (ev: VirtualAsyncEvent ) =
2003
2005
# # Closes event ``ev``.
0 commit comments