Skip to content

Commit 0df3bd1

Browse files
committed
only oversubscribe if no pending thread creates
1 parent 9255ebd commit 0df3bd1

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/event/workqueue.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,12 @@ _dispatch_workq_monitor_pools(void *context DISPATCH_UNUSED)
207207
int32_t limit =
208208
mon->num_runnable == 0 ? limit_b : MIN(limit_a, limit_b);
209209
if (count + limit > 0) {
210-
_dispatch_debug("workq: adding one worker to %s", dq->dq_label);
211-
_dispatch_pthread_root_queue_oversubscribe(dq, 1);
210+
bool r = _dispatch_pthread_root_queue_oversubscribe(dq, 1);
211+
if (r) {
212+
_dispatch_debug("workq: requested overscribe worker for %s", dq->dq_label);
213+
} else {
214+
_dispatch_debug("workq: still pending worker create on %s", dq->dq_label);
215+
}
212216
} else {
213217
_dispatch_debug("workq: %s already over by %d; taking no action",
214218
dq->dq_label, -count);

src/queue.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3975,16 +3975,22 @@ _dispatch_pthread_root_queue_spawn_workers(dispatch_queue_t dq,
39753975
} while (--n);
39763976
}
39773977

3978-
void
3978+
bool
39793979
_dispatch_pthread_root_queue_oversubscribe(dispatch_queue_t dq, int n)
39803980
{
39813981
dispatch_root_queue_context_t qc = dq->do_ctxt;
39823982
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
39833983
pthread_attr_t *attr = &pqc->dpq_thread_attr;
39843984

3985+
if (!os_atomic_cmpxchg2o(qc, dgq_pending, 0, n, relaxed)) {
3986+
return false;
3987+
}
3988+
39853989
(void)os_atomic_sub2o(qc, dgq_thread_pool_size, n, release);
39863990

39873991
_dispatch_pthread_root_queue_spawn_workers(dq, attr, n);
3992+
3993+
return true;
39883994
}
39893995
#endif
39903996

src/queue_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ void _dispatch_mgr_queue_drain(void);
594594
#if DISPATCH_USE_MGR_THREAD && DISPATCH_ENABLE_PTHREAD_ROOT_QUEUES
595595
void _dispatch_mgr_priority_init(void);
596596
int32_t _dispatch_pthread_root_queue_thread_pool_size(dispatch_queue_t dq);
597-
void _dispatch_pthread_root_queue_oversubscribe(dispatch_queue_t dq, int n);
597+
bool _dispatch_pthread_root_queue_oversubscribe(dispatch_queue_t dq, int n);
598598
#else
599599
static inline void _dispatch_mgr_priority_init(void) {}
600600
#endif

0 commit comments

Comments
 (0)