diff options
author | mattn <mattn.jp@gmail.com> | 2020-03-02 15:21:58 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-01 22:21:58 -0800 |
commit | ebd50d0cfa3664d454ffdf246fcd228c3b370a11 (patch) | |
tree | 7c8ebcd23e4fef2b38c01432eb91e9f10b69bcf9 | |
parent | d465747660ecf9ebbaddf8c3db37e4a13d0c9103 (diff) | |
download | pthreadpool-ebd50d0cfa3664d454ffdf246fcd228c3b370a11.tar.gz |
Build on Windows/mingw64 (#6)
Support Windows/mingw64 build
-rw-r--r-- | bench/latency.cc | 3 | ||||
-rw-r--r-- | src/threadpool-pthreads.c | 29 |
2 files changed, 29 insertions, 3 deletions
diff --git a/bench/latency.cc b/bench/latency.cc index f500cdf..e72ea49 100644 --- a/bench/latency.cc +++ b/bench/latency.cc @@ -4,9 +4,10 @@ #include <pthreadpool.h> +#include <thread> static void SetNumberOfThreads(benchmark::internal::Benchmark* benchmark) { - const int max_threads = sysconf(_SC_NPROCESSORS_ONLN); + const int max_threads = std::thread::hardware_concurrency(); for (int t = 1; t <= max_threads; t++) { benchmark->Arg(t); } 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 <sysinfoapi.h> +# include <malloc.h> +#endif + /* Dependencies */ #include <fxdiv.h> @@ -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 } } |