sptk2 logo
SPTK Home Page
Locks.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/Exception.h>
30#include <sptk5/sptk.h>
31
32#include <mutex>
33#include <shared_mutex>
34
35namespace sptk {
36
37using SharedMutex = std::shared_timed_mutex;
38using ReadLockType = std::shared_lock<SharedMutex>;
39using WriteLockType = std::unique_lock<SharedMutex>;
40
47class SP_EXPORT UniqueLockInt
48{
49public:
55 explicit UniqueLockInt(SharedMutex& mutex);
56
57 UniqueLockInt(const UniqueLockInt&) = delete;
58
67 UniqueLockInt(SharedMutex& mutex, std::chrono::milliseconds timeout, const char* file, size_t line);
68
74 {
75 if (locked)
76 {
77 mutex.unlock();
78 }
79 }
80
81private:
82 SharedMutex& mutex;
83 bool locked {true};
84};
85
92class SP_EXPORT SharedLockInt
93{
94public:
99 explicit SharedLockInt(SharedMutex& mutex);
100
109 SharedLockInt(SharedMutex& mutex, std::chrono::milliseconds timeout, const char* file, size_t line);
110
111 SharedLockInt(const SharedLockInt&) = delete;
112
118 {
119 if (locked)
120#if USE_SHARED_MUTEX
121 mutex.unlock_shared();
122#else
123 {
124 mutex.unlock();
125 }
126#endif
127 }
128
129private:
130 SharedMutex& mutex;
131 bool locked {true};
132};
133
141{
142 WriteLockType destinationLock;
143 ReadLockType sourceLock;
144
145
146public:
153 CopyLockInt(SharedMutex& destinationMutex, SharedMutex& sourceMutex);
154};
155
162{
163 ReadLockType lock1;
164 ReadLockType lock2;
165
166public:
173 CompareLockInt(SharedMutex& lock1, SharedMutex& lock2);
174};
175
176#define UniqueLock(amutex) sptk::UniqueLockInt lock(amutex)
177#define TimedUniqueLock(amutex, timeout) sptk::UniqueLockInt lock(amutex, timeout, __FILE__, __LINE__)
178#define SharedLock(amutex) sptk::SharedLockInt lock(amutex)
179#define TimedSharedLock(amutex, timeout) sptk::SharedLockInt lock(amutex, timeout, __FILE__, __LINE__)
180#define CompareLock(mutex1, mutex2) sptk::CompareLockInt lock(mutex1, mutex2)
181#define CopyLock(destinationMutex, sourceMutex) sptk::CopyLockInt lock(destinationMutex, sourceMutex)
182
183} // namespace sptk
Definition: Locks.h:162
CompareLockInt(SharedMutex &lock1, SharedMutex &lock2)
Definition: Locks.h:141
CopyLockInt(SharedMutex &destinationMutex, SharedMutex &sourceMutex)
Definition: Locks.h:93
SharedLockInt(SharedMutex &mutex)
virtual ~SharedLockInt()
Definition: Locks.h:117
SharedLockInt(SharedMutex &mutex, std::chrono::milliseconds timeout, const char *file, size_t line)
Definition: Locks.h:48
virtual ~UniqueLockInt()
Definition: Locks.h:73
UniqueLockInt(SharedMutex &mutex)
UniqueLockInt(SharedMutex &mutex, std::chrono::milliseconds timeout, const char *file, size_t line)

Fri Oct 14 2022 09:58:32: SPTK 5.4.1