2626#include "threadpool-common.h"
2727
2828/* POSIX headers */
29+ #if PTHREADPOOL_USE_PTHREADS
2930#include <pthread.h>
3031#include <unistd.h>
32+ #else
33+ #include <threads.h>
34+ #endif // PTHREADPOOL_USE_PTHREADS
3135
3236/* Futex-specific headers */
3337#if PTHREADPOOL_USE_FUTEX
5761
5862/* Windows-specific headers */
5963#ifdef _WIN32
64+ #ifndef WIN32_LEAN_AND_MEAN
65+ #define WIN32_LEAN_AND_MEAN
66+ #endif
67+ #include <windows.h>
6068#include <sysinfoapi.h>
6169#endif
6270
@@ -135,7 +143,7 @@ size_t pthreadpool_set_threads_count(struct pthreadpool* threadpool,
135143 return 1 ;
136144 }
137145 /* We shouldn't change this while a parallel computation is running. */
138- pthread_mutex_lock (& threadpool -> execution_mutex );
146+ pthreadpool_mutex_lock (& threadpool -> execution_mutex );
139147
140148 // Adjust `num_threads` to the feasible limits.
141149 if (num_threads == 0 ) {
@@ -155,7 +163,7 @@ size_t pthreadpool_set_threads_count(struct pthreadpool* threadpool,
155163 }
156164 pthreadpool_log_debug ("setting max_num_threads to %zu." , num_threads );
157165
158- pthread_mutex_unlock (& threadpool -> execution_mutex );
166+ pthreadpool_mutex_unlock (& threadpool -> execution_mutex );
159167
160168 return num_threads ;
161169}
@@ -167,7 +175,7 @@ static void wait_on_num_recruited_threads(pthreadpool_t threadpool,
167175
168176#if !PTHREADPOOL_USE_FUTEX
169177 if (num_recruited_threads != expected ) {
170- pthread_mutex_lock (& threadpool -> completion_mutex );
178+ pthreadpool_mutex_lock (& threadpool -> completion_mutex );
171179#endif // !PTHREADPOOL_USE_FUTEX
172180
173181 for (size_t iter = 0 ; num_recruited_threads != expected ; iter ++ ) {
@@ -180,16 +188,16 @@ static void wait_on_num_recruited_threads(pthreadpool_t threadpool,
180188#if PTHREADPOOL_USE_FUTEX
181189 futex_wait (& threadpool -> num_recruited_threads , num_recruited_threads );
182190#else
183- pthread_cond_wait (& threadpool -> completion_condvar ,
184- & threadpool -> completion_mutex );
191+ pthreadpool_cond_wait (& threadpool -> completion_condvar ,
192+ & threadpool -> completion_mutex );
185193#endif // PTHREADPOOL_USE_FUTEX
186194 }
187195 num_recruited_threads =
188196 pthreadpool_load_acquire_uint32_t (& threadpool -> num_recruited_threads );
189197 }
190198
191199#if !PTHREADPOOL_USE_FUTEX
192- pthread_mutex_unlock (& threadpool -> completion_mutex );
200+ pthreadpool_mutex_unlock (& threadpool -> completion_mutex );
193201 }
194202#endif // !PTHREADPOOL_USE_FUTEX
195203}
@@ -201,7 +209,7 @@ static int32_t wait_on_num_active_threads(pthreadpool_t threadpool,
201209
202210 if (curr_active_threads <= 0 ) {
203211#if !PTHREADPOOL_USE_FUTEX
204- pthread_mutex_lock (& threadpool -> num_active_threads_mutex );
212+ pthreadpool_mutex_lock (& threadpool -> num_active_threads_mutex );
205213#endif // !PTHREADPOOL_USE_FUTEX
206214
207215 for (size_t iter = 0 ; curr_active_threads <= 0 ; iter ++ ) {
@@ -246,8 +254,8 @@ static int32_t wait_on_num_active_threads(pthreadpool_t threadpool,
246254 pthreadpool_log_debug (
247255 "thread %u waiting on change in num active threads (curr=%i)..." ,
248256 thread_id , curr_active_threads );
249- pthread_cond_wait (& threadpool -> num_active_threads_condvar ,
250- & threadpool -> num_active_threads_mutex );
257+ pthreadpool_cond_wait (& threadpool -> num_active_threads_condvar ,
258+ & threadpool -> num_active_threads_mutex );
251259#endif // PTHREADPOOL_USE_FUTEX
252260 }
253261
@@ -256,7 +264,7 @@ static int32_t wait_on_num_active_threads(pthreadpool_t threadpool,
256264 }
257265
258266#if !PTHREADPOOL_USE_FUTEX
259- pthread_mutex_unlock (& threadpool -> num_active_threads_mutex );
267+ pthreadpool_mutex_unlock (& threadpool -> num_active_threads_mutex );
260268#endif // !PTHREADPOOL_USE_FUTEX
261269 }
262270
@@ -269,7 +277,7 @@ static void wait_on_work_is_done(pthreadpool_t threadpool) {
269277
270278#if !PTHREADPOOL_USE_FUTEX
271279 if (!work_is_done ) {
272- pthread_mutex_lock (& threadpool -> completion_mutex );
280+ pthreadpool_mutex_lock (& threadpool -> completion_mutex );
273281#endif // !PTHREADPOOL_USE_FUTEX
274282
275283 for (size_t iter = 0 ; !work_is_done ; iter ++ ) {
@@ -283,8 +291,8 @@ static void wait_on_work_is_done(pthreadpool_t threadpool) {
283291 futex_wait ((pthreadpool_atomic_uint32_t * )& threadpool -> work_is_done ,
284292 work_is_done );
285293#else
286- pthread_cond_wait (& threadpool -> completion_condvar ,
287- & threadpool -> completion_mutex );
294+ pthreadpool_cond_wait (& threadpool -> completion_condvar ,
295+ & threadpool -> completion_mutex );
288296#endif // PTHREADPOOL_USE_FUTEX
289297 }
290298
@@ -293,7 +301,7 @@ static void wait_on_work_is_done(pthreadpool_t threadpool) {
293301 }
294302
295303#if !PTHREADPOOL_USE_FUTEX
296- pthread_mutex_unlock (& threadpool -> completion_mutex );
304+ pthreadpool_mutex_unlock (& threadpool -> completion_mutex );
297305 }
298306#endif // !PTHREADPOOL_USE_FUTEX
299307}
@@ -302,9 +310,9 @@ static void signal_num_recruited_threads(pthreadpool_t threadpool) {
302310#if PTHREADPOOL_USE_FUTEX
303311 futex_wake_all (& threadpool -> num_recruited_threads );
304312#else
305- pthread_mutex_lock (& threadpool -> completion_mutex );
306- pthread_cond_signal (& threadpool -> completion_condvar );
307- pthread_mutex_unlock (& threadpool -> completion_mutex );
313+ pthreadpool_mutex_lock (& threadpool -> completion_mutex );
314+ pthreadpool_cond_signal (& threadpool -> completion_condvar );
315+ pthreadpool_mutex_unlock (& threadpool -> completion_mutex );
308316#endif // PTHREADPOOL_USE_FUTEX
309317}
310318
@@ -318,9 +326,9 @@ static void signal_num_active_threads(pthreadpool_t threadpool,
318326 num_waiting_threads - max_num_waiting );
319327 }
320328#else
321- pthread_mutex_lock (& threadpool -> num_active_threads_mutex );
322- pthread_cond_broadcast (& threadpool -> num_active_threads_condvar );
323- pthread_mutex_unlock (& threadpool -> num_active_threads_mutex );
329+ pthreadpool_mutex_lock (& threadpool -> num_active_threads_mutex );
330+ pthreadpool_cond_broadcast (& threadpool -> num_active_threads_condvar );
331+ pthreadpool_mutex_unlock (& threadpool -> num_active_threads_mutex );
324332#endif // PTHREADPOOL_USE_FUTEX
325333}
326334
@@ -331,9 +339,9 @@ static void signal_work_is_done(pthreadpool_t threadpool) {
331339#if PTHREADPOOL_USE_FUTEX
332340 futex_wake_all (& threadpool -> work_is_done );
333341#else
334- pthread_mutex_lock (& threadpool -> completion_mutex );
335- pthread_cond_signal (& threadpool -> completion_condvar );
336- pthread_mutex_unlock (& threadpool -> completion_mutex );
342+ pthreadpool_mutex_lock (& threadpool -> completion_mutex );
343+ pthreadpool_cond_signal (& threadpool -> completion_condvar );
344+ pthreadpool_mutex_unlock (& threadpool -> completion_mutex );
337345#endif // PTHREADPOOL_USE_FUTEX
338346}
339347
@@ -516,7 +524,7 @@ static size_t get_num_cpus() {
516524 SYSTEM_INFO system_info ;
517525 ZeroMemory (& system_info , sizeof (system_info ));
518526 GetSystemInfo (& system_info );
519- return = (size_t )system_info .dwNumberOfProcessors ;
527+ return (size_t )system_info .dwNumberOfProcessors ;
520528#else
521529#error \
522530 "Platform-specific implementation of sysconf(_SC_NPROCESSORS_ONLN) required"
@@ -562,15 +570,15 @@ struct pthreadpool* pthreadpool_create_v2(struct pthreadpool_executor* executor,
562570 threadpool -> num_active_threads = 0 ;
563571
564572 /* Initialize the execution mutex. */
565- pthread_mutex_init (& threadpool -> execution_mutex , NULL );
573+ pthreadpool_mutex_init (& threadpool -> execution_mutex );
566574
567575 if (num_threads > 1 ) {
568576#if !PTHREADPOOL_USE_FUTEX
569577 /* Initialize the condition variables and mutexes. */
570- pthread_mutex_init (& threadpool -> completion_mutex , NULL );
571- pthread_cond_init (& threadpool -> completion_condvar , NULL );
572- pthread_mutex_init (& threadpool -> num_active_threads_mutex , NULL );
573- pthread_cond_init (& threadpool -> num_active_threads_condvar , NULL );
578+ pthreadpool_mutex_init (& threadpool -> completion_mutex );
579+ pthreadpool_cond_init (& threadpool -> completion_condvar );
580+ pthreadpool_mutex_init (& threadpool -> num_active_threads_mutex );
581+ pthreadpool_cond_init (& threadpool -> num_active_threads_condvar );
574582#endif
575583
576584 /* If we weren't given an executor, start our own threads. */
@@ -579,8 +587,8 @@ struct pthreadpool* pthreadpool_create_v2(struct pthreadpool_executor* executor,
579587 * starting with worker #1. */
580588 pthreadpool_register_threads (threadpool , num_threads - 1 );
581589 for (size_t tid = 1 ; tid < num_threads ; tid ++ ) {
582- pthread_create (& threadpool -> threads [tid ].thread_object , NULL ,
583- & thread_main , & threadpool -> threads [tid ]);
590+ pthreadpool_thread_create (& threadpool -> threads [tid ].thread_object ,
591+ & thread_main , & threadpool -> threads [tid ]);
584592 }
585593 }
586594 }
@@ -629,7 +637,7 @@ PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
629637 assert (linear_range > 1 );
630638
631639 /* Protect the global threadpool structures */
632- pthread_mutex_lock (& threadpool -> execution_mutex );
640+ pthreadpool_mutex_lock (& threadpool -> execution_mutex );
633641
634642 /* Make changes by other threads visible to this thread. */
635643 pthreadpool_fence_acquire ();
@@ -705,7 +713,7 @@ PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
705713 threadpool -> threads_count = fxdiv_init_size_t (prev_num_threads );
706714
707715 /* Unprotect the global threadpool structures now that we're done. */
708- pthread_mutex_unlock (& threadpool -> execution_mutex );
716+ pthreadpool_mutex_unlock (& threadpool -> execution_mutex );
709717}
710718
711719static void pthreadpool_release_all_threads (struct pthreadpool * threadpool ) {
@@ -745,17 +753,18 @@ void pthreadpool_destroy(struct pthreadpool* threadpool) {
745753 if (!threadpool -> executor .num_threads ) {
746754 /* Wait until all threads return */
747755 for (size_t thread = 1 ; thread < threadpool -> max_num_threads ; thread ++ ) {
748- pthread_join (threadpool -> threads [thread ].thread_object , NULL );
756+ pthreadpool_thread_join (threadpool -> threads [thread ].thread_object ,
757+ NULL );
749758 }
750759 }
751760
752761 /* Release resources */
753- pthread_mutex_destroy (& threadpool -> execution_mutex );
762+ pthreadpool_mutex_destroy (& threadpool -> execution_mutex );
754763#if !PTHREADPOOL_USE_FUTEX
755- pthread_mutex_destroy (& threadpool -> num_active_threads_mutex );
756- pthread_cond_destroy (& threadpool -> num_active_threads_condvar );
757- pthread_mutex_destroy (& threadpool -> completion_mutex );
758- pthread_cond_destroy (& threadpool -> completion_condvar );
764+ pthreadpool_mutex_destroy (& threadpool -> num_active_threads_mutex );
765+ pthreadpool_cond_destroy (& threadpool -> num_active_threads_condvar );
766+ pthreadpool_mutex_destroy (& threadpool -> completion_mutex );
767+ pthreadpool_cond_destroy (& threadpool -> completion_condvar );
759768#endif
760769
761770#if PTHREADPOOL_USE_CPUINFO
0 commit comments