Skip to content

Commit bcb5d66

Browse files
committed
fixes to address Pierre's comments from May 2 review
1 parent ad5d515 commit bcb5d66

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/event/workqueue.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ _dispatch_workq_monitor_pools(void *context DISPATCH_UNUSED)
215215

216216
// temporary until we switch over to QoS based interface.
217217
static dispatch_queue_t
218-
get_root_queue_from_legacy_priority(int priority) {
218+
get_root_queue_from_legacy_priority(int priority)
219+
{
219220
switch (priority) {
220221
case WORKQ_HIGH_PRIOQUEUE:
221222
return &_dispatch_root_queues[DISPATCH_ROOT_QUEUE_IDX_USER_INITIATED_QOS];
@@ -252,6 +253,7 @@ _dispatch_workq_init_once(void *context DISPATCH_UNUSED)
252253
dispatch_source_set_timer(ds, dispatch_time(DISPATCH_TIME_NOW, 0),
253254
NSEC_PER_SEC, 0);
254255
dispatch_source_set_event_handler_f(ds, _dispatch_workq_monitor_pools);
256+
dispatch_set_context(ds, ds); // avoid appearing as leaked
255257
dispatch_activate(ds);
256258
}
257259

src/queue.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static struct dispatch_pthread_root_queue_context_s
151151
struct dispatch_root_queue_context_s {
152152
union {
153153
struct {
154-
unsigned int volatile dgq_pending;
154+
int volatile dgq_pending;
155155
#if HAVE_PTHREAD_WORKQUEUES
156156
qos_class_t dgq_qos;
157157
int dgq_wq_priority, dgq_wq_options;
@@ -669,6 +669,10 @@ _dispatch_root_queues_init_workq(int *wq_supported)
669669
}
670670
#endif // DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
671671
qc->dgq_kworkqueue = pwq ? pwq : (void*)(~0ul);
672+
// because the fastpath of _dispatch_global_queue_poke didn't
673+
// know yet that we're using the internal pool implementation
674+
// we have to undo its setting of dgq_pending
675+
qc->dgq_pending = 0;
672676
}
673677
#if DISPATCH_USE_LEGACY_WORKQUEUE_FALLBACK
674678
if (!disable_wq) {
@@ -694,13 +698,6 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
694698
thread_pool_size = pool_size;
695699
}
696700
qc->dgq_thread_pool_size = thread_pool_size;
697-
if (qc->dgq_kworkqueue == (void*)(~0ul)) {
698-
// when _dispatch_root_queues_init is invoked from
699-
// _dispatch_global_queue_poke_slow we need to clear
700-
// dgq_pending here because qc->dgq_kworkqueue wasn't
701-
// initialized to (~0ul) when we went through _dispatch_global_queue_poke
702-
qc->dgq_pending = 0;
703-
}
704701
#if HAVE_PTHREAD_WORKQUEUES
705702
if (qc->dgq_qos) {
706703
(void)dispatch_assume_zero(pthread_attr_init(&pqc->dpq_thread_attr));
@@ -1991,9 +1988,8 @@ _dispatch_pthread_root_queue_create(const char *label, unsigned long flags,
19911988
dispatch_pthread_root_queue_context_t pqc;
19921989
dispatch_queue_flags_t dqf = 0;
19931990
size_t dqs;
1994-
uint8_t pool_size_u = flags & _DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE ?
1991+
int32_t pool_size = flags & _DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE ?
19951992
(int8_t)(flags & ~_DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE) : 0;
1996-
int32_t pool_size = (int32_t)pool_size_u;
19971993

19981994
dqs = sizeof(struct dispatch_queue_s) - DISPATCH_QUEUE_CACHELINE_PAD;
19991995
dqs = roundup(dqs, _Alignof(struct dispatch_root_queue_context_s));
@@ -5445,7 +5441,7 @@ _dispatch_worker_thread4(void *context)
54455441
dispatch_root_queue_context_t qc = dq->do_ctxt;
54465442

54475443
_dispatch_introspection_thread_add();
5448-
int pending = (int)os_atomic_dec2o(qc, dgq_pending, relaxed);
5444+
int pending = os_atomic_dec2o(qc, dgq_pending, relaxed);
54495445
dispatch_assert(pending >= 0);
54505446
_dispatch_root_queue_drain(dq, _dispatch_get_priority());
54515447
_dispatch_voucher_debug("root queue clear", NULL);
@@ -5491,8 +5487,10 @@ _dispatch_worker_thread(void *context)
54915487
dispatch_root_queue_context_t qc = dq->do_ctxt;
54925488
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
54935489

5494-
int pending = (int)os_atomic_dec2o(qc, dgq_pending, relaxed);
5495-
dispatch_assert(pending >= 0);
5490+
int pending = os_atomic_dec2o(qc, dgq_pending, relaxed);
5491+
if (unlikely(pending < 0)) {
5492+
DISPATCH_INTERNAL_CRASH(pending, "Pending thread request underflow");
5493+
}
54965494

54975495
if (pqc->dpq_observer_hooks.queue_will_execute) {
54985496
_dispatch_set_pthread_root_queue_observer_hooks(

0 commit comments

Comments
 (0)