diff --git a/asynctools/asyncipc.nim b/asynctools/asyncipc.nim index 2ec7905..8b4b67c 100644 --- a/asynctools/asyncipc.nim +++ b/asynctools/asyncipc.nim @@ -142,8 +142,19 @@ elif defined(windows): proc interlockedAnd(a: ptr int32, b: int32) {.importc: "_InterlockedAnd", header: "intrin.h".} - proc getCurrentDispatcher(): HackDispatcher = - result = cast[HackDispatcher](getGlobalDispatcher()) + proc getCurrentDispatcher(): auto = + 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: HackDispatcher): Handle = + ## Returns the IO handle for a dispatcher. + p.ioPort proc `$`*(ipc: AsyncIpc): string = if ipc.handleMap == Handle(0): @@ -183,11 +194,10 @@ elif defined(windows): let mapSize = size + mapMinSize doAssert(size > mapMinSize) - let handleMap = createFileMappingW(INVALID_HANDLE_VALUE, cast[pointer](addr sa), PAGE_READWRITE, 0, mapSize.Dword, - cast[pointer](mapName)) + cast[pointer](WideCString(mapName))) if handleMap == 0: raiseOSError(osLastError()) var eventChange = createEvent(addr sa, 0, 0, addr nameChange[0]) @@ -322,7 +332,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)