📄 ncbimtx.hpp
字号:
#if defined(SYSTEM_MUTEX_INITIALIZER)/// Define static fast mutex initial value.# define STATIC_FAST_MUTEX_INITIALIZER \ { SYSTEM_MUTEX_INITIALIZER, NCBI_NS_NCBI::SSystemFastMutex::eMutexInitialized }/// Define static fast mutex and initialize it.# define DEFINE_STATIC_FAST_MUTEX(id) \static NCBI_NS_NCBI::SSystemFastMutex id = STATIC_FAST_MUTEX_INITIALIZER/// Declare static fast mutex.# define DECLARE_CLASS_STATIC_FAST_MUTEX(id) \static NCBI_NS_NCBI::SSystemFastMutex id/// Define fast mutex and initialize it.# define DEFINE_CLASS_STATIC_FAST_MUTEX(id) \NCBI_NS_NCBI::SSystemFastMutex id = STATIC_FAST_MUTEX_INITIALIZER/// Define static mutex initializer.# define STATIC_MUTEX_INITIALIZER \ { STATIC_FAST_MUTEX_INITIALIZER, THREAD_SYSTEM_ID_INITIALIZER, 0 }/// Define static mutex and initialize it.# define DEFINE_STATIC_MUTEX(id) \static NCBI_NS_NCBI::SSystemMutex id = STATIC_MUTEX_INITIALIZER/// Declare static mutex.# define DECLARE_CLASS_STATIC_MUTEX(id) \static NCBI_NS_NCBI::SSystemMutex id/// Define mutex and initialize it.# define DEFINE_CLASS_STATIC_MUTEX(id) \NCBI_NS_NCBI::SSystemMutex id = STATIC_MUTEX_INITIALIZER#else/// Auto initialization for mutex will be used.# define NEED_AUTO_INITIALIZE_MUTEX/// Define auto-initialized static fast mutex.# define DEFINE_STATIC_FAST_MUTEX(id) \static NCBI_NS_NCBI::CAutoInitializeStaticFastMutex id/// Declare auto-initialized static fast mutex.# define DECLARE_CLASS_STATIC_FAST_MUTEX(id) \static NCBI_NS_NCBI::CAutoInitializeStaticFastMutex id/// Define auto-initialized mutex.# define DEFINE_CLASS_STATIC_FAST_MUTEX(id) \NCBI_NS_NCBI::CAutoInitializeStaticFastMutex id/// Define auto-initialized static mutex.# define DEFINE_STATIC_MUTEX(id) \static NCBI_NS_NCBI::CAutoInitializeStaticMutex id/// Declare auto-initialized static mutex.# define DECLARE_CLASS_STATIC_MUTEX(id) \static NCBI_NS_NCBI::CAutoInitializeStaticMutex id/// Define auto-initialized mutex.# define DEFINE_CLASS_STATIC_MUTEX(id) \NCBI_NS_NCBI::CAutoInitializeStaticMutex id#endif#if defined(NEED_AUTO_INITIALIZE_MUTEX)/////////////////////////////////////////////////////////////////////////////////// CAutoInitializeStaticFastMutex --////// Define thread safe initializer static for SSystemFastMutex. ////// Needed on platforms where system mutex struct cannot be initialized at/// compile time (e.g. Win32).class CAutoInitializeStaticFastMutex{public: typedef SSystemFastMutex TObject; ///< Simplified alias name for fast mutex /// Lock mutex. void Lock(void); /// Unlock mutex. void Unlock(void); /// Try locking the mutex. bool TryLock(void); /// Return initialized mutex object. operator TObject&(void);protected: /// Initialize mutex. /// /// This method can be called many times it will return only after /// successful initialization of m_Mutex. NCBI_XNCBI_EXPORT void Initialize(void); /// Get initialized mutex object. TObject& Get(void);private: TObject m_Mutex; ///< Mutex object.};/////////////////////////////////////////////////////////////////////////////////// CAutoInitializeStaticMutex --////// Define thread safe initializer static for SSystemMutex. ////// Needed on platforms where system mutex struct cannot be initialized at/// compile time (e.g. Win32).class CAutoInitializeStaticMutex{public: typedef SSystemMutex TObject; ///< Simplified alias name for fast mutex /// Lock mutex. void Lock(void); /// Unlock mutex. void Unlock(void); /// Try locking the mutex. bool TryLock(void); /// Return initialized mutex object. operator TObject&(void);protected: /// Initialize mutex. /// /// This method can be called many times it will return only after /// successful initialization of m_Mutex. NCBI_XNCBI_EXPORT void Initialize(void); /// Get initialized mutex object. TObject& Get(void);private: TObject m_Mutex; ///< Mutex object.};#endif///////////////////////////////////////////////////////////////////////////////// FAST MUTEX//// CFastMutex::// CFastMutexGuard::///////////////////////////////////////////////////////////////////////////////////// CFastMutex --////// Simple mutex with fast lock/unlock functions.////// This mutex can be used instead of CMutex if it's guaranteed that/// there is no nesting. This mutex does not check nesting or owner./// It has better performance than CMutex, but is less secure.class CFastMutex{public: /// Constructor. /// /// Creates mutex handle. CFastMutex(void); /// Destructor. /// /// Close mutex handle. No checks if it's still acquired. ~CFastMutex(void); /// Define Read Lock Guard. typedef CFastMutexGuard TReadLockGuard; /// Define Write Lock Guard. typedef CFastMutexGuard TWriteLockGuard; /// Acquire mutex for the current thread with no nesting checks. void Lock(void); /// Release mutex with no owner or nesting checks. void Unlock(void); /// Try locking the mutex. bool TryLock(void); /// Get SSystemFastMutex. operator SSystemFastMutex&(void);private:#if defined(NCBI_WIN32_THREADS) /// Get handle - Windows version. /// /// Also used by CRWLock. HANDLE GetHandle(void) { return m_Mutex.m_Handle; }#else /// Get handle - Unix version. /// /// Also used by CRWLock. TSystemMutex* GetHandle(void) { return &m_Mutex.m_Handle; }#endif friend class CRWLock; /// Platform-dependent mutex handle, also used by CRWLock. SSystemFastMutex m_Mutex; /// Private copy constructor to disallow initialization. CFastMutex(const CFastMutex&); /// Private assignment operator to disallow assignment. CFastMutex& operator= (const CFastMutex&);};/////////////////////////////////////////////////////////////////////////////////// CFastMutexGuard --////// For acquiring fast mutex, then guaranting its release.class CFastMutexGuard{public: /// Constructor. CFastMutexGuard(void); /// Constructor. /// /// Register the mutex "mtx", to be released by the guard destructor. CFastMutexGuard(SSystemFastMutex& mtx); /// Destructor. /// /// Release the mutex, if it was (and still is) successfully acquired. ~CFastMutexGuard(void); /// Release the mutex right now but do not release it in the guard /// destructor. void Release(void); /// Guard the specified mutex. /// /// Lock on mutex "mtx" (if it's not guarded yet) and start guarding it. /// NOTE: It never holds more than one lock on the guarded mutex! void Guard(SSystemFastMutex& mtx);private: SSystemFastMutex* m_Mutex; ///< The mutex - is NULL if released. /// Private copy constructor to disallow initialization. CFastMutexGuard(const CFastMutexGuard&); /// Private assignment operator to disallow assignment. CFastMutexGuard& operator= (const CFastMutexGuard&);};///////////////////////////////////////////////////////////////////////////////// MUTEX//// CMutex::// CMutexGuard::///////////////////////////////////////////////////////////////////////////////////// CMutex --////// Mutex that allows nesting with runtime checks.////// Allows for recursive locks by the same thread. Checks the mutex/// owner before unlocking. This mutex should be used when performance/// is less important than data protection. For faster performance see/// CFastMutex.////// @sa/// http://www.ncbi.nlm.nih.gov/books/bv.fcgi?call=bv.View..ShowSection&rid=toolkit.section.threads#CMutexclass CMutex{public: /// Constructor. CMutex(void); /// Destructor. /// /// Report error if the mutex is locked. ~CMutex(void); /// Define Read Lock Guard. typedef CMutexGuard TReadLockGuard; /// Define Write Lock Guard. typedef CMutexGuard TWriteLockGuard; /// Get SSystemMutex. operator SSystemMutex&(void); /// Lock mutex. /// /// Operation: /// - If the mutex is unlocked, then acquire it for the calling thread. /// - If the mutex is acquired by this thread, then increase the /// lock counter (each call to Lock() must have corresponding /// call to Unlock() in the same thread). /// - If the mutex is acquired by another thread, then wait until it's /// unlocked, then act like a Lock() on an unlocked mutex. void Lock(void); /// Try locking mutex. /// /// Try to acquire the mutex. /// @return /// - On success, return TRUE, and acquire the mutex just as the Lock() does. /// - If the mutex is already acquired by another thread, then return FALSE. /// @sa /// Lock() bool TryLock(void); /// Unlock mutex. /// /// Operation: /// - If the mutex is acquired by this thread, then decrease the lock counter. /// - If the lock counter becomes zero, then release the mutex completely. /// - Report error if the mutex is not locked or locked by another thread. void Unlock(void);private: SSystemMutex m_Mutex; ///< System mutex /// Private copy constructor to disallow initialization. CMutex(const CMutex&); /// Private assignment operator to disallow assignment. CMutex& operator= (const CMutex&); friend class CRWLock; ///< Allow use of m_Mtx and m_Owner members directly};/////////////////////////////////////////////////////////////////////////////////// CMutexGuard --////// Guarded mutex.////// Acquires the mutex and then gurantees its release.class CMutexGuard{public: /// Constructor. CMutexGuard(void); /// Constructor. /// /// Acquire the mutex "mtx"; register it to be released by the guard /// destructor. CMutexGuard(SSystemMutex& mtx); /// Destructor. /// /// Release the mutex, if it was (and still is) successfully acquired. ~CMutexGuard(void); /// Release mutex. /// /// Release the mutex right now -- do not release it in the guard /// destructor. void Release(void); /// Guard mutex. /// /// Lock on mutex "mtx" (if it's not guarded yet) and start guarding it. /// NOTE: it never holds more than one lock on the guarded mutex! void Guard(SSystemMutex& mtx);private: SSystemMutex* m_Mutex; ///< The mutex -- NULL if released. /// Private copy constructor to disallow initialization. CMutexGuard(const CMutexGuard&); /// Private assignment operator to disallow assignment. CMutexGuard& operator= (const CMutexGuard&);};///////////////////////////////////////////////////////////////////////////////// RW-LOCK//// CRWLock::
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -