CPN
Computational Process Networks
Main Page
Namespaces
Classes
Files
File List
File Members
lib
threading
PthreadReadWriteLock.cc
Go to the documentation of this file.
1
//=============================================================================
2
// PthreadReadWriteLock and protection classes
3
//-----------------------------------------------------------------------------
4
// POSIX Pthread class library
5
// Copyright (C) 1997-1999 The University of Texas
6
//
7
// This library is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Library General Public License as published
9
// by the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Library General Public License for more details.
16
//
17
// The GNU Public License is available in the file LICENSE, or you
18
// can write to the Free Software Foundation, Inc., 59 Temple Place -
19
// Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
20
// World Wide Web at http://www.fsf.org.
21
//=============================================================================
22
23
24
#include <
cpn/threading/PthreadReadWriteLock.h
>
25
#ifdef _POSIX_THREADS
26
27
28
//-----------------------------------------------------------------------------
29
PthreadReadWriteLock::PthreadReadWriteLock
(
int
_priorityWrites)
30
//-----------------------------------------------------------------------------
31
: numReaders(0),
32
writing(0),
33
noNewReaders(0),
34
priorityWrites(_priorityWrites)
35
// mutex(), lockFree()
36
{
37
}
38
39
40
//-----------------------------------------------------------------------------
41
void
PthreadReadWriteLock::ReadLock
(
void
)
42
//-----------------------------------------------------------------------------
43
{
44
PthreadMutexProtected
protect(
mutex
);
45
while
(
noNewReaders
)
46
lockFree
.
Wait
(
mutex
);
47
numReaders
++;
48
}
49
50
51
//-----------------------------------------------------------------------------
52
void
PthreadReadWriteLock::WriteLock
(
void
)
53
//-----------------------------------------------------------------------------
54
{
55
PthreadMutexProtected
protect(
mutex
);
56
if
(
priorityWrites
)
57
noNewReaders
= 1;
58
while
(
writing
||
numReaders
)
59
lockFree
.
Wait
(
mutex
);
60
writing
++;
61
noNewReaders
= 1;
62
}
63
64
65
//-----------------------------------------------------------------------------
66
void
PthreadReadWriteLock::ReadUnlock
(
void
)
67
//-----------------------------------------------------------------------------
68
{
69
PthreadMutexProtected
protect(
mutex
);
70
71
if
(
numReaders
<=0) {
72
TrapError
(-1);
73
return
;
74
}
75
76
numReaders
--;
77
if
(!
numReaders
)
78
lockFree
.
Signal
();
79
}
80
81
82
//-----------------------------------------------------------------------------
83
void
PthreadReadWriteLock::WriteUnlock
(
void
)
84
//-----------------------------------------------------------------------------
85
{
86
PthreadMutexProtected
protect(
mutex
);
87
88
if
(!
writing
) {
89
TrapError
(-1);
90
return
;
91
}
92
93
writing
= 0;
94
noNewReaders
= 0;
95
lockFree
.
Broadcast
();
96
}
97
98
99
#endif
PthreadCondition::Wait
PthreadCondition & Wait(PthreadMutex &mutex)
Definition:
PthreadCondition.cc:84
PthreadCondition::Broadcast
PthreadCondition & Broadcast(void)
Definition:
PthreadCondition.cc:76
PthreadReadWriteLock::noNewReaders
int noNewReaders
Definition:
PthreadReadWriteLock.h:54
PthreadReadWriteLock::writing
int writing
Definition:
PthreadReadWriteLock.h:53
PthreadReadWriteLock::PthreadReadWriteLock
PthreadReadWriteLock(int _priorityWrites=1)
Definition:
PthreadReadWriteLock.cc:29
PthreadReadWriteLock::ReadLock
void ReadLock(void)
Definition:
PthreadReadWriteLock.cc:41
PthreadReadWriteLock::priorityWrites
int priorityWrites
Definition:
PthreadReadWriteLock.h:55
PthreadMutexProtected
Definition:
PthreadMutex.h:61
PthreadReadWriteLock::mutex
PthreadMutex mutex
Definition:
PthreadReadWriteLock.h:50
PthreadReadWriteLock::ReadUnlock
void ReadUnlock(void)
Definition:
PthreadReadWriteLock.cc:66
PthreadReadWriteLock::numReaders
int numReaders
Definition:
PthreadReadWriteLock.h:52
PthreadErrorHandler::TrapError
void TrapError(int result)
Definition:
PthreadErrorHandler.h:48
PthreadReadWriteLock::lockFree
PthreadCondition lockFree
Definition:
PthreadReadWriteLock.h:51
PthreadCondition::Signal
PthreadCondition & Signal(void)
Definition:
PthreadCondition.cc:68
PthreadReadWriteLock::WriteUnlock
void WriteUnlock(void)
Definition:
PthreadReadWriteLock.cc:83
PthreadReadWriteLock::WriteLock
void WriteLock(void)
Definition:
PthreadReadWriteLock.cc:52
PthreadReadWriteLock.h
Generated by
1.8.5