/*---------------------------------------------------------------------------* Project: Horizon File: os_InterCoreLightSemaphore.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: 33014 $ *---------------------------------------------------------------------------*/ #ifndef NN_OS_OS_INTERCORE_LIGHTSEMAPHORE_H_ #define NN_OS_OS_INTERCORE_LIGHTSEMAPHORE_H_ #ifdef __cplusplus #include #include #include #include #include #include namespace nn { namespace os { /*! :private @file @brief InterCoreLightSemaphore に関するAPI の宣言 :include nn/os.h */ /*! :private @brief スレッド間でリソース数の排他制御を行う同期機構です。 内部にカウンタ値を持ち、これを各スレッドで減算/加算する形で リソース数を管理します。 カウンタ値が 0 の場合は 1 以上になるまで待つことができます。 複数の同期オブジェクトを同時に待つことができないという点を除いて nn::os::InterCoreLightSemaphore の方が nn::os::Semaphore より 優れているため、通常は nn::os::InterCoreLightSemaphore を使用すべきです。 このクラスの初期化/終了以外のメンバ関数はスレッドセーフです。 */ class InterCoreLightSemaphore { public: static const s32 MAX_MAX_COUNT = 0x7fff; private: struct DecrementIfPositive { bool operator()(s32& x) { if( x > 0 ) { --x; return true; } else { return false; } } }; struct LimitedAdd { s32 max; s32 value; s32 beforeUpdate; bool operator()(s32& x) { beforeUpdate = x; if( x > max - value ) { x = max; } else { x += value; } return true; } }; private: WaitableCounter m_Counter; #if NN_PLATFORM_HAS_16BIT_LL_SC fnd::InterlockedVariable m_NumWaiting; s16 m_Max; #else fnd::InterlockedVariable m_NumWaiting; #endif public: //---------------------------------------- //! @name 初期化/終了 //@{ /*! :private @brief コンストラクタです。 初期化を行わないコンストラクタです。 別途 @ref Initialize を呼び出す必要があります。 */ InterCoreLightSemaphore() {} /*! :private @brief コンストラクタです。 初期化を行うコンストラクタです。 別途 @ref Initialize を呼び出す必要はありません。 @param[in] initialCount 初期カウンタ値を指定します。 @param[in] maxCount カウンタ値の最大値を指定します。 指定できる最大値は 32767 です。 */ InterCoreLightSemaphore(s32 initialCount, s32 maxCount) { Initialize(initialCount, maxCount); } InterCoreLightSemaphore(s32 initialCount) { Initialize(initialCount); } /*! :private @brief 初期化を行います。 @param[in] initialCount 初期カウンタ値を指定します。 @param[in] maxCount カウンタ値の最大値を指定します。 指定できる最大値は 32767 です。 @return なし。 */ void Initialize(s32 initialCount, s32 maxCount) { NN_MIN_TASSERT_( initialCount, 0 ); NN_MIN_TASSERT_( maxCount, 1 ); NN_MAX_TASSERT_( initialCount, maxCount ); NN_MAX_TASSERT_( maxCount, MAX_MAX_COUNT ); *m_Counter = initialCount; m_NumWaiting = 0; #if NN_PLATFORM_HAS_16BIT_LL_SC m_Max = maxCount; #else NN_UNUSED_VAR(maxCount); #endif } void Initialize(s32 initialCount) { Initialize(initialCount, MAX_MAX_COUNT); } /*! :private @brief 終了処理を行います。 @return なし。 */ void Finalize() {} //@} //---------------------------------------- //! @name デバッグ用情報取得 //@{ /*! :private @brief コンストラクタまたは @ref Initialize で指定したカウンタ値の 最大値を取得します。 @return 設定されているカウンタ値の最大値を返します。 */ #if NN_PLATFORM_HAS_16BIT_LL_SC s32 GetMax() const { return m_Max; } #endif /*! :private @brief 現在のカウンタ値を取得します。 @return 現在のカウンタ値を返します。 */ s32 GetCount() const { return *m_Counter; } //@} //---------------------------------------- //! @name カウンタの操作と待ち合わせ //@{ /*! :private @brief カウンタ値を 1 減算します。 カウンタ値が 0 であった場合は 1 以上になるまで待ったうえで 減算を行います。 @return なし。 */ void Acquire() { while( ! TryAcquire() ) { ++m_NumWaiting; m_Counter.WaitIfLessThan(1); --m_NumWaiting; } } /*! :private @brief カウンタ値を 1 減算しようとします。 カウンタ値が 0 であった場合は減算を行わずに返ります。 @return 減算を行ったなら true、カウンタ値が 0 であったために 減算を行えなかった場合は false を返します。 */ bool TryAcquire() { DecrementIfPositive updater; bool ret = m_Counter->AtomicUpdateConditional(updater); DataSynchronizationBarrier(); return ret; } /*! :private @brief カウンタ値に加算します。 カウンタ値が 0 であり、かついずれかのスレッドが カウンタ値が 1 以上になるのを待機している場合 カウンタ値が加算されるとそのスレッドの待機が解除されます。 複数のスレッドが待機している場合に 2 以上の値を 加算すると複数のスレッドの待機が解除されます。 加算した値より待機しているスレッドの方が多い場合は 加算した値分のスレッドだけが起床し、 残りは待機したままです。 起床するスレッドはスレッドの優先度に応じて優先度が高い方から起床します。 @param[in] releaseCount 加算する値を指定します。 1 以上でなければなりません。 指定しなかった場合は 1 加算します。 @return 加算前のカウンタ値を返します。 */ s32 Release(s32 releaseCount = 1); //@} class ScopedAcquire { private: InterCoreLightSemaphore* m_Semaphore; public: ScopedAcquire(InterCoreLightSemaphore& semaphore, bool wait = true) : m_Semaphore(wait ? (semaphore.Acquire(), &semaphore) : (semaphore.TryAcquire() ? &semaphore : 0)) {} bool Aquired() const { return m_Semaphore != 0; } void Detach() { this->m_Semaphore = 0; } ~ScopedAcquire() { if (m_Semaphore) { m_Semaphore->Release(); } } }; }; }} // namespace nn::os #endif // __cplusplus // 以下、C 用宣言 #include NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnosInterCoreLightSemaphore, nn::os::InterCoreLightSemaphore, 8, u32); NN_EXTERN_C void nnosInterCoreLightSemaphoreInitialize(nnosInterCoreLightSemaphore* this_, s32 initialCount, s32 maxCount); #if NN_PLATFORM_HAS_16BIT_LL_SC NN_EXTERN_C s32 nnosInterCoreLightSemaphoreGetMax(nnosInterCoreLightSemaphore* p); #endif NN_EXTERN_C s32 nnosInterCoreLightSemaphoreGetCount(nnosInterCoreLightSemaphore* p); NN_EXTERN_C s32 nnosInterCoreLightSemaphoreRelease(nnosInterCoreLightSemaphore* this_, s32 releaseCount); NN_EXTERN_C void nnosInterCoreLightSemaphoreAcquire(nnosInterCoreLightSemaphore* this_); NN_EXTERN_C bool nnosInterCoreLightSemaphoreTryAcquire(nnosInterCoreLightSemaphore* this_); NN_EXTERN_C void nnosInterCoreLightSemaphoreFinalize(nnosInterCoreLightSemaphore* this_); #endif // ifndef NN_OS_OS_INTERCORE_LIGHTSEMAPHORE_H_