1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_Semaphore.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
23 #ifndef NN_OS_OS_SEMAPHORE_H_
24 #define NN_OS_OS_SEMAPHORE_H_
25
26 #include <nn/types.h>
27 #include <nn/Handle.h>
28 #include <nn/Result.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 class Semaphore : public InterruptEvent
58 {
59 public:
60
61 /* Please see man pages for details
62
63
64
65
66
67
68
69 */
70 Semaphore(s32 initialCount, s32 maxCount);
71
72 /* Please see man pages for details
73
74
75 */
Semaphore()76 Semaphore() {}
77
78 /* Please see man pages for details
79
80
81
82
83
84
85 */
86 void Initialize(s32 initialCount, s32 maxCount);
87
88 /* Please see man pages for details
89
90
91
92
93
94
95 */
96 nn::Result TryInitialize(s32 initialCount, s32 maxCount);
97
98 /* Please see man pages for details
99
100
101
102
103
104 */
Finalize()105 void Finalize() { InterruptEvent::Finalize(); }
106
107 /* Please see man pages for details
108
109 */
~Semaphore()110 ~Semaphore() {}
111
112 /* Please see man pages for details
113
114
115
116
117
118
119
120 */
121 s32 Release(s32 releaseCount = 1);
122
123 /* Please see man pages for details
124
125
126
127 */
Acquire()128 void Acquire() { this->WaitOne(); }
129
130 /* Please see man pages for details
131
132
133
134
135
136
137
138 */
TryAcquire(nn::fnd::TimeSpan timeout)139 bool TryAcquire(nn::fnd::TimeSpan timeout) { return this->WaitOne(timeout); }
140
141 class ScopedLock;
142
143 private:
144
145 Result TryInitializeImpl(s32 initialCount, s32 maxCount);
146
147 };
148
TryInitializeImpl(s32 initialCount,s32 maxCount)149 inline Result Semaphore::TryInitializeImpl(s32 initialCount, s32 maxCount)
150 {
151 Handle handle;
152 NN_UTIL_RETURN_IF_FAILED(nn::svc::CreateSemaphore(&handle, initialCount, maxCount));
153 this->SetHandle(handle);
154 return ResultSuccess();
155 }
156
Initialize(s32 initialCount,s32 maxCount)157 inline void Semaphore::Initialize(s32 initialCount, s32 maxCount)
158 {
159 NN_OS_ERROR_IF_FAILED(TryInitializeImpl(initialCount, maxCount));
160 }
161
TryInitialize(s32 initialCount,s32 maxCount)162 inline nn::Result Semaphore::TryInitialize(s32 initialCount, s32 maxCount)
163 {
164 Result result = TryInitializeImpl(initialCount, maxCount);
165 if (result.GetSummary() == Result::SUMMARY_OUT_OF_RESOURCE)
166 {
167 return result;
168 }
169 NN_OS_ERROR_IF_FAILED(result);
170 return result;
171 }
172
Semaphore(s32 initialCount,s32 maxCount)173 inline Semaphore::Semaphore(s32 initialCount, s32 maxCount)
174 {
175 Initialize(initialCount, maxCount);
176 }
177
Release(s32 releaseCount)178 inline s32 Semaphore::Release(s32 releaseCount)
179 {
180 s32 ret;
181 NN_OS_ERROR_IF_FAILED(nn::svc::ReleaseSemaphore(&ret, GetHandle(), releaseCount));
182 return ret;
183 }
184
185 NN_UTIL_DETAIL_DEFINE_SCOPED_LOCK(Semaphore, Acquire(), Release());
186
187 }} // namespace nn::os
188
189 #endif // __cplusplus
190
191 // C declarations follow
192
193 #include <nn/util/detail/util_CLibImpl.h>
194
195 /* Please see man pages for details
196
197
198
199
200
201
202
203
204 */
205
206 /* Please see man pages for details
207
208
209
210 */
211 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosSemaphore, nn::os::Semaphore, 4, u32);
212 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosSemaphoreToWaitObject, nnosSemaphore, nnosWaitObject);
213 NN_UTIL_DETAIL_CLIBIMPL_DECLARE_CONVERSION(nnosWaitObjectToSemaphore, nnosWaitObject, nnosSemaphore);
214
215 /* Please see man pages for details
216
217 */
218 NN_EXTERN_C void nnosSemaphoreInitialize(nnosSemaphore* this_, s32 initialCount, s32 maxCount);
219
220 /* Please see man pages for details
221
222 */
223 NN_EXTERN_C bool nnosSemaphoreTryInitialize(nnosSemaphore* this_, s32 initialCount, s32 maxCount);
224
225 /* Please see man pages for details
226
227 */
228 NN_EXTERN_C s32 nnosSemaphoreRelease(nnosSemaphore* this_, s32 releaseCount);
229
230 /* Please see man pages for details
231
232 */
233 NN_EXTERN_C void nnosSemaphoreAcquire(nnosSemaphore* this_);
234
235 /* Please see man pages for details
236
237 */
238 NN_EXTERN_C bool nnosSemaphoreTryAcquire(nnosSemaphore* this_, s64 nanoSeconds);
239
240 /* Please see man pages for details
241
242 */
243 NN_EXTERN_C void nnosSemaphoreFinalize(nnosSemaphore* this_);
244
245 /*
246
247
248
249 */
250
251 #endif
252