2
* Copyright © 2014 Canonical Ltd.
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.
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.
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/>.
16
* Authored By: Alan Griffiths <alan@octopull.co.uk>
19
#include "mir/recursive_read_write_mutex.h"
23
void mir::RecursiveReadWriteMutex::read_lock()
25
auto const my_id = std::this_thread::get_id();
27
std::unique_lock<decltype(mutex)> lock{mutex};
29
return !write_locking_thread.count ||
30
write_locking_thread.id == my_id; });
32
auto const my_count = std::find_if(
33
read_locking_threads.begin(),
34
read_locking_threads.end(),
35
[my_id](ThreadLockCount const& candidate) { return my_id == candidate.id; });
37
if (my_count == read_locking_threads.end())
39
read_locking_threads.push_back(ThreadLockCount(my_id, 1U));
47
void mir::RecursiveReadWriteMutex::read_unlock()
49
auto const my_id = std::this_thread::get_id();
51
std::lock_guard<decltype(mutex)> lock{mutex};
52
auto const my_count = std::find_if(
53
read_locking_threads.begin(),
54
read_locking_threads.end(),
55
[my_id](ThreadLockCount const& candidate) { return my_id == candidate.id; });
62
void mir::RecursiveReadWriteMutex::write_lock()
64
auto const my_id = std::this_thread::get_id();
66
std::unique_lock<decltype(mutex)> lock{mutex};
69
if (write_locking_thread.count &&
70
write_locking_thread.id != my_id) return false;
71
for (auto const& candidate : read_locking_threads)
73
if (candidate.id != my_id && candidate.count != 0) return false;
78
++write_locking_thread.count;
79
write_locking_thread.id = my_id;
82
void mir::RecursiveReadWriteMutex::write_unlock()
84
std::lock_guard<decltype(mutex)> lock{mutex};
85
--write_locking_thread.count;