Skip to content

Commit 1051065

Browse files
author
Anton Pogonets
committed
Add LIBDISPATCH_DEFAULT_THREAD_POOL_SIZE env variable
1 parent 6a9d597 commit 1051065

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,4 +977,11 @@ extern int _dispatch_kevent_workqueue_enabled;
977977
#include "inline_internal.h"
978978
#include "firehose/firehose_internal.h"
979979

980+
#if !defined(HAVE_PTHREAD_WORKQUEUE_KEVENT)
981+
// copied from https://opensource.apple.com/source/libpthread/libpthread-301.50.1/kern/workqueue_internal.h.auto.html
982+
#define WORKQUEUE_MAXTHREADS 512
983+
#define WORKQUEUE_CONSTRAINED_MAXTHREADS (WORKQUEUE_MAXTHREADS >> 3)
984+
#define WORKQUEUE_CONSTRAINED_FACTOR 5
985+
#endif
986+
980987
#endif /* __DISPATCH_INTERNAL__ */

src/queue.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,16 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
755755
int32_t pool_size, bool overcommit)
756756
{
757757
dispatch_pthread_root_queue_context_t pqc = qc->dgq_ctxt;
758-
int32_t thread_pool_size = overcommit ? DISPATCH_WORKQ_MAX_PTHREAD_COUNT :
759-
(int32_t)dispatch_hw_config(active_cpus);
758+
int32_t default_pool_size = 0;
759+
char* default_pool_size_env = getenv("LIBDISPATCH_DEFAULT_THREAD_POOL_SIZE");
760+
if (default_pool_size_env) {
761+
default_pool_size = (int32_t) atoi(default_pool_size_env);
762+
}
763+
if (!default_pool_size) {
764+
default_pool_size = (int32_t) MAX(dispatch_hw_config(active_cpus) * 5, WORKQUEUE_CONSTRAINED_MAXTHREADS);
765+
}
766+
int32_t thread_pool_size = overcommit ? DISPATCH_WORKQ_MAX_PTHREAD_COUNT : default_pool_size;
767+
760768
if (slowpath(pool_size) && pool_size < thread_pool_size) {
761769
thread_pool_size = pool_size;
762770
}

0 commit comments

Comments
 (0)