Ardour  9.10-48-g0f45032f6d
rwlock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <map>
22 #include <shared_mutex>
23 #include <thread>
24 
25 #include "pbd/libpbd_visibility.h"
26 
27 namespace PBD
28 {
29 
31 {
32 public:
33  enum LockFlags {
36  TryLock
37  };
38 
39  class ReaderLock;
40  class WriterLock;
41 
42  RWLock ();
43  ~RWLock ();
44 
45  inline void reader_lock ()
46  {
47  _rw_lock.lock_shared ();
48  }
49  inline bool reader_trylock ()
50  {
51  return _rw_lock.try_lock_shared ();
52  }
53  inline void reader_unlock ()
54  {
55  _rw_lock.unlock_shared ();
56  }
57 
58  inline void writer_lock ()
59  {
60  _rw_lock.lock ();
61  }
62  inline bool writer_trylock ()
63  {
64  return _rw_lock.try_lock ();
65  }
66  inline void writer_unlock ()
67  {
68  _rw_lock.unlock ();
69  }
70 
71 private:
72  class shared_recursive_mutex : public std::shared_mutex
73  {
74  public:
75  void lock_shared ()
76  {
77  std::thread::id id = std::this_thread::get_id ();
78  if (_count[id] > 0) {
79  ++_count[id];
80  } else {
81  std::shared_mutex::lock_shared ();
82  _count[id] = 1;
83  }
84  }
85 
87  {
88  std::thread::id id = std::this_thread::get_id ();
89  if (_count[id] > 0) {
90  ++_count[id];
91  return true;
92  } else if (std::shared_mutex::try_lock_shared ()) {
93  _count[id] = 1;
94  return true;
95  } else {
96  return false;
97  }
98  }
99 
101  {
102  std::thread::id id = std::this_thread::get_id ();
103  if (_count[id] > 1) {
104  --_count[id];
105  } else {
106  _count[id] = 0;
107  std::shared_mutex::unlock_shared ();
108  }
109  }
110 
111  private:
112  std::map<std::thread::id, unsigned int> _count;
113  };
114 
115  RWLock (const RWLock&) = delete;
116  RWLock& operator= (const RWLock&) = delete;
117 
119 };
120 
122 {
123 public:
126 
127  inline void acquire ()
128  {
129  _rwlock.reader_lock ();
130  _locked = true;
131  }
132 
133  inline bool try_acquire ()
134  {
135  _locked = _rwlock.reader_trylock ();
136  return _locked;
137  };
138 
139  inline void release ()
140  {
141  _rwlock.reader_unlock ();
142  _locked = false;
143  }
144 
145  inline bool locked () const
146  {
147  return _locked;
148  }
149 
150 private:
152  bool _locked;
153 
154  ReaderLock (RWLock::ReaderLock const&) = delete;
155  RWLock::ReaderLock& operator= (RWLock::ReaderLock const&) = delete;
156 };
157 
159 {
160 public:
163 
164  inline void acquire ()
165  {
166  _rwlock.writer_lock ();
167  _locked = true;
168  }
169 
170  inline bool try_acquire ()
171  {
172  _locked = _rwlock.writer_trylock ();
173  return _locked;
174  }
175 
176  inline void release ()
177  {
178  _rwlock.writer_unlock ();
179  _locked = false;
180  }
181 
182  inline bool locked () const
183  {
184  return _locked;
185  }
186 
187 private:
189  bool _locked;
190 
191  WriterLock (RWLock::WriterLock const&) = delete;
192  RWLock::WriterLock& operator= (RWLock::WriterLock const&) = delete;
193 };
194 
195 } // namespace PBD
ReaderLock(RWLock &rwlock, RWLock::LockFlags m=Lock)
ReaderLock(RWLock::ReaderLock const &)=delete
bool locked() const
Definition: rwlock.h:145
WriterLock(RWLock &rwlock, RWLock::LockFlags m=Lock)
bool locked() const
Definition: rwlock.h:182
WriterLock(RWLock::WriterLock const &)=delete
std::map< std::thread::id, unsigned int > _count
Definition: rwlock.h:112
bool writer_trylock()
Definition: rwlock.h:62
void reader_lock()
Definition: rwlock.h:45
bool reader_trylock()
Definition: rwlock.h:49
void reader_unlock()
Definition: rwlock.h:53
RWLock(const RWLock &)=delete
void writer_lock()
Definition: rwlock.h:58
void writer_unlock()
Definition: rwlock.h:66
shared_recursive_mutex _rw_lock
Definition: rwlock.h:118
@ NotLock
Definition: rwlock.h:35
#define LIBPBD_API
Definition: axis_view.h:42