32 #ifndef _QORE_INTERN_RSECTION_H
34 #define _QORE_INTERN_RSECTION_H
36 #include "qore/intern/qore_var_rwlock_priv.h"
39 class qore_rsection_priv;
43 DLLLOCAL RNotifier(
const RNotifier&);
44 DLLLOCAL RNotifier& operator=(
const RNotifier&);
51 DLLLOCAL RNotifier() : setp(false) {
54 DLLLOCAL ~RNotifier() {
58 DLLLOCAL
void done() {
71 DLLLOCAL
void wait() {
79 typedef std::list<RNotifier*> n_list_t;
84 class qore_rsection_priv :
public qore_var_rwlock_priv {
87 qore_rsection_priv(
const qore_rsection_priv&) =
delete;
88 qore_rsection_priv& operator=(
const qore_rsection_priv&) =
delete;
104 DLLLOCAL
virtual void notifyIntern() {
105 for (n_list_t::iterator i = list.begin(), e = list.end(); i != e; ++i)
110 DLLLOCAL
void setNotificationIntern(RNotifier* rn) {
111 assert(write_tid != -1 || rs_tid != -1);
118 DLLLOCAL qore_rsection_priv() : rs_tid(-1), rsection_waiting(0) {
122 DLLLOCAL
virtual ~qore_rsection_priv() {
123 assert(rs_tid == -1);
124 assert(list.empty());
128 DLLLOCAL
int tryRSectionLockNotifyWaitRead(RNotifier* rn);
130 DLLLOCAL
void upgradeReadToRSection(
int tid =
q_gettid()) {
132 assert(write_tid == -1);
134 while (rs_tid != -1) {
136 rsection_cond.
wait(l);
143 DLLLOCAL
void rSectionUnlock() {
145 assert(write_tid == -1);
152 qore_rsection_priv::notifyIntern();
154 if (rsection_waiting)
158 unlock_read_signal();
161 DLLLOCAL
bool hasRSectionLock(
int tid =
q_gettid()) {
162 return rs_tid == tid;
165 DLLLOCAL
bool checkRSectionExclusive(
int tid =
q_gettid()) {
166 return (rs_tid == tid || write_tid == tid);
169 DLLLOCAL
int rSectionTid()
const {
174 class RSectionLock :
public QoreVarRWLock {
176 DLLLOCAL RSectionLock() : QoreVarRWLock(new qore_rsection_priv) {
179 DLLLOCAL ~RSectionLock() {
183 DLLLOCAL
int tryRSectionLockNotifyWaitRead(RNotifier* rn) {
184 return static_cast<qore_rsection_priv*
>(priv)->tryRSectionLockNotifyWaitRead(rn);
187 DLLLOCAL
void rSectionUnlock() {
188 static_cast<qore_rsection_priv*
>(priv)->rSectionUnlock();
191 DLLLOCAL
bool hasRSectionLock(
int tid =
q_gettid()) {
192 return static_cast<qore_rsection_priv*
>(priv)->hasRSectionLock(tid);
195 DLLLOCAL
bool checkRSectionExclusive(
int tid =
q_gettid()) {
196 return static_cast<qore_rsection_priv*
>(priv)->checkRSectionExclusive(tid);
199 DLLLOCAL
void upgradeReadToRSection(
int tid =
q_gettid()) {
200 static_cast<qore_rsection_priv*
>(priv)->upgradeReadToRSection(tid);
203 DLLLOCAL
int rSectionTid()
const {
204 return static_cast<qore_rsection_priv*
>(priv)->rSectionTid();
208 class QoreSafeRSectionReadLocker :
private QoreSafeVarRWReadLocker {
210 DLLLOCAL QoreSafeRSectionReadLocker(RSectionLock& n_l) : QoreSafeVarRWReadLocker(n_l), has_rsection(false) {
213 DLLLOCAL ~QoreSafeRSectionReadLocker() {
214 if (locked && has_rsection) {
215 static_cast<RSectionLock*
>(l)->rSectionUnlock();
220 DLLLOCAL
void acquireRSection(
int tid =
q_gettid()) {
221 static_cast<RSectionLock*
>(l)->upgradeReadToRSection(tid);
226 DLLLOCAL
void unlock() {
231 static_cast<RSectionLock*
>(l)->rSectionUnlock();
240 class QoreRSectionLocker :
private QoreSafeVarRWReadLocker {
242 DLLLOCAL QoreRSectionLocker(RSectionLock& n_l) : QoreSafeVarRWReadLocker(n_l) {
243 static_cast<RSectionLock*
>(l)->upgradeReadToRSection();
246 DLLLOCAL ~QoreRSectionLocker() {
247 static_cast<RSectionLock*
>(l)->rSectionUnlock();
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
DLLEXPORT int wait(pthread_mutex_t *m)
blocks a thread on a mutex until the condition is signaled
DLLEXPORT int signal()
signals a single waiting thread to wake up
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
DLLEXPORT int q_gettid() noexcept
returns the current TID number