Import Geant4 10.5.0.beta source tree
This commit is contained in:
@@ -40,179 +40,242 @@
|
||||
|
||||
#include "G4Types.hh"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <future>
|
||||
#include <vector>
|
||||
|
||||
// Macro to put current thread to sleep
|
||||
//
|
||||
#if defined(WIN32)
|
||||
#define G4THREADSLEEP( tick ) { Sleep(tick); }
|
||||
#else
|
||||
#include <unistd.h> // needed for sleep()
|
||||
#define G4THREADSLEEP( tick ) { sleep(tick); }
|
||||
#endif
|
||||
#define G4THREADSLEEP(tick) \
|
||||
std::this_thread::sleep_for(std::chrono::seconds( tick ))
|
||||
|
||||
// will be used in the future when migrating threading to task-based style
|
||||
//template <typename _Tp> using G4Future = std::future<_Tp>;
|
||||
//template <typename _Tp> using G4SharedFuture = std::shared_future<_Tp>;
|
||||
//template <typename _Tp> using G4Promise = std::promise<_Tp>;
|
||||
|
||||
//
|
||||
// NOTE ON GEANT4 SERIAL BUILDS AND MUTEX/UNIQUE_LOCK
|
||||
// ==================================================
|
||||
//
|
||||
// G4Mutex and G4RecursiveMutex are always C++11 std::mutex types
|
||||
// however, in serial mode, using G4MUTEXLOCK and G4MUTEXUNLOCK on these
|
||||
// types has no effect -- i.e. the mutexes are not actually locked or unlocked
|
||||
//
|
||||
// Additionally, when a G4Mutex or G4RecursiveMutex is used with G4AutoLock
|
||||
// and G4RecursiveAutoLock, respectively, these classes also suppressing
|
||||
// the locking and unlocking of the mutex. Regardless of the build type,
|
||||
// G4AutoLock and G4RecursiveAutoLock inherit from std::unique_lock<std::mutex>
|
||||
// and std::unique_lock<std::recursive_mutex>, respectively. This means
|
||||
// that in situations (such as is needed by the analysis category), the
|
||||
// G4AutoLock and G4RecursiveAutoLock can be passed to functions requesting
|
||||
// a std::unique_lock. Within these functions, since std::unique_lock
|
||||
// member functions are not virtual, they will not retain the dummy locking
|
||||
// and unlocking behavior
|
||||
// --> An example of this behavior can be found in G4AutoLock.hh
|
||||
//
|
||||
// Jonathan R. Madsen (February 21, 2018)
|
||||
//
|
||||
|
||||
// global mutex types
|
||||
typedef std::mutex G4Mutex;
|
||||
typedef std::recursive_mutex G4RecursiveMutex;
|
||||
|
||||
// mutex macros
|
||||
#define G4MUTEX_INITIALIZER {}
|
||||
#define G4MUTEXINIT(mutex) ;;
|
||||
#define G4MUTEXDESTROY(mutex) ;;
|
||||
|
||||
// static functions: get_id(), sleep_for(...), sleep_until(...), yield(),
|
||||
namespace G4ThisThread { using namespace std::this_thread; }
|
||||
|
||||
// will be used in the future when migrating threading to task-based style
|
||||
// and are currently used in unit tests
|
||||
template <typename _Tp> using G4Promise = std::promise<_Tp>;
|
||||
template <typename _Tp> using G4Future = std::future<_Tp>;
|
||||
template <typename _Tp> using G4SharedFuture = std::shared_future<_Tp>;
|
||||
|
||||
// Some useful types
|
||||
typedef void* G4ThreadFunReturnType;
|
||||
typedef void* G4ThreadFunArgType;
|
||||
typedef G4int (*thread_lock)(G4Mutex*);
|
||||
typedef G4int (*thread_unlock)(G4Mutex*);
|
||||
|
||||
// Helper function for getting a unique static mutex for a specific
|
||||
// class or type
|
||||
// Usage example:
|
||||
// a template class "G4Cache<T>" that required a static
|
||||
// mutex for specific to type T:
|
||||
// G4AutoLock l(G4TypeMutex<G4Cache<T>>());
|
||||
template <typename _Tp>
|
||||
G4Mutex& G4TypeMutex(const unsigned int& _n = 0)
|
||||
{
|
||||
static G4Mutex* _mutex = new G4Mutex();
|
||||
if(_n == 0)
|
||||
return *_mutex;
|
||||
|
||||
static std::vector<G4Mutex*> _mutexes;
|
||||
if(_n > _mutexes.size())
|
||||
_mutexes.resize(_n, nullptr);
|
||||
if(!_mutexes[_n])
|
||||
_mutexes[_n] = new G4Mutex();
|
||||
return *(_mutexes[_n-1]);
|
||||
}
|
||||
|
||||
// Helper function for getting a unique static recursive_mutex for a
|
||||
// specific class or type
|
||||
// Usage example:
|
||||
// a template class "G4Cache<T>" that required a static
|
||||
// recursive_mutex for specific to type T:
|
||||
// G4RecursiveAutoLock l(G4TypeRecursiveMutex<G4Cache<T>>());
|
||||
template <typename _Tp>
|
||||
G4RecursiveMutex& G4TypeRecursiveMutex(const unsigned int& _n = 0)
|
||||
{
|
||||
static G4RecursiveMutex* _mutex = new G4RecursiveMutex();
|
||||
if(_n == 0)
|
||||
return *(_mutex);
|
||||
|
||||
static std::vector<G4RecursiveMutex*> _mutexes;
|
||||
if(_n > _mutexes.size())
|
||||
_mutexes.resize(_n, nullptr);
|
||||
if(!_mutexes[_n])
|
||||
_mutexes[_n] = new G4RecursiveMutex();
|
||||
return *(_mutexes[_n-1]);
|
||||
}
|
||||
|
||||
#if defined(G4MULTITHREADED)
|
||||
//===============================
|
||||
// Multi-threaded build
|
||||
//===============================
|
||||
#if ( defined(__MACH__) && defined(__clang__) && defined(__x86_64__) ) || \
|
||||
( defined(__MACH__) && defined(__GNUC__) && (__GNUC__>=4 && __GNUC_MINOR__>=7 || __GNUC__>=5) ) || \
|
||||
defined(__linux__) || defined(_AIX)
|
||||
//
|
||||
// Multi-threaded build: for POSIX systems
|
||||
//
|
||||
#include <pthread.h>
|
||||
#if defined(__MACH__) // needed only for MacOSX for definition of pid_t
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
//==========================================
|
||||
// G4MULTITHREADED is ON - threading enabled
|
||||
//==========================================
|
||||
|
||||
typedef pthread_mutex_t G4Mutex;
|
||||
typedef pthread_t G4Thread;
|
||||
// global thread types
|
||||
typedef std::thread G4Thread;
|
||||
typedef std::thread::native_handle_type G4NativeThread;
|
||||
|
||||
// G4Mutex initializer macro
|
||||
//
|
||||
#define G4MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
// Lock/unlock a G4Mutex function name
|
||||
//
|
||||
#define G4MUTEXLOCK pthread_mutex_lock
|
||||
#define G4MUTEXUNLOCK pthread_mutex_unlock
|
||||
|
||||
// Macro to initialize a Mutex
|
||||
//
|
||||
#define G4MUTEXINIT(mutex) pthread_mutex_init( &mutex , NULL);
|
||||
#define G4MUTEXDESTROY(mutex) pthread_mutex_destroy( &mutex );
|
||||
|
||||
// Macro to create a G4Thread object
|
||||
//
|
||||
#define G4THREADCREATE( worker , func , arg ) { \
|
||||
pthread_attr_t attr; \
|
||||
pthread_attr_init(&attr); \
|
||||
pthread_attr_setstacksize(&attr,16*1024*1024); \
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); \
|
||||
pthread_create( worker, &attr, func , arg ); \
|
||||
}
|
||||
// mutex macros
|
||||
#define G4MUTEXLOCK(mutex) { (mutex)->lock(); }
|
||||
#define G4MUTEXUNLOCK(mutex) { (mutex)->unlock(); }
|
||||
|
||||
// Macro to join thread
|
||||
//
|
||||
#define G4THREADJOIN( worker ) pthread_join( worker , NULL)
|
||||
#define G4THREADJOIN(worker) (worker).join()
|
||||
|
||||
// Macro to retrieve caller thread
|
||||
//
|
||||
#define G4THREADSELF pthread_self
|
||||
// std::thread::id does not cast to integer
|
||||
typedef std::thread::id G4Pid_t;
|
||||
|
||||
// Some useful types
|
||||
//
|
||||
typedef void* G4ThreadFunReturnType;
|
||||
typedef void* G4ThreadFunArgType;
|
||||
typedef G4int (*thread_lock)(G4Mutex*);
|
||||
typedef G4int (*thread_unlock)(G4Mutex*);
|
||||
|
||||
typedef pid_t G4Pid_t;
|
||||
// Instead of previous macro taking one argument, define function taking
|
||||
// unlimited arguments
|
||||
template <typename _Worker, typename _Func, typename... _Args>
|
||||
void G4THREADCREATE(_Worker*& worker, _Func func, _Args... args)
|
||||
{
|
||||
*worker = G4Thread(func, std::forward<_Args>(args)...);
|
||||
}
|
||||
|
||||
// Conditions
|
||||
//
|
||||
// See G4MTRunManager for example on how to use these
|
||||
// This complication is needed to be portable with WIN32
|
||||
// Note that WIN32 requires an additional initialization step.
|
||||
// See example code
|
||||
//
|
||||
typedef pthread_cond_t G4Condition;
|
||||
#define G4CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER
|
||||
|
||||
#define G4CONDITIONWAIT( cond, mutex ) pthread_cond_wait( cond , mutex );
|
||||
#define G4CONDITIONBROADCAST( cond ) pthread_cond_broadcast( cond );
|
||||
|
||||
#elif defined(WIN32)
|
||||
typedef std::condition_variable G4Condition;
|
||||
#define G4CONDITION_INITIALIZER {}
|
||||
#define G4CONDITIONWAIT(cond, lock) (cond)->wait(*lock);
|
||||
#define G4CONDITIONWAITLAMBDA(cond, lock, lambda) (cond)->wait(*lock, lambda);
|
||||
#define G4CONDITIONBROADCAST(cond) (cond)->notify_all();
|
||||
//
|
||||
// Multi-threaded build: for Windows systems
|
||||
// we don't define above globally so single-threaded code does not get
|
||||
// caught in condition with no other thread to wake it up
|
||||
//
|
||||
#include "windefs.hh" // Include 'safe...' <windows.h>
|
||||
|
||||
typedef HANDLE G4Mutex;
|
||||
typedef HANDLE G4Thread;
|
||||
|
||||
#define G4MUTEX_INITIALIZER CreateMutex(NULL,FALSE,NULL)
|
||||
DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m );
|
||||
#define G4MUTEXLOCK G4WaitForSingleObjectInf
|
||||
|
||||
// #define G4MUTEXINIT(mutex) InitializeCriticalSection( &mutex );
|
||||
#define G4MUTEXINIT(mutex);
|
||||
#define G4MUTEXDESTROY(mutex);
|
||||
|
||||
// Not clear why following two lines are needed...
|
||||
//
|
||||
BOOL G4ReleaseMutex( __in G4Mutex m);
|
||||
#define G4MUTEXUNLOCK G4ReleaseMutex
|
||||
|
||||
#define G4THREADCREATE( worker, func, arg ) { *worker = CreateThread( NULL, 16*1024*1024 , func , arg , 0 , NULL ); }
|
||||
#define G4THREADJOIN( worker ) WaitForSingleObject( worker , INFINITE);
|
||||
#define G4THREADSELF GetCurrentThreadId
|
||||
#define G4ThreadFunReturnType DWORD WINAPI
|
||||
typedef LPVOID G4ThreadFunArgType;
|
||||
typedef DWORD (*thread_lock)(G4Mutex);
|
||||
typedef BOOL (*thread_unlock)(G4Mutex);
|
||||
typedef DWORD G4Pid_t;
|
||||
|
||||
// Conditions
|
||||
//
|
||||
typedef CONDITION_VARIABLE G4Condition;
|
||||
#define G4CONDITION_INITIALIZER CONDITION_VARIABLE_INIT
|
||||
|
||||
#define G4CONDITIONWAIT( cond , criticalsectionmutex ) SleepConditionVariableCS( cond, criticalsectionmutex , INFINITE );
|
||||
#define G4CONDITIONBROADCAST( cond ) WakeAllConditionVariable( cond );
|
||||
|
||||
#else
|
||||
|
||||
#error "No Threading model technology supported for this platform. Use sequential build !"
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
//==========================================
|
||||
// G4MULTITHREADED is OFF - Sequential build
|
||||
//==========================================
|
||||
typedef G4int G4Mutex;
|
||||
typedef G4int G4Thread;
|
||||
#define G4MUTEX_INITIALIZER 1
|
||||
G4int fake_mutex_lock_unlock( G4Mutex* );// { return 0; }
|
||||
#define G4MUTEXINIT(mutex) ;;
|
||||
#define G4MUTEXDESTROY(mutex) ;;
|
||||
#define G4MUTEXLOCK fake_mutex_lock_unlock
|
||||
#define G4MUTEXUNLOCK fake_mutex_lock_unlock
|
||||
#define G4THREADCREATE( worker , func , arg ) ;;
|
||||
#define G4THREADJOIN( worker ) ;;
|
||||
#define G4THREADSELF( nothing ) G4Thread(nothing);
|
||||
typedef void* G4ThreadFunReturnType;
|
||||
typedef void* G4ThreadFunArgType;
|
||||
typedef G4int (*thread_lock)(G4Mutex*);
|
||||
typedef G4int (*thread_unlock)(G4Mutex*);
|
||||
typedef G4int G4Pid_t;
|
||||
typedef G4int G4Condition;
|
||||
#define G4CONDITION_INITIALIZER 1
|
||||
#define G4CONDITIONWAIT( cond, mutex ) { ++(*cond); ++(*mutex); }
|
||||
#define G4CONDITIONBROADCAST( cond ) { ++(*cond); }
|
||||
//==========================================
|
||||
// G4MULTITHREADED is OFF - Sequential build
|
||||
//==========================================
|
||||
|
||||
// implement a dummy thread class that acts like a thread
|
||||
class G4DummyThread
|
||||
{
|
||||
public:
|
||||
typedef G4int native_handle_type;
|
||||
typedef std::thread::id id;
|
||||
|
||||
public:
|
||||
// does nothing
|
||||
G4DummyThread()
|
||||
{ }
|
||||
// a std::thread-like constructor that execute upon construction
|
||||
template <typename _Func, typename... _Args>
|
||||
G4DummyThread(_Func func, _Args&&... _args)
|
||||
{
|
||||
func(std::forward<_Args>(_args)...);
|
||||
}
|
||||
|
||||
public:
|
||||
native_handle_type native_handle() const { return native_handle_type(); }
|
||||
bool joinable() const { return true; }
|
||||
id get_id() const noexcept { return std::this_thread::get_id(); }
|
||||
void swap(G4DummyThread&) { }
|
||||
void join() { }
|
||||
void detach() { }
|
||||
|
||||
public:
|
||||
static unsigned int hardware_concurrency() noexcept
|
||||
{
|
||||
return std::thread::hardware_concurrency();
|
||||
}
|
||||
};
|
||||
|
||||
// global thread types
|
||||
typedef G4DummyThread G4Thread;
|
||||
typedef G4DummyThread::native_handle_type G4NativeThread;
|
||||
|
||||
// mutex macros
|
||||
#define G4MUTEXLOCK(mutex) ;;
|
||||
#define G4MUTEXUNLOCK(mutex) ;;
|
||||
|
||||
// Macro to join thread
|
||||
#define G4THREADJOIN(worker) ;;
|
||||
|
||||
typedef G4int G4Pid_t;
|
||||
|
||||
// Instead of previous macro taking one argument, define function taking
|
||||
// unlimited arguments
|
||||
template <typename _Worker, typename _Func, typename... _Args>
|
||||
void G4THREADCREATE(_Worker*& worker, _Func func, _Args... args)
|
||||
{
|
||||
*worker = G4Thread(func, std::forward<_Args>(args)...);
|
||||
}
|
||||
|
||||
typedef G4int G4Condition;
|
||||
#define G4CONDITION_INITIALIZER 1
|
||||
#define G4CONDITIONWAIT( cond, mutex ) { (*cond)++; }
|
||||
#define G4CONDITIONWAITLAMBDA( cond, mutex, lambda ) { (*cond)++; }
|
||||
#define G4CONDITIONBROADCAST( cond ) { (*cond)++; }
|
||||
|
||||
#endif //G4MULTITHREADING
|
||||
|
||||
namespace G4Threading
|
||||
{
|
||||
enum {
|
||||
enum
|
||||
{
|
||||
SEQUENTIAL_ID = -2,
|
||||
MASTER_ID = -1,
|
||||
WORKER_ID = 0,
|
||||
GENERICTHREAD_ID = -1000
|
||||
};
|
||||
G4Pid_t G4GetPidId();
|
||||
G4int G4GetNumberOfCores();
|
||||
G4int G4GetThreadId();
|
||||
G4bool IsWorkerThread();
|
||||
G4bool IsMasterThread();
|
||||
void G4SetThreadId( G4int aNewValue );
|
||||
G4bool G4SetPinAffinity( G4int idx , G4Thread& at);
|
||||
void SetMultithreadedApplication(G4bool value);
|
||||
G4bool IsMultithreadedApplication();
|
||||
int WorkerThreadLeavesPool();
|
||||
int WorkerThreadJoinsPool();
|
||||
G4int GetNumberOfRunningWorkerThreads();
|
||||
|
||||
G4Pid_t G4GetPidId();
|
||||
G4int G4GetNumberOfCores();
|
||||
G4int G4GetThreadId();
|
||||
G4bool IsWorkerThread();
|
||||
G4bool IsMasterThread();
|
||||
void G4SetThreadId( G4int aNewValue );
|
||||
G4bool G4SetPinAffinity( G4int idx , G4NativeThread& at);
|
||||
void SetMultithreadedApplication(G4bool value);
|
||||
G4bool IsMultithreadedApplication();
|
||||
int WorkerThreadLeavesPool();
|
||||
int WorkerThreadJoinsPool();
|
||||
G4int GetNumberOfRunningWorkerThreads();
|
||||
}
|
||||
|
||||
#endif //G4Threading_hh
|
||||
|
||||
Reference in New Issue
Block a user