Skip to content

Commit 9800728

Browse files
committed
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
1 parent 1adb841 commit 9800728

File tree

5 files changed

+52
-61
lines changed

5 files changed

+52
-61
lines changed

src/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
11581158
int ret, active_threads = 0;
11591159

11601160
if (!pb->nr_threads)
1161-
pb->nr_threads = git_online_cpus();
1161+
pb->nr_threads = git__online_cpus();
11621162

11631163
if (pb->nr_threads <= 1) {
11641164
find_deltas(pb, list, &list_size, window, depth);

src/thread-utils.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/thread-utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,6 @@ GIT_INLINE(int64_t) git_atomic64_get(git_atomic64 *a)
349349

350350
#define git__load(ptr) (void *)git___load((void * volatile *)&ptr)
351351

352-
extern int git_online_cpus(void);
353-
354352
#if defined(GIT_THREADS)
355353

356354
# if defined(GIT_WIN32)

src/util.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
# include "win32/utf-conv.h"
1414
# include "win32/w32_buffer.h"
1515

16+
# ifndef WIN32_LEAN_AND_MEAN
17+
# define WIN32_LEAN_AND_MEAN
18+
# endif
19+
# include <windows.h>
20+
1621
# ifdef HAVE_QSORT_S
1722
# include <search.h>
1823
# endif
@@ -22,6 +27,10 @@
2227
# include <Shlwapi.h>
2328
#endif
2429

30+
#if defined(hpux) || defined(__hpux) || defined(_hpux)
31+
# include <sys/pstat.h>
32+
#endif
33+
2534
int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
2635
{
2736
const char *p;
@@ -893,3 +902,43 @@ int git__getenv(git_buf *out, const char *name)
893902
return git_buf_puts(out, val);
894903
}
895904
#endif
905+
906+
/*
907+
* By doing this in two steps we can at least get
908+
* the function to be somewhat coherent, even
909+
* with this disgusting nest of #ifdefs.
910+
*/
911+
#ifndef _SC_NPROCESSORS_ONLN
912+
# ifdef _SC_NPROC_ONLN
913+
# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
914+
# elif defined _SC_CRAY_NCPU
915+
# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
916+
# endif
917+
#endif
918+
919+
int git__online_cpus(void)
920+
{
921+
#ifdef _SC_NPROCESSORS_ONLN
922+
long ncpus;
923+
#endif
924+
925+
#ifdef _WIN32
926+
SYSTEM_INFO info;
927+
GetSystemInfo(&info);
928+
929+
if ((int)info.dwNumberOfProcessors > 0)
930+
return (int)info.dwNumberOfProcessors;
931+
#elif defined(hpux) || defined(__hpux) || defined(_hpux)
932+
struct pst_dynamic psd;
933+
934+
if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
935+
return (int)psd.psd_proc_cnt;
936+
#endif
937+
938+
#ifdef _SC_NPROCESSORS_ONLN
939+
if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
940+
return (int)ncpus;
941+
#endif
942+
943+
return 1;
944+
}

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ GIT_INLINE(double) git__timer(void)
414414

415415
extern int git__getenv(git_buf *out, const char *name);
416416

417+
extern int git__online_cpus(void);
418+
417419
GIT_INLINE(int) git__noop(void) { return 0; }
418420

419421
#include "alloc.h"

0 commit comments

Comments
 (0)