summaryrefslogtreecommitdiff
path: root/cppe/src/IceE/StaticMutex.cpp
blob: 334ea1ae99d791b891cb72391895308fc4405971 (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
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceE/StaticMutex.h>

#ifdef _WIN32
void IceUtil::StaticMutex::initialize() const
{
    //
    // First we create the new critical section
    //
    CRITICAL_SECTION* newCriticalSection = new CRITICAL_SECTION;
    InitializeCriticalSection(newCriticalSection);
	    	
    //
    // Then assign it to _mutex
    // Note that Windows performs a full memory barrier before the assignment;
    // this ensures we write the initialized critical section before we write _mutex.
    //
    if(InterlockedCompareExchangePointer(reinterpret_cast<void**>(&_mutex), newCriticalSection, 0) != 0)
    {
	//
	// Another thread was doing the same thing
	//
	DeleteCriticalSection(newCriticalSection);
	delete newCriticalSection;
    }

    //
    // This implementation does not attempt to clean up the initialized and assigned critical sections:
    // they leak
    //
}
#endif

IceUtil::StaticMutex IceUtil::globalMutex = ICE_STATIC_MUTEX_INITIALIZER;