1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_Mutex.h
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 47236 $
14 *---------------------------------------------------------------------------*/
15
16 /* Please see man pages for details
17
18
19
20 */
21
22 #ifndef NN_OS_OS_MUTEX_H_
23 #define NN_OS_OS_MUTEX_H_
24
25 #include <nn/types.h>
26 #include <nn/Handle.h>
27 #include <nn/Result.h>
28 #include <nn/WithInitialize.h>
29 #include <nn/os/os_Synchronization.h>
30 #include <nn/os/os_ErrorHandlerSelect.h>
31
32 #include <nn/util/util_Result.h>
33 #include <nn/util/detail/util_ScopedLockImpl.h>
34
35 #ifdef __cplusplus
36
37 namespace nn { namespace os {
38
39 /* Please see man pages for details
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 */
75
76 class Mutex : public WaitObject
77 {
78 public:
79
80 /* Please see man pages for details
81
82
83
84 */
85 explicit Mutex(bool initialLocked);
86
87 /* Please see man pages for details
88
89
90
91
92 */
Mutex()93 Mutex() {}
94
95 /* Please see man pages for details
96
97 */
98 Mutex(const nn::WithInitialize&);
99
100 /* Please see man pages for details
101
102
103
104 */
105 void Initialize(bool initialLocked = false);
106
107 /* Please see man pages for details
108
109
110
111
112
113 */
114 nn::Result TryInitialize(bool initialLocked = false);
115
116 /* Please see man pages for details
117
118
119
120
121 */
Finalize()122 void Finalize() { WaitObject::Finalize(); }
123
124 /* Please see man pages for details
125
126 */
~Mutex()127 ~Mutex() {}
128
129 /* Please see man pages for details
130
131
132
133
134
135
136 */
Lock()137 void Lock() { this->WaitOne(); }
138
139 /* Please see man pages for details
140
141
142
143
144
145
146
147
148
149
150 */
151 bool TryLock(nn::fnd::TimeSpan timeout = 0) { return this->WaitOne(timeout); }
152
153 /* Please see man pages for details
154
155
156
157
158
159
160 */
161 void Unlock();
162
163 /* Please see man pages for details
164
165
166 */
167 class ScopedLock;
168
169 //----------------------------------------------------------------------
170 //
171 //
172 //
173 //
174 //
175 //
176 //
177 //
178 //----------------------------------------------------------------------
179 static s32 GetCurrentCount();
180
181 //----------------------------------------------------------------------
182 //
183 //
184 //
185 //----------------------------------------------------------------------
186 static s32 GetMaxCount();
187
188 private:
189
190 Result TryInitializeImpl(bool initialLocked);
191
192 };
193
194 // In-line implementation
195
TryInitializeImpl(bool initialLocked)196 inline Result Mutex::TryInitializeImpl(bool initialLocked)
197 {
198 Handle handle;
199 NN_UTIL_RETURN_IF_FAILED(nn::svc::CreateMutex(&handle, initialLocked));
200 this->SetHandle(handle);
201 return ResultSuccess();
202 }
203
Initialize(bool initialLocked)204 inline void Mutex::Initialize(bool initialLocked)
205 {
206 NN_OS_ERROR_IF_FAILED(TryInitializeImpl(initialLocked));
207 }
208
TryInitialize(bool initialLocked)209 inline nn::Result Mutex::TryInitialize(bool initialLocked)
210 {
211 Result result = TryInitializeImpl(initialLocked);
212 if (result.GetSummary() == Result::SUMMARY_OUT_OF_RESOURCE)
213 {
214 return result;
215 }
216 NN_OS_ERROR_IF_FAILED(result);
217 return result;
218 }
219
Mutex(bool initialLocked)220 inline Mutex::Mutex(bool initialLocked)
221 {
222 Initialize(initialLocked);
223 }
224
Mutex(const nn::WithInitialize &)225 inline Mutex::Mutex(const nn::WithInitialize&)
226 {
227 Initialize(false);
228 }
229
Unlock()230 inline void Mutex::Unlock()
231 {
232 NN_OS_ERROR_IF_FAILED(nn::svc::ReleaseMutex(GetHandle()));
233 }
234
235 NN_UTIL_DETAIL_DEFINE_SCOPED_LOCK(Mutex, Lock(), Unlock());
236
237 }} // namespace nn::os
238
239 #endif // __cplusplus
240
241 // Below is the C declaration
242
243 #include <nn/util/detail/util_CLibImpl.h>
244
245 /* Please see man pages for details
246
247
248
249
250
251
252
253
254 */
255
256 /* Please see man pages for details
257
258
259
260 */
261 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosMutex, nn::os::Mutex, 4, u32);
262 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosMutexToWaitObject, nnosMutex, nnosWaitObject);
263 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosWaitObjectToMutex, nnosWaitObject, nnosMutex);
264
265 /* Please see man pages for details
266
267 */
268 NN_EXTERN_C void nnosMutexInitialize(nnosMutex* this_, bool initialLocked);
269
270 /* Please see man pages for details
271
272 */
273 NN_EXTERN_C bool nnosMutexTryInitialize(nnosMutex* this_, bool initialLocked);
274
275 /* Please see man pages for details
276
277 */
278 NN_EXTERN_C void nnosMutexLock(nnosMutex* this_);
279
280 /* Please see man pages for details
281
282 */
283 NN_EXTERN_C bool nnosMutexTryLock(nnosMutex* this_, s64 timeout);
284
285 /* Please see man pages for details
286
287 */
288 NN_EXTERN_C void nnosMutexUnlock(nnosMutex* this_);
289
290 /* Please see man pages for details
291
292 */
293 NN_EXTERN_C void nnosMutexFinalize(nnosMutex* this_);
294
295 /*
296
297
298
299 */
300
301 #endif /* NN_OS_IPC_OS_MUTEX_H_ */
302