1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_Semaphore.cpp
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: 46347 $
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 #include "os_Limits.h"
23
24 //---------------------------------------------------------------------------
25
26 using namespace nn;
27 using namespace nn::svc;
28
29
30 // C Function Definitions
31
32 #include <new>
33
34 using namespace nn::fnd;
35 using namespace nn::os;
36
37 namespace nn { namespace os {
38
39 #if NN_PLATFORM_HAS_MMU
GetCurrentCount()40 s32 Semaphore::GetCurrentCount()
41 {
42 return os::detail::GetLimitCurrentCount(LIMITABLE_RESOURCE_MAX_SEMAPHORE);
43 }
44
GetMaxCount()45 s32 Semaphore::GetMaxCount()
46 {
47 return os::detail::GetLimitMaxCount(LIMITABLE_RESOURCE_MAX_SEMAPHORE);
48 }
49 #endif // if NN_PLATFORM_HAS_MMU
50
51 }}
52
53 extern "C" {
54
NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosSemaphoreToWaitObject,nnosSemaphore,nnosWaitObject,Semaphore,WaitObject)55 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosSemaphoreToWaitObject, nnosSemaphore, nnosWaitObject, Semaphore, WaitObject)
56 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosWaitObjectToSemaphore, nnosWaitObject, nnosSemaphore, WaitObject, Semaphore)
57
58
59 void nnosSemaphoreInitialize(nnosSemaphore* p, s32 initialCount, s32 maxCount)
60 {
61 new (p) Semaphore(initialCount, maxCount);
62 }
63
nnosSemaphoreTryInitialize(nnosSemaphore * p,s32 initialCount,s32 maxCount)64 bool nnosSemaphoreTryInitialize(nnosSemaphore* p, s32 initialCount, s32 maxCount)
65 {
66 new (p) Semaphore();
67 Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
68 Result result = pSemaphore->TryInitialize(initialCount, maxCount);
69 return result.IsSuccess();
70 }
71
nnosSemaphoreRelease(nnosSemaphore * p,s32 releaseCount)72 s32 nnosSemaphoreRelease(nnosSemaphore* p, s32 releaseCount)
73 {
74 Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
75 return pSemaphore->Release(releaseCount);
76 }
77
nnosSemaphoreAcquire(nnosSemaphore * p)78 void nnosSemaphoreAcquire(nnosSemaphore* p)
79 {
80 Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
81 pSemaphore->Acquire();
82 }
83
nnosSemaphoreTryAcquire(nnosSemaphore * p,s64 nanoSeconds)84 bool nnosSemaphoreTryAcquire(nnosSemaphore* p, s64 nanoSeconds)
85 {
86 Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
87 return pSemaphore->TryAcquire(TimeSpan::FromNanoSeconds(nanoSeconds));
88 }
89
nnosSemaphoreFinalize(nnosSemaphore * p)90 void nnosSemaphoreFinalize(nnosSemaphore* p)
91 {
92 Semaphore* pSemaphore = reinterpret_cast<Semaphore*>(p);
93 pSemaphore->~Semaphore();
94 }
95
96 }
97