mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-08 09:02:55 +00:00
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>
This commit is contained in:
parent
a57e9a8cc1
commit
36f0f61fc8
25
src/git/thread-utils.h
Normal file
25
src/git/thread-utils.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef INCLUDE_git_thread_utils_h__
|
||||
#define INCLUDE_git_thread_utils_h__
|
||||
/*
|
||||
* How TLS works is compiler+platform dependant
|
||||
* Sources: http://en.wikipedia.org/wiki/Thread-Specific_Storage
|
||||
* http://predef.sourceforge.net/precomp.html
|
||||
*/
|
||||
#define GIT_HAS_TLS 1
|
||||
#if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || \
|
||||
defined(__xlc__) || defined(__xlC__)
|
||||
# define GIT_TLS __thread
|
||||
#elif defined(__INTEL_COMPILER)
|
||||
# if defined(_WIN32) || defined(_WIN32_CE)
|
||||
# define GIT_TLS __declspec(thread)
|
||||
# else
|
||||
# define GIT_TLS __thread
|
||||
# endif
|
||||
#elif defined(_WIN32) || defined(_WIN32_CE) || defined(__BORLANDC__)
|
||||
# define GIT_TLS __declspec(thread)
|
||||
#else
|
||||
# undef GIT_HAS_TLS
|
||||
# define GIT_TLS /* nothing: tls vars are thread-global */
|
||||
#endif
|
||||
|
||||
#endif /* INCLUDE_git_thread_utils_h__ */
|
48
src/thread-utils.c
Normal file
48
src/thread-utils.c
Normal file
@ -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;
|
||||
}
|
8
src/thread-utils.h
Normal file
8
src/thread-utils.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef INCLUDE_thread_utils_h__
|
||||
#define INCLUDE_thread_utils_h__
|
||||
|
||||
#include "git/thread-utils.h"
|
||||
|
||||
extern int git_online_cpus(void);
|
||||
|
||||
#endif /* INCLUDE_thread_utils_h__ */
|
Loading…
Reference in New Issue
Block a user