summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/RWRecMutex.cpp
blob: 0137ca79668161b836cd785bdbfc616b6755700a (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
// **********************************************************************
//
// Copyright (c) 2001
// IONA Technologies, Inc.
// Waltham, MA, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <IceUtil/RWRecMutex.h>
#include <IceUtil/Exception.h>

#include <assert.h>

IceUtil::RWRecMutex::RWRecMutex() :
    _count(0),
    _waitingWriters(0)
{
}

IceUtil::RWRecMutex::~RWRecMutex()
{
}

void
IceUtil::RWRecMutex::readLock()
{
    Mutex::Lock lock(_mutex);

    //
    // Wait while a writer holds the lock or while writers are waiting
    // to get the lock.
    //
    while (_count < 0 || _waitingWriters != 0)
    {
	_readers.wait(lock);
    }
    _count++;
}

void
IceUtil::RWRecMutex::tryReadLock()
{
    Mutex::Lock lock(_mutex);

    //
    // Would block if a writer holds the lock or if writers are
    // waiting to get the lock.
    //
    if (_count < 0 || _waitingWriters != 0)
    {
	throw LockedException(__FILE__, __LINE__);
    }
    _count++;
}

void
IceUtil::RWRecMutex::writeLock()
{
    Mutex::Lock lock(_mutex);

    //
    // Wait for the lock to become available and increment the number
    // of waiting writers.
    //
    while (_count != 0)
    {
	_waitingWriters++;
	try
	{
	    _writers.wait(lock);
	}
	catch(...)
	{
	    --_waitingWriters;
	    throw;
	}
	_waitingWriters--;
    }

    //
    // Got the lock, indicate it's held by a writer.
    //
    _count = -1;
}

void
IceUtil::RWRecMutex::tryWriteLock()
{
    Mutex::Lock lock(_mutex);

    //
    // If there are readers or other writers then the call would block.
    //
    if (_count != 0)
    {
	throw LockedException(__FILE__, __LINE__);
    }

    //
    // Got the lock, indicate it's held by a writer.
    //
    _count = -1;
}

void
IceUtil::RWRecMutex::unlock()
{
    bool ww;
    bool wr;
    {
	Mutex::Lock lock(_mutex);

	assert(_count != 0);

	//
	// If _count < 0, the calling thread is a writer that holds the
	// lock, so release the lock.  Otherwise, _count is guaranteed to
	// be > 0, so the calling thread is a reader releasing the lock.
	//
	if (_count < 0)
	{
	    //
	    // Writer called unlock
	    //
	    _count = 0;
	}
	else
	{
	    //
	    // Reader called unlock
	    //
	    --_count;
	}

	//
	// Writers are waiting (ww) if _waitingWriters > 0.  In that
	// case, it's OK to let another writer into the region once there
	// are no more readers (_count == 0).  Otherwise, no writers are
	// waiting but readers may be waiting (wr).
	//
	ww = (_waitingWriters != 0 && _count == 0);
	wr = (_waitingWriters == 0);
    }

    //
    // Wake up a waiting writer if there is one. If not, wake up all
    // readers (just in case -- there may be none).
    //
    if (ww)
    {
	//
	// Wake writer
	//
	_writers.signal();
    }
    else if (wr)
    {
	//
	// Wake readers
	//
	_readers.broadcast();
    }
}