root/juggler/tags/1.0.5/Sync/vjRWMutex.cpp

Revision 2828, 4.5 kB (checked in by patrickh, 8 years ago)

Updated the copyright to what ISU's lawyers decided they want now.
The vast majority of this was done using Kevin's auto-copyright.pl script
which definitely made this easier. All the copyright blocks now have
begin and end tags so that if and when we have to update the copyright
information again, it will be even simpler.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33
34 #include <vjConfig.h>
35
36 #include <Sync/vjRWMutex.h>
37
38
39 //----------------------------------------------------------
40 // Acquire a read mutex.
41 //----------------------------------------------------------
42 int
43 vjRWMutex::acquireRead () {
44     int retVal = 0;
45
46     if (stateLock.acquire() == -1)
47         retVal = -1;                    // Didn't get the lock
48     else
49     {
50         // let those writers who are waiting have first shot
51         while(refCount < 0 || numWaitingWriters > 0)
52         {
53             numWaitingReaders++;            // Another one waiting
54             waitingReaders.wait();          // So wait until something changes
55             numWaitingReaders--;            //
56         }
57     }
58
59     if (retVal == 0) {
60         refCount++;
61         stateLock.release();
62     }
63
64     return retVal;
65 }
66
67 //----------------------------------------------------------
68 // Acquire a write mutex.
69 //----------------------------------------------------------
70 int
71 vjRWMutex::acquireWrite() {
72     int retVal = 0;
73
74     if (stateLock.acquire() == -1)
75         retVal = -1;                    // Didn't get the lock
76     else
77     {
78         while(refCount != 0)        // While there are readers
79         {
80             numWaitingWriters++;        // One more waiting
81             waitingWriters.wait();      // Wait for soemthing to change
82             numWaitingWriters--;        // Not waiting any more
83         }
84     }
85
86     if(retVal == 0)
87     {
88         refCount = -1;          // Tell everyone that there is a writer
89         stateLock.release();
90     }
91
92     return retVal;
93 }
94
95 //----------------------------------------------------------
96 // Try to acquire a read mutex.
97 //----------------------------------------------------------
98 int
99 vjRWMutex::tryAcquireRead () {
100     int retVal = -1;
101
102     if (stateLock.acquire() != -1)
103     {
104         if(refCount == -1 || numWaitingWriters >0)
105             retVal = -1;
106         else
107         {
108             refCount++;
109             retVal = 0;
110         }
111         stateLock.release();
112     }
113     return retVal;
114 }
115
116 //----------------------------------------------------------
117 // Try to acquire a write mutex.
118 //----------------------------------------------------------
119 int
120 vjRWMutex::tryAcquireWrite () {
121     int retVal = -1;
122
123     if (stateLock.acquire() != -1)
124     {
125         if(refCount != 0)
126             retVal = -1;
127         else
128         {
129             refCount = -1;
130             retVal = 0;
131         }
132         stateLock.release();
133     }
134
135     return retVal;
136 }
137
138 //---------------------------------------------------------
139 // Release the mutex.
140 //
141 // RETURNS:  0 - Success
142 // RETURNS: -1 - Error
143 //---------------------------------------------------------
144 int
145 vjRWMutex::release () {
146     int retVal = 0;
147
148     if (stateLock.acquire() == -1)
149         return -1;
150
151     if(refCount > 0)        // We have a reader to release
152         refCount--;
153     else if (refCount == -1)    // We have writer
154         refCount = 0;
155     else                        // We have an error
156         std::cerr << "vjRWMutex::release: Should not have refCount of 0!!!"
157                   << std::endl;
158
159     // Preference to writers
160     if (numWaitingWriters > 0)
161     {
162         retVal = waitingWriters.signal();
163     }
164     else if (numWaitingReaders > 0)
165     {
166         retVal = waitingReaders.broadcast();
167     }
168     else
169         retVal = 0;
170
171     stateLock.release();
172
173     return retVal;     
174 }
Note: See TracBrowser for help on using the browser.