The Pedigree Project 0.1
LockGuard.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_LOCKGUARD_H
21#define KERNEL_LOCKGUARD_H
22#include "pedigree/kernel/compiler.h"
23
24#include <config.h>
25
26#if THREADS && !defined(STANDALONE_MUTEXES)
27#include "pedigree/kernel/process/TerminationDeferral.h"
28#endif
29
30class Spinlock;
31class Mutex;
32class Semaphore;
33
37template <class T, bool Condition>
38class EXPORTED_PUBLIC ConstexprLockGuard {
39 public:
40 ConstexprLockGuard(T& Lock) : m_Lock(Lock) {
41 if constexpr (Condition)
42 m_Lock.acquire();
43 }
45 if (Condition)
46 m_Lock.release();
47 }
48
49 private:
50 ConstexprLockGuard() = delete;
51 NOT_COPYABLE_OR_ASSIGNABLE(ConstexprLockGuard);
52
53 T& m_Lock;
54};
55
56template <class T>
57class EXPORTED_PUBLIC LockGuard {
58 public:
59 LockGuard(T& Lock, bool Condition = true) : m_Lock(Lock), m_bCondition(Condition) {
60 if (m_bCondition)
61 m_Lock.acquire();
62 }
63 ~LockGuard() {
64 if (m_bCondition)
65 m_Lock.release();
66 }
67
69 void disown() {
70 m_bCondition = false;
71 }
72
73 bool ownsLock() const {
74 return m_bCondition;
75 }
76
77 private:
78 LockGuard() = delete;
79 NOT_COPYABLE_OR_ASSIGNABLE(LockGuard);
80
81 T& m_Lock;
82 bool m_bCondition;
83};
84
85#if THREADS && !defined(STANDALONE_MUTEXES)
90template <>
91class EXPORTED_PUBLIC LockGuard<Mutex> {
92 public:
93 explicit LockGuard(Mutex& Lock, bool Condition = true);
94 ~LockGuard();
95
101 void disown();
102
103 bool ownsLock() const {
104 return m_bCondition;
105 }
106
107 private:
108 LockGuard() = delete;
109 NOT_COPYABLE_OR_ASSIGNABLE(LockGuard);
110
111 TerminationDeferral m_TerminationDeferral;
112 Mutex& m_Lock;
113 bool m_bCondition;
114};
115
116template <>
117class EXPORTED_PUBLIC ConstexprLockGuard<Mutex, true> {
118 public:
119 explicit ConstexprLockGuard(Mutex& Lock) : m_Guard(Lock) {}
120
121 private:
122 ConstexprLockGuard() = delete;
123 NOT_COPYABLE_OR_ASSIGNABLE(ConstexprLockGuard);
124
125 LockGuard<Mutex> m_Guard;
126};
127
129template <>
131 public:
132 LockGuard(Semaphore&, bool = true) = delete;
133};
134#endif
135
136template <class T>
137class EXPORTED_PUBLIC RecursingLockGuard {
138 public:
139 RecursingLockGuard(T& Lock, bool Condition = true) : m_Lock(Lock), m_bCondition(Condition) {
140 // T::allow_recursion must exist to be able to use RecursingLockGuard.
141 if (m_bCondition)
142 m_Lock.acquire(T::allow_recursion);
143 }
145 if (m_bCondition)
146 m_Lock.release();
147 }
148
149 private:
150 RecursingLockGuard() = delete;
151 NOT_COPYABLE_OR_ASSIGNABLE(RecursingLockGuard);
152
153 T& m_Lock;
154 bool m_bCondition;
155};
156
157extern template class LockGuard<Spinlock>;
158extern template class RecursingLockGuard<Spinlock>;
159
162#endif
void disown()
Definition LockGuard.h:69
Definition Mutex.h:56