/*---------------------------------------------------------------------------* Project: Horizon File: os_CriticalSection.h Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 34502 $ *--------------------------------------------------------------------------- */ /* Please see man pages for details */ #ifndef NN_OS_OS_CRITICALSECTION_H_ #define NN_OS_OS_CRITICALSECTION_H_ #include #include #include #include #include #ifdef __cplusplus namespace nn { namespace os { /* Please see man pages for details */ class CriticalSection : private nn::util::NonCopyable { private: struct ReverseIfPositiveUpdater { bool operator()(s32& x) { if( x > 0 ) { x = -x; return true; } else { return false; } } }; struct ReverseUpdater { s32 afterUpdate; bool operator()(s32& x) { x = -x; afterUpdate = x; return true; } }; public: static const bool CAN_LOCK_RECURSIVELY = true; /* Please see man pages for details */ CriticalSection() : m_ThreadUniqueValue(GetInvalidThreadUniqueValue()), m_LockCount(-1) {} /* Please see man pages for details */ CriticalSection(const nn::WithInitialize&) { Initialize(); } /* Please see man pages for details */ void Initialize() { *m_Counter = 1; m_ThreadUniqueValue = GetInvalidThreadUniqueValue(); m_LockCount = 0; } /* Please see man pages for details */ nn::Result TryInitialize() { Initialize(); return ResultSuccess(); } /* Please see man pages for details */ void Finalize() { m_LockCount = -1; } /* Please see man pages for details */ ~CriticalSection() {} /* Please see man pages for details */ void Enter() { NN_TASSERT_(IsInitialized()); // Attempt entry to critical section if this thread has not entered critical section. // if (!LockedByCurrentThread() && !TryEnterImpl()) { EnterImpl(); } ++this->m_LockCount; } /* Please see man pages for details */ bool TryEnter() { NN_TASSERT_(IsInitialized()); // Attempt entry to critical section if this thread has not entered critical section. // if (LockedByCurrentThread() || TryEnterImpl()) { ++this->m_LockCount; return true; } else { return false; } } /* Please see man pages for details */ void Leave() { NN_TASSERT_(IsInitialized()); NN_TASSERTMSG_(LockedByCurrentThread() && m_LockCount > 0, "CriticalSection is not entered on the current thread."); if (--this->m_LockCount == 0) { NN_TASSERTMSG_( *m_Counter < 0 , "CriticalSection is not entered."); // Clear the thread ID that currently has the critical section. m_ThreadUniqueValue = GetInvalidThreadUniqueValue(); // Unlock by setting the counter's sign to positive. ReverseUpdater updater; m_Counter->AtomicUpdateConditional(updater); // Wake the waiting thread that has the highest priority. if( updater.afterUpdate > 1 ) { m_Counter.Signal(1); } } } /* Please see man pages for details */ class ScopedLock; bool IsLocked() const { return (*m_Counter < 0); } private: void EnterImpl(); bool TryEnterImpl() { ReverseIfPositiveUpdater updater; if (m_Counter->AtomicUpdateConditional(updater)) { // If critical section is successfully entered, the counter's sign changes from positive to negative. // NN_TASSERT_(m_LockCount == 0); // Save the thread ID that currently has the critical section. this->m_ThreadUniqueValue = GetThreadUniqueValue(); return true; } else { return false; } } // TODO: We recommend moving this to ARM. #ifdef NN_PROCESSOR_ARM946ES static uptr GetThreadUniqueValue() { return nn::os::CTR::ARM946ES::GetThreadId(); } static uptr GetInvalidThreadUniqueValue() { return static_cast(-1); } #else static uptr GetThreadUniqueValue() { uptr v; HW_GET_CP15_THREAD_ID_USER_READ_ONLY(v); return v; } static uptr GetInvalidThreadUniqueValue() { return 0; } #endif bool LockedByCurrentThread() const { return GetThreadUniqueValue() == m_ThreadUniqueValue; } private: nn::os::WaitableCounter m_Counter; uptr m_ThreadUniqueValue; s32 m_LockCount; bool IsInitialized() const { return m_LockCount >= 0; } }; NN_UTIL_DETAIL_DEFINE_SCOPED_LOCK(CriticalSection, Enter(), Leave()); }} // namespace nn::os #endif // __cplusplus // C declarations follow #include #define NN_OS_CRITICALSECTION_SIZE 12 /* Please see man pages for details */ /* Please see man pages for details */ NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosCriticalSection, nn::os::CriticalSection, NN_OS_CRITICALSECTION_SIZE, u32); /* Please see man pages for details */ NN_EXTERN_C void nnosCriticalSectionInitialize(nnosCriticalSection* this_); /* Please see man pages for details */ NN_EXTERN_C bool nnosCriticalSectionTryInitialize(nnosCriticalSection* this_); /* Please see man pages for details */ NN_EXTERN_C void nnosCriticalSectionEnter(nnosCriticalSection* this_); /* Please see man pages for details */ NN_EXTERN_C bool nnosCriticalSectionTryEnter(nnosCriticalSection* this_); /* Please see man pages for details */ NN_EXTERN_C void nnosCriticalSectionLeave(nnosCriticalSection* this_); /* Please see man pages for details */ NN_EXTERN_C void nnosCriticalSectionFinalize(nnosCriticalSection* this_); /* */ #endif