1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_InterCoreCriticalSection.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: 28736 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/assert.h>
17 #include <nn/config.h>
18 #include <nn/os/os_InterCoreCriticalSection.h>
19 #include <nn/fnd/fnd_Interlocked.h>
20 #include <nn/os/os_Timer.h>
21 
22 //---------------------------------------------------------------------------
23 
24 using namespace nn;
25 using namespace nn::fnd;
26 using namespace nn::svc;
27 using namespace nn::os;
28 using namespace nn::util;
29 
30 namespace nn{ namespace os{
31 
EnterImpl()32 void InterCoreCriticalSection::EnterImpl()
33 {
34     for(;;)
35     {
36         // この時点でカウンタをチェックすることで TryEnterImpl を呼び出すよりも
37         // 高速に他のスレッドがクリティカルセクションに侵入しているかを判定します。
38         if( *m_Counter > 0 )
39         {
40             if( TryEnterImpl() )
41             {
42                 break;
43             }
44         }
45 
46         // 他のスレッドがクリティカルセクションに侵入していたら
47         // カウンタをデクリメントして待機します。
48         m_Counter.DecrementAndWaitIfLessThan(0);
49     }
50 }
51 
52 }} // namespace nn::os
53 
54 #include <new>
55 
56 using namespace nn::os;
57 
58 extern "C" {
59 
nnosInterCoreCriticalSectionInitialize(nnosInterCoreCriticalSection * p)60 void nnosInterCoreCriticalSectionInitialize(nnosInterCoreCriticalSection* p)
61 {
62     new (p) InterCoreCriticalSection(nn::WithInitialize());
63 }
64 
nnosInterCoreCriticalSectionTryInitialize(nnosInterCoreCriticalSection * p)65 bool nnosInterCoreCriticalSectionTryInitialize(nnosInterCoreCriticalSection* p)
66 {
67     new (p) InterCoreCriticalSection(nn::WithInitialize());
68     InterCoreCriticalSection* pInterCoreCriticalSection = reinterpret_cast<InterCoreCriticalSection*>(p);
69     Result result = pInterCoreCriticalSection->TryInitialize();
70     return result.IsSuccess();
71 }
72 
nnosInterCoreCriticalSectionEnter(nnosInterCoreCriticalSection * p)73 void nnosInterCoreCriticalSectionEnter(nnosInterCoreCriticalSection* p)
74 {
75     InterCoreCriticalSection* pInterCoreCriticalSection = reinterpret_cast<InterCoreCriticalSection*>(p);
76     pInterCoreCriticalSection->Enter();
77 }
78 
nnosInterCoreCriticalSectionTryEnter(nnosInterCoreCriticalSection * p)79 bool nnosInterCoreCriticalSectionTryEnter(nnosInterCoreCriticalSection* p)
80 {
81     InterCoreCriticalSection* pInterCoreCriticalSection = reinterpret_cast<InterCoreCriticalSection*>(p);
82     return pInterCoreCriticalSection->TryEnter();
83 }
84 
nnosInterCoreCriticalSectionLeave(nnosInterCoreCriticalSection * p)85 void nnosInterCoreCriticalSectionLeave(nnosInterCoreCriticalSection* p)
86 {
87     InterCoreCriticalSection* pInterCoreCriticalSection = reinterpret_cast<InterCoreCriticalSection*>(p);
88     pInterCoreCriticalSection->Leave();
89 }
90 
nnosInterCoreCriticalSectionFinalize(nnosInterCoreCriticalSection * p)91 void nnosInterCoreCriticalSectionFinalize(nnosInterCoreCriticalSection* p)
92 {
93     InterCoreCriticalSection* pInterCoreCriticalSection = reinterpret_cast<InterCoreCriticalSection*>(p);
94     pInterCoreCriticalSection->~InterCoreCriticalSection();
95 }
96 
97 }
98