Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadRWlock.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_SUBSTRATE_THREAD_RW_LOCK_H
21 #define GALOIS_SUBSTRATE_THREAD_RW_LOCK_H
22 
23 #include "galois/config.h"
26 
27 namespace galois {
28 namespace substrate {
29 
30 class ThreadRWlock {
31 
33  // typedef galois::runtime::LL::SimpleLock<true> Lock_ty;
35 
36  PerThreadLock locks;
37 
38 public:
39  void readLock() { locks.getLocal()->lock(); }
40 
41  void readUnlock() { locks.getLocal()->unlock(); }
42 
43  void writeLock() {
44  for (unsigned i = 0; i < locks.size(); ++i) {
45  locks.getRemote(i)->lock();
46  }
47  }
48 
49  void writeUnlock() {
50  for (unsigned i = 0; i < locks.size(); ++i) {
51  locks.getRemote(i)->unlock();
52  }
53  }
54 };
55 
63 template <typename L, typename R, typename W>
64 void readUpdateProtected(L& rwmutex, R& readAndCheck, W& write) {
65 
66  rwmutex.readLock();
67 
68  if (readAndCheck()) {
69 
70  rwmutex.readUnlock();
71  return;
72 
73  } else {
74 
75  rwmutex.readUnlock();
76 
77  rwmutex.writeLock();
78  {
79  // check again in case another thread made the write
80  if (!readAndCheck()) {
81  write();
82  }
83  }
84  rwmutex.writeUnlock();
85  }
86 }
87 
88 } // end namespace substrate
89 } // end namespace galois
90 
91 #endif // GALOIS_SUBSTRATE_THREAD_RW_LOCK_H
void readLock()
Definition: ThreadRWlock.h:39
void lock() const
Definition: PaddedLock.h:39
void unlock() const
Definition: PaddedLock.h:41
Definition: ThreadRWlock.h:30
T * getLocal()
Definition: PerThreadStorage.h:128
Definition: PaddedLock.h:35
void writeUnlock()
Definition: ThreadRWlock.h:49
unsigned size() const
Definition: PerThreadStorage.h:159
T * getRemote(unsigned int thread)
Definition: PerThreadStorage.h:149
void writeLock()
Definition: ThreadRWlock.h:43
void readUnlock()
Definition: ThreadRWlock.h:41
void readUpdateProtected(L &rwmutex, R &readAndCheck, W &write)
readOrUpdate is a generic function to perform reads or writes using a rwmutex
Definition: ThreadRWlock.h:64