root/juggler/tags/1.0.5/Sync/vjMutexPosix.h

Revision 7539, 9.7 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • 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 /*
35  * --------------------------------------------------------------------------
36  * Author:
37  *   Patrick Hartling (based on vjMutexSGI by Allen Bierbaum).
38  * --------------------------------------------------------------------------
39  * NOTES:
40  *    - This file (vjMutexPosix.h) must be included by vjMutex.h, not the
41  *      other way around.
42  *    - The following libraries must be linked in at compile time:
43  *         HP-UX 10.20 --> -lcma
44  *         IRIX 6.x    --> -lpthread
45  * --------------------------------------------------------------------------
46  */
47
48 #ifndef _VJ_MUTEX_POSIX_H_
49 #define _VJ_MUTEX_POSIX_H_
50
51 #include <vjConfig.h>
52 #include <stdio.h>
53 #include <pthread.h>
54 #include <errno.h>
55
56
57 //: Mutex wrapper for POSIX-compliant systems using pthreads mutex variables
58 //+ for the implementation.
59
60 //!PUBLIC_API:
61 class vjMutexPosix {
62 public:
63     // -----------------------------------------------------------------------
64     //: Constructor for vjMutexPosix class.
65     //
66     //! PRE: None.
67     //! POST: The mutex variable is initialized for use.  It must be
68     //+       initialized before any other member functions can do anything
69     //+       with it.
70     // -----------------------------------------------------------------------
71     vjMutexPosix (void) {
72         // Initialize the mutex.
73 #ifdef _PTHREADS_DRAFT_4
74         pthread_mutex_init(&mMutex, pthread_mutexattr_default);
75 #else
76         pthread_mutex_init(&mMutex, NULL);
77 #endif
78     }
79
80     // -----------------------------------------------------------------------
81     //: Destructor for vjMutexPosix class.
82     //
83     //! PRE: The mutex variable should be unlocked before being destroyed,
84     //+      but if it is not, this routine will unlock it and then destroy
85     //+      it.
86     //! POST: The mutex variable is destroyed and unlocked if necessary.
87     // -----------------------------------------------------------------------
88     ~vjMutexPosix (void) {
89         // Destroy the mutex.
90         if ( pthread_mutex_destroy(&mMutex) == -1 ) {
91             pthread_mutex_unlock(&mMutex);
92             pthread_mutex_destroy(&mMutex);
93         }
94     }
95
96     // -----------------------------------------------------------------------
97     //: Lock the mutex.
98     //
99     //! PRE: None.
100     //! POST: A lock on the mutex variable is acquired by the caller.  If a
101     //+       lock has already been acquired by another process/thread, the
102     //+       caller blocks until the mutex has been freed.
103     //
104     //! RETURNS:  1 - Lock acquired
105     //! RETURNS: -1 - Error
106     // -----------------------------------------------------------------------
107     inline int
108     acquire (void) {
109         int retval;
110
111         retval = pthread_mutex_lock(&mMutex);
112
113         // Locking succeeded.
114         if ( retval == 0 ) {
115             return 1;
116         }
117 #ifdef _DEBUG
118         // This thread tried to lock the mutex twice and a deadlock condition
119         // was reported.
120         else if ( retval == EDEADLK ) {
121             perror("Tried to lock mutex twice (vjMutexPosix.h:111)");
122
123             return -1;
124         }
125 #endif
126         // Some other error occurred.
127         else {
128             return -1;
129         }
130     }
131
132     // -----------------------------------------------------------------------
133     //: Acquire a read mutex lock.
134     //
135     //! PRE: None.
136     //! POST: A lock on the mutex variable is acquired by the caller.  If a
137     //+       lock has already been acquired by another process/thread, the
138     //+       caller blocks until the mutex has been freed.
139     //
140     //! RETURNS:  1 - Lock acquired
141     //! RETURNS: -1 - Error
142     //
143     //! NOTE: No special read mutex has been defined for now.
144     // -----------------------------------------------------------------------
145     inline int
146     acquireRead (void) {
147         return this->acquire();
148     }
149
150     // -----------------------------------------------------------------------
151     //: Acquire a write mutex lock.
152     //
153     //! PRE: None.
154     //! POST: A lock on the mutex variable is acquired by the caller.  If a
155     //+       lock has already been acquired by another process/thread, the
156     //+       caller blocks until the mutex has been freed.
157     //
158     //! RETURNS:  1 - Acquired
159     //! RETURNS: -1 - Error
160     //
161     //! NOTE: No special write mutex has been defined for now.
162     // -----------------------------------------------------------------------
163     inline int
164     acquireWrite (void) {
165         return this->acquire();
166     }
167
168     // -----------------------------------------------------------------------
169     //: Try to acquire a lock on the mutex variable (does not block).
170     //
171     //! PRE: None.
172     //! POST: A lock on the mutex variable is acquired by the caller.  If a
173     //+       lock has already been acquired by another process/thread, the
174     //+       caller returns does not wait for it to be unlocked.
175     //
176     //! RETURNS: 1 - Acquired
177     //! RETURNS: 0 - Mutex is busy
178     // -----------------------------------------------------------------------
179     inline int
180     tryAcquire (void) {
181         if ( pthread_mutex_trylock(&mMutex) == 0 ) {
182             return 1;
183         } else {
184             return 0;
185         }
186     }
187
188     // -----------------------------------------------------------------------
189     //: Try to acquire a read mutex lock (does not block).
190     //
191     //! PRE: None.
192     //! POST: A lock on the mutex variable is acquired by the caller.  If a
193     //+       lock has already been acquired by another process/thread, the
194     //+       caller returns does not wait for it to be unlocked.
195     //
196     //! RETURNS: 1 - Acquired
197     //! RETURNS: 0 - Mutex is busy
198     // -----------------------------------------------------------------------
199     inline int
200     tryAcquireRead (void) {
201         return this->tryAcquire();
202     }
203
204     // -----------------------------------------------------------------------
205     //: Try to acquire a write mutex (does not block)..
206     //
207     //! PRE: None.
208     //! POST: A lock on the mutex variable is acquired by the caller.  If a
209     //+       lock has already been acquired by another process/thread, the
210     //+       caller returns does not wait for it to be unlocked.
211     //
212     //! RETURNS: 1 - Acquired
213     //! RETURNS: 0 - Mutex is busy
214     // -----------------------------------------------------------------------
215     inline int
216     tryAcquireWrite (void) {
217         return this->tryAcquire();
218     }
219
220     // -----------------------------------------------------------------------
221     //: Release the mutex.
222     //
223     //! PRE: The mutex variable must be locked.
224     //! POST: The mutex variable is unlocked.
225     //
226     //! RETURNS:  0 - Success
227     //! RETURNS: -1 - Error
228     // -----------------------------------------------------------------------
229     inline int
230     release (void) {
231         return pthread_mutex_unlock(&mMutex);
232     }
233
234     // -----------------------------------------------------------------------
235     //: Test the current lock status.
236     //
237     //! PRE: None.
238     //! POST: The state of the mutex variable is returned.
239     //
240     //! RETURNS: 0 - Not locked
241     //! RETURNS: 1 - Locked
242     // -----------------------------------------------------------------------
243     int test(void);
244
245     // -----------------------------------------------------------------------
246     //: Dump the mutex debug stuff and current state.
247     //
248     //! PRE: None.
249     //! POST: All important data and debugging information related to the
250     //+       mutex are dumped to the specified file descriptor (or to stderr
251     //+       if none is given).
252     //
253     //! ARGS: dest - File descriptor to which the output will be written.
254     //+              It defaults to stderr if no descriptor is specified.
255     //! ARGS: message - Message printed out before the output is dumped.
256     // -----------------------------------------------------------------------
257     void
258     dump (FILE* dest = stderr,
259           const char* message = "\n------ Mutex Dump -----\n") const
260     {
261         fprintf(dest, "%s", message);
262         fprintf(dest, "This is not currently implemented ...\n");
263         // More needed ...
264     }
265
266
267 // Allow the vjCondPosix class to access the private and protected members of
268 // this class.  Specifically, direct access is needed to the mutex variable.
269 friend class  vjCondPosix;
270
271 protected:
272     pthread_mutex_t mMutex;    //: Mutex variable for the class.
273
274     // = Prevent assignment and initialization.
275     void operator= (const vjMutexPosix &) {}
276     vjMutexPosix (const vjMutexPosix &) {}
277 };
278
279
280 #endif  /* ifdef _VJ_MUTEX_POSIX_H_ */
Note: See TracBrowser for help on using the browser.