Skip to content

Commit 41fadfb

Browse files
gonnetxnnpack-bot
authored andcommitted
Remove windows.c and use C11's concurrency support library instead.
PiperOrigin-RevId: 808969331
1 parent 19f6725 commit 41fadfb

6 files changed

Lines changed: 154 additions & 513 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ ENDIF()
4444
SET(PTHREADPOOL_LIBRARY_TYPE "default" CACHE STRING "Type of library (shared, static, or default) to build")
4545
SET_PROPERTY(CACHE PTHREADPOOL_LIBRARY_TYPE PROPERTY STRINGS default static shared)
4646
OPTION(PTHREADPOOL_ALLOW_DEPRECATED_API "Enable deprecated API functions" ON)
47-
SET(PTHREADPOOL_SYNC_PRIMITIVE "default" CACHE STRING "Synchronization primitive (condvar, futex, event, or default) for worker threads")
48-
SET_PROPERTY(CACHE PTHREADPOOL_SYNC_PRIMITIVE PROPERTY STRINGS default condvar futex event)
47+
SET(PTHREADPOOL_SYNC_PRIMITIVE "default" CACHE STRING "Synchronization primitive (condvar, futex, or default) for worker threads")
48+
SET_PROPERTY(CACHE PTHREADPOOL_SYNC_PRIMITIVE PROPERTY STRINGS default condvar futex)
4949
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?)$")
5050
OPTION(PTHREADPOOL_ENABLE_FASTPATH "Enable fast path using atomic decrement instead of atomic compare-and-swap" ON)
5151
ELSE()
@@ -108,12 +108,7 @@ ENDIF()
108108
IF(EMSCRIPTEN)
109109
LIST(APPEND PTHREADPOOL_SRCS src/shim.c)
110110
ELSE()
111-
LIST(APPEND PTHREADPOOL_SRCS src/portable-api.c src/memory.c)
112-
IF(CMAKE_SYSTEM_NAME MATCHES "^(Windows|CYGWIN|MSYS)$" AND (PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default" OR PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event"))
113-
LIST(APPEND PTHREADPOOL_SRCS src/windows.c)
114-
ELSE()
115-
LIST(APPEND PTHREADPOOL_SRCS src/pthreads.c)
116-
ENDIF()
111+
LIST(APPEND PTHREADPOOL_SRCS src/portable-api.c src/memory.c src/pthreads.c)
117112
IF(PTHREADPOOL_ENABLE_FASTPATH)
118113
LIST(APPEND PTHREADPOOL_SRCS src/fastpath.c)
119114
ENDIF()
@@ -139,15 +134,9 @@ ENDIF()
139134
IF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "condvar")
140135
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=0)
141136
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0)
142-
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=0)
143137
ELSEIF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "futex")
144138
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=1)
145139
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0)
146-
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=0)
147-
ELSEIF(PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "event")
148-
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_FUTEX=0)
149-
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_GCD=0)
150-
TARGET_COMPILE_DEFINITIONS(pthreadpool PRIVATE PTHREADPOOL_USE_EVENT=1)
151140
ELSEIF(NOT PTHREADPOOL_SYNC_PRIMITIVE STREQUAL "default")
152141
MESSAGE(FATAL_ERROR "Unsupported synchronization primitive ${PTHREADPOOL_SYNC_PRIMITIVE}")
153142
ENDIF()

src/pthreads.c

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
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
@@ -57,6 +61,10 @@
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

711719
static 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

src/threadpool-atomics.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,95 @@
4545
/* Configuration header */
4646
#include "threadpool-common.h"
4747

48+
/* POSIX headers */
49+
#if PTHREADPOOL_USE_PTHREADS
50+
#include <pthread.h>
51+
#else
52+
#include <threads.h>
53+
#endif
54+
55+
#if PTHREADPOOL_USE_PTHREADS
56+
typedef pthread_t pthreadpool_thread_t;
57+
typedef pthread_mutex_t pthreadpool_mutex_t;
58+
typedef pthread_cond_t pthreadpool_cond_t;
59+
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
60+
pthread_mutex_init(mutex, NULL);
61+
}
62+
static inline void pthreadpool_mutex_destroy(pthreadpool_mutex_t* mutex) {
63+
pthread_mutex_destroy(mutex);
64+
}
65+
static inline void pthreadpool_mutex_lock(pthreadpool_mutex_t* mutex) {
66+
pthread_mutex_lock(mutex);
67+
}
68+
static inline void pthreadpool_mutex_unlock(pthreadpool_mutex_t* mutex) {
69+
pthread_mutex_unlock(mutex);
70+
}
71+
static inline void pthreadpool_cond_init(pthreadpool_cond_t* cond) {
72+
pthread_cond_init(cond, NULL);
73+
}
74+
static inline void pthreadpool_cond_destroy(pthreadpool_cond_t* cond) {
75+
pthread_cond_destroy(cond);
76+
}
77+
static inline void pthreadpool_cond_wait(pthreadpool_cond_t* cond,
78+
pthreadpool_mutex_t* mutex) {
79+
pthread_cond_wait(cond, mutex);
80+
}
81+
static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
82+
pthread_cond_signal(cond);
83+
}
84+
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
85+
pthread_cond_broadcast(cond);
86+
}
87+
static inline void pthreadpool_thread_create(pthreadpool_thread_t* thread,
88+
void*(fun)(void*), void* arg) {
89+
pthread_create(thread, NULL, fun, arg);
90+
}
91+
static inline void pthreadpool_thread_join(pthreadpool_thread_t thread,
92+
void* return_value) {
93+
pthread_join(thread, (void**)return_value);
94+
}
95+
#else
96+
typedef thrd_t pthreadpool_thread_t;
97+
typedef mtx_t pthreadpool_mutex_t;
98+
typedef cnd_t pthreadpool_cond_t;
99+
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
100+
mtx_init(mutex, mtx_plain);
101+
}
102+
static inline void pthreadpool_mutex_destroy(pthreadpool_mutex_t* mutex) {
103+
mtx_destroy(mutex);
104+
}
105+
static inline void pthreadpool_mutex_lock(pthreadpool_mutex_t* mutex) {
106+
mtx_lock(mutex);
107+
}
108+
static inline void pthreadpool_mutex_unlock(pthreadpool_mutex_t* mutex) {
109+
mtx_unlock(mutex);
110+
}
111+
static inline void pthreadpool_cond_init(pthreadpool_cond_t* cond) {
112+
cnd_init(cond);
113+
}
114+
static inline void pthreadpool_cond_destroy(pthreadpool_cond_t* cond) {
115+
cnd_destroy(cond);
116+
}
117+
static inline void pthreadpool_cond_wait(pthreadpool_cond_t* cond,
118+
pthreadpool_mutex_t* mutex) {
119+
cnd_wait(cond, mutex);
120+
}
121+
static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
122+
cnd_signal(cond);
123+
}
124+
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
125+
cnd_broadcast(cond);
126+
}
127+
static inline void pthreadpool_thread_create(pthreadpool_thread_t* thread,
128+
void*(fun)(void*), void* arg) {
129+
thrd_create(thread, fun, arg);
130+
}
131+
static inline void pthreadpool_thread_join(pthreadpool_thread_t thread,
132+
void* return_value) {
133+
thrd_join(thread, return_value);
134+
}
135+
#endif // PTHREADPOOL_USE_PTHREADS
136+
48137
/* Align the atomic values on the size of a cache line to avoid false sharing,
49138
* i.e. two or more atomic variables sharing the same cache line will block
50139
* each other during atomic operations.

0 commit comments

Comments
 (0)