/*---------------------------------------------------------------------------* Project: Horizon File: os_WaitableCounter.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: 18508 $ *---------------------------------------------------------------------------*/ /* @file :include nn/os.h */ #ifndef NN_OS_OS_WAITABLETIMER_H_ #define NN_OS_OS_WAITABLETIMER_H_ #ifdef __cplusplus #include #include #include #include #include namespace nn { namespace os { /* @brief スレッド間でカウンタの数値を管理する同期機構です。 内部にカウンタを持ち、このカウンタの数値をスレッド間で安全に操作したり、 カウンタの数値に対する条件が成立するまで待ち合わせたりすることができます。 */ class WaitableCounter { private: static nnHandle s_Handle; private: typedef nn::fnd::InterlockedVariable ValueType; ValueType m_Value; //!< カウンタ public: /* @brief カウンタを初期化します。 */ static void Initialize(); /* @brief カウンタを破棄します。 */ static void Finalize(); ValueType& operator *() { return m_Value; } const ValueType& operator *() const { return m_Value; } ValueType* operator->() { return &m_Value; } /* @brief カウンタの数値が value よりも小さければ、数値をデクリメントして待機します。 カウンタの数値が value 以上になるか、シグナルを通知されると待機を解除します。 @param[in] value カウンタの数値と比較する値 @return 関数の実行結果を返します。 */ Result DecrementAndWaitIfLessThan(s32 value) { return ArbitrateAddress(nn::os::ARBITRATION_TYPE_DECREMENT_AND_WAIT_IF_LESS_THAN, value); } /* @brief カウンタの数値が value よりも小さければ待機します。 カウンタの数値が value 以上になるか、シグナルを通知されると待機を解除します。 @param[in] value カウンタと比較する値 @return 関数の実行結果を返します。 */ Result WaitIfLessThan(s32 value) { return ArbitrateAddress(nn::os::ARBITRATION_TYPE_WAIT_IF_LESS_THAN, value); } /* @brief 指定した数の待機しているスレッドに対してシグナルを送ります。 オブジェクト数に -1 を指定した場合は全ての待機しているスレッドに対してシグナルを送ります。 @param[in] num シグナルを送るスレッド数 @return 関数の実行結果を返します。 */ Result Signal(s32 num) { return ArbitrateAddress(nn::os::ARBITRATION_TYPE_SIGNAL, num); } /* @brief 全ての待機しているスレッドに対してシグナルを送ります。 @return 関数の実行結果を返します。 */ Result SignalAll() { return Signal(-1); } private: Result ArbitrateAddress(nn::os::ArbitrationType type, s32 value) { return nn::svc::ArbitrateAddress(s_Handle, reinterpret_cast(&m_Value), type, value); } }; }} // namespace nn::os #endif // __cplusplus #endif