1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_LightSemaphore.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: 32759 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/os/os_LightSemaphore.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         // 加算前のカウンタが 0 であれば、加算した値分のスレッドを起床します。
39         if( (beforeUpdate <= 0) || (m_NumWaiting > 0) )
40         {
41             m_Counter.Signal(releaseCount);
42         }
43 
44         return beforeUpdate;
45     }
46 
47 }
48 }
49 
50 using namespace nn;
51 
52 
53 // C 関数の定義
54 
55 #include <new>
56 
57 using namespace nn::os;
58 
59 extern "C" {
60 
nnosLightSemaphoreInitialize(nnosLightSemaphore * p,s32 initialCount,s32 maxCount)61 void nnosLightSemaphoreInitialize(nnosLightSemaphore* p, s32 initialCount, s32 maxCount)
62 {
63     new (p) LightSemaphore();
64     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
65     pLightSemaphore->Initialize(initialCount, maxCount);
66 }
67 
68 #if NN_PLATFORM_HAS_16BIT_LL_SC
nnosLightSemaphoreGetMax(nnosLightSemaphore * p)69 s32 nnosLightSemaphoreGetMax(nnosLightSemaphore* p)
70 {
71     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
72     return pLightSemaphore->GetMax();
73 }
74 #endif
75 
nnosLightSemaphoreGetCount(nnosLightSemaphore * p)76 s32 nnosLightSemaphoreGetCount(nnosLightSemaphore* p)
77 {
78     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
79     return pLightSemaphore->GetCount();
80 }
81 
nnosLightSemaphoreRelease(nnosLightSemaphore * p,s32 releaseCount)82 s32 nnosLightSemaphoreRelease(nnosLightSemaphore* p, s32 releaseCount)
83 {
84     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
85     return pLightSemaphore->Release(releaseCount);
86 }
87 
nnosLightSemaphoreAcquire(nnosLightSemaphore * p)88 void nnosLightSemaphoreAcquire(nnosLightSemaphore* p)
89 {
90     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
91     pLightSemaphore->Acquire();
92 }
93 
nnosLightSemaphoreTryAcquire(nnosLightSemaphore * p)94 bool nnosLightSemaphoreTryAcquire(nnosLightSemaphore* p)
95 {
96     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
97     return pLightSemaphore->TryAcquire();
98 }
99 
nnosLightSemaphoreFinalize(nnosLightSemaphore * p)100 void nnosLightSemaphoreFinalize(nnosLightSemaphore* p)
101 {
102     LightSemaphore* pLightSemaphore = reinterpret_cast<LightSemaphore*>(p);
103     pLightSemaphore->~LightSemaphore();
104 }
105 
106 } // extern "C"
107