Commit 9800728a746b872fdd22db9e68bc85ef0c851f27

Edward Thomson 2020-12-05T16:08:34

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

diff --git a/src/pack-objects.c b/src/pack-objects.c
index 03d05b7..8167853 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1158,7 +1158,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
 	int ret, active_threads = 0;
 
 	if (!pb->nr_threads)
-		pb->nr_threads = git_online_cpus();
+		pb->nr_threads = git__online_cpus();
 
 	if (pb->nr_threads <= 1) {
 		find_deltas(pb, list, &list_size, window, depth);
diff --git a/src/thread-utils.c b/src/thread-utils.c
deleted file mode 100644
index e5ec6a8..0000000
--- a/src/thread-utils.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-
-#include "common.h"
-#include "thread-utils.h"
-
-#ifdef _WIN32
-#ifndef WIN32_LEAN_AND_MEAN
-#	define WIN32_LEAN_AND_MEAN
-#endif
-#	include <windows.h>
-#elif defined(hpux) || defined(__hpux) || defined(_hpux)
-#	include <sys/pstat.h>
-#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;
-}
diff --git a/src/thread-utils.h b/src/thread-utils.h
index d71fabe..1014347 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -349,8 +349,6 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
 
 #define git__load(ptr) (void *)git___load((void * volatile *)&ptr)
 
-extern int git_online_cpus(void);
-
 #if defined(GIT_THREADS)
 
 # if defined(GIT_WIN32)
diff --git a/src/util.c b/src/util.c
index 2efb212..18a02a2 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;
+}
diff --git a/src/util.h b/src/util.h
index 185a1b1..ef5e4eb 100644
--- a/src/util.h
+++ b/src/util.h
@@ -414,6 +414,8 @@ GIT_INLINE(double) git__timer(void)
 
 extern int git__getenv(git_buf *out, const char *name);
 
+extern int git__online_cpus(void);
+
 GIT_INLINE(int) git__noop(void) { return 0; }
 
 #include "alloc.h"