Mir
recursive_read_write_mutex.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored By: Alan Griffiths <alan@octopull.co.uk>
17  */
18 
19 #ifndef MIR_RECURSIVE_READ_WRITE_MUTEX_H_
20 #define MIR_RECURSIVE_READ_WRITE_MUTEX_H_
21 
22 #include <condition_variable>
23 #include <thread>
24 #include <vector>
25 
26 namespace mir
27 {
32 {
33 public:
34  void read_lock();
35 
36  void read_unlock();
37 
38  void write_lock();
39 
40  void write_unlock();
41 
42 private:
43  std::mutex mutex;
44  std::condition_variable cv;
45  struct ThreadLockCount
46  {
47  ThreadLockCount() : id(), count(0) {}
48  ThreadLockCount(std::thread::id id, unsigned int count) : id(id), count(count) {}
49  std::thread::id id;
50  unsigned int count;
51  };
52  std::vector<ThreadLockCount> read_locking_threads;
53  ThreadLockCount write_locking_thread;
54 };
55 
57 {
58 public:
59  explicit RecursiveReadLock(RecursiveReadWriteMutex& mutex) : mutex(mutex) { mutex.read_lock(); }
60  ~RecursiveReadLock() { mutex.read_unlock(); }
61 
62 private:
64 };
65 
67 {
68 public:
69  explicit RecursiveWriteLock(RecursiveReadWriteMutex& mutex) : mutex(mutex) { mutex.write_lock(); }
70  ~RecursiveWriteLock() { mutex.write_unlock(); }
71 
72 private:
74 };
75 }
76 
77 #endif /* MIR_RECURSIVE_READ_WRITE_MUTEX_H_ */
All things Mir.
Definition: atomic_callback.h:25
void write_unlock()
Definition: recursive_read_write_mutex.cpp:82
~RecursiveReadLock()
Definition: recursive_read_write_mutex.h:60
void read_unlock()
Definition: recursive_read_write_mutex.cpp:47
Definition: recursive_read_write_mutex.h:56
~RecursiveWriteLock()
Definition: recursive_read_write_mutex.h:70
RecursiveWriteLock(RecursiveReadWriteMutex &mutex)
Definition: recursive_read_write_mutex.h:69
void write_lock()
Definition: recursive_read_write_mutex.cpp:62
a recursive read-write mutex.
Definition: recursive_read_write_mutex.h:31
void read_lock()
Definition: recursive_read_write_mutex.cpp:23
RecursiveReadLock(RecursiveReadWriteMutex &mutex)
Definition: recursive_read_write_mutex.h:59
Definition: recursive_read_write_mutex.h:66

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Sep 8 14:50:19 UTC 2016