The Pedigree Project
0.1
src
system
include
pedigree
kernel
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
30
class
Spinlock
;
31
class
Mutex
;
32
class
Semaphore
;
33
37
template
<
class
T,
bool
Condition>
38
class
EXPORTED_PUBLIC
ConstexprLockGuard
{
39
public
:
40
ConstexprLockGuard
(T& Lock) : m_Lock(Lock) {
41
if
constexpr
(Condition)
42
m_Lock.acquire();
43
}
44
~ConstexprLockGuard
() {
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
56
template
<
class
T>
57
class
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)
90
template
<>
91
class
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
116
template
<>
117
class
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
129
template
<>
130
class
LockGuard
<
Semaphore
> {
131
public
:
132
LockGuard
(
Semaphore
&,
bool
=
true
) =
delete
;
133
};
134
#endif
135
136
template
<
class
T>
137
class
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
}
144
~RecursingLockGuard
() {
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
157
extern
template
class
LockGuard<Spinlock>
;
158
extern
template
class
RecursingLockGuard<Spinlock>
;
159
162
#endif
ConstexprLockGuard
Definition
LockGuard.h:38
LockGuard
Definition
LockGuard.h:57
LockGuard::disown
void disown()
Definition
LockGuard.h:69
Mutex
Definition
Mutex.h:56
RecursingLockGuard
Definition
LockGuard.h:137
Semaphore
Definition
Semaphore.h:36
Spinlock
Definition
Spinlock.h:27
TerminationDeferral
Definition
TerminationDeferral.h:27
Generated on Sat Oct 3 2026 06:38:07 for The Pedigree Project by
1.9.8