Skip to content

Commit

Permalink
Problem: libzmq uses API not documented on learn.microsoft.com
Browse files Browse the repository at this point in the history
Solution: use WaitOnAddress instead of KeyedEvents APIs
  • Loading branch information
rzhao271 committed Apr 29, 2024
1 parent aa885c5 commit 8bf5e71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ if(ZMQ_HAVE_WINDOWS)
check_cxx_symbol_exists(GetAdaptersAddresses "winsock2.h;iphlpapi.h" HAVE_IPHLAPI)
check_cxx_symbol_exists(if_nametoindex "iphlpapi.h" HAVE_IF_NAMETOINDEX)

set(CMAKE_REQUIRED_LIBRARIES "synchronization.lib")
check_cxx_symbol_exists(WaitOnAddress "windows.h" HAVE_WAITONADDRESS)

set(CMAKE_REQUIRED_LIBRARIES "")
# TODO: This not the symbol we're looking for. What is the symbol?
check_library_exists(ws2 fopen "" HAVE_WS2)
Expand Down Expand Up @@ -596,6 +599,10 @@ if(WIN32 AND NOT CYGWIN)
if(NOT HAVE_IPHLAPI)
message(FATAL_ERROR "Cannot link to iphlapi")
endif()

if(NOT HAVE_WAITONADDRESS)
message(FATAL_ERROR "Cannot link to Synchronization")
endif()
endif()

if(NOT MSVC)
Expand Down Expand Up @@ -1505,6 +1512,10 @@ if(BUILD_SHARED)
target_link_libraries(libzmq iphlpapi)
endif()

if(HAVE_WAITONADDRESS)
target_link_libraries(libzmq synchronization)
endif()

if(RT_LIBRARY)
target_link_libraries(libzmq -lrt)
endif()
Expand Down Expand Up @@ -1555,6 +1566,10 @@ if(BUILD_STATIC)
target_link_libraries(libzmq-static iphlpapi)
endif()

if(HAVE_WAITONADDRESS)
target_link_libraries(libzmq-static synchronization)
endif()

if(RT_LIBRARY)
target_link_libraries(libzmq-static -lrt)
endif()
Expand Down
24 changes: 4 additions & 20 deletions external/wepoll/wepoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ typedef struct reflock {
volatile long state; /* 32-bit Interlocked APIs operate on `long` values. */
} reflock_t;

WEPOLL_INTERNAL int reflock_global_init(void);

WEPOLL_INTERNAL void reflock_init(reflock_t* reflock);
WEPOLL_INTERNAL void reflock_ref(reflock_t* reflock);
WEPOLL_INTERNAL void reflock_unref(reflock_t* reflock);
Expand Down Expand Up @@ -848,7 +846,7 @@ static BOOL CALLBACK init__once_callback(INIT_ONCE* once,

/* N.b. that initialization order matters here. */
if (ws_global_init() < 0 || nt_global_init() < 0 ||
reflock_global_init() < 0 || epoll_global_init() < 0)
epoll_global_init() < 0)
return FALSE;

init__done = true;
Expand Down Expand Up @@ -1523,31 +1521,17 @@ bool queue_is_enqueued(const queue_node_t* node) {
#define REFLOCK__DESTROY_MASK ((long) 0xf0000000UL)
#define REFLOCK__POISON ((long) 0x300dead0UL)

static HANDLE reflock__keyed_event = NULL;

int reflock_global_init(void) {
NTSTATUS status = NtCreateKeyedEvent(
&reflock__keyed_event, KEYEDEVENT_ALL_ACCESS, NULL, 0);
if (status != STATUS_SUCCESS)
return_set_error(-1, RtlNtStatusToDosError(status));
return 0;
}

void reflock_init(reflock_t* reflock) {
reflock->state = 0;
}

static void reflock__signal_event(void* address) {
NTSTATUS status =
NtReleaseKeyedEvent(reflock__keyed_event, address, FALSE, NULL);
if (status != STATUS_SUCCESS)
abort();
WakeByAddressSingle(address);
}

static void reflock__await_event(void* address) {
NTSTATUS status =
NtWaitForKeyedEvent(reflock__keyed_event, address, FALSE, NULL);
if (status != STATUS_SUCCESS)
BOOL status = WaitOnAddress(address, address, sizeof (void *), INFINITE);
if (status != TRUE)
abort();
}

Expand Down

0 comments on commit 8bf5e71

Please sign in to comment.