Skip to content

Commit

Permalink
Still use HackDispatcher when using the old runtime since it doesn't …
Browse files Browse the repository at this point in the history
…have any problems there

This is because we need to access the handles which wasn't exported til recently so 1.2.12 needs the hack. But if you are using the new runtime then you are likely using a newer version of Nim
  • Loading branch information
ire4ever1190 committed Jun 5, 2023
1 parent dab1b6d commit 59e1c9a
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions asynctools/asyncipc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ elif defined(windows):
ioPort: Handle
handles: HashSet[AsyncFD]
HackDispatcher = ptr HackDispatcherImpl
Dispatcher = HackDispatcher | PDispatcher

proc openEvent(dwDesiredAccess: Dword, bInheritHandle: WINBOOL,
lpName: WideCString): Handle
Expand All @@ -142,8 +143,22 @@ elif defined(windows):
proc interlockedAnd(a: ptr int32, b: int32)
{.importc: "_InterlockedAnd", header: "intrin.h".}

proc getCurrentDispatcher(): HackDispatcher =
result = cast[HackDispatcher](getGlobalDispatcher())
proc getCurrentDispatcher(): Dispatcher =
when defined(nimv2):
# New runtime will run into segfault if using HackDispatcher, but old versions of Nim require
# HackDispatcher so we can access some internal fields.
# If using the new runtime then likely using a new enough Nim version that the needed fields
# are exported
getGlobalDispatcher()
else:
cast[HackDispatcher](getGlobalDispatcher())

template getIoHandler(p: Dispatcher): Handle =
## Returns the IO handle for a dispatcher.
when p is PDispatcher:
p.getIoHandler()
else:
p.ioPort

proc `$`*(ipc: AsyncIpc): string =
if ipc.handleMap == Handle(0):
Expand Down Expand Up @@ -266,7 +281,7 @@ elif defined(windows):
interlockedOr(cast[ptr int32](data), 1)

if register:
let p = getGlobalDispatcher()
let p = getCurrentDispatcher()
p.handles.incl(AsyncFD(eventChange))

result = AsyncIpcHandle(
Expand All @@ -284,7 +299,7 @@ elif defined(windows):
interlockedAnd(cast[ptr int32](ipch.data), not(1))

if unregister:
let p = getGlobalDispatcher()
let p = getCurrentDispatcher()
p.handles.excl(AsyncFD(ipch.eventChange))

if unmapViewOfFile(ipch.data) == 0:
Expand Down Expand Up @@ -321,7 +336,7 @@ elif defined(windows):
template registerWaitableChange(ipc: AsyncIpcHandle, pcd, handleCallback) =
let p = getCurrentDispatcher()
var flags = (WT_EXECUTEINWAITTHREAD or WT_EXECUTEONLYONCE).Dword
pcd.ioPort = cast[Handle](p.ioPort)
pcd.ioPort = p.getIoHandler()
pcd.handleFd = AsyncFD(ipc.eventChange)
var ol = PCustomOverlapped()
GC_ref(ol)
Expand Down

0 comments on commit 59e1c9a

Please sign in to comment.