/*---------------------------------------------------------------------------* Project: Horizon File: os_LockPolicy.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: 12449 $ *---------------------------------------------------------------------------*/ #ifndef NN_OS_OS_LOCKPOLICY_H_ #define NN_OS_OS_LOCKPOLICY_H_ #ifdef __cplusplus #include #include namespace nn { namespace os { /*! @brief ロックポリシーをまとめたコンテナクラス */ struct LockPolicy { /*! @brief ロックをしないポリシーを表すポリシークラス */ struct NoLock { class LockObject { public: void Initialize() {} nn::Result TryInitialize() { return ResultSuccess(); } void Finalize() {} }; class ScopedLock { public: ScopedLock(const LockObject&) {} ~ScopedLock() {} }; }; /*! @brief 個々のオブジェクトに対して用意されたロック機構を使ってロックすることを表すポリシークラス @tparam Locker ロックに用いるオブジェクトの型を指定します。@ref nn::os::Mutex もしくは @ref nn::os::CriticalSection を指定できます。 LockPolicy に @ref nn::os::Mutex と @ref nn::os::CriticalSection 以外を指定した場合の動作・互換性は保障されません。 */ template struct Object { class ScopedLock; class LockObject { friend class ScopedLock; mutable Locker mutex; public: void Initialize() { mutex.Initialize(); } nn::Result TryInitialize() { return mutex.TryInitialize(); } void Finalize() { mutex.Finalize(); } }; class ScopedLock : private Locker::ScopedLock { public: ScopedLock(const LockObject& obj) : Locker::ScopedLock(obj.mutex) {} }; }; /*! @brief グローバルに一つだけ用意されたロック機構を使ってロックすることを表すポリシークラス @tparam Locker ロックに用いるオブジェクトの型を指定します。@ref nn::os::Mutex もしくは @ref nn::os::CriticalSection を指定できます。 LockPolicy に @ref nn::os::Mutex と @ref nn::os::CriticalSection 以外を指定した場合の動作・互換性は保障されません。 */ template struct Global { private: static Locker g_Mutex; public: class LockObject { public: void Initialize() {} nn::Result TryInitialize() { return ResultSuccess(); } void Finalize() {} }; class ScopedLock : private Locker::ScopedLock { public: ScopedLock(const LockObject&) : Locker::ScopedLock(g_Mutex) {} }; }; }; template Locker LockPolicy::Global::g_Mutex = nn::WithInitialize(); }} #endif #endif