From b0fe8398e821ec55feff2d51b62d2a154a8bf371 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Mon, 23 Sep 2019 20:24:26 +0300 Subject: [PATCH] Fix behavior which was agreed in #46. --- chronos/asyncloop.nim | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/chronos/asyncloop.nim b/chronos/asyncloop.nim index 3d7d5f06..515a446a 100644 --- a/chronos/asyncloop.nim +++ b/chronos/asyncloop.nim @@ -319,6 +319,11 @@ when defined(windows) or defined(nimdoc): sizeof(GUID).DWORD, addr fun, sizeof(pointer).DWORD, addr bytesRet, nil, nil) == 0 + proc globalInit() = + var wsa: WSAData + if wsaStartup(0x0202'i16, addr wsa) != 0: + raiseOSError(osLastError()) + proc initAPI(loop: PDispatcher) = var WSAID_TRANSMITFILE = GUID( @@ -326,10 +331,6 @@ when defined(windows) or defined(nimdoc): D4: [0x95'i8, 0xca'i8, 0x00'i8, 0x80'i8, 0x5f'i8, 0x48'i8, 0xa1'i8, 0x92'i8]) - var wsa: WSAData - if wsaStartup(0x0202'i16, addr wsa) != 0: - raiseOSError(osLastError()) - let sock = winlean.socket(winlean.AF_INET, 1, 6) if sock == INVALID_SOCKET: raiseOSError(osLastError()) @@ -498,10 +499,13 @@ elif unixPlatform: proc `==`*(x, y: AsyncFD): bool {.borrow.} - proc initAPI(disp: PDispatcher) = + proc globalInit() = # We are ignoring SIGPIPE signal, because we are working with EPIPE. posix.signal(cint(SIGPIPE), SIG_IGN) + proc initAPI(disp: PDispatcher) = + discard + proc newDispatcher*(): PDispatcher = ## Create new dispatcher. new result @@ -714,6 +718,7 @@ elif unixPlatform: else: proc initAPI() = discard + proc globalInit() = discard proc addTimer*(at: Moment, cb: CallbackFunc, udata: pointer = nil) = ## Arrange for the callback ``cb`` to be called at the given absolute @@ -934,3 +939,6 @@ proc getTracker*(id: string): TrackerBase = ## Get ``tracker`` from current thread dispatcher using identifier ``id``. let loop = getGlobalDispatcher() result = loop.trackers.getOrDefault(id, nil) + +# Perform global per-module initialization. +globalInit()