📄 sbtrdmutex.hpp
字号:
/* SBtrdMutex, classes for managing various types of mutexes */ /****************License************************************************ * * Copyright 2000-2003. ScanSoft, Inc. * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.2 which is * included with this software. * * ScanSoft is a registered trademark of ScanSoft, Inc., and OpenSpeech, * SpeechWorks and the SpeechWorks logo are registered trademarks or * trademarks of SpeechWorks International, Inc. in the United States * and other countries. * ***********************************************************************/ /* $Id: SBtrdMutex.hpp,v 7.2.2.3 2003/10/06 17:59:33 mpanacci Exp $ */ // -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8 #ifndef _SBTRD_MUTEX_H__ #define _SBTRD_MUTEX_H__ #include "VXIheaderPrefix.h" // For SYMBOL_EXPORT_CPP_DECL #include "VXItrd.h" // For return codes #include <cwchar> #ifdef SBTRDUTIL_EXPORTS #define SBTRDUTIL_API_CLASS SYMBOL_EXPORT_CPP_CLASS_DECL #else #define SBTRDUTIL_API_CLASS SYMBOL_IMPORT_CPP_CLASS_DECL #endif extern "C" struct VXIlogInterface; // Basic mutex class, simple wrapper around VXItrdMutex where each // mutex that gets created points at a different underlying mutex class SBTRDUTIL_API_CLASS SBtrdMutex { public: // Constructor and destructor SBtrdMutex ( ) : _name(0), _mutex(0) { } virtual ~SBtrdMutex( ); // Creation method, the name is simply for logging purposes virtual VXItrdResult Create (const wchar_t *name); // Provide access to the name for logging purposes const wchar_t *GetName( ) { return _name; } // Lock and unlock the mutex. Declared as const so that users can // lock/unlock for read access within const methods. virtual VXItrdResult Lock( ) const; virtual VXItrdResult Unlock( ) const; private: // Disable the copy constructor and equality operator to catch making // copies at compile or link time, neither is really implemented SBtrdMutex (const SBtrdMutex &); SBtrdMutex & operator= (const SBtrdMutex &); private: wchar_t *_name; VXItrdMutex *_mutex; }; // Reader/writer mutex class, mutex class that permits any number of // readers and no writers, or one writer with no readers, with a // strong writer preference to prevent starvation. This is implemented // using the classic algorithm, see // http://faculty.juniata.edu/rhodes/os/ch5d.htm class SBTRDUTIL_API_CLASS SBtrdReaderWriterMutex : public SBtrdMutex { public: // Constructor and destructor SBtrdReaderWriterMutex(VXIlogInterface *log = NULL, VXIunsigned diagTagBase = 0) : SBtrdMutex( ), _diagTagBase(diagTagBase), _log(log), _readerCount(0), _writerCount(0), _readerMutex( ), _writerMutex( ), _readerCountMutex( ), _writerCountMutex( ) { } virtual ~SBtrdReaderWriterMutex( ) { } // Creation method, the name is simply for logging purposes virtual VXItrdResult Create (const wchar_t *name); // Obtain/release write access virtual VXItrdResult Lock( ) const; virtual VXItrdResult Unlock( ) const; // Obtain/release read access. Declared as const so that users can // lock/unlock for read access within const methods. VXItrdResult StartRead( ) const; VXItrdResult EndRead( ) const; private: // Error logging void Error (VXIunsigned errorID, const VXIchar *format, ...) const; // Diagnostic logging void Diag (VXIunsigned tag, const VXIchar *subtag, const VXIchar *format, ...) const; // Disable the copy constructor and equality operator to catch making // copies at compile or link time, neither is really implemented SBtrdReaderWriterMutex (const SBtrdReaderWriterMutex &); SBtrdReaderWriterMutex & operator= (const SBtrdReaderWriterMutex &); private: // Internal class that provides a mutex which can be locked by one // thread, unlocked by another, and if locked by one thread and that // same thread comes back and locks it again, that thread will wait // (behave like any other thread that does a double lock). The // entire algorithm of the reader/writer mutex requires this // independance. class SBTRDUTIL_API_CLASS CrossThreadMutex : public SBtrdMutex { public: // Constructor and destructor CrossThreadMutex( ) : SBtrdMutex( ), _locked(false), _timer(NULL) {} virtual ~CrossThreadMutex( ) { if (_timer) VXItrdTimerDestroy (&_timer); } // Creation method, the name is simply for logging purposes virtual VXItrdResult Create (const wchar_t *name); // Obtain/release write access virtual VXItrdResult Lock( ) const; virtual VXItrdResult Unlock( ) const; private: volatile bool _locked; VXItrdTimer *_timer; }; private: VXIunsigned _diagTagBase; VXIlogInterface *_log; unsigned long _readerCount, _writerCount; CrossThreadMutex _readerMutex, _writerMutex; SBtrdMutex _readerCountMutex, _writerCountMutex; }; // Mutex pool mutex class, manages a mutex pool of a specified size // where you can then request mutexes out of the pool (allocated in a // round robin fashion) for shared use by multiple data instances. class SBTRDUTIL_API_CLASS SBtrdMutexPool { public: // Constructor and destructor SBtrdMutexPool( ) : _size(0), _curIndex(0), _pool(NULL) { } ~SBtrdMutexPool( ) { if ( _pool ) delete [] _pool; } // Creation method, the name is simply for logging purposes VXItrdResult Create (const wchar_t *name, unsigned int size = 50); // Obtain a mutex out of the pool, the mutex pool continues to own // this mutex so you must not destroy it, and this mutex may be // returned multiple times. This is thread safe. SBtrdMutex *GetMutex( ); private: // Disable the copy constructor and equality operator to catch making // copies at compile or link time, neither is really implemented SBtrdMutexPool (const SBtrdMutexPool &); SBtrdMutexPool & operator= (const SBtrdMutexPool &); private: unsigned int _size; unsigned int _curIndex; SBtrdMutex *_pool; }; // Reader/writer mutex pool class class SBTRDUTIL_API_CLASS SBtrdReaderWriterMutexPool { public: // Constructor and destructor SBtrdReaderWriterMutexPool( ) : _size(0), _curIndex(0), _allocationMutex(NULL), _pool(NULL) { } ~SBtrdReaderWriterMutexPool( ) { if ( _allocationMutex ) delete _allocationMutex; if ( _pool ) delete [] _pool; } // Creation method, the name is simply for logging purposes VXItrdResult Create (const wchar_t *name, unsigned int size = 50); // Obtain a mutex out of the pool, the mutex pool continues to own // this mutex so you must not destroy it, and this mutex may be // returned multiple times. This is thread safe. SBtrdReaderWriterMutex *GetMutex( ); private: // Disable the copy constructor and equality operator to catch making // copies at compile or link time, neither is really implemented SBtrdReaderWriterMutexPool (const SBtrdReaderWriterMutexPool &); SBtrdReaderWriterMutexPool & operator= (const SBtrdReaderWriterMutexPool &); private: unsigned int _size; unsigned int _curIndex; SBtrdMutex *_allocationMutex; SBtrdReaderWriterMutex *_pool; }; #include "VXIheaderSuffix.h" #endif // _SBTRD_MUTEX_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -