CPN
Computational Process Networks
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 
25 #ifdef _POSIX_THREADS
26 
27 
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
31 : numReaders(0),
32  writing(0),
33  noNewReaders(0),
34  priorityWrites(_priorityWrites)
35 // mutex(), lockFree()
36 {
37 }
38 
39 
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 {
45  while (noNewReaders)
47  numReaders++;
48 }
49 
50 
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 {
56  if (priorityWrites)
57  noNewReaders = 1;
58  while (writing || numReaders)
60  writing++;
61  noNewReaders = 1;
62 }
63 
64 
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 {
70 
71  if (numReaders<=0) {
72  TrapError(-1);
73  return;
74  }
75 
76  numReaders--;
77  if (!numReaders)
78  lockFree.Signal();
79 }
80 
81 
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 {
87 
88  if (!writing) {
89  TrapError(-1);
90  return;
91  }
92 
93  writing = 0;
94  noNewReaders = 0;
96 }
97 
98 
99 #endif
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadCondition & Broadcast(void)
PthreadReadWriteLock(int _priorityWrites=1)
void TrapError(int result)
PthreadCondition lockFree
PthreadCondition & Signal(void)