Ardour  9.2-70-g6916ee188f
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 <pthread.h>
22 
23 #include "pbd/libpbd_visibility.h"
24 
25 namespace PBD
26 {
27 
29 {
30 public:
31  enum LockFlags {
34  TryLock
35  };
36 
37  class ReaderLock;
38  class WriterLock;
39 
40  RWLock ();
41  ~RWLock ();
42 
43  inline void reader_lock ()
44  {
45  pthread_rwlock_rdlock (&_rw_lock);
46  }
47  inline bool reader_trylock ()
48  {
49  return 0 == pthread_rwlock_tryrdlock (&_rw_lock);
50  }
51  inline void reader_unlock ()
52  {
53  pthread_rwlock_unlock (&_rw_lock);
54  }
55 
56  inline void writer_lock ()
57  {
58  pthread_rwlock_wrlock (&_rw_lock);
59  }
60  inline bool writer_trylock ()
61  {
62  return 0 == pthread_rwlock_trywrlock (&_rw_lock);
63  }
64  inline void writer_unlock ()
65  {
66  pthread_rwlock_unlock (&_rw_lock);
67  }
68 
69 private:
70  RWLock (const RWLock&) = delete;
71  RWLock& operator= (const RWLock&) = delete;
72 
73  pthread_rwlock_t _rw_lock;
74 };
75 
77 {
78 public:
81 
82  inline void acquire ()
83  {
84  _rwlock.reader_lock ();
85  _locked = true;
86  }
87 
88  inline bool try_acquire ()
89  {
90  _locked = _rwlock.reader_trylock ();
91  return _locked;
92  };
93 
94  inline void release ()
95  {
96  _rwlock.reader_unlock ();
97  _locked = false;
98  }
99 
100  inline bool locked () const
101  {
102  return _locked;
103  }
104 
105 private:
107  bool _locked;
108 
109  ReaderLock (RWLock::ReaderLock const&) = delete;
110  RWLock::ReaderLock& operator= (RWLock::ReaderLock const&) = delete;
111 };
112 
114 {
115 public:
118 
119  inline void acquire ()
120  {
121  _rwlock.writer_lock ();
122  _locked = true;
123  }
124 
125  inline bool try_acquire ()
126  {
127  _locked = _rwlock.writer_trylock ();
128  return _locked;
129  }
130 
131  inline void release ()
132  {
133  _rwlock.writer_unlock ();
134  _locked = false;
135  }
136 
137  inline bool locked () const
138  {
139  return _locked;
140  }
141 
142 private:
144  bool _locked;
145 
146  WriterLock (RWLock::WriterLock const&) = delete;
147  RWLock::WriterLock& operator= (RWLock::WriterLock const&) = delete;
148 };
149 
150 } // namespace PBD
ReaderLock(RWLock &rwlock, RWLock::LockFlags m=Lock)
ReaderLock(RWLock::ReaderLock const &)=delete
bool locked() const
Definition: rwlock.h:100
WriterLock(RWLock &rwlock, RWLock::LockFlags m=Lock)
bool locked() const
Definition: rwlock.h:137
WriterLock(RWLock::WriterLock const &)=delete
bool writer_trylock()
Definition: rwlock.h:60
void reader_lock()
Definition: rwlock.h:43
pthread_rwlock_t _rw_lock
Definition: rwlock.h:73
bool reader_trylock()
Definition: rwlock.h:47
void reader_unlock()
Definition: rwlock.h:51
RWLock(const RWLock &)=delete
void writer_lock()
Definition: rwlock.h:56
void writer_unlock()
Definition: rwlock.h:64
@ NotLock
Definition: rwlock.h:33
#define LIBPBD_API
Definition: axis_view.h:42