Skip to content

clarify config of pthread_workqueue vs. internal_workqueue #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ AS_IF([test -n "$apple_libpthread_source_path" -a -n "$apple_xnu_source_osfmk_pa
AC_CHECK_HEADERS([pthread_machdep.h pthread/qos.h])

# pthread_workqueues.
# Look for own version first, than see if there is a system version.
# We can either use libdispatch's internal_workqueue or pthread_workqueue.
# If not specifically configured, default to internal_workqueues on
# Linux and pthread_workqueue on all other platforms.
# On any platform, if pthread_workqueue is not available, fall back
# to using internal_workqueue.
AC_ARG_ENABLE([internal-libpwq],
[AS_HELP_STRING([--enable-internal-libpwq],
[Use libdispatch's own implementation of pthread workqueues.])],,
Expand All @@ -333,15 +337,15 @@ AC_ARG_ENABLE([internal-libpwq],
esac]
)
AS_IF([test "x$enable_internal_libpwq" = "xyes"],
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread_workqueue API])
AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
dispatch_use_internal_workqueue=true
have_pthread_workqueues=true],
[dispatch_use_internal_workqueue=false
AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
[AC_DEFINE(DISPATCH_USE_INTERNAL_WORKQUEUE, 1, [Use libdispatch's own implementation of pthread workqueues])
have_pthread_workqueues=false,
dispatch_use_internal_workqueue=true],
[AC_CHECK_HEADERS([pthread/workqueue_private.h pthread_workqueue.h],
[AC_DEFINE(HAVE_PTHREAD_WORKQUEUES, 1, [Define if pthread work queues are present])
have_pthread_workqueues=true],
[have_pthread_workqueues=false]
have_pthread_workqueues=true,
dispatch_use_internal_workqueue=false],
[have_pthread_workqueues=false,
dispatch_use_internal_workqueue=true]
)]
)
AM_CONDITIONAL(DISPATCH_USE_INTERNAL_WORKQUEUE, $dispatch_use_internal_workqueue)
Expand Down
5 changes: 0 additions & 5 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,10 @@ AM_OBJCFLAGS=$(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS)
AM_CXXFLAGS=$(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)
AM_OBJCXXFLAGS=$(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)

if DISPATCH_USE_INTERNAL_WORKQUEUE
PTHREAD_WORKQUEUE_LIBS=
PTHREAD_WORKQUEUE_CFLAGS=
else
if HAVE_PTHREAD_WORKQUEUES
PTHREAD_WORKQUEUE_LIBS=-lpthread_workqueue
PTHREAD_WORKQUEUE_CFLAGS=
endif
endif

if BUILD_OWN_BLOCKS_RUNTIME
libdispatch_la_SOURCES+= BlocksRuntime/data.c BlocksRuntime/runtime.c
Expand Down
4 changes: 2 additions & 2 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,11 @@ _dispatch_fork_becomes_unsafe(void)

// Older Mac OS X and iOS Simulator fallbacks

#if HAVE_PTHREAD_WORKQUEUES
#if HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
#ifndef WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
#define WORKQ_ADDTHREADS_OPTION_OVERCOMMIT 0x00000001
#endif
#endif // HAVE_PTHREAD_WORKQUEUES
#endif // HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
#if HAVE__PTHREAD_WORKQUEUE_INIT && PTHREAD_WORKQUEUE_SPI_VERSION >= 20140213 \
&& !defined(HAVE_PTHREAD_WORKQUEUE_QOS)
#define HAVE_PTHREAD_WORKQUEUE_QOS 1
Expand Down
59 changes: 30 additions & 29 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@
#include "protocol.h" // _dispatch_send_wakeup_runloop_thread
#endif

#if (!HAVE_PTHREAD_WORKQUEUES || DISPATCH_DEBUG || DISPATCH_USE_INTERNAL_WORKQUEUE) && \
!defined(DISPATCH_ENABLE_THREAD_POOL)
#if HAVE_PTHREAD_WORKQUEUES || DISPATCH_USE_INTERNAL_WORKQUEUE
#define DISPATCH_USE_WORKQUEUES 1
#endif
#if (!HAVE_PTHREAD_WORKQUEUES || DISPATCH_DEBUG) && !defined(DISPATCH_ENABLE_THREAD_POOL)
#define DISPATCH_ENABLE_THREAD_POOL 1
#endif
#if DISPATCH_ENABLE_PTHREAD_ROOT_QUEUES || DISPATCH_ENABLE_THREAD_POOL
#define DISPATCH_USE_PTHREAD_POOL 1
#endif
#if HAVE_PTHREAD_WORKQUEUES && (!HAVE_PTHREAD_WORKQUEUE_QOS || DISPATCH_DEBUG) && \
!HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP && \
!DISPATCH_USE_INTERNAL_WORKQUEUE && \
!defined(DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK)
#define DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK 1
#endif
#if HAVE_PTHREAD_WORKQUEUES && DISPATCH_USE_PTHREAD_POOL && \
#if DISPATCH_USE_WORKQUEUES && DISPATCH_USE_PTHREAD_POOL && \
!DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
#define pthread_workqueue_t void*
#endif
Expand Down Expand Up @@ -151,13 +152,13 @@ struct dispatch_root_queue_context_s {
union {
struct {
int volatile dgq_pending;
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
qos_class_t dgq_qos;
int dgq_wq_priority, dgq_wq_options;
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_USE_PTHREAD_POOL
pthread_workqueue_t dgq_kworkqueue;
#endif
#endif // HAVE_PTHREAD_WORKQUEUES
#endif // DISPATCH_USE_WORKQUEUES
#if DISPATCH_USE_PTHREAD_POOL
void *dgq_ctxt;
int32_t volatile dgq_thread_pool_size;
Expand All @@ -179,7 +180,7 @@ typedef struct dispatch_root_queue_context_s *dispatch_root_queue_context_t;
DISPATCH_CACHELINE_ALIGN
static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_MAINTENANCE,
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
.dgq_wq_options = 0,
Expand All @@ -190,7 +191,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_MAINTENANCE_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_MAINTENANCE,
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand All @@ -201,7 +202,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_BACKGROUND,
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
.dgq_wq_options = 0,
Expand All @@ -212,7 +213,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_BACKGROUND_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_BACKGROUND,
.dgq_wq_priority = WORKQ_BG_PRIOQUEUE_CONDITIONAL,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand All @@ -223,7 +224,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_UTILITY,
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
.dgq_wq_options = 0,
Expand All @@ -234,7 +235,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_UTILITY_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_UTILITY,
.dgq_wq_priority = WORKQ_LOW_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand All @@ -245,7 +246,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_DEFAULT,
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
.dgq_wq_options = 0,
Expand All @@ -256,7 +257,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_DEFAULT_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_DEFAULT,
.dgq_wq_priority = WORKQ_DEFAULT_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand All @@ -267,7 +268,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_USER_INITIATED,
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
.dgq_wq_options = 0,
Expand All @@ -278,7 +279,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_USER_INITIATED,
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand All @@ -289,7 +290,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
.dgq_wq_options = 0,
Expand All @@ -300,7 +301,7 @@ static struct dispatch_root_queue_context_s _dispatch_root_queue_contexts[] = {
#endif
}}},
[DISPATCH_ROOT_QUEUE_IDX_USER_INTERACTIVE_QOS_OVERCOMMIT] = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_qos = QOS_CLASS_USER_INTERACTIVE,
.dgq_wq_priority = WORKQ_HIGH_PRIOQUEUE_CONDITIONAL,
.dgq_wq_options = WORKQ_ADDTHREADS_OPTION_OVERCOMMIT,
Expand Down Expand Up @@ -576,11 +577,11 @@ dispatch_assert_queue_barrier(dispatch_queue_t dq)
static inline bool
_dispatch_root_queues_init_workq(int *wq_supported)
{
int r;
int r; (void)r;
bool result = false;
*wq_supported = 0;
#if HAVE_PTHREAD_WORKQUEUES
bool disable_wq = false;
#if DISPATCH_USE_WORKQUEUES
bool disable_wq = false; (void)disable_wq;
#if DISPATCH_ENABLE_THREAD_POOL && DISPATCH_DEBUG
disable_wq = slowpath(getenv("LIBDISPATCH_DISABLE_KWQ"));
#endif
Expand Down Expand Up @@ -683,7 +684,7 @@ _dispatch_root_queues_init_workq(int *wq_supported)
#endif
}
#endif // DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK || DISPATCH_ENABLE_THREAD_POOL
#endif // HAVE_PTHREAD_WORKQUEUES
#endif // DISPATCH_USE_WORKQUEUES
return result;
}

Expand All @@ -699,7 +700,7 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
thread_pool_size = pool_size;
}
qc->dgq_thread_pool_size = thread_pool_size;
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
if (qc->dgq_qos) {
(void)dispatch_assume_zero(pthread_attr_init(&pqc->dpq_thread_attr));
(void)dispatch_assume_zero(pthread_attr_setdetachstate(
Expand Down Expand Up @@ -1757,7 +1758,7 @@ static struct dispatch_pthread_root_queue_context_s
_dispatch_mgr_root_queue_pthread_context;
static struct dispatch_root_queue_context_s
_dispatch_mgr_root_queue_context = {{{
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
.dgq_kworkqueue = (void*)(~0ul),
#endif
.dgq_ctxt = &_dispatch_mgr_root_queue_pthread_context,
Expand Down Expand Up @@ -2019,7 +2020,7 @@ _dispatch_pthread_root_queue_create(const char *label, unsigned long flags,

pqc->dpq_thread_mediator.do_vtable = DISPATCH_VTABLE(semaphore);
qc->dgq_ctxt = pqc;
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
qc->dgq_kworkqueue = (void*)(~0ul);
#endif
_dispatch_root_queue_init_pthread_pool(qc, pool_size, true);
Expand Down Expand Up @@ -3963,7 +3964,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)

_dispatch_root_queues_init();
_dispatch_debug_root_queue(dq, __func__);
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
#if DISPATCH_USE_PTHREAD_POOL
if (qc->dgq_kworkqueue != (void*)(~0ul))
#endif
Expand Down Expand Up @@ -3992,7 +3993,7 @@ _dispatch_global_queue_poke_slow(dispatch_queue_t dq, int n, int floor)
(void)dispatch_assume_zero(r);
return;
}
#endif // HAVE_PTHREAD_WORKQUEUES
#endif // DISPATCH_USE_WORKQUEUES
#if DISPATCH_USE_PTHREAD_POOL
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
if (fastpath(pqc->dpq_thread_mediator.do_vtable)) {
Expand Down Expand Up @@ -4061,7 +4062,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
if (!_dispatch_queue_class_probe(dq)) {
return;
}
#if HAVE_PTHREAD_WORKQUEUES
#if DISPATCH_USE_WORKQUEUES
dispatch_root_queue_context_t qc = dq->do_ctxt;
if (
#if DISPATCH_USE_PTHREAD_POOL
Expand All @@ -4072,7 +4073,7 @@ _dispatch_global_queue_poke(dispatch_queue_t dq, int n, int floor)
"global queue: %p", dq);
return;
}
#endif // HAVE_PTHREAD_WORKQUEUES
#endif // DISPATCH_USE_WORKQUEUES
return _dispatch_global_queue_poke_slow(dq, n, floor);
}

Expand Down
6 changes: 4 additions & 2 deletions src/shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#if HAVE_PTHREAD_WORKQUEUES
#if __has_include(<pthread/workqueue_private.h>)
#include <pthread/workqueue_private.h>
#elif DISPATCH_USE_INTERNAL_WORKQUEUE
#include <event/workqueue_internal.h>
#else
#include <pthread_workqueue.h>
#endif
Expand All @@ -51,6 +49,10 @@
#endif
#endif // HAVE_PTHREAD_WORKQUEUES

#if DISPATCH_USE_INTERNAL_WORKQUEUE
#include <event/workqueue_internal.h>
#endif

#if HAVE_PTHREAD_NP_H
#include <pthread_np.h>
#endif
Expand Down
2 changes: 0 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ AM_OBJCFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CBLOCKS_FLAGS)
AM_CXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS) $(BSD_OVERLAY_CFLAGS)
AM_OBJCXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS)

if !DISPATCH_USE_INTERNAL_WORKQUEUE
if HAVE_PTHREAD_WORKQUEUES
PTHREAD_WORKQUEUE_LIBS=-lpthread_workqueue
endif
endif

if BUILD_OWN_BLOCKS_RUNTIME
CBLOCKS_FLAGS+= -I$(top_srcdir)/src/BlocksRuntime
Expand Down