33 #ifndef MADNESS_WORLD_WORLDMUTEX_H__INCLUDED
34 #define MADNESS_WORLD_WORLDMUTEX_H__INCLUDED
40 #include <libkern/OSAtomic.h>
41 typedef OSSpinLock pthread_spinlock_t;
43 inline void pthread_spin_init(pthread_spinlock_t* p,
int ) {
46 inline int pthread_spin_trylock(pthread_spinlock_t* p) {
47 return !OSSpinLockTry(p);
49 inline int pthread_spin_lock(pthread_spinlock_t* p) {
53 inline int pthread_spin_unlock(pthread_spinlock_t* p) {
57 inline void pthread_spin_destroy(pthread_spinlock_t* ) {}
79 #if !defined(HAVE_IBMBGP) && !defined(HAVE_IBMBGQ)
96 mutable pthread_mutex_t mutex;
102 void operator=(
const Mutex&);
108 const int result = pthread_mutex_init(&mutex, 0);
114 return pthread_mutex_trylock(&mutex)==0;
119 const int result = pthread_mutex_lock(&mutex);
125 const int result = pthread_mutex_unlock(&mutex);
130 pthread_mutex_t*
ptr()
const {
135 pthread_mutex_destroy(&mutex);
142 mutable pthread_mutex_t mutex;
156 return pthread_mutex_trylock(&mutex)==0;
161 int result = pthread_mutex_lock(&mutex);
167 int result = pthread_mutex_unlock(&mutex);
172 pthread_mutex_t*
ptr()
const {
177 pthread_mutex_destroy(&mutex);
185 template <
class mutexT = Mutex>
197 typedef Mutex Spinlock;
203 mutable pthread_spinlock_t spinlock;
206 Spinlock(
const Spinlock&);
209 void operator=(
const Spinlock&);
215 pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE);
220 return pthread_spin_trylock(&spinlock)==0;
225 int result = pthread_spin_lock(&spinlock);
231 int result = pthread_spin_unlock(&spinlock);
236 pthread_spin_destroy(&spinlock);
246 volatile mutable int nreader;
247 volatile mutable bool writeflag;
257 bool gotit = !writeflag;
258 if (gotit) ++nreader;
264 bool gotit = (!writeflag) && (nreader==0);
265 if (gotit) writeflag =
true;
270 if (lockmode == READLOCK) {
273 else if (lockmode == WRITELOCK) {
276 else if (lockmode == NOLOCK) {
286 bool gotit = (!writeflag) && (nreader==1);
302 void lock(
int lockmode)
const {
321 else if (lockmode != NOLOCK)
MADNESS_EXCEPTION(
"MutexReaderWriter: try_lock: invalid lock mode", lockmode);
344 mutable AtomicInt nreader;
345 mutable AtomicInt writeflag;
346 enum {UNLOCKED, LOCKED};
355 if (writeflag == UNLOCKED)
return true;
361 return (writeflag.compare_and_swap((
int) UNLOCKED, (
int) LOCKED) == 0);
371 else if (lockmode ==
NOLOCK) {
397 void lock(
int lockmode)
const {
406 writeflag = UNLOCKED;
409 void unlock(
int lockmode)
const {
425 writeflag = UNLOCKED;
446 TAU_START(
"madness:ConditionVariable::wait()");
447 volatile bool myturn =
false;
451 if (b >= MAX_NTHREAD) back = 0;
457 TAU_STOP(
"madness:ConditionVariable::wait()");
465 if (ff >= MAX_NTHREAD)
476 while (front != back)
491 static const int MAX_NTHREAD = 64;
492 mutable volatile bool*
volatile q[MAX_NTHREAD];
493 mutable volatile int n;
494 mutable volatile int front;
495 mutable volatile int back;
502 volatile bool myturn =
false;
510 if (b >= MAX_NTHREAD) b = 0;
522 volatile bool* p = 0;
527 if (f >= MAX_NTHREAD) f = 0;
541 got_lock = (nn == 0);
542 if (got_lock) n = nn + 1;
568 mutable pthread_cond_t cv;
569 mutable pthread_mutex_t mutex;
573 pthread_cond_init(&cv, NULL);
574 pthread_mutex_init(&mutex, 0);
582 int result = pthread_mutex_lock(&mutex);
587 int result = pthread_mutex_unlock(&mutex);
593 pthread_cond_wait(&cv,&mutex);
597 int result = pthread_cond_signal(&cv);
602 int result = pthread_cond_broadcast(&cv);
607 pthread_mutex_destroy(&mutex);
608 pthread_cond_destroy(&cv);
626 volatile bool* pflags[64];
663 __asm__ __volatile__(
"" : : :
"memory");
666 for (
int i = 0; i < nthread; ++i)
667 *(pflags[i]) = lsense;
669 volatile bool* myflag = pflags[id];
670 while (*myflag != lsense) {
680 #endif // MADNESS_WORLD_WORLDMUTEX_H__INCLUDED
Mutex that is applied/released at start/end of a scope.
Definition: worldmutex.h:186
Mutex(int junk=0)
Make and initialize a mutex ... initial state is unlocked.
Definition: worldmutex.h:106
void unlock(int lockmode) const
Definition: worldmutex.h:318
Mutex using pthread mutex operations.
Definition: worldmutex.h:94
virtual ~MutexReaderWriter()
Definition: worldmutex.h:337
void lock() const
Definition: worldmutex.h:581
volatile int front
Definition: worldmutex.h:435
Simple wrapper for Pthread condition variable with its own mutex.
Definition: worldmutex.h:566
PthreadConditionVariable()
Definition: worldmutex.h:572
virtual ~ScopedMutex()
Definition: worldmutex.h:193
void signal() const
Definition: worldmutex.h:596
ConditionVariable()
Definition: worldmutex.h:439
bool try_two_locks(const Mutex &m1, const Mutex &m2)
Attempt to acquire two locks without blocking holding either one.
Definition: worldmutex.h:554
static const int READLOCK
Definition: worldmutex.h:250
void broadcast() const
You should acquire the mutex before broadcasting.
Definition: worldmutex.h:475
PthreadConditionVariable CONDITION_VARIABLE_TYPE
Definition: worldmutex.h:617
static const int NOLOCK
Definition: worldmutex.h:249
void broadcast() const
Definition: worldmutex.h:601
volatile bool *volatile q[MAX_NTHREAD]
Definition: worldmutex.h:436
void unlock() const
Free a spinlock owned by this thread.
Definition: worldmutex.h:230
bool try_lock() const
Try to acquire the mutex ... return true on success, false on failure.
Definition: worldmutex.h:113
Mutex SCALABLE_MUTEX_TYPE
Definition: worldmutex.h:619
bool enter(const int id)
Each thread calls this with its id (0,..,nthread-1) to enter the barrier.
Definition: worldmutex.h:651
bool try_lock() const
Definition: worldmutex.h:536
MutexWaiter()
Definition: worldmutex.h:85
void lock() const
Acquire the spinlock waiting if necessary.
Definition: worldmutex.h:224
virtual ~Spinlock()
Definition: worldmutex.h:235
void lock() const
Acquire the mutex waiting if necessary.
Definition: worldmutex.h:160
Definition: worldmutex.h:72
void lock() const
Definition: worldmutex.h:500
pthread_mutex_t * ptr() const
Return a pointer to the pthread mutex for use by a condition variable.
Definition: worldmutex.h:130
bool try_lock(int lockmode) const
Definition: worldmutex.h:269
NDIM & f
Definition: mra.h:2179
void wait()
Definition: worldmutex.cc:43
void unlock() const
Definition: worldmutex.h:586
bool dec_and_test()
Decrements the counter and returns true if the new value is zero.
Definition: atomicint.h:178
~RecursiveMutex()
Definition: worldmutex.h:176
Recursive mutex using pthread mutex operations.
Definition: worldmutex.h:140
pthread_mutex_t * ptr() const
Return a pointer to the pthread mutex for use by a condition variable.
Definition: worldmutex.h:172
bool try_lock() const
Try to acquire the spinlock ... return true on success, false on failure.
Definition: worldmutex.h:219
void convert_write_lock_to_read_lock() const
Always succeeds immediately.
Definition: worldmutex.h:332
void wait() const
You should have acquired the mutex before entering here.
Definition: worldmutex.h:592
void cpu_relax()
Do nothing and especially do not touch memory.
Definition: worldtime.h:141
void unlock() const
Free a mutex owned by this thread.
Definition: worldmutex.h:166
void lock(int lockmode) const
Definition: worldmutex.h:302
ScopedMutex(const mutexT *m)
Definition: worldmutex.h:189
void convert_read_lock_to_write_lock() const
Converts read to write lock without releasing the read lock.
Definition: worldmutex.h:327
virtual ~Mutex()
Definition: worldmutex.h:134
bool try_convert_read_lock_to_write_lock() const
Definition: worldmutex.h:284
MutexFair()
Definition: worldmutex.h:498
Mutex SPINLOCK_TYPE
Definition: worldmutex.h:618
void reset()
Definition: worldmutex.h:87
void write_lock() const
Definition: worldmutex.h:298
An integer with atomic set, get, read+inc, read+dec, dec+test operations.
Definition: atomicint.h:73
Definition: worldmutex.h:622
void register_thread(int id, volatile bool *pflag)
Each thread calls this once before first use.
Definition: worldmutex.h:640
Spinlock(int junk=0)
Make and initialize a spinlock ... initial state is unlocked.
Definition: worldmutex.h:213
void signal() const
You should acquire the mutex before signalling.
Definition: worldmutex.h:461
Scalable and fair condition variable (spins on local value)
Definition: worldmutex.h:431
const double m
Definition: gfit.cc:199
void unlock() const
Definition: worldmutex.h:520
virtual ~ConditionVariable()
Definition: worldmutex.h:481
ScopedMutex(const mutexT &m)
Definition: worldmutex.h:191
static const int MAX_NTHREAD
Definition: worldmutex.h:433
void read_unlock() const
Definition: worldmutex.h:306
RecursiveMutex()
Make and initialize a mutex ... initial state is unlocked.
Definition: worldmutex.cc:67
bool try_write_lock() const
Definition: worldmutex.h:262
MutexReaderWriter()
Definition: worldmutex.h:253
static const int WRITELOCK
Definition: worldmutex.h:251
void wait() const
You should acquire the mutex before waiting.
Definition: worldmutex.h:442
Definition: worldmutex.h:245
bool try_lock() const
Try to acquire the mutex ... return true on success, false on failure.
Definition: worldmutex.h:155
void read_lock() const
Definition: worldmutex.h:294
virtual ~PthreadConditionVariable()
Definition: worldmutex.h:606
Implements MadnessException.
Wrappers around platform dependent timers and performance info.
bool try_read_lock() const
Definition: worldmutex.h:255
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
#define TAU_STOP(a)
Definition: TAU.h:7
Barrier(int nthread)
Definition: worldmutex.h:629
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
volatile int back
Definition: worldmutex.h:434
void write_unlock() const
Definition: worldmutex.h:311
FLOAT b(int j, FLOAT z)
Definition: y1.cc:79
void unlock() const
Free a mutex owned by this thread.
Definition: worldmutex.h:124
#define TAU_START(a)
Definition: TAU.h:6
void lock() const
Acquire the mutex waiting if necessary.
Definition: worldmutex.h:118
A scalable and fair mutex (not recursive)
Definition: worldmutex.h:489
pthread_mutex_t & get_pthread_mutex()
Definition: worldmutex.h:577
Disables default copy constructor and assignment operators.
Definition: nodefaults.h:49