diff --git a/CMakeLists.txt b/CMakeLists.txt index 9da5ccf..dfc32de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,7 +86,6 @@ include_directories( option(STATIC_KQUEUE "Enable to build libkqueue as static lib" OFF) if(STATIC_KQUEUE) message("-- building libkqueue as static lib") - add_definitions(-DMAKE_STATIC) add_library(kqueue STATIC ${SRC} ${INCL}) else() add_library(kqueue SHARED ${SRC} ${INCL}) diff --git a/src/common/kqueue.c b/src/common/kqueue.c index 782bff4..c05f4e1 100644 --- a/src/common/kqueue.c +++ b/src/common/kqueue.c @@ -27,7 +27,10 @@ int DEBUG_KQUEUE = 0; char *KQUEUE_DEBUG_IDENT = "KQ"; -#ifndef _WIN32 +#ifdef _WIN32 +static LONG kq_init_begin = 0; +static int kq_init_complete = 0; +#else pthread_mutex_t kq_mtx = PTHREAD_MUTEX_INITIALIZER; pthread_once_t kq_is_initialized = PTHREAD_ONCE_INIT; #endif @@ -56,7 +59,6 @@ get_fd_limit(void) static struct map *kqmap; - void libkqueue_init(void) { @@ -66,6 +68,14 @@ libkqueue_init(void) char *s = getenv("KQUEUE_DEBUG"); if (s != NULL && strlen(s) > 0) { DEBUG_KQUEUE = 1; + +#ifdef _WIN32 + /* Initialize the Winsock library */ + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) + abort(); +#endif + # if defined(_WIN32) && !defined(__GNUC__) /* Enable heap surveillance */ { @@ -83,6 +93,9 @@ libkqueue_init(void) if (knote_init() < 0) abort(); dbg_puts("library initialization complete"); +#ifdef _WIN32 + kq_init_complete = 1; +#endif } #if DEADWOOD @@ -116,7 +129,15 @@ kqueue(void) struct kqueue *kq; struct kqueue *tmp; -#ifndef _WIN32 +#ifdef _WIN32 + if (InterlockedCompareExchange(&kq_init_begin, 0, 1) == 0) { + libkqueue_init(); + } else { + while (kq_init_complete == 0) { + sleep(1); + } + } +#else (void) pthread_mutex_lock(&kq_mtx); (void) pthread_once(&kq_is_initialized, libkqueue_init); (void) pthread_mutex_unlock(&kq_mtx); diff --git a/src/windows/platform.c b/src/windows/platform.c index 8fb7cdd..1f8c04e 100644 --- a/src/windows/platform.c +++ b/src/windows/platform.c @@ -43,36 +43,6 @@ const struct kqueue_vtable kqops = { windows_filter_free, }; -#ifndef MAKE_STATIC - -BOOL WINAPI DllMain( - HINSTANCE self, - DWORD reason, - LPVOID unused) -{ - switch (reason) { - case DLL_PROCESS_ATTACH: - -#if XXX - //move to EVFILT_READ? - if (WSAStartup(MAKEWORD(2,2), NULL) != 0) - return (FALSE); -#endif - libkqueue_init(); - break; - - case DLL_PROCESS_DETACH: -#if XXX - WSACleanup(); -#endif - break; - } - - return (TRUE); -} - -#endif - int windows_kqueue_init(struct kqueue *kq) { diff --git a/test/main.c b/test/main.c index e1a5e03..110d64a 100644 --- a/test/main.c +++ b/test/main.c @@ -188,7 +188,7 @@ test_harness(struct unit_test tests[], int iterations, int concurrency) abort(); ctx->iteration = n++; ctx->kqfd = kqfd; - memcpy(&ctx->tests, tests, sizeof(ctx->tests)); //FIXME: invalid read + memcpy(&ctx->tests, tests, sizeof(*tests)); ctx->iterations = iterations; ctx->concurrency = concurrency; @@ -254,10 +254,6 @@ main(int argc, char **argv) char *arg; int match; -#ifdef MAKE_STATIC - libkqueue_init(); -#endif - #ifdef _WIN32 /* Initialize the Winsock library */ WSADATA wsaData; @@ -265,10 +261,11 @@ main(int argc, char **argv) err(1, "WSAStartup failed"); #endif -/* Windows does not provide a POSIX-compatible getopt */ -#ifndef _WIN32 iterations = 1; concurrency = 1; + +/* Windows does not provide a POSIX-compatible getopt */ +#ifndef _WIN32 while ((c = getopt (argc, argv, "hc:n:")) != -1) { switch (c) { case 'c':