Skip to content

Commit

Permalink
Few fixes to get asyncipc working with devel on windows (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ire4ever1190 committed Jun 5, 2023
1 parent 84ced6d commit a1a17d0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions asynctools/asyncipc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a1a17d0

Please sign in to comment.