1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_LightSemaphore.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.h>
17
18 //---------------------------------------------------------------------------
19
20 namespace nn {
21 namespace os {
22
Release(s32 releaseCount)23 s32 LightSemaphore::Release(s32 releaseCount /*= 1*/)
24 {
25 NN_MIN_TASSERT_( releaseCount, 1 );
26
27 #if NN_PLATFORM_HAS_16BIT_LL_SC
28 LimitedAdd updater;
29 updater.max = m_Max;
30 updater.value = releaseCount;
31
32 m_Counter->AtomicUpdateConditional(updater);
33 const s32 beforeUpdate = updater.beforeUpdate;
34 #else
35 const s32 beforeUpdate = m_Counter->operator++(0);
36 #endif
37
38 // If the counter before addition is zero, the thread for the value to be added is woken up.
39 if( (beforeUpdate <= 0) || (m_NumWaiting > 0) )
40 {
41 m_Counter.Signal(releaseCount);
42 }
43
44 return beforeUpdate;
45 }
46
TryAcquire(fnd::TimeSpan timeout)47 bool LightSemaphore::TryAcquire(fnd::TimeSpan timeout)
48 {
49 if( TryAcquire() )
50 {
51 return true;
52 }
53
54 os::Tick begin = os::Tick::GetSystemCurrent();
55 os::Tick end = begin + timeout;
56
57 do
58 {
59 os::Tick remainTick = end - os::Tick::GetSystemCurrent();
60
61 if( remainTick <= 0 )
62 {
63 return false;
64 }
65
66 ++m_NumWaiting;
67 Result result = m_Counter.WaitIfLessThan(1, remainTick);
68 --m_NumWaiting;
69
70 if( result != ResultSuccess() )
71 {
72 return false;
73 }
74
75 } while( ! TryAcquire() );
76
77 return true;
78 }
79
80 }
81 }
82
83 using namespace nn;
84
85
86 // C Function Definitions
87
88 #include <new>
89
90 using namespace nn::os;
91
92 extern "C" {
93
nnosLightSemaphoreInitialize(nnosLightSemaphore * p,s32 initialCount,s32 maxCount)94 void nnosLightSemaphoreInitialize(nnosLightSemaphore* p, s32 initialCount, s32 maxCount)
95 {
96 new (p) LightSemaphore();
97 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
98 pLightSemaphore->Initialize(initialCount, maxCount);
99 }
100
101 #if NN_PLATFORM_HAS_16BIT_LL_SC
nnosLightSemaphoreGetMax(nnosLightSemaphore * p)102 s32 nnosLightSemaphoreGetMax(nnosLightSemaphore* p)
103 {
104 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
105 return pLightSemaphore->GetMax();
106 }
107 #endif
108
nnosLightSemaphoreGetCount(nnosLightSemaphore * p)109 s32 nnosLightSemaphoreGetCount(nnosLightSemaphore* p)
110 {
111 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
112 return pLightSemaphore->GetCount();
113 }
114
nnosLightSemaphoreRelease(nnosLightSemaphore * p,s32 releaseCount)115 s32 nnosLightSemaphoreRelease(nnosLightSemaphore* p, s32 releaseCount)
116 {
117 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
118 return pLightSemaphore->Release(releaseCount);
119 }
120
nnosLightSemaphoreAcquire(nnosLightSemaphore * p)121 void nnosLightSemaphoreAcquire(nnosLightSemaphore* p)
122 {
123 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
124 pLightSemaphore->Acquire();
125 }
126
nnosLightSemaphoreTryAcquire(nnosLightSemaphore * p)127 bool nnosLightSemaphoreTryAcquire(nnosLightSemaphore* p)
128 {
129 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
130 return pLightSemaphore->TryAcquire();
131 }
132
nnosLightSemaphoreFinalize(nnosLightSemaphore * p)133 void nnosLightSemaphoreFinalize(nnosLightSemaphore* p)
134 {
135 LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
136 pLightSemaphore->~LightSemaphore();
137 }
138
139 } // extern "C"
140