Qore Programming Language  0.9.16
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 - 2015 David Nichols
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 private:
53  int readRequests;
54  QoreCondition read;
55  bool prefer_writers;
56  tid_map_t tmap; // map of TIDs to read lock counts
57  vlock_map_t vmap; // map of TIDs to VLock data structures
58  int num_readers; // number of threads holding the read lock
59 
60  // 0 = last read lock in this thread released
61  DLLLOCAL int cleanup_read_lock_intern(tid_map_t::iterator i);
62  DLLLOCAL void mark_read_lock_intern(int mtid, VLock *nvl);
63  DLLLOCAL void release_read_lock_intern(tid_map_t::iterator i);
64  DLLLOCAL int grab_read_lock_intern(int mtid, VLock *nvl, int64 timeout_ms, ExceptionSink *xsink);
65  DLLLOCAL void set_initial_read_lock_intern(int mtid, VLock *nvl);
66 
67  DLLLOCAL virtual void cleanupImpl();
68  DLLLOCAL virtual void signalAllImpl();
69  DLLLOCAL virtual void signalImpl();
70  DLLLOCAL virtual int releaseImpl();
71  DLLLOCAL virtual int releaseImpl(ExceptionSink *xsink);
72  DLLLOCAL virtual int grabImpl(int mtid, VLock *nvl, ExceptionSink *xsink, int64 timeout_ms = 0);
73  DLLLOCAL virtual int tryGrabImpl(int mtid, VLock *nvl);
74  DLLLOCAL virtual int externWaitImpl(int mtid, QoreCondition *cond, ExceptionSink *xsink, int64 timeout_ms = 0);
75  DLLLOCAL virtual void destructorImpl(ExceptionSink *xsink);
76 
77 protected:
78 
79 public:
80  DLLLOCAL RWLock(bool p = false);
81 
82 #ifdef DEBUG
83  DLLLOCAL virtual ~RWLock();
84 #endif
85 
86  DLLLOCAL int readLock(ExceptionSink *xsink, int64 timeout_ms = 0);
87  DLLLOCAL int readUnlock(ExceptionSink *xsink);
88  DLLLOCAL int tryReadLock();
89  //DLLLOCAL void writeToRead(ExceptionSink *xsink);
90 
91  DLLLOCAL int numReaders();
92 
93  DLLLOCAL int getReadWaiting() const {
94  return readRequests;
95  }
96  DLLLOCAL int getWriteWaiting() const {
97  return waiting;
98  }
99 
100  DLLLOCAL bool lockOwner() const {
101  if (writeLockOwner())
102  return true;
103 
104  return readLockOwner();
105  }
106 
107  DLLLOCAL bool writeLockOwner() const {
108  return tid == q_gettid();
109  }
110 
111  DLLLOCAL bool readLockOwner() const {
112  // if the write lock is held or the lock is deleted or nobody has the read lock, then return false
113  if (tid > -1 || tid == Lock_Deleted || !num_readers)
114  return false;
115 
116  // to check the read lock status, er have to acquire the asl_lock
117  int mtid = q_gettid();
118  AutoLocker al(&asl_lock);
119  return tmap.find(mtid) == tmap.end() ? false : true;
120  }
121 
122  DLLLOCAL virtual const char *getName() const { return "RWLock"; }
123 };
124 
125 #endif // _QORE_CLASS_RWLOCK
q_gettid
DLLEXPORT int q_gettid() noexcept
returns the current TID number
QoreCondition
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
int64
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
AutoLocker
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48