Skip to content

Commit bdcd753

Browse files
dgrove-ossdas
authored andcommitted
clarify config of pthread_workqueue vs. internal_workqueue
As suggested in review of PR-206, DISPATCH_USE_INTERNAL_WORKQUEUES should not enable HAVE_PTHREAD_WORKQUEUES. Instead, a new DISPATCH_USE_WORKQUEUES is defined in queue.c to guard code that should be enabled in either case. Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
1 parent 72c267b commit bdcd753

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

configure.ac

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ AS_IF([test -n "$apple_libpthread_source_path" -a -n "$apple_xnu_source_osfmk_pa
307307
AC_CHECK_HEADERS([pthread_machdep.h pthread/qos.h])
308308

309309
# pthread_workqueues.
310-
# Look for own version first, than see if there is a system version.
310+
# We can either use libdispatch's internal_workqueue or pthread_workqueue.
311+
# If not specifically configured, default to internal_workqueues on
312+
# Linux and pthread_workqueue on all other platforms.
313+
# On any platform, if pthread_workqueue is not available, fall back
314+
# to using internal_workqueue.
311315
AC_ARG_ENABLE([internal-libpwq],
312316
[AS_HELP_STRING([--enable-internal-libpwq],
313317
[Use libdispatch's own implementation of pthread workqueues.])],,
@@ -320,15 +324,15 @@ AC_ARG_ENABLE([internal-libpwq],
320324
esac]
321325
)
322326
AS_IF([test "x$enable_internal_libpwq" = "xyes"],
323-
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread_workqueue API])
324-
AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
325-
dispatch_use_internal_workqueue=true
326-
have_pthread_workqueues=true],
327-
[dispatch_use_internal_workqueue=false
328-
AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
327+
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread workqueues])
328+
have_pthread_workqueues=false,
329+
dispatch_use_internal_workqueue=true],
330+
[AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
329331
[AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
330-
have_pthread_workqueues=true],
331-
[have_pthread_workqueues=false]
332+
have_pthread_workqueues=true,
333+
dispatch_use_internal_workqueue=false],
334+
[have_pthread_workqueues=false,
335+
dispatch_use_internal_workqueue=true]
332336
)]
333337
)
334338
AM_CONDITIONAL(DISPATCH_USE_INTERNAL_WORKQUEUE, $dispatch_use_internal_workqueue)

src/Makefile.am

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,10 @@ AM_OBJCFLAGS=$(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS)
8484
AM_CXXFLAGS=$(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)
8585
AM_OBJCXXFLAGS=$(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)
8686

87-
if DISPATCH_USE_INTERNAL_WORKQUEUE
88-
PTHREAD_WORKQUEUE_LIBS=
89-
PTHREAD_WORKQUEUE_CFLAGS=
90-
else
9187
if HAVE_PTHREAD_WORKQUEUES
9288
PTHREAD_WORKQUEUE_LIBS=-lpthread_workqueue
9389
PTHREAD_WORKQUEUE_CFLAGS=
9490
endif
95-
endif
9691

9792
if BUILD_OWN_BLOCKS_RUNTIME
9893
libdispatch_la_SOURCES+= BlocksRuntime/data.c BlocksRuntime/runtime.c

src/queue.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
#include "protocol.h" // _dispatch_send_wakeup_runloop_thread
2424
#endif
2525

26-
#if (!HAVE_PTHREAD_WORKQUEUES || DISPATCH_DEBUG || DISPATCH_USE_INTERNAL_WORKQUEUE) && \
27-
!defined(DISPATCH_ENABLE_THREAD_POOL)
26+
#if HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
27+
#define DISPATCH_USE_WORKQUEUES 1
28+
#endif
29+
#if (!HAVE_PTHREAD_WORKQUEUES || DISPATCH_DEBUG) && !defined(DISPATCH_ENABLE_THREAD_POOL)
2830
#define DISPATCH_ENABLE_THREAD_POOL 1
2931
#endif
3032
#if DISPATCH_ENABLE_PTHREAD_ROOT_QUEUES || DISPATCH_ENABLE_THREAD_POOL
3133
#define DISPATCH_USE_PTHREAD_POOL 1
3234
#endif
3335
#if HAVE_PTHREAD_WORKQUEUES && (!HAVE_PTHREAD_WORKQUEUE_QOS || DISPATCH_DEBUG) && \
3436
!HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP && \
35-
!DISPATCH_USE_INTERNAL_WORKQUEUE && \
3637
!defined(DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK)
3738
#define DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK 1
3839
#endif
39-
#if HAVE_PTHREAD_WORKQUEUES && DISPATCH_USE_PTHREAD_POOL && \
40+
#if DISPATCH_USE_WORKQUEUES && DISPATCH_USE_PTHREAD_POOL && \
4041
!DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
4142
#define pthread_workqueue_t void*
4243
#endif
@@ -155,13 +156,13 @@ struct dispatch_root_queue_context_s {
155156
union {
156157
struct {
157158
int volatile dgq_pending;
158-
#if HAVE_PTHREAD_WORKQUEUES
159+
#if DISPATCH_USE_WORKQUEUES
159160
qos_class_t dgq_qos;
160161
int dgq_wq_priority, dgq_wq_options;
161162
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_USE_PTHREAD_POOL
162163
pthread_workqueue_t dgq_kworkqueue;
163164
#endif
164-
#endif // HAVE_PTHREAD_WORKQUEUES
165+
#endif // DISPATCH_USE_WORKQUEUES
165166
#if DISPATCH_USE_PTHREAD_POOL
166167
void *dgq_ctxt;
167168
int32_t volatile dgq_thread_pool_size;
@@ -183,7 +184,7 @@ typedef struct dispatch_root_queue_context_s *dispatch_root_queue_context_t;
183184
DISPATCH_CACHELINE_ALIGN
184185
static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
185186
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS] = {{{
186-
#if HAVE_PTHREAD_WORKQUEUES
187+
#if DISPATCH_USE_WORKQUEUES
187188
.dgq_qos = QOS_CLASS_MAINTENANCE,
188189
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
189190
.dgq_wq_options = 0,
@@ -194,7 +195,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
194195
#endif
195196
}}},
196197
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS_OVERCOMMIT] = {{{
197-
#if HAVE_PTHREAD_WORKQUEUES
198+
#if DISPATCH_USE_WORKQUEUES
198199
.dgq_qos = QOS_CLASS_MAINTENANCE,
199200
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
200201
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -205,7 +206,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
205206
#endif
206207
}}},
207208
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS] = {{{
208-
#if HAVE_PTHREAD_WORKQUEUES
209+
#if DISPATCH_USE_WORKQUEUES
209210
.dgq_qos = QOS_CLASS_BACKGROUND,
210211
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
211212
.dgq_wq_options = 0,
@@ -216,7 +217,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
216217
#endif
217218
}}},
218219
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS_OVERCOMMIT] = {{{
219-
#if HAVE_PTHREAD_WORKQUEUES
220+
#if DISPATCH_USE_WORKQUEUES
220221
.dgq_qos = QOS_CLASS_BACKGROUND,
221222
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
222223
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -227,7 +228,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
227228
#endif
228229
}}},
229230
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS] = {{{
230-
#if HAVE_PTHREAD_WORKQUEUES
231+
#if DISPATCH_USE_WORKQUEUES
231232
.dgq_qos = QOS_CLASS_UTILITY,
232233
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
233234
.dgq_wq_options = 0,
@@ -238,7 +239,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
238239
#endif
239240
}}},
240241
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS_OVERCOMMIT] = {{{
241-
#if HAVE_PTHREAD_WORKQUEUES
242+
#if DISPATCH_USE_WORKQUEUES
242243
.dgq_qos = QOS_CLASS_UTILITY,
243244
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
244245
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -249,7 +250,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
249250
#endif
250251
}}},
251252
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS] = {{{
252-
#if HAVE_PTHREAD_WORKQUEUES
253+
#if DISPATCH_USE_WORKQUEUES
253254
.dgq_qos = QOS_CLASS_DEFAULT,
254255
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
255256
.dgq_wq_options = 0,
@@ -260,7 +261,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
260261
#endif
261262
}}},
262263
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS_OVERCOMMIT] = {{{
263-
#if HAVE_PTHREAD_WORKQUEUES
264+
#if DISPATCH_USE_WORKQUEUES
264265
.dgq_qos = QOS_CLASS_DEFAULT,
265266
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
266267
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -271,7 +272,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
271272
#endif
272273
}}},
273274
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS] = {{{
274-
#if HAVE_PTHREAD_WORKQUEUES
275+
#if DISPATCH_USE_WORKQUEUES
275276
.dgq_qos = QOS_CLASS_USER_INITIATED,
276277
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
277278
.dgq_wq_options = 0,
@@ -282,7 +283,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
282283
#endif
283284
}}},
284285
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS_OVERCOMMIT] = {{{
285-
#if HAVE_PTHREAD_WORKQUEUES
286+
#if DISPATCH_USE_WORKQUEUES
286287
.dgq_qos = QOS_CLASS_USER_INITIATED,
287288
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
288289
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -293,7 +294,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
293294
#endif
294295
}}},
295296
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS] = {{{
296-
#if HAVE_PTHREAD_WORKQUEUES
297+
#if DISPATCH_USE_WORKQUEUES
297298
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
298299
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
299300
.dgq_wq_options = 0,
@@ -304,7 +305,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
304305
#endif
305306
}}},
306307
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS_OVERCOMMIT] = {{{
307-
#if HAVE_PTHREAD_WORKQUEUES
308+
#if DISPATCH_USE_WORKQUEUES
308309
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
309310
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
310311
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -572,11 +573,11 @@ dispatch_assert_queue_barrier(dispatch_queue_t dq)
572573
static inline bool
573574
_dispatch_root_queues_init_workq(int *wq_supported)
574575
{
575-
int r;
576+
int r; (void)r;
576577
bool result = false;
577578
*wq_supported = 0;
578-
#if HAVE_PTHREAD_WORKQUEUES
579-
bool disable_wq = false;
579+
#if DISPATCH_USE_WORKQUEUES
580+
bool disable_wq = false; (void)disable_wq;
580581
#if DISPATCH_ENABLE_THREAD_POOL && DISPATCH_DEBUG
581582
disable_wq = slowpath(getenv("LIBDISPATCH_DISABLE_KWQ"));
582583
#endif
@@ -676,7 +677,7 @@ _dispatch_root_queues_init_workq(int *wq_supported)
676677
#endif
677678
}
678679
#endif // DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
679-
#endif // HAVE_PTHREAD_WORKQUEUES
680+
#endif // DISPATCH_USE_WORKQUEUES
680681
return result;
681682
}
682683

@@ -692,7 +693,7 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
692693
thread_pool_size = pool_size;
693694
}
694695
qc->dgq_thread_pool_size = thread_pool_size;
695-
#if HAVE_PTHREAD_WORKQUEUES
696+
#if DISPATCH_USE_WORKQUEUES
696697
if (qc->dgq_qos) {
697698
(void)dispatch_assume_zero(pthread_attr_init(&pqc->dpq_thread_attr));
698699
(void)dispatch_assume_zero(pthread_attr_setdetachstate(
@@ -1906,7 +1907,7 @@ static struct dispatch_pthread_root_queue_context_s
19061907
_dispatch_mgr_root_queue_pthread_context;
19071908
static struct dispatch_root_queue_context_s
19081909
_dispatch_mgr_root_queue_context = {{{
1909-
#if HAVE_PTHREAD_WORKQUEUES
1910+
#if DISPATCH_USE_WORKQUEUES
19101911
.dgq_kworkqueue = (void*)(~0ul),
19111912
#endif
19121913
.dgq_ctxt = &_dispatch_mgr_root_queue_pthread_context,
@@ -2165,7 +2166,7 @@ _dispatch_pthread_root_queue_create(const char *label, unsigned long flags,
21652166

21662167
pqc->dpq_thread_mediator.do_vtable = DISPATCH_VTABLE(semaphore);
21672168
qc->dgq_ctxt = pqc;
2168-
#if HAVE_PTHREAD_WORKQUEUES
2169+
#if DISPATCH_USE_WORKQUEUES
21692170
qc->dgq_kworkqueue = (void*)(~0ul);
21702171
#endif
21712172
_dispatch_root_queue_init_pthread_pool(qc, pool_size, true);
@@ -4378,7 +4379,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)
43784379

43794380
_dispatch_root_queues_init();
43804381
_dispatch_debug_root_queue(dq, __func__);
4381-
#if HAVE_PTHREAD_WORKQUEUES
4382+
#if DISPATCH_USE_WORKQUEUES
43824383
#if DISPATCH_USE_PTHREAD_POOL
43834384
if (qc->dgq_kworkqueue != (void*)(~0ul))
43844385
#endif
@@ -4407,7 +4408,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)
44074408
(void)dispatch_assume_zero(r);
44084409
return;
44094410
}
4410-
#endif // HAVE_PTHREAD_WORKQUEUES
4411+
#endif // DISPATCH_USE_WORKQUEUES
44114412
#if DISPATCH_USE_PTHREAD_POOL
44124413
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
44134414
if (fastpath(pqc->dpq_thread_mediator.do_vtable)) {
@@ -4476,7 +4477,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
44764477
if (!_dispatch_queue_class_probe(dq)) {
44774478
return;
44784479
}
4479-
#if HAVE_PTHREAD_WORKQUEUES
4480+
#if DISPATCH_USE_WORKQUEUES
44804481
dispatch_root_queue_context_t qc = dq->do_ctxt;
44814482
if (
44824483
#if DISPATCH_USE_PTHREAD_POOL
@@ -4487,7 +4488,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
44874488
"global queue: %p", dq);
44884489
return;
44894490
}
4490-
#endif // HAVE_PTHREAD_WORKQUEUES
4491+
#endif // DISPATCH_USE_WORKQUEUES
44914492
return _dispatch_global_queue_poke_slow(dq, n, floor);
44924493
}
44934494

src/shims.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#if HAVE_PTHREAD_WORKQUEUES
4242
#if __has_include(<pthread/workqueue_private.h>)
4343
#include <pthread/workqueue_private.h>
44-
#elif DISPATCH_USE_INTERNAL_WORKQUEUE
45-
#include "event/workqueue_internal.h"
4644
#else
4745
#include <pthread_workqueue.h>
4846
#endif
@@ -51,6 +49,10 @@
5149
#endif
5250
#endif // HAVE_PTHREAD_WORKQUEUES
5351

52+
#if DISPATCH_USE_INTERNAL_WORKQUEUE
53+
#include "event/workqueue_internal.h"
54+
#endif
55+
5456
#if HAVE_PTHREAD_NP_H
5557
#include <pthread_np.h>
5658
#endif

0 commit comments

Comments
 (0)