From ebd50d0cfa3664d454ffdf246fcd228c3b370a11 Mon Sep 17 00:00:00 2001 From: mattn Date: Mon, 2 Mar 2020 15:21:58 +0900 Subject: Build on Windows/mingw64 (#6) Support Windows/mingw64 build --- src/threadpool-pthreads.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/threadpool-pthreads.c b/src/threadpool-pthreads.c index 6c6a6d4..2cb834d 100644 --- a/src/threadpool-pthreads.c +++ b/src/threadpool-pthreads.c @@ -31,6 +31,12 @@ #endif #endif +#ifdef _WIN32 +# define NOMINMAX +# include +# include +#endif + /* Dependencies */ #include @@ -424,6 +430,11 @@ static struct pthreadpool* pthreadpool_allocate(size_t threads_count) { if (threadpool == NULL) { return NULL; } + #elif defined(_WIN32) + threadpool = _aligned_malloc(threadpool_size, PTHREADPOOL_CACHELINE_SIZE); + if (threadpool == NULL) { + return NULL; + } #else if (posix_memalign((void**) &threadpool, PTHREADPOOL_CACHELINE_SIZE, threadpool_size) != 0) { return NULL; @@ -439,8 +450,18 @@ struct pthreadpool* pthreadpool_create(size_t threads_count) { #endif if (threads_count == 0) { - threads_count = (size_t) sysconf(_SC_NPROCESSORS_ONLN); + #if defined(_SC_NPROCESSORS_ONLN) + threads_count = (size_t) sysconf(_SC_NPROCESSORS_ONLN); + #elif defined(_WIN32) + SYSTEM_INFO system_info; + ZeroMemory(&system_info, sizeof(system_info)); + GetSystemInfo(&system_info); + threads_count = (size_t) system_info.dwNumberOfProcessors; + #else + #error "Unsupported platform" + #endif } + struct pthreadpool* threadpool = pthreadpool_allocate(threads_count); if (threadpool == NULL) { return NULL; @@ -1204,6 +1225,10 @@ void pthreadpool_destroy(struct pthreadpool* threadpool) { pthread_cond_destroy(&threadpool->command_condvar); #endif } - free(threadpool); + #ifdef _WIN32 + _aligned_free(threadpool); + #else + free(threadpool); + #endif } } -- cgit v1.2.3