/** * @file include/ActivMiddleware/ActivBase/SharedMutexFactory.h * * @brief Header file for the shared mutex factory class. */ #if (!defined ACTIV_BASE_SHARED_MUTEX_FACTORY_H) #define ACTIV_BASE_SHARED_MUTEX_FACTORY_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/IMutex.h" #include "ActivMiddleware/ActivBase/Timeout.h" #include "ActivMiddleware/Misc/Singleton.h" //! Activ namespace. namespace Activ { /** * @brief Shared mutex factory class. * * Mutexes will, by default, be recursive. This can be overridden by setting the ACTIV_NON_RECURSIVE_MUTEX environment * variable (to any value). This is a first step to switching to non-recursive mutexes. * * On Posix, the environment variable ACTIV_COUNT_MUTEX_CONTENTION can be used to hackily count mutex contentions. * It should only be used for temporary profiling, as it will incur a small performance hit. * Windows always counts mutex contentions. */ class SharedMutexFactory : public Singleton { public: /** * @brief Create a mutex. * * @param name the mutex name. * @param isInitialOwner whether the mutex is initially locked on creation. * * @return pointer to an object providing an IMutex interface. * * @throw Exception. */ ACTIV_BASE_API IMutex *Create(const String &name, const bool isInitialOwner = false); /** * @brief Open an existing mutex. * * @param name the mutex name. * @param timeout how long to wait for the object to exist, in ms. * * @return pointer to an object providing an IMutex interface. * * @throw Exception. */ ACTIV_BASE_API IMutex *Open(const String &name, Timeout &timeout = Timeout::GetInfinite()); /** * @brief Get a reference to the singleton instance. * * @return Reference. */ ACTIV_BASE_API static SharedMutexFactory &GetInstance(); private: /** * @brief Constructor. */ ACTIV_BASE_API SharedMutexFactory(); /** * @brief Destructor. */ ACTIV_BASE_API ~SharedMutexFactory(); /** * @brief Create a mutex. * * @param name the mutex name. * @param isInitialOwner whether the mutex is initially locked on creation. * * @return pointer to an object providing an IMutex interface. * * @throw Exception. */ template IMutex *InternalCreate(const String &name, const bool isInitialOwner); /** * @brief Open an existing mutex. * * @param name the mutex name. * @param timeout how long to wait for the object to exist, in ms. * * @return pointer to an object providing an IMutex interface. * * @throw Exception. */ template IMutex *InternalOpen(const String &name, Timeout &timeout); const bool m_isRecursive; ///< default is recursive unless overridden by ACTIV_NON_RECURSIVE_MUTEX env var const bool m_shouldCountContention; ///< ignored on Windows (always counts); default false on Posix const uint32_t m_longAcquireTimeUs; ///< Posix only, length of time in us to class an acquire as long enough to log. friend class Singleton; }; } // namespace Activ #endif // (!defined ACTIV_BASE_SHARED_MUTEX_FACTORY_H)