summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/StaticMutex.cpp
blob: a40804e78a54b291307abf222d34ca0f1ec5f617 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// **********************************************************************
//
// Copyright (c) 2003-2011 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.
//
// **********************************************************************

//
// We disable deprecation warning here, to allow clean compilation of
// of deprecated methods.
//
#ifdef _MSC_VER
#   pragma warning( disable : 4996 )
#endif

#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);
}

bool 
IceUtil::StaticMutex::initialized() const
{
    //
    // Read mutex and then inserts a memory barrier to ensure we can't 
    // see tmp != 0 before we see the initialized object
    //
    void* tmp = _mutex;
    return InterlockedCompareExchangePointer(reinterpret_cast<void**>(&tmp), 0, 0) != 0;
}

void
IceUtil::StaticMutex::lock() const
{
    if(!initialized())
    {
        initialize();
    }
    EnterCriticalSection(_mutex);
    assert(_mutex->RecursionCount == 1);
}

bool
IceUtil::StaticMutex::tryLock() const
{
    if(!initialized())
    {
        initialize();
    }
    if(!TryEnterCriticalSection(_mutex))
    {
        return false;
    }
    if(_mutex->RecursionCount > 1)
    {
        LeaveCriticalSection(_mutex);
        throw ThreadLockedException(__FILE__, __LINE__);
    }
    return true;
}

void
IceUtil::StaticMutex::unlock() const
{
    assert(_mutex != 0);
    assert(_mutex->RecursionCount == 1);
    LeaveCriticalSection(_mutex);
}

void
IceUtil::StaticMutex::unlock(LockState&) const
{
    assert(_mutex != 0);
    assert(_mutex->RecursionCount == 1);
    LeaveCriticalSection(_mutex);
}

void
IceUtil::StaticMutex::lock(LockState&) const
{
    if(!initialized())
    {
        initialize();
    }
    EnterCriticalSection(_mutex);
}

#else

void
IceUtil::StaticMutex::lock() const
{
    int rc = pthread_mutex_lock(&_mutex);
    if(rc != 0)
    {
        if(rc == EDEADLK)
        {
            throw ThreadLockedException(__FILE__, __LINE__);
        }
        else
        {
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
        }
    }
}

bool
IceUtil::StaticMutex::tryLock() const
{
    int rc = pthread_mutex_trylock(&_mutex);
    if(rc != 0 && rc != EBUSY)
    {
        if(rc == EDEADLK)
        {
            throw ThreadLockedException(__FILE__, __LINE__);
        }
        else
        {
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
        }
    }
    return (rc == 0);
}

void
IceUtil::StaticMutex::unlock() const
{
    int rc = pthread_mutex_unlock(&_mutex);
    if(rc != 0)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
}

void
IceUtil::StaticMutex::unlock(LockState& state) const
{
    state.mutex = &_mutex;
}

void
IceUtil::StaticMutex::lock(LockState&) const
{
}

#endif