From a1a17d06713727d97810cad291e29dd7c672738f Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Tue, 6 Jun 2023 07:43:29 +1000 Subject: [PATCH] Few fixes to get `asyncipc` working with devel on windows (#44) * Multiple fixes to get asyncipc working with devel on windows - First was to make sure the object is first before casting to pointer so that we are actually accessing the data (On devel this calls a converter which returns the type - I was running into a nil access error on ORC which I fixed by using the stdlib getGlobalDispatcher() instead * Still use HackDispatcher when using the old runtime since it doesn't 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 * Just define getIoHandler for HackDispatcher instead of using when Simplifies it and feels less hacky Remove the type that Araq says sn't required --- asynctools/asyncipc.nim | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) 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)