/*---------------------------------------------------------------------------* Project: Horizon File: os_InterCoreCriticalSection.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: 31762 $ *--------------------------------------------------------------------------- */ /* Please see man pages for details */ #ifndef NN_OS_OS_INTERCORE_CRITICALSECTION_H_ #define NN_OS_OS_INTERCORE_CRITICALSECTION_H_ #include #include #include #include #include #include #ifdef __cplusplus namespace nn { namespace os { /* Please see man pages for details */ class InterCoreCriticalSection : 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 */ InterCoreCriticalSection() : m_ThreadUniqueValue(GetInvalidThreadUniqueValue()), m_LockCount(-1) {} /* Please see man pages for details */ InterCoreCriticalSection(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 */ ~InterCoreCriticalSection() {} /* 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, "InterCoreCriticalSection is not entered on the current thread."); if (--this->m_LockCount == 0) { NN_TASSERTMSG_( *m_Counter < 0 , "InterCoreCriticalSection 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); DataSynchronizationBarrier(); // 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; bool ret = m_Counter->AtomicUpdateConditional(updater); DataSynchronizationBarrier(); if (ret) { // 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(InterCoreCriticalSection, Enter(), Leave()); }} // namespace nn::os #endif // __cplusplus // C declarations follow #include #define NN_OS_INTERCORE_CRITICALSECTION_SIZE 12 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosInterCoreCriticalSection, nn::os::InterCoreCriticalSection, NN_OS_INTERCORE_CRITICALSECTION_SIZE, u32); NN_EXTERN_C void nnosInterCoreCriticalSectionInitialize(nnosInterCoreCriticalSection* this_); NN_EXTERN_C bool nnosInterCoreCriticalSectionTryInitialize(nnosInterCoreCriticalSection* this_); NN_EXTERN_C void nnosInterCoreCriticalSectionEnter(nnosInterCoreCriticalSection* this_); NN_EXTERN_C bool nnosInterCoreCriticalSectionTryEnter(nnosInterCoreCriticalSection* this_); NN_EXTERN_C void nnosInterCoreCriticalSectionLeave(nnosInterCoreCriticalSection* this_); NN_EXTERN_C void nnosInterCoreCriticalSectionFinalize(nnosInterCoreCriticalSection* this_); #endif