blob: 6206e245445c9812e848e0bef94a67bb82b94861 (
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
|
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. 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;
//
// Although apparently not documented by Microsoft, static objects are
// initialized before DllMain/DLL_PROCESS_ATTACH and finalized after
// DllMain/DLL_PROCESS_DETACH ... However, note that after the DLL is
// detached the allocated StaticMutexes may still be accessed. See
// http://blogs.msdn.com/larryosterman/archive/2004/06/10/152794.aspx
// for some details. This means that there is no convenient place to
// cleanup the globally allocated static mutexes.
//
namespace IceUtil
{
class Init
{
public:
Init();
};
static Init _init;
Init::Init()
{
InitializeCriticalSection(&_criticalSection);
}
}
void IceUtil::StaticMutex::initialize() const
{
//
// Yes, a double-check locking. It should be safe since we use memory barriers
// (through InterlockedCompareExchangePointer) in both reader and writer threads
//
EnterCriticalSection(&_criticalSection);
//
// The second check
//
if(_mutex == 0)
{
CRITICAL_SECTION* newMutex = new CRITICAL_SECTION;
InitializeCriticalSection(newMutex);
//
// _mutex is written after the new initialized CRITICAL_SECTION/Mutex
//
void* oldVal = InterlockedCompareExchangePointer(reinterpret_cast<void**>(&_mutex), newMutex, 0);
assert(oldVal == 0);
}
LeaveCriticalSection(&_criticalSection);
}
#endif
IceUtil::StaticMutex IceUtil::globalMutex = ICE_STATIC_MUTEX_INITIALIZER;
|