📄 ncbimtx.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbimtx.hpp,v $ * PRODUCTION Revision 1000.2 2004/04/21 14:34:52 gouriano * PRODUCTION PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R1.35 * PRODUCTION * =========================================================================== */#ifndef CORELIB___NCBIMTX__HPP#define CORELIB___NCBIMTX__HPP/* $Id: ncbimtx.hpp,v 1000.2 2004/04/21 14:34:52 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Aleksey Grichenko * * *//// @file ncbimtx.hpp/// Multi-threading -- mutexes; rw-locks; semaphore////// MUTEX:////// MUTEX CLASSES:/// - SSystemFastMutex -- platform-dependent mutex functionality/// - SSystemMutex -- platform-dependent mutex functionality/// - CFastMutex -- simple mutex with fast lock/unlock functions/// - CMutex -- mutex that allows nesting (with runtime checks)/// - CFastMutexGuard -- acquire fast mutex, then guarantee for its release/// - CMutexGuard -- acquire mutex, then guarantee for its release////// RW-LOCK:/// - CInternalRWLock -- platform-dependent RW-lock structure (fwd-decl)/// - CRWLock -- Read/Write lock related data and methods/// - CAutoRW -- guarantee RW-lock release/// - CReadLockGuard -- acquire R-lock, then guarantee for its release/// - CWriteLockGuard -- acquire W-lock, then guarantee for its release////// SEMAPHORE:/// - CSemaphore -- application-wide semaphore///#include <corelib/ncbithr_conf.hpp>#include <corelib/ncbicntr.hpp>#include <vector>#include <memory>/** @addtogroup Threads * * @{ */#if defined(_DEBUG)/// Mutex debug setting.# define INTERNAL_MUTEX_DEBUG#else# undef INTERNAL_MUTEX_DEBUG/// Mutex debug setting.# define INTERNAL_MUTEX_DEBUG#endifBEGIN_NCBI_SCOPE///////////////////////////////////////////////////////////////////////////////// DECLARATIONS of internal (platform-dependent) representations//// TMutex -- internal mutex type//#if defined(NCBI_NO_THREADS)/// Define a platform independent system mutex.typedef int TSystemMutex; // fake# define SYSTEM_MUTEX_INITIALIZER 0#elif defined(NCBI_POSIX_THREADS)/// Define a platform independent system mutex.typedef pthread_mutex_t TSystemMutex;# define SYSTEM_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER#elif defined(NCBI_WIN32_THREADS)/// Define a platform independent system mutex.typedef HANDLE TSystemMutex;# undef SYSTEM_MUTEX_INITIALIZER#else# error Unknown threads type#endif/////////////////////////////////////////////////////////////////////////////////// CThreadSystemID --////// Define thread system ID.////// The CThreadSystemID is based on the platform dependent thread ID type,/// TThreadSystemID, defined in ncbithr_conf.hpp.class NCBI_XNCBI_EXPORT CThreadSystemID{public: /// Define a simpler alias for TThreadSystemID. typedef TThreadSystemID TID; TID m_ID; ///< Thread ID. /// Get the current thread ID. static CThreadSystemID GetCurrent(void) { CThreadSystemID id;#if defined(NCBI_NO_THREADS) id.m_ID = TID(0);#elif defined(NCBI_POSIX_THREADS) id.m_ID = pthread_self();#elif defined(NCBI_WIN32_THREADS) id.m_ID = GetCurrentThreadId();#endif return id; } /// Equality operator for thread ID. bool operator==(const CThreadSystemID& id) const { return m_ID == id.m_ID; } /// Non-equality operator for thread ID. bool operator!=(const CThreadSystemID& id) const { return m_ID != id.m_ID; } /// volatile versions of above methods void Set(const CThreadSystemID& id) volatile { m_ID = id.m_ID; } bool Is(const CThreadSystemID& id) const volatile { return m_ID == id.m_ID; } bool IsNot(const CThreadSystemID& id) const volatile { return m_ID != id.m_ID; }};/// Use in defining initial value of system mutex.#define THREAD_SYSTEM_ID_INITIALIZER { 0 }/////////////////////////////////////////////////////////////////////////////////// CMutexException --////// Define exceptions generated by mutexes.////// CMutexException inherits its basic functionality from CCoreException/// and defines additional error codes for applications.class NCBI_XNCBI_EXPORT CMutexException : public CCoreException{public: /// Error types that a mutex can generate. enum EErrCode { eLock, ///< Lock error eUnlock, ///< Unlock error eTryLock, ///< Attempted lock error eOwner, ///< Not owned error eUninitialized ///< Uninitialized error }; /// Translate from the error code value to its string representation. virtual const char* GetErrCodeString(void) const; // Standard exception boilerplate code. NCBI_EXCEPTION_DEFAULT(CMutexException,CCoreException);};///////////////////////////////////////////////////////////////////////////////// SYSTEM MUTEX//// SSystemFastMutex// SSystemMutex//class CFastMutex;class CFastMutexGuard;/////////////////////////////////////////////////////////////////////////////////// SSystemFastMutex --////// Define system fast mutex.////// Internal platform-dependent fast mutex implementation to be used by CMutex/// and CFastMutex only.struct SSystemFastMutex{ TSystemMutex m_Handle; ///< Mutex handle /// Initialization flag values. enum EMagic { eMutexUninitialized = 0, ///< Uninitialized value. eMutexInitialized = 0x2487adab ///< Magic initialized value, }; volatile EMagic m_Magic; ///< Magic flag /// 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 to lock. /// /// @return /// TRUE on success; FALSE, otherwise. bool TryLock(void); /// Check initialized value of mutex. void CheckInitialized(void) const; // Methods for throwing exceptions, to make inlined methods lighter /// Throw uninitialized ("eUninitialized") exception. NCBI_XNCBI_EXPORT static void ThrowUninitialized(void); /// Throw lock failed("eLocked") exception. NCBI_XNCBI_EXPORT static void ThrowLockFailed(void); /// Throw unlock failed("eUnlocked") exception. NCBI_XNCBI_EXPORT static void ThrowUnlockFailed(void); /// Throw try lock failed("eTryLock") exception. NCBI_XNCBI_EXPORT static void ThrowTryLockFailed(void);#if !defined(NCBI_OS_MSWIN) // MS VC 6 treats classes with any non-public member as non-POD.protected:#endif /// Check if mutex is initialized. /// /// @return /// TRUE if initialized; FALSE, otherwise. bool IsInitialized(void) const; /// Check if mutex is un-initialized. /// /// @return /// TRUE if un-initialized; FALSE, otherwise. bool IsUninitialized(void) const; /// Initialize static mutex. /// /// Must be called only once. NCBI_XNCBI_EXPORT void InitializeStatic(void); /// Initialize dynamic mutex. /// /// Initialize mutex if it located in heap or stack. This must be called /// only once. Do not count on zeroed memory values for initializing /// mutex values. NCBI_XNCBI_EXPORT void InitializeDynamic(void); /// Destroy mutex. NCBI_XNCBI_EXPORT void Destroy(void); /// Initialize mutex handle. /// /// Must be called only once. NCBI_XNCBI_EXPORT void InitializeHandle(void); /// Destroy mutex handle. /// /// Must be called only once. NCBI_XNCBI_EXPORT void DestroyHandle(void); friend struct SSystemMutex; friend class CAutoInitializeStaticFastMutex; friend class CFastMutex; friend class CFastMutexGuard; friend class CSafeStaticPtr_Base;};class CMutex;class CMutexGuard;/////////////////////////////////////////////////////////////////////////////////// SSystemMutex --////// Define system mutex.////// Internal platform-dependent mutex implementation to be used by CMutex/// and CFastMutex only.struct SSystemMutex{ SSystemFastMutex m_Mutex; ///< Mutex value volatile CThreadSystemID m_Owner; ///< Platform-dependent owner thread ID volatile int m_Count; ///< # of recursive (in the same thread) locks /// Acquire mutex for the current thread. NCBI_XNCBI_EXPORT void Lock(void); /// Release mutex. NCBI_XNCBI_EXPORT void Unlock(void); /// Try to lock. /// /// @return /// TRUE on success; FALSE, otherwise. NCBI_XNCBI_EXPORT bool TryLock(void); // Methods for throwing exceptions, to make inlined methods lighter // throw exception eOwner /// Throw not owned("eOwner") exception. NCBI_XNCBI_EXPORT static void ThrowNotOwned(void);#if !defined(NCBI_OS_MSWIN)protected:#endif /// Check if mutex is initialized. /// /// @return /// TRUE if initialized; FALSE, otherwise. bool IsInitialized(void) const; /// Check if mutex is un-initialized. /// /// @return /// TRUE if un-initialized; FALSE, otherwise. bool IsUninitialized(void) const; /// Initialize static mutex. /// /// Must be called only once. void InitializeStatic(void); /// Initialize dynamic mutex. /// /// Initialize mutex if it located in heap or stack. This must be called /// only once. Do not count on zeroed memory values for initializing /// mutex values. void InitializeDynamic(void); /// Destroy mutex. NCBI_XNCBI_EXPORT void Destroy(void); friend class CAutoInitializeStaticMutex; friend class CMutex; friend class CMutexGuard;};/// Determine type of system mutex initialization.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -