1 | #include "mars_debug.h"
|
---|
2 |
|
---|
3 | #include <assert.h>
|
---|
4 |
|
---|
5 | namespace mars
|
---|
6 | {
|
---|
7 | ThreadingModelManager gtmm_ThreadingModelManager;
|
---|
8 |
|
---|
9 | ThreadingModelMonitor::ThreadingModelMonitor( const char * szFuncName, const void * oInst, const ThreadingModel entmThreadingModel )
|
---|
10 | : _entmThreadingModel(entmThreadingModel), _idThread(boost::this_thread::get_id()), _szFuncName(szFuncName), _pInst(oInst)
|
---|
11 | {
|
---|
12 | if (_entmThreadingModel == ThrMdl_Main)
|
---|
13 | assert(gtmm_ThreadingModelManager.checkMainThread(szFuncName) && "Only the MAIN thread may enter this block");
|
---|
14 | else
|
---|
15 | {
|
---|
16 | const size_t nRefCount = gtmm_ThreadingModelManager.registerThread(szFuncName, oInst);
|
---|
17 | if (nRefCount > 1 && _entmThreadingModel == ThrMdl_Single)
|
---|
18 | assert(_entmThreadingModel == ThrMdl_Any && "Threading model is SINGLE but this method is not synchronized");
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | ThreadingModelMonitor::~ThreadingModelMonitor()
|
---|
23 | {
|
---|
24 | if (_entmThreadingModel != ThrMdl_Main)
|
---|
25 | {
|
---|
26 | const size_t nRefCount = gtmm_ThreadingModelManager.deregisterThread(_szFuncName, _pInst);
|
---|
27 | assert(nRefCount == 0 || _entmThreadingModel == ThrMdl_Any && "Threading model is SINGLE or MAIN but this method is not synchronized");
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | void ThreadingModelManager::registerMainThread()
|
---|
32 | {
|
---|
33 | { boost::mutex::scoped_lock lock(_mtxMutex);
|
---|
34 | assert(!_bMainThreadSet && "Main thread already registered");
|
---|
35 | _idMainThread = boost::this_thread::get_id();
|
---|
36 | _bMainThreadSet = true;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | bool ThreadingModelManager::checkMainThread( const char * szFuncName )
|
---|
41 | {
|
---|
42 | { boost::mutex::scoped_lock lock(_mtxMutex);
|
---|
43 | return _bMainThreadSet && boost::this_thread::get_id() == _idMainThread && _mapThreadTracker.find(szFuncName) == _mapThreadTracker.end();
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | size_t ThreadingModelManager::registerThread (const char * szFuncName, const void * pThis)
|
---|
48 | {
|
---|
49 | { boost::mutex::scoped_lock lock(_mtxMutex);
|
---|
50 | ThreadConcurrencyMap::iterator i = _mapThreadTracker.find(szFuncName);
|
---|
51 |
|
---|
52 | if (i == _mapThreadTracker.end())
|
---|
53 | i = _mapThreadTracker.insert(_mapThreadTracker.begin(), std::pair< std::string, ThreadIDSet > (szFuncName, ThreadIDSet()));
|
---|
54 |
|
---|
55 | i->second.insert(boost::this_thread::get_id());
|
---|
56 |
|
---|
57 | std::pair< const char *, const void * > hCallHandle(szFuncName, pThis);
|
---|
58 | ThreadReferenceCountMap::iterator j = _mapRefCounter.find(hCallHandle);
|
---|
59 |
|
---|
60 | if (j == _mapRefCounter.end())
|
---|
61 | return _mapRefCounter[hCallHandle] = 1;
|
---|
62 | else
|
---|
63 | {
|
---|
64 | return ++j->second;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | size_t ThreadingModelManager::deregisterThread(const char * szFuncName, const void * pThis)
|
---|
69 | {
|
---|
70 | { boost::mutex::scoped_lock lock(_mtxMutex);
|
---|
71 | std::pair< const char *, const void * > hCallHandle(szFuncName, pThis);
|
---|
72 | ThreadReferenceCountMap::iterator j = _mapRefCounter.find(hCallHandle);
|
---|
73 |
|
---|
74 | if (j == _mapRefCounter.end())
|
---|
75 | assert(j != _mapRefCounter.end() && "Impossible, no reference encountered in DTOR");
|
---|
76 | else
|
---|
77 | {
|
---|
78 | const size_t nRef = --j->second;
|
---|
79 | if (nRef == 0)
|
---|
80 | _mapRefCounter.erase(j);
|
---|
81 | return nRef;
|
---|
82 | }
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | ThreadingModelManager::ThreadingModelManager()
|
---|
88 | : _bMainThreadSet(false) {}
|
---|
89 | } |
---|