Qore Programming Language  1.12.0
RWLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  RWLock.h
4 
5  Read-Write Lock object (default: prefer readers)
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_CLASS_RWLOCK
35 
36 #define _QORE_CLASS_RWLOCK
37 
38 #include "qore/intern/AbstractSmartLock.h"
39 #include "qore/intern/VLock.h"
40 
41 #include <map>
42 
43 // to track TIDs and read counts of readers
44 typedef std::map<int, int> tid_map_t;
45 
46 // ASL mapping:
47 // asl_cond: write cond
48 // waiting: waiting write requests
49 // tid: write TID
50 
51 class RWLock : public AbstractSmartLock {
52 public:
53  DLLLOCAL RWLock(bool p = false);
54 
55 #ifdef DEBUG
56  DLLLOCAL virtual ~RWLock();
57 #endif
58 
59  DLLLOCAL int readLock(ExceptionSink *xsink, int64 timeout_ms = 0);
60  DLLLOCAL int readUnlock(ExceptionSink *xsink);
61  DLLLOCAL int tryReadLock();
62  //DLLLOCAL void writeToRead(ExceptionSink *xsink);
63 
64  DLLLOCAL int numReaders();
65 
66  DLLLOCAL int getReadWaiting() const {
67  return readRequests;
68  }
69  DLLLOCAL int getWriteWaiting() const {
70  return waiting;
71  }
72 
73  DLLLOCAL bool lockOwner() const {
74  if (writeLockOwner())
75  return true;
76 
77  return readLockOwner();
78  }
79 
80  DLLLOCAL bool writeLockOwner() const {
81  return tid == q_gettid();
82  }
83 
84  DLLLOCAL bool readLockOwner() const {
85  // if the write lock is held or the lock is deleted or nobody has the read lock, then return false
86  if (tid > -1 || tid == Lock_Deleted || !num_readers)
87  return false;
88 
89  // to check the read lock status, er have to acquire the asl_lock
90  int mtid = q_gettid();
91  AutoLocker al(&asl_lock);
92  return tmap.find(mtid) == tmap.end() ? false : true;
93  }
94 
95  DLLLOCAL virtual const char *getName() const { return "RWLock"; }
96 
97 private:
98  int readRequests;
99  QoreCondition read;
100  bool prefer_writers;
101  tid_map_t tmap; // map of TIDs to read lock counts
102  vlock_map_t vmap; // map of TIDs to VLock data structures
103  int num_readers; // number of threads holding the read lock
104 
105  // 0 = last read lock in this thread released
106  DLLLOCAL int cleanup_read_lock_intern(tid_map_t::iterator i);
107  DLLLOCAL void mark_read_lock_intern(int mtid, VLock *nvl);
108  DLLLOCAL void release_read_lock_intern(tid_map_t::iterator i);
109  DLLLOCAL int grab_read_lock_intern(int mtid, VLock *nvl, int64 timeout_ms, ExceptionSink *xsink);
110  DLLLOCAL void set_initial_read_lock_intern(int mtid, VLock *nvl);
111 
112  DLLLOCAL virtual int cleanupImpl();
113  DLLLOCAL virtual void signalAllImpl();
114  DLLLOCAL virtual void signalImpl();
115  DLLLOCAL virtual int releaseImpl();
116  DLLLOCAL virtual int releaseImpl(ExceptionSink *xsink);
117  DLLLOCAL virtual int grabImpl(int mtid, VLock *nvl, ExceptionSink *xsink, int64 timeout_ms = 0);
118  DLLLOCAL virtual int tryGrabImpl(int mtid, VLock *nvl);
119  DLLLOCAL virtual int externWaitImpl(int mtid, QoreCondition *cond, ExceptionSink *xsink, int64 timeout_ms = 0);
120  DLLLOCAL virtual void destructorImpl(ExceptionSink *xsink);
121 };
122 
123 #endif // _QORE_CLASS_RWLOCK
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
DLLEXPORT int q_gettid() noexcept
returns the current TID number