33#ifndef MADNESS_WORLD_WORLDMUTEX_H__INCLUDED
34#define MADNESS_WORLD_WORLDMUTEX_H__INCLUDED
41#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
46typedef std::remove_pointer<os_unfair_lock_t>::type pthread_spinlock_t;
48inline void pthread_spin_init(pthread_spinlock_t*
p,
int ) {
49 *
p = OS_UNFAIR_LOCK_INIT;
51inline int pthread_spin_trylock(pthread_spinlock_t*
p) {
52 return !os_unfair_lock_trylock(
p);
54inline int pthread_spin_lock(pthread_spinlock_t*
p) {
55 os_unfair_lock_lock(
p);
58inline int pthread_spin_unlock(pthread_spinlock_t*
p) {
59 os_unfair_lock_unlock(
p);
63inline int pthread_spin_destroy(pthread_spinlock_t*) {
69#include <libkern/OSAtomic.h>
70typedef OSSpinLock pthread_spinlock_t;
72inline void pthread_spin_init(pthread_spinlock_t*
p,
int ) {
75inline int pthread_spin_trylock(pthread_spinlock_t*
p) {
76 return !OSSpinLockTry(
p);
78inline int pthread_spin_lock(pthread_spinlock_t*
p) {
82inline int pthread_spin_unlock(pthread_spinlock_t*
p) {
86inline void pthread_spin_destroy(pthread_spinlock_t* ) {}
116#if !defined(HAVE_IBMBGP) && !defined(HAVE_IBMBGQ)
145 const int result = pthread_mutex_init(&
mutex, 0);
151 return pthread_mutex_trylock(&
mutex)==0;
156 const int result = pthread_mutex_lock(&
mutex);
158 fprintf(stderr,
"!! MADNESS ERROR: Mutex::lock() failed acquiring mutex\n");
166 const int result = pthread_mutex_unlock(&
mutex);
168 fprintf(stderr,
"!! MADNESS ERROR: Mutex::unlock() failed releasing mutex\n");
175 pthread_mutex_t*
ptr()
const {
180 pthread_mutex_destroy(&
mutex);
201 return pthread_mutex_trylock(&
mutex)==0;
206 int result = pthread_mutex_lock(&
mutex);
208 fprintf(stderr,
"!! MADNESS ERROR: RecursiveMutex::lock() failed acquiring mutex\n");
216 int result = pthread_mutex_unlock(&
mutex);
218 fprintf(stderr,
"!! MADNESS ERROR: RecursiveMutex::unlock() failed releasing mutex\n");
225 pthread_mutex_t*
ptr()
const {
230 pthread_mutex_destroy(&
mutex);
238 template <
class mutexT = Mutex>
250 typedef Mutex Spinlock;
268 pthread_spin_init(&
spinlock, PTHREAD_PROCESS_PRIVATE);
273 return pthread_spin_trylock(&
spinlock)==0;
278 int result = pthread_spin_lock(&
spinlock);
280 fprintf(stderr,
"!! MADNESS ERROR: Spinlock::lock() failed acquiring spinlock\n");
288 int result = pthread_spin_unlock(&
spinlock);
290 fprintf(stderr,
"!! MADNESS ERROR: Spinlock::unlock() failed releasing spinlock\n");
337 else if (lockmode ==
NOLOCK) {
363 void lock(
int lockmode)
const {
407 enum {UNLOCKED, LOCKED};
422 return (
writeflag.compare_and_swap((
int) UNLOCKED, (
int) LOCKED) == 0);
432 else if (lockmode ==
NOLOCK) {
458 void lock(
int lockmode)
const {
470 void unlock(
int lockmode)
const {
517 volatile bool myturn =
false;
525 while (!myturn) std::this_thread::yield();
527 while (!myturn) std::this_thread::sleep_for(this->
wait_usleep_);
537 if (f == this->back)
return;
574 volatile bool myturn =
false;
592 volatile bool*
p = 0;
610 got_lock = (nn == 0);
611 if (got_lock)
n = nn + 1;
637 mutable pthread_cond_t
cv;
642 pthread_cond_init(&
cv,
nullptr);
643 pthread_mutex_init(&
mutex, 0);
651 int result = pthread_mutex_lock(&
mutex);
653 fprintf(stderr,
"!! MADNESS ERROR: PthreadConditionVariable::lock() failed acquiring mutex\n");
655 MADNESS_EXCEPTION(
"PthreadConditionVariable::lock() failed acquiring mutex", result);
660 int result = pthread_mutex_unlock(&
mutex);
662 fprintf(stderr,
"!! MADNESS ERROR: PthreadConditionVariable::unlock() failed releasing mutex\n");
664 MADNESS_EXCEPTION(
"PthreadConditionVariable::unlock() failed releasing mutex", result);
674 int result = pthread_cond_signal(&
cv);
679 int result = pthread_cond_broadcast(&
cv);
684 pthread_mutex_destroy(&
mutex);
685 pthread_cond_destroy(&
cv);
741 __asm__ __volatile__(
"" : : :
"memory");
744 for (
int i = 0; i <
nthread; ++i)
747 volatile bool* myflag =
pflags[id];
748 while (*myflag != lsense) {
Disables default copy constructor and assignment operators.
Definition nodefaults.h:49
An integer with atomic set, get, read+increment, read+decrement, and decrement+test operations.
Definition atomicint.h:126
bool dec_and_test()
Decrements the counter and returns true if the new value is zero,.
Definition atomicint.h:297
Definition worldmutex.h:700
const int nthread
Definition worldmutex.h:701
volatile bool * pflags[128]
Definition worldmutex.h:704
volatile bool sense
Definition worldmutex.h:702
AtomicInt nworking
Definition worldmutex.h:703
bool enter(const int id)
Each thread calls this with its id (0,..,nthread-1) to enter the barrier.
Definition worldmutex.h:729
Barrier(int nthread)
Definition worldmutex.h:707
void register_thread(int id, volatile bool *pflag)
Each thread calls this once before first use.
Definition worldmutex.h:718
Scalable and fair condition variable (spins on local value)
Definition worldmutex.h:497
void wait() const
You should acquire the mutex before waiting.
Definition worldmutex.h:513
int front
Definition worldmutex.h:501
void signal() const
You should acquire the mutex before signalling.
Definition worldmutex.h:535
void broadcast() const
You should acquire the mutex before broadcasting.
Definition worldmutex.h:544
int back
Definition worldmutex.h:500
static const int MAX_NTHREAD
Definition worldmutex.h:499
virtual ~ConditionVariable()
Definition worldmutex.h:549
volatile bool * fifo[MAX_NTHREAD]
Definition worldmutex.h:502
ConditionVariable()
Definition worldmutex.h:510
std::chrono::microseconds wait_usleep_
Definition worldmutex.h:553
void set_wait_policy(WaitPolicy p, int us=0)
Definition worldmutex.h:504
WaitPolicy wait_policy_
Definition worldmutex.h:552
A scalable and fair mutex (not recursive)
Definition worldmutex.h:562
bool try_lock() const
Definition worldmutex.h:605
void lock() const
Definition worldmutex.h:573
int front
Definition worldmutex.h:567
MutexFair()
Definition worldmutex.h:571
static const int MAX_NTHREAD
Definition worldmutex.h:564
void unlock() const
Definition worldmutex.h:591
int n
Definition worldmutex.h:566
int back
Definition worldmutex.h:568
volatile bool * q[MAX_NTHREAD]
Definition worldmutex.h:565
Definition worldmutex.h:306
void convert_read_lock_to_write_lock() const
Converts read to write lock without releasing the read lock.
Definition worldmutex.h:388
void write_unlock() const
Definition worldmutex.h:372
static const int WRITELOCK
Definition worldmutex.h:312
void convert_write_lock_to_read_lock() const
Always succeeds immediately.
Definition worldmutex.h:393
virtual ~MutexReaderWriter()
Definition worldmutex.h:398
bool try_read_lock() const
Definition worldmutex.h:316
MutexReaderWriter()
Definition worldmutex.h:314
static const int READLOCK
Definition worldmutex.h:311
void lock(int lockmode) const
Definition worldmutex.h:363
int nreader
Definition worldmutex.h:307
bool try_lock(int lockmode) const
Definition worldmutex.h:330
void unlock(int lockmode) const
Definition worldmutex.h:379
static const int NOLOCK
Definition worldmutex.h:310
void write_lock() const
Definition worldmutex.h:359
void read_lock() const
Definition worldmutex.h:355
bool try_write_lock() const
Definition worldmutex.h:323
bool writeflag
Definition worldmutex.h:308
bool try_convert_read_lock_to_write_lock() const
Definition worldmutex.h:345
void read_unlock() const
Definition worldmutex.h:367
Definition worldmutex.h:109
void wait()
Definition worldmutex.cc:103
MutexWaiter()
Definition worldmutex.h:122
unsigned int count
Definition worldmutex.h:111
void yield(int us)
Definition worldmutex.h:115
void reset()
Definition worldmutex.h:124
Mutex using pthread mutex operations.
Definition worldmutex.h:131
pthread_mutex_t mutex
Definition worldmutex.h:133
pthread_mutex_t * ptr() const
Return a pointer to the pthread mutex for use by a condition variable.
Definition worldmutex.h:175
void operator=(const Mutex &)
Assignment is forbidden.
Mutex(const Mutex &)
Copy constructor is forbidden.
Mutex(int junk=0)
Make and initialize a mutex ... initial state is unlocked.
Definition worldmutex.h:143
void unlock() const
Free a mutex owned by this thread.
Definition worldmutex.h:165
virtual ~Mutex()
Definition worldmutex.h:179
bool try_lock() const
Try to acquire the mutex ... return true on success, false on failure.
Definition worldmutex.h:150
void lock() const
Acquire the mutex waiting if necessary.
Definition worldmutex.h:155
Simple wrapper for Pthread condition variable with its own mutex.
Definition worldmutex.h:635
pthread_mutex_t mutex
Definition worldmutex.h:638
void lock() const
Definition worldmutex.h:650
pthread_cond_t cv
Definition worldmutex.h:637
virtual ~PthreadConditionVariable()
Definition worldmutex.h:683
void unlock() const
Definition worldmutex.h:659
void broadcast() const
Definition worldmutex.h:678
void signal() const
Definition worldmutex.h:673
pthread_mutex_t & get_pthread_mutex()
Definition worldmutex.h:646
void wait() const
You should have acquired the mutex before entering here.
Definition worldmutex.h:669
PthreadConditionVariable()
Definition worldmutex.h:641
Recursive mutex using pthread mutex operations.
Definition worldmutex.h:185
void operator=(const RecursiveMutex &)
Assignment is forbidden.
void lock() const
Acquire the mutex waiting if necessary.
Definition worldmutex.h:205
RecursiveMutex(const RecursiveMutex &)
Copy constructor is forbidden.
pthread_mutex_t * ptr() const
Return a pointer to the pthread mutex for use by a condition variable.
Definition worldmutex.h:225
void unlock() const
Free a mutex owned by this thread.
Definition worldmutex.h:215
bool try_lock() const
Try to acquire the mutex ... return true on success, false on failure.
Definition worldmutex.h:200
pthread_mutex_t mutex
Definition worldmutex.h:187
~RecursiveMutex()
Definition worldmutex.h:229
RecursiveMutex()
Make and initialize a mutex ... initial state is unlocked.
Definition worldmutex.cc:127
Mutex that is applied/released at start/end of a scope.
Definition worldmutex.h:239
const mutexT * mutex
Definition worldmutex.h:240
ScopedMutex(const mutexT &m)
Definition worldmutex.h:244
ScopedMutex(const mutexT *m)
Definition worldmutex.h:242
virtual ~ScopedMutex()
Definition worldmutex.h:246
Spinlock using pthread spinlock operations.
Definition worldmutex.h:253
void lock() const
Acquire the spinlock waiting if necessary.
Definition worldmutex.h:277
bool try_lock() const
Try to acquire the spinlock ... return true on success, false on failure.
Definition worldmutex.h:272
void operator=(const Spinlock &)
Assignment is forbidden.
Spinlock(int junk=0)
Make and initialize a spinlock ... initial state is unlocked.
Definition worldmutex.h:266
void unlock() const
Free a spinlock owned by this thread.
Definition worldmutex.h:287
Spinlock(const Spinlock &)
Copy constructor is forbidden.
virtual ~Spinlock()
Definition worldmutex.h:296
pthread_spinlock_t spinlock
Definition worldmutex.h:256
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Macros and tools pertaining to the configuration of MADNESS.
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
void print_mutex_error(int error_number)
Definition worldmutex.cc:44
Mutex printmutex
Definition worldmutex.cc:146
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
PthreadConditionVariable CONDITION_VARIABLE_TYPE
Definition worldmutex.h:694
WaitPolicy
wait policies supported by ConditionVariable/DQueue/ThreadPool
Definition worldmutex.h:492
bool try_two_locks(const Mutex &m1, const Mutex &m2)
Attempt to acquire two locks without blocking holding either one.
Definition worldmutex.h:623
Mutex SPINLOCK_TYPE
Definition worldmutex.h:695
static void myusleep(unsigned int us)
Sleep or spin for specified number of microseconds.
Definition timers.h:164
NDIM & f
Definition mra.h:2416
void cpu_relax()
Do nothing and especially do not touch memory.
Definition timers.h:148
Mutex SCALABLE_MUTEX_TYPE
Definition worldmutex.h:696
static const double b
Definition nonlinschro.cc:119
static const double m
Definition relops.cc:9
Wrappers around platform dependent timers and performance info.