summaryrefslogtreecommitdiff
path: root/src/thread-utils.c
diff options
context:
space:
mode:
authorAndreas Ericsson <ae@op5.se>2008-11-18 19:06:25 +0100
committerShawn O. Pearce <spearce@spearce.org>2008-11-18 10:32:53 -0800
commit36f0f61fc8578478a9dc9b33a1a0d07cdb98a5e5 (patch)
tree14be9ebaa30a1d6085e889eecaf2a24968eea24e /src/thread-utils.c
parenta57e9a8cc15b61ae1ab908b362fd829b3e36e513 (diff)
downloadlibgit2-36f0f61fc8578478a9dc9b33a1a0d07cdb98a5e5.tar.gz
Add compiler/platform agnostic thread-local storage
It doesn't cover all cases, but we can work on those as we go along. For now, gcc, MSVC++, Intel C/C++, IBM XL C/C++, Sun Studio C/C++ and Borland C++ Builder are the supported compilers (although we boldly assume that they all are of a recent enough version to support thread-local storage). This is intended to be used in upcoming patches that implement graceful (but TLS-dependant) error-handling in the library. As an added bonus, we also bring the online_cpus() function from git.git to detect the number of usable cpu's. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/thread-utils.c')
-rw-r--r--src/thread-utils.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/thread-utils.c b/src/thread-utils.c
new file mode 100644
index 000000000..c945829d4
--- /dev/null
+++ b/src/thread-utils.c
@@ -0,0 +1,48 @@
+#include "thread-utils.h"
+
+#ifdef _WIN32
+# define WIN32_LEAN_AND_MEAN
+# 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;
+}