summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/StaticMutex.cpp
blob: 3e326a8f1730c0d03702b9cef8519dabc00f0138 (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
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <IceUtil/StaticMutex.h>
#include <IceUtil/ThreadException.h>

#ifdef _WIN32
#   include <list>
#   include <algorithm>

using namespace std;

static CRITICAL_SECTION _criticalSection;
static list<CRITICAL_SECTION*>* _criticalSectionList; 

// 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);
    _criticalSectionList = new list<CRITICAL_SECTION*>;
}

Init::~Init()
{
    for(list<CRITICAL_SECTION*>::iterator p = _criticalSectionList->begin(); 
	p != _criticalSectionList->end(); ++p)
    {
	DeleteCriticalSection(*p);
	delete *p;
    }
  
    delete _criticalSectionList;
    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)
    {
        _mutex = new CRITICAL_SECTION;
	InitializeCriticalSection(_mutex);
	_mutexInitialized = true;
	_criticalSectionList->push_back(_mutex);
    }
    LeaveCriticalSection(&_criticalSection);
}

#endif

IceUtil::StaticMutex IceUtil::globalMutex = ICE_STATIC_MUTEX_INITIALIZER;