reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
RWMutex.cxx
Go to the documentation of this file.
1/* ====================================================================
2 * The Vovida Software License, Version 1.0
3 *
4 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
19 * and "Vovida Open Communication Application Library (VOCAL)" must
20 * not be used to endorse or promote products derived from this
21 * software without prior written permission. For written
22 * permission, please contact vocal@vovida.org.
23 *
24 * 4. Products derived from this software may not be called "VOCAL", nor
25 * may "VOCAL" appear in their name, without prior written
26 * permission of Vovida Networks, Inc.
27 *
28 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
31 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
32 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
33 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
37 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * DAMAGE.
41 *
42 * ====================================================================
43 *
44 * This software consists of voluntary contributions made by Vovida
45 * Networks, Inc. and many individuals on behalf of Vovida Networks,
46 * Inc. For more information on Vovida Networks, Inc., please see
47 * <http://www.vovida.org/>.
48 *
49 */
50
51#include "RWMutex.hxx"
52#include "Lock.hxx"
53#include "rutil/ResipAssert.h"
54
55using resip::RWMutex;
56using resip::Lock;
57
59 : mReaderCount(0),
60 mWriterHasLock(false),
61 mPendingWriterCount(0)
62{
63}
64
65
69
70
71void
73{
74 Lock lock(mMutex);
75
76 while ( mWriterHasLock || mPendingWriterCount > 0 )
77 {
78 mReadCondition.wait(lock);
79 }
80
81 mReaderCount++;
82}
83
84
85void
87{
88 Lock lock(mMutex);
89
90 mPendingWriterCount++;
91
92 while ( mWriterHasLock || mReaderCount > 0 )
93 {
94 mPendingWriteCondition.wait(lock);
95 }
96
97 mPendingWriterCount--;
98
99 mWriterHasLock = true;
100}
101
102
103void
105{
106 writelock();
107}
108
109
110void
112{
113 Lock lock(mMutex);
114
115 // Unlocking a write lock.
116 //
117 if ( mWriterHasLock )
118 {
119 resip_assert( mReaderCount == 0 );
120
121 mWriterHasLock = false;
122
123 // Pending writers have priority. Could potentially starve readers.
124 //
125 if ( mPendingWriterCount > 0 )
126 {
127 mPendingWriteCondition.notify_one();
128 }
129
130 // No writer, no pending writers, so all the readers can go.
131 //
132 else
133 {
134 mReadCondition.notify_all();
135 }
136
137 }
138
139 // Unlocking a read lock.
140 //
141 else
142 {
143 resip_assert( mReaderCount > 0 );
144
145 mReaderCount--;
146
147 if ( mReaderCount == 0 && mPendingWriterCount > 0 )
148 {
149 mPendingWriteCondition.notify_one();
150 }
151 }
152}
153
154unsigned int
156{
157 return ( mReaderCount );
158}
159
160unsigned int
162{
163 return ( mPendingWriterCount );
164}
#define resip_assert(x)
This file needs to work for both C and C++ code.
Wraps the readers/writers mutex implementation on your platform.
Definition RWMutex.hxx:29
void writelock()
Definition RWMutex.cxx:86
unsigned int pendingWriterCount() const
Definition RWMutex.cxx:161
void readlock()
Definition RWMutex.cxx:72
unsigned int readerCount() const
Definition RWMutex.cxx:155
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28
void list()
Definition userAdmin.cxx:66