blob: 00af53e7498f6d37660c9697221f785606b874a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// **********************************************************************
//
// Copyright (c) 2003 - 2004
// ZeroC, Inc.
// North Palm Beach, FL, USA
//
// All Rights Reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/StaticMutex.h>
#include <IceUtil/ThreadException.h>
#ifdef _WIN32
# include <list>
# include <algorithm>
using namespace std;
static CRITICAL_SECTION _criticalSection;
# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
typedef list<CRITICAL_SECTION*> MutexList;
# else
typedef list<HANDLE> MutexList;
# endif
static MutexList* _mutexList;
//
// Although apparently not documented by Microsoft, static objects are
// initialized before DllMain/DLL_PROCESS_ATTACH and finalized after
// DllMain/DLL_PROCESS_DETACH ... that's why we use a static object.
//
namespace IceUtil
{
class Init
{
public:
Init();
~Init();
};
static Init _init;
Init::Init()
{
InitializeCriticalSection(&_criticalSection);
_mutexList = new MutexList;
}
Init::~Init()
{
# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
for(MutexList::iterator p = _mutexList->begin();
p != _mutexList->end(); ++p)
{
DeleteCriticalSection(*p);
delete *p;
}
# else
for_each(_mutexList->begin(), _mutexList->end(),
CloseHandle);
# endif
delete _mutexList;
DeleteCriticalSection(&_criticalSection);
}
}
//
// For full thread-safety, we assume that _mutexInitialized cannot be seen as true
// before CRITICAL_SECTION has been updated. This is true on x86. Does IA64
// provide the same memory ordering guarantees?
//
void IceUtil::StaticMutex::initialize() const
{
EnterCriticalSection(&_criticalSection);
if(!_mutexInitialized)
{
# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
_mutex = new CRITICAL_SECTION;
InitializeCriticalSection(_mutex);
_mutexList->push_back(_mutex);
# else
_recursionCount = 0;
_mutex = CreateMutex(0, false, 0);
if(_mutex == 0)
{
throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
_mutexList->push_back(_mutex);
# endif
_mutexInitialized = true;
}
LeaveCriticalSection(&_criticalSection);
}
#endif
IceUtil::StaticMutex IceUtil::globalMutex = ICE_STATIC_MUTEX_INITIALIZER;
|