1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: os_CriticalSection.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/assert.h>
17 #include <nn/config.h>
18 #include <nn/os/os_CriticalSection.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
32
Initialize()33 void CriticalSection::Initialize()
34 {
35 m_Lock.Initialize();
36 m_ThreadUniqueValue = GetInvalidThreadUniqueValue();
37 m_LockCount = 0;
38 }
39
Enter()40 void CriticalSection::Enter()
41 {
42 NN_TASSERT_(IsInitialized());
43
44 // If own thread has not entered critical section, attempt to enter it.
45 //
46
47 if( ! LockedByCurrentThread() )
48 {
49 m_Lock.Lock();
50 OnLocked();
51 }
52
53 ++this->m_LockCount;
54 }
55
TryEnter()56 bool CriticalSection::TryEnter()
57 {
58 NN_TASSERT_(IsInitialized());
59
60 // If own thread has not entered critical section, attempt to enter it.
61 //
62
63 if( ! LockedByCurrentThread() )
64 {
65 if( ! m_Lock.TryLock() )
66 {
67 return false;
68 }
69
70 OnLocked();
71 }
72
73 ++this->m_LockCount;
74 return true;
75 }
76
Leave()77 void CriticalSection::Leave()
78 {
79 NN_TASSERT_(IsInitialized());
80 NN_TASSERTMSG_(LockedByCurrentThread() && m_LockCount > 0, "CriticalSection is not entered on the current thread.");
81
82 if (--this->m_LockCount == 0)
83 {
84 NN_TASSERTMSG_( m_Lock.IsLocked(), "CriticalSection is not entered.");
85
86 // Clear ID of thread currently getting critical section.
87 m_ThreadUniqueValue = GetInvalidThreadUniqueValue();
88
89 m_Lock.Unlock();
90 }
91 }
92
93
94 }} // namespace nn::os
95
96 #include <new>
97
98 using namespace nn::os;
99
100 extern "C" {
101
nnosCriticalSectionInitialize(nnosCriticalSection * p)102 void nnosCriticalSectionInitialize(nnosCriticalSection* p)
103 {
104 new (p) CriticalSection(nn::WithInitialize());
105 }
106
nnosCriticalSectionTryInitialize(nnosCriticalSection * p)107 bool nnosCriticalSectionTryInitialize(nnosCriticalSection* p)
108 {
109 new (p) CriticalSection(nn::WithInitialize());
110 CriticalSection* pCriticalSection = reinterpret_cast<CriticalSection*>(p);
111 Result result = pCriticalSection->TryInitialize();
112 return result.IsSuccess();
113 }
114
nnosCriticalSectionEnter(nnosCriticalSection * p)115 void nnosCriticalSectionEnter(nnosCriticalSection* p)
116 {
117 CriticalSection* pCriticalSection = reinterpret_cast<CriticalSection*>(p);
118 pCriticalSection->Enter();
119 }
120
nnosCriticalSectionTryEnter(nnosCriticalSection * p)121 bool nnosCriticalSectionTryEnter(nnosCriticalSection* p)
122 {
123 CriticalSection* pCriticalSection = reinterpret_cast<CriticalSection*>(p);
124 return pCriticalSection->TryEnter();
125 }
126
nnosCriticalSectionLeave(nnosCriticalSection * p)127 void nnosCriticalSectionLeave(nnosCriticalSection* p)
128 {
129 CriticalSection* pCriticalSection = reinterpret_cast<CriticalSection*>(p);
130 pCriticalSection->Leave();
131 }
132
nnosCriticalSectionFinalize(nnosCriticalSection * p)133 void nnosCriticalSectionFinalize(nnosCriticalSection* p)
134 {
135 CriticalSection* pCriticalSection = reinterpret_cast<CriticalSection*>(p);
136 pCriticalSection->~CriticalSection();
137 }
138
139 }
140