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

Commit c02b5ae

Browse files
author
Anton Pogonets
committed
Add LIBDISPATCH_DEFAULT_THREAD_POOL_SIZE env variable
1 parent 047524c commit c02b5ae

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
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 !defined(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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6168,10 +6168,16 @@ _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-
int thread_pool_size = DISPATCH_WORKQ_MAX_PTHREAD_COUNT;
6172-
if (!(pri & DISPATCH_PRIORITY_FLAG_OVERCOMMIT)) {
6173-
thread_pool_size = (int32_t)dispatch_hw_config(active_cpus);
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);
61746178
}
6179+
int32_t thread_pool_size = overcommit ? DISPATCH_WORKQ_MAX_PTHREAD_COUNT : default_pool_size;
6180+
61756181
if (pool_size && pool_size < thread_pool_size) thread_pool_size = pool_size;
61766182
dq->dgq_thread_pool_size = thread_pool_size;
61776183
qos_class_t cls = _dispatch_qos_to_qos_class(_dispatch_priority_qos(pri) ?:

0 commit comments

Comments
 (0)