summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-12-05 16:08:34 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2020-12-06 00:55:35 +0000
commit9800728a746b872fdd22db9e68bc85ef0c851f27 (patch)
treeb9d289d9c36d69a67daa0adae2719a3c1c656705 /src/util.c
parent1adb84107f406a00178296257289663860d277ae (diff)
downloadlibgit2-9800728a746b872fdd22db9e68bc85ef0c851f27.tar.gz
util: move git_online_cpus into util
The number of CPUs is useful information for creating a thread pool or a number of workers, but it's not really about threading directly. Evict it from the thread file
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 2efb212bc..18a02a265 100644
--- a/src/util.c
+++ b/src/util.c
@@ -13,6 +13,11 @@
# include "win32/utf-conv.h"
# include "win32/w32_buffer.h"
+# ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+# endif
+# include <windows.h>
+
# ifdef HAVE_QSORT_S
# include <search.h>
# endif
@@ -22,6 +27,10 @@
# include <Shlwapi.h>
#endif
+#if defined(hpux) || defined(__hpux) || defined(_hpux)
+# include <sys/pstat.h>
+#endif
+
int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
const char *p;
@@ -893,3 +902,43 @@ int git__getenv(git_buf *out, const char *name)
return git_buf_puts(out, val);
}
#endif
+
+/*
+ * By doing this in two steps we can at least get
+ * the function to be somewhat coherent, even
+ * with this disgusting nest of #ifdefs.
+ */
+#ifndef _SC_NPROCESSORS_ONLN
+# ifdef _SC_NPROC_ONLN
+# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
+# elif defined _SC_CRAY_NCPU
+# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
+# endif
+#endif
+
+int git__online_cpus(void)
+{
+#ifdef _SC_NPROCESSORS_ONLN
+ long ncpus;
+#endif
+
+#ifdef _WIN32
+ SYSTEM_INFO info;
+ GetSystemInfo(&info);
+
+ if ((int)info.dwNumberOfProcessors > 0)
+ return (int)info.dwNumberOfProcessors;
+#elif defined(hpux) || defined(__hpux) || defined(_hpux)
+ struct pst_dynamic psd;
+
+ if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
+ return (int)psd.psd_proc_cnt;
+#endif
+
+#ifdef _SC_NPROCESSORS_ONLN
+ if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
+ return (int)ncpus;
+#endif
+
+ return 1;
+}