1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Semaphore.cpp
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: 12449 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/os/os_Semaphore.h>
17 #include <nn/assert.h>
18 #include <nn/os/os_Result.h>
19 #include <nn/os/os_Synchronization.h>
20 #include <nn/svc/svc_Stub.h>
21 
22 //---------------------------------------------------------------------------
23 
24 using namespace nn;
25 using namespace nn::svc;
26 
27 
28 // C 関数の定義
29 
30 #include <new>
31 
32 using namespace nn::fnd;
33 using namespace nn::os;
34 
35 extern "C" {
36 
NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosSemaphoreToWaitObject,nnosSemaphore,nnosWaitObject,Semaphore,WaitObject)37 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosSemaphoreToWaitObject, nnosSemaphore, nnosWaitObject, Semaphore, WaitObject)
38 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosWaitObjectToSemaphore, nnosWaitObject, nnosSemaphore, WaitObject, Semaphore)
39 
40 
41 void nnosSemaphoreInitialize(nnosSemaphore* p, s32 initialCount, s32 maxCount)
42 {
43     new (p) Semaphore(initialCount, maxCount);
44 }
45 
nnosSemaphoreTryInitialize(nnosSemaphore * p,s32 initialCount,s32 maxCount)46 bool nnosSemaphoreTryInitialize(nnosSemaphore* p, s32 initialCount, s32 maxCount)
47 {
48     new (p) Semaphore();
49     Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
50     Result result = pSemaphore->TryInitialize(initialCount, maxCount);
51     return result.IsSuccess();
52 }
53 
nnosSemaphoreRelease(nnosSemaphore * p,s32 releaseCount)54 s32 nnosSemaphoreRelease(nnosSemaphore* p, s32 releaseCount)
55 {
56     Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
57     return pSemaphore->Release(releaseCount);
58 }
59 
nnosSemaphoreAcquire(nnosSemaphore * p)60 void nnosSemaphoreAcquire(nnosSemaphore* p)
61 {
62     Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
63     pSemaphore->Acquire();
64 }
65 
nnosSemaphoreTryAcquire(nnosSemaphore * p,s64 nanoSeconds)66 bool nnosSemaphoreTryAcquire(nnosSemaphore* p, s64 nanoSeconds)
67 {
68     Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
69     return pSemaphore->TryAcquire(TimeSpan::FromNanoSeconds(nanoSeconds));
70 }
71 
nnosSemaphoreFinalize(nnosSemaphore * p)72 void nnosSemaphoreFinalize(nnosSemaphore* p)
73 {
74     Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
75     pSemaphore->~Semaphore();
76 }
77 
78 }
79