Skip to content

Commit 5a3c02a

Browse files
authored
Merge pull request swiftlang#248 from dgrove-oss/queue_config_cleanup
clarify config of pthread_workqueue vs. internal_workqueue
2 parents 7b63c3f + dbb4518 commit 5a3c02a

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

configure.ac

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

322322
# pthread_workqueues.
323-
# Look for own version first, than see if there is a system version.
323+
# We can either use libdispatch's internal_workqueue or pthread_workqueue.
324+
# If not specifically configured, default to internal_workqueues on
325+
# Linux and pthread_workqueue on all other platforms.
326+
# On any platform, if pthread_workqueue is not available, fall back
327+
# to using internal_workqueue.
324328
AC_ARG_ENABLE([internal-libpwq],
325329
[AS_HELP_STRING([--enable-internal-libpwq],
326330
[Use libdispatch's own implementation of pthread workqueues.])],,
@@ -333,15 +337,15 @@ AC_ARG_ENABLE([internal-libpwq],
333337
esac]
334338
)
335339
AS_IF([test "x$enable_internal_libpwq" = "xyes"],
336-
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread_workqueue API])
337-
AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
338-
dispatch_use_internal_workqueue=true
339-
have_pthread_workqueues=true],
340-
[dispatch_use_internal_workqueue=false
341-
AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
340+
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread workqueues])
341+
have_pthread_workqueues=false,
342+
dispatch_use_internal_workqueue=true],
343+
[AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
342344
[AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
343-
have_pthread_workqueues=true],
344-
[have_pthread_workqueues=false]
345+
have_pthread_workqueues=true,
346+
dispatch_use_internal_workqueue=false],
347+
[have_pthread_workqueues=false,
348+
dispatch_use_internal_workqueue=true]
345349
)]
346350
)
347351
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/internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,11 @@ _dispatch_fork_becomes_unsafe(void)
630630

631631
// Older Mac OS X and iOS Simulator fallbacks
632632

633-
#if HAVE_PTHREAD_WORKQUEUES
633+
#if HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
634634
#ifndef WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
635635
#define WORKQ_ADDTHREADS_OPTION_OVERCOMMIT 0x00000001
636636
#endif
637-
#endif // HAVE_PTHREAD_WORKQUEUES
637+
#endif // HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
638638
#if HAVE__PTHREAD_WORKQUEUE_INIT && PTHREAD_WORKQUEUE_SPI_VERSION >= 20140213 \
639639
&& !defined(HAVE_PTHREAD_WORKQUEUE_QOS)
640640
#define HAVE_PTHREAD_WORKQUEUE_QOS 1

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
@@ -151,13 +152,13 @@ struct dispatch_root_queue_context_s {
151152
union {
152153
struct {
153154
int volatile dgq_pending;
154-
#if HAVE_PTHREAD_WORKQUEUES
155+
#if DISPATCH_USE_WORKQUEUES
155156
qos_class_t dgq_qos;
156157
int dgq_wq_priority, dgq_wq_options;
157158
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_USE_PTHREAD_POOL
158159
pthread_workqueue_t dgq_kworkqueue;
159160
#endif
160-
#endif // HAVE_PTHREAD_WORKQUEUES
161+
#endif // DISPATCH_USE_WORKQUEUES
161162
#if DISPATCH_USE_PTHREAD_POOL
162163
void *dgq_ctxt;
163164
int32_t volatile dgq_thread_pool_size;
@@ -179,7 +180,7 @@ typedef struct dispatch_root_queue_context_s *dispatch_root_queue_context_t;
179180
DISPATCH_CACHELINE_ALIGN
180181
static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
181182
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS] = {{{
182-
#if HAVE_PTHREAD_WORKQUEUES
183+
#if DISPATCH_USE_WORKQUEUES
183184
.dgq_qos = QOS_CLASS_MAINTENANCE,
184185
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
185186
.dgq_wq_options = 0,
@@ -190,7 +191,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
190191
#endif
191192
}}},
192193
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS_OVERCOMMIT] = {{{
193-
#if HAVE_PTHREAD_WORKQUEUES
194+
#if DISPATCH_USE_WORKQUEUES
194195
.dgq_qos = QOS_CLASS_MAINTENANCE,
195196
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
196197
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -201,7 +202,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
201202
#endif
202203
}}},
203204
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS] = {{{
204-
#if HAVE_PTHREAD_WORKQUEUES
205+
#if DISPATCH_USE_WORKQUEUES
205206
.dgq_qos = QOS_CLASS_BACKGROUND,
206207
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
207208
.dgq_wq_options = 0,
@@ -212,7 +213,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
212213
#endif
213214
}}},
214215
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS_OVERCOMMIT] = {{{
215-
#if HAVE_PTHREAD_WORKQUEUES
216+
#if DISPATCH_USE_WORKQUEUES
216217
.dgq_qos = QOS_CLASS_BACKGROUND,
217218
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
218219
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -223,7 +224,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
223224
#endif
224225
}}},
225226
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS] = {{{
226-
#if HAVE_PTHREAD_WORKQUEUES
227+
#if DISPATCH_USE_WORKQUEUES
227228
.dgq_qos = QOS_CLASS_UTILITY,
228229
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
229230
.dgq_wq_options = 0,
@@ -234,7 +235,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
234235
#endif
235236
}}},
236237
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS_OVERCOMMIT] = {{{
237-
#if HAVE_PTHREAD_WORKQUEUES
238+
#if DISPATCH_USE_WORKQUEUES
238239
.dgq_qos = QOS_CLASS_UTILITY,
239240
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
240241
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -245,7 +246,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
245246
#endif
246247
}}},
247248
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS] = {{{
248-
#if HAVE_PTHREAD_WORKQUEUES
249+
#if DISPATCH_USE_WORKQUEUES
249250
.dgq_qos = QOS_CLASS_DEFAULT,
250251
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
251252
.dgq_wq_options = 0,
@@ -256,7 +257,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
256257
#endif
257258
}}},
258259
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS_OVERCOMMIT] = {{{
259-
#if HAVE_PTHREAD_WORKQUEUES
260+
#if DISPATCH_USE_WORKQUEUES
260261
.dgq_qos = QOS_CLASS_DEFAULT,
261262
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
262263
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -267,7 +268,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
267268
#endif
268269
}}},
269270
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS] = {{{
270-
#if HAVE_PTHREAD_WORKQUEUES
271+
#if DISPATCH_USE_WORKQUEUES
271272
.dgq_qos = QOS_CLASS_USER_INITIATED,
272273
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
273274
.dgq_wq_options = 0,
@@ -278,7 +279,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
278279
#endif
279280
}}},
280281
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS_OVERCOMMIT] = {{{
281-
#if HAVE_PTHREAD_WORKQUEUES
282+
#if DISPATCH_USE_WORKQUEUES
282283
.dgq_qos = QOS_CLASS_USER_INITIATED,
283284
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
284285
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -289,7 +290,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
289290
#endif
290291
}}},
291292
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS] = {{{
292-
#if HAVE_PTHREAD_WORKQUEUES
293+
#if DISPATCH_USE_WORKQUEUES
293294
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
294295
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
295296
.dgq_wq_options = 0,
@@ -300,7 +301,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
300301
#endif
301302
}}},
302303
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS_OVERCOMMIT] = {{{
303-
#if HAVE_PTHREAD_WORKQUEUES
304+
#if DISPATCH_USE_WORKQUEUES
304305
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
305306
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
306307
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
@@ -576,11 +577,11 @@ dispatch_assert_queue_barrier(dispatch_queue_t dq)
576577
static inline bool
577578
_dispatch_root_queues_init_workq(int *wq_supported)
578579
{
579-
int r;
580+
int r; (void)r;
580581
bool result = false;
581582
*wq_supported = 0;
582-
#if HAVE_PTHREAD_WORKQUEUES
583-
bool disable_wq = false;
583+
#if DISPATCH_USE_WORKQUEUES
584+
bool disable_wq = false; (void)disable_wq;
584585
#if DISPATCH_ENABLE_THREAD_POOL && DISPATCH_DEBUG
585586
disable_wq = slowpath(getenv("LIBDISPATCH_DISABLE_KWQ"));
586587
#endif
@@ -683,7 +684,7 @@ _dispatch_root_queues_init_workq(int *wq_supported)
683684
#endif
684685
}
685686
#endif // DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
686-
#endif // HAVE_PTHREAD_WORKQUEUES
687+
#endif // DISPATCH_USE_WORKQUEUES
687688
return result;
688689
}
689690

@@ -699,7 +700,7 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
699700
thread_pool_size = pool_size;
700701
}
701702
qc->dgq_thread_pool_size = thread_pool_size;
702-
#if HAVE_PTHREAD_WORKQUEUES
703+
#if DISPATCH_USE_WORKQUEUES
703704
if (qc->dgq_qos) {
704705
(void)dispatch_assume_zero(pthread_attr_init(&pqc->dpq_thread_attr));
705706
(void)dispatch_assume_zero(pthread_attr_setdetachstate(
@@ -1757,7 +1758,7 @@ static struct dispatch_pthread_root_queue_context_s
17571758
_dispatch_mgr_root_queue_pthread_context;
17581759
static struct dispatch_root_queue_context_s
17591760
_dispatch_mgr_root_queue_context = {{{
1760-
#if HAVE_PTHREAD_WORKQUEUES
1761+
#if DISPATCH_USE_WORKQUEUES
17611762
.dgq_kworkqueue = (void*)(~0ul),
17621763
#endif
17631764
.dgq_ctxt = &_dispatch_mgr_root_queue_pthread_context,
@@ -2019,7 +2020,7 @@ _dispatch_pthread_root_queue_create(const char *label, unsigned long flags,
20192020

20202021
pqc->dpq_thread_mediator.do_vtable = DISPATCH_VTABLE(semaphore);
20212022
qc->dgq_ctxt = pqc;
2022-
#if HAVE_PTHREAD_WORKQUEUES
2023+
#if DISPATCH_USE_WORKQUEUES
20232024
qc->dgq_kworkqueue = (void*)(~0ul);
20242025
#endif
20252026
_dispatch_root_queue_init_pthread_pool(qc, pool_size, true);
@@ -3963,7 +3964,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)
39633964

39643965
_dispatch_root_queues_init();
39653966
_dispatch_debug_root_queue(dq, __func__);
3966-
#if HAVE_PTHREAD_WORKQUEUES
3967+
#if DISPATCH_USE_WORKQUEUES
39673968
#if DISPATCH_USE_PTHREAD_POOL
39683969
if (qc->dgq_kworkqueue != (void*)(~0ul))
39693970
#endif
@@ -3992,7 +3993,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)
39923993
(void)dispatch_assume_zero(r);
39933994
return;
39943995
}
3995-
#endif // HAVE_PTHREAD_WORKQUEUES
3996+
#endif // DISPATCH_USE_WORKQUEUES
39963997
#if DISPATCH_USE_PTHREAD_POOL
39973998
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
39983999
if (fastpath(pqc->dpq_thread_mediator.do_vtable)) {
@@ -4061,7 +4062,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
40614062
if (!_dispatch_queue_class_probe(dq)) {
40624063
return;
40634064
}
4064-
#if HAVE_PTHREAD_WORKQUEUES
4065+
#if DISPATCH_USE_WORKQUEUES
40654066
dispatch_root_queue_context_t qc = dq->do_ctxt;
40664067
if (
40674068
#if DISPATCH_USE_PTHREAD_POOL
@@ -4072,7 +4073,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
40724073
"global queue: %p", dq);
40734074
return;
40744075
}
4075-
#endif // HAVE_PTHREAD_WORKQUEUES
4076+
#endif // DISPATCH_USE_WORKQUEUES
40764077
return _dispatch_global_queue_poke_slow(dq, n, floor);
40774078
}
40784079

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

tests/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ AM_OBJCFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CBLOCKS_FLAGS)
9393
AM_CXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS) $(BSD_OVERLAY_CFLAGS)
9494
AM_OBJCXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS)
9595

96-
if !DISPATCH_USE_INTERNAL_WORKQUEUE
9796
if HAVE_PTHREAD_WORKQUEUES
9897
PTHREAD_WORKQUEUE_LIBS=-lpthread_workqueue
9998
endif
100-
endif
10199

102100
if BUILD_OWN_BLOCKS_RUNTIME
103101
CBLOCKS_FLAGS+= -I$(top_srcdir)/src/BlocksRuntime

0 commit comments

Comments
 (0)