/*---------------------------------------------------------------------------* 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: 20477 $ *---------------------------------------------------------------------------*/ #ifndef NN_OS_OS_NONRECURSIVECRITICALSECTION_H_ #define NN_OS_OS_NONRECURSIVECRITICALSECTION_H_ #include #include #include #include #ifdef __cplusplus // 未テスト namespace nn { namespace os { class NonRecursiveCriticalSection : 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 = false; // 初期化不要 NonRecursiveCriticalSection() { *m_Counter = 1; } ~NonRecursiveCriticalSection() {} void Enter() { while (!TryEnter()) { m_Counter.DecrementAndWaitIfLessThan(0); } } bool TryEnter() { ReverseIfPositiveUpdater updater; return m_Counter->AtomicUpdateConditional(updater); } void Leave() { NN_TASSERTMSG_( *m_Counter < 0 , "CriticalSection is not entered."); // カウンタの符号を正に設定することでロックを解除します。 ReverseUpdater updater; m_Counter->AtomicUpdateConditional(updater); // 待機している最も優先度の高いスレッドを起床します。 if( updater.afterUpdate > 1 ) { m_Counter.Signal(1); } } class ScopedLock; private: nn::os::WaitableCounter m_Counter; }; NN_UTIL_DETAIL_DEFINE_SCOPED_LOCK(NonRecursiveCriticalSection, Enter(), Leave()); }} #endif // __cplusplus #endif