Ardour  9.2-70-g6916ee188f
mutex.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 <chrono>
22 #include <condition_variable>
23 #include <mutex>
24 
25 #include "pbd/libpbd_visibility.h"
26 
27 namespace PBD
28 {
29 
35 {
36 public:
37  enum LockFlags {
40  TryLock
41  };
42  Mutex ();
43 
44  class Lock;
45 
46  void lock ()
47  {
48  _mutex.lock ();
49  }
50 
51  bool trylock ()
52  {
53  return _mutex.try_lock ();
54  }
55 
56  void unlock ()
57  {
58  _mutex.unlock ();
59  }
60 
61 private:
62  friend class Cond;
63  Mutex (Mutex const&) = delete;
64  Mutex& operator= (Mutex const&) = delete;
65 
66  std::mutex _mutex;
67 };
68 
71 {
72 public:
73  Lock (Mutex&, Mutex::LockFlags m = Acquire);
74  ~Lock ();
75 
76  inline void acquire ()
77  {
78  _mutex.lock ();
79  _locked = true;
80  }
81 
82  inline bool try_acquire ()
83  {
84  _locked = _mutex.trylock ();
85  return _locked;
86  }
87 
88  inline void release ()
89  {
90  _mutex.unlock ();
91  _locked = false;
92  }
93 
94  inline bool locked () const
95  {
96  return _locked;
97  }
98 
99 private:
100  Lock (Mutex::Lock const&) = delete;
101  Mutex::Lock& operator= (Mutex::Lock const&) = delete;
102 
104  bool _locked;
105 };
106 
107 class Cond
108 {
109 public:
110  Cond ();
111 
112  void signal ()
113  {
114  _cond.notify_one ();
115  }
116 
117  void broadcast ()
118  {
119  _cond.notify_all ();
120  }
121 
122  void wait (Mutex& mutex)
123  {
124  std::unique_lock m (mutex._mutex, std::adopt_lock);
125  _cond.wait (m);
126  }
127 
128  bool wait_for (Mutex& mutex, std::chrono::milliseconds const& rel_time)
129  {
130  std::unique_lock m (mutex._mutex, std::adopt_lock);
131  return std::cv_status::no_timeout == _cond.wait_for (m, rel_time);
132  }
133 
134 private:
135  Cond (Cond const&) = delete;
136  Cond& operator= (Cond const&) = delete;
137 
138  std::condition_variable _cond;
139 };
140 
141 } // namespace PBD
void wait(Mutex &mutex)
Definition: mutex.h:122
void signal()
Definition: mutex.h:112
bool wait_for(Mutex &mutex, std::chrono::milliseconds const &rel_time)
Definition: mutex.h:128
void broadcast()
Definition: mutex.h:117
Cond & operator=(Cond const &)=delete
Cond(Cond const &)=delete
std::condition_variable _cond
Definition: mutex.h:138
Mutex & _mutex
Definition: mutex.h:103
bool _locked
Definition: mutex.h:104
Lock(Mutex::Lock const &)=delete
bool try_acquire()
Definition: mutex.h:82
bool locked() const
Definition: mutex.h:94
Lock(Mutex &, Mutex::LockFlags m=Acquire)
void acquire()
Definition: mutex.h:76
void release()
Definition: mutex.h:88
LockFlags
Definition: mutex.h:37
@ Acquire
Definition: mutex.h:38
@ NotLock
Definition: mutex.h:39
void lock()
Definition: mutex.h:46
void unlock()
Definition: mutex.h:56
bool trylock()
Definition: mutex.h:51
std::mutex _mutex
Definition: mutex.h:66
Mutex(Mutex const &)=delete
#define LIBPBD_API
Definition: axis_view.h:42