1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Mutex.h
4 
5   Copyright (C)2009 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: 38846 $
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 class Mutex : public WaitObject
75 {
76 public:
77 
78     /* Please see man pages for details
79 
80 
81 
82     */
83     explicit Mutex(bool initialLocked);
84 
85     /* Please see man pages for details
86 
87 
88 
89 
90     */
Mutex()91     Mutex() {}
92 
93     /* Please see man pages for details
94 
95     */
96     Mutex(const nn::WithInitialize&);
97 
98     /* Please see man pages for details
99 
100 
101 
102     */
103     void Initialize(bool initialLocked = false);
104 
105     /* Please see man pages for details
106 
107 
108 
109 
110 
111 */
112     nn::Result TryInitialize(bool initialLocked = false);
113 
114     /* Please see man pages for details
115 
116 
117 
118 
119     */
Finalize()120     void Finalize() { WaitObject::Finalize(); }
121 
122     /* Please see man pages for details
123 
124     */
~Mutex()125     ~Mutex() {}
126 
127     /* Please see man pages for details
128 
129 
130 
131 
132 
133 
134 */
Lock()135     void Lock() { this->WaitOne(); }
136 
137     /* Please see man pages for details
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 */
149     bool TryLock(nn::fnd::TimeSpan timeout = 0) { return this->WaitOne(timeout); }
150 
151     /* Please see man pages for details
152 
153 
154 
155 
156 
157 
158 */
159     void Unlock();
160 
161     /* Please see man pages for details
162 
163 
164     */
165     class ScopedLock;
166 
167 private:
168 
169     Result TryInitializeImpl(bool initialLocked);
170 
171 };
172 
173 // In-line implementation
174 
TryInitializeImpl(bool initialLocked)175 inline Result Mutex::TryInitializeImpl(bool initialLocked)
176 {
177     Handle handle;
178     NN_UTIL_RETURN_IF_FAILED(nn::svc::CreateMutex(&handle, initialLocked));
179     this->SetHandle(handle);
180     return ResultSuccess();
181 }
182 
Initialize(bool initialLocked)183 inline void Mutex::Initialize(bool initialLocked)
184 {
185     NN_OS_ERROR_IF_FAILED(TryInitializeImpl(initialLocked));
186 }
187 
TryInitialize(bool initialLocked)188 inline nn::Result Mutex::TryInitialize(bool initialLocked)
189 {
190     Result result = TryInitializeImpl(initialLocked);
191     if (result.GetSummary() == Result::SUMMARY_OUT_OF_RESOURCE)
192     {
193         return result;
194     }
195     NN_OS_ERROR_IF_FAILED(result);
196     return result;
197 }
198 
Mutex(bool initialLocked)199 inline Mutex::Mutex(bool initialLocked)
200 {
201     Initialize(initialLocked);
202 }
203 
Mutex(const nn::WithInitialize &)204 inline Mutex::Mutex(const nn::WithInitialize&)
205 {
206     Initialize(false);
207 }
208 
Unlock()209 inline void Mutex::Unlock()
210 {
211     NN_OS_ERROR_IF_FAILED(nn::svc::ReleaseMutex(GetHandle()));
212 }
213 
214 NN_UTIL_DETAIL_DEFINE_SCOPED_LOCK(Mutex, Lock(), Unlock());
215 
216 }} // namespace nn::os
217 
218 #endif // __cplusplus
219 
220 // C declarations follow
221 
222 #include <nn/util/detail/util_CLibImpl.h>
223 
224 /* Please see man pages for details
225 
226 
227 
228 
229 
230 
231 
232 
233 */
234 
235 /* Please see man pages for details
236 
237 
238 
239 */
240 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosMutex, nn::os::Mutex, 4, u32);
241 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosMutexToWaitObject, nnosMutex, nnosWaitObject);
242 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosWaitObjectToMutex, nnosWaitObject, nnosMutex);
243 
244 /* Please see man pages for details
245 
246 */
247 NN_EXTERN_C void nnosMutexInitialize(nnosMutex* this_, bool initialLocked);
248 
249 /* Please see man pages for details
250 
251 */
252 NN_EXTERN_C bool nnosMutexTryInitialize(nnosMutex* this_, bool initialLocked);
253 
254 /* Please see man pages for details
255 
256 */
257 NN_EXTERN_C void nnosMutexLock(nnosMutex* this_);
258 
259 /* Please see man pages for details
260 
261 */
262 NN_EXTERN_C bool nnosMutexTryLock(nnosMutex* this_, s64 timeout);
263 
264 /* Please see man pages for details
265 
266 */
267 NN_EXTERN_C void nnosMutexUnlock(nnosMutex* this_);
268 
269 /* Please see man pages for details
270 
271 */
272 NN_EXTERN_C void nnosMutexFinalize(nnosMutex* this_);
273 
274 /*
275 
276 
277 
278 */
279 
280 #endif /* NN_OS_IPC_OS_MUTEX_H_ */
281