Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c49c13c

Browse files
Anton Pogonetszayass
Anton Pogonets
authored andcommitted
Add LIBDISPATCH_DEFAULT_THREAD_POOL_SIZE env variable
1 parent 047524c commit c49c13c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/internal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,12 @@ extern bool _dispatch_kevent_workqueue_enabled;
11121112
#include "inline_internal.h"
11131113
#include "firehose/firehose_internal.h"
11141114

1115-
__END_DECLS
1115+
#if !HAVE_PTHREAD_WORKQUEUE_KEVENT
1116+
// copied from https://opensource.apple.com/source/libpthread/libpthread-301.50.1/kern/workqueue_internal.h.auto.html
1117+
#define WORKQUEUE_MAXTHREADS 512
1118+
#define WORKQUEUE_CONSTRAINED_MAXTHREADS (WORKQUEUE_MAXTHREADS >> 3)
1119+
#define WORKQUEUE_CONSTRAINED_FACTOR 5
1120+
#endif
11161121

1122+
__END_DECLS
11171123
#endif /* __DISPATCH_INTERNAL__ */

src/queue.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6168,10 +6168,19 @@ _dispatch_root_queue_init_pthread_pool(dispatch_queue_global_t dq,
61686168
int pool_size, dispatch_priority_t pri)
61696169
{
61706170
dispatch_pthread_root_queue_context_t pqc = dq->do_ctxt;
6171+
int32_t default_pool_size = 0;
6172+
char* default_pool_size_env = getenv("LIBDISPATCH_DEFAULT_THREAD_POOL_SIZE");
6173+
if (default_pool_size_env) {
6174+
default_pool_size = (int32_t) atoi(default_pool_size_env);
6175+
}
6176+
if (!default_pool_size) {
6177+
default_pool_size = (int32_t) MAX(dispatch_hw_config(active_cpus) * 5, WORKQUEUE_CONSTRAINED_MAXTHREADS);
6178+
}
61716179
int thread_pool_size = DISPATCH_WORKQ_MAX_PTHREAD_COUNT;
61726180
if (!(pri & DISPATCH_PRIORITY_FLAG_OVERCOMMIT)) {
6173-
thread_pool_size = (int32_t)dispatch_hw_config(active_cpus);
6181+
thread_pool_size = default_pool_size;
61746182
}
6183+
61756184
if (pool_size && pool_size < thread_pool_size) thread_pool_size = pool_size;
61766185
dq->dgq_thread_pool_size = thread_pool_size;
61776186
qos_class_t cls = _dispatch_qos_to_qos_class(_dispatch_priority_qos(pri) ?:

0 commit comments

Comments
 (0)