aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/platform.h4
-rw-r--r--src/core/taskq.c11
-rw-r--r--src/platform/posix/posix_thread.c12
-rw-r--r--src/platform/windows/win_thread.c9
4 files changed, 32 insertions, 4 deletions
diff --git a/src/core/platform.h b/src/core/platform.h
index 07bfc14c..b709e3ba 100644
--- a/src/core/platform.h
+++ b/src/core/platform.h
@@ -190,6 +190,10 @@ extern int nni_plat_init(int (*)(void));
// will be called until nni_platform_init is called.
extern void nni_plat_fini(void);
+// nni_plat_ncpu returns the number of logical CPUs on the system. This is
+// used to scale the number of independent threads started.
+extern int nni_plat_ncpu(void);
+
//
// TCP Support.
//
diff --git a/src/core/taskq.c b/src/core/taskq.c
index ae66ec67..82a7456b 100644
--- a/src/core/taskq.c
+++ b/src/core/taskq.c
@@ -261,11 +261,14 @@ nni_task_fini(nni_task *task)
int
nni_taskq_sys_init(void)
{
- int rv;
+ int nthrs;
- // XXX: Make the "16" = NCPUs * 2
- rv = nni_taskq_init(&nni_taskq_systq, 16);
- return (rv);
+ nthrs = nni_plat_ncpu() * 2;
+ if (nthrs < 2) {
+ nthrs = 2;
+ }
+
+ return (nni_taskq_init(&nni_taskq_systq, nthrs));
}
void
diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c
index f016ca3f..df2ee9d2 100644
--- a/src/platform/posix/posix_thread.c
+++ b/src/platform/posix/posix_thread.c
@@ -336,4 +336,16 @@ nni_plat_fini(void)
pthread_mutex_unlock(&nni_plat_init_lock);
}
+int
+nni_plat_ncpu(void)
+{
+ // POSIX specifies sysconf exists, but not the value
+ // _SC_NPROCESSORS_ONLN. Nonetheless, everybody implements it.
+ // If you don't we'll assume you only have a single logical CPU.
+#ifdef _SC_NPROCESSORS_ONLN
+ return (sysconf(_SC_NPROCESSORS_ONLN));
+#else
+ return (1);
+#endif
+}
#endif // NNG_PLATFORM_POSIX
diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c
index 2e9d58d7..a3d932aa 100644
--- a/src/platform/windows/win_thread.c
+++ b/src/platform/windows/win_thread.c
@@ -148,6 +148,15 @@ nni_plat_thr_is_self(nni_plat_thr *thr)
static LONG plat_inited = 0;
int
+nni_plat_ncpu(void)
+{
+ SYSTEM_INFO info;
+
+ GetSystemInfo(&info);
+ return ((int) (info.dwNumberOfProcessors));
+}
+
+int
nni_plat_init(int (*helper)(void))
{
int rv = 0;