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