/*---------------------------------------------------------------------------* 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: 31411 $ *---------------------------------------------------------------------------*/ /*! :private @file @brief InterCoreCriticalSection に関する API の宣言 :include nn/os.h */ #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 { /*! :private @brief マルチコア間での排他制御を行うためのクラスです。 基本的な使い方などについては @ref nn::os::CriticalSection を参照してください。 */ 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; /*! :private @brief オブジェクトを構築します。 初期化しないコンストラクタと、初期化するコンストラクタが用意されています。 初期化しない場合、使用する前に、@ref nn::os::InterCoreCriticalSection::Initialize を呼んで明示的に初期化する必要があります。 */ InterCoreCriticalSection() : m_ThreadUniqueValue(GetInvalidThreadUniqueValue()), m_LockCount(-1) {} /*! :private @brief オブジェクトを構築し、初期化を行います。 */ InterCoreCriticalSection(const nn::WithInitialize&) { Initialize(); } /*! :private @brief オブジェクトを初期化します。 @return 無し。 */ void Initialize() { *m_Counter = 1; m_ThreadUniqueValue = GetInvalidThreadUniqueValue(); m_LockCount = 0; } /*! :private @brief オブジェクトの初期化を試みます。 @return 処理結果を返します。 */ nn::Result TryInitialize() { Initialize(); return ResultSuccess(); } /*! :private @brief クリティカルセクションを破棄します。 デストラクタから自動的に呼び出されますが、明示的に呼ぶこともできます。 @return 無し。 */ void Finalize() { m_LockCount = -1; } /*! :private @brief デストラクタです。 */ ~InterCoreCriticalSection() {} /*! :private @brief ロックして他のスレッドがクリティカルセクションに進入するのを防ぎます。ブロックします。 @return 無し。 */ void Enter() { NN_TASSERT_(IsInitialized()); // 自スレッドがクリティカルセクションに進入していなければ // クリティカルセクションに進入を試みます。 if (!LockedByCurrentThread() && !TryEnterImpl()) { EnterImpl(); } ++this->m_LockCount; } /*! :private @brief ロックして他のスレッドがクリティカルセクションに進入するのを防ぎます。ブロックしません。 @return ロックに成功したかを返します。 */ bool TryEnter() { NN_TASSERT_(IsInitialized()); // 自スレッドがクリティカルセクションに進入していなければ // クリティカルセクションに進入を試みます。 if (LockedByCurrentThread() || TryEnterImpl()) { ++this->m_LockCount; return true; } else { return false; } } /*! :private @brief アンロックして他のスレッドがクリティカルセクションに進入できるようにします。 @return 無し。 */ 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."); // クリティカルセクションを取得中のスレッド ID をクリアします。 m_ThreadUniqueValue = GetInvalidThreadUniqueValue(); // カウンタの符号を正に設定することでロックを解除します。 ReverseUpdater updater; m_Counter->AtomicUpdateConditional(updater); DataSynchronizationBarrier(); // 待機している最も優先度の高いスレッドを起床します。 if( updater.afterUpdate > 1 ) { m_Counter.Signal(1); } } } /*! :private @class nn::os::InterCoreCriticalSection::ScopedLock @brief オブジェクトの生成時からオブジェクトの存在するスコープを抜けるまで間クリティカルセクションに入ります。 */ 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) { // クリティカルセクションの進入に成功すれば // カウンタの符号が正から負に変わります。 NN_TASSERT_(m_LockCount == 0); // クリティカルセクションを取得したスレッド ID を保存します。 this->m_ThreadUniqueValue = GetThreadUniqueValue(); return true; } else { return false; } } // TODO: 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 用宣言 #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