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