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

Revision 7539, 9.3 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 vjCondSGI by Allen Bierbaum).
38  * --------------------------------------------------------------------------
39  * NOTES:
40  *    - This file (vjCondPosix.h) must be included by vjCond.h, not the
41  *      other way around.
42  * --------------------------------------------------------------------------
43  */
44
45 #ifndef _VJ_COND_POSIX_H_
46 #define _VJ_COND_POSIX_H_
47
48
49 #include <vjConfig.h>
50 #include <pthread.h>
51 #include <Sync/vjMutexPosix.h>
52
53
54 //: Condition variable wrapper for POSIX-compliant systems using pthreads
55 //+ condition variables for the implementation.
56 //!PUBLIC_API:
57 class vjCondPosix {
58 public:
59     // -----------------------------------------------------------------------
60     //: Constructor for vjCondPosix class.
61     //
62     //! PRE: None.
63     //! POST: The condition variable is intialized, and the mutex variable
64     //+       associated with it is defined.  These two steps must be done
65     //+       before any other member functions can use them.
66     //
67     //! ARGS: mutex - Pointer to a vjMutexPosix variable that is used in
68     //+               association with the condition variable in this class
69     //+               (optional).
70     // -----------------------------------------------------------------------
71     vjCondPosix (vjMutexPosix* mutex = NULL) {
72         // Initialize the condition variable.
73 #ifdef _PTHREADS_DRAFT_4
74         pthread_cond_init(&mCondVar, pthread_condattr_default);
75 #else
76         pthread_cond_init(&mCondVar, NULL);
77 #endif
78
79         // If the caller did not specify a mutex variable to use with
80         // the condition variable, use mDefaultMutex.
81         if ( mutex == NULL ) {
82             mutex = &mDefaultMutex;
83         }
84
85         mCondMutex = mutex;
86     }
87
88     // -----------------------------------------------------------------------
89     //: Destructor for vjCondPosix class.
90     //
91     //! PRE: None.
92     //! POST: The condition variable is destroyed.
93     // -----------------------------------------------------------------------
94     ~vjCondPosix (void) {
95         pthread_cond_destroy(&mCondVar);
96     }
97
98     // -----------------------------------------------------------------------
99     //: Block on a condition.
100     //
101     //! PRE: The mutex variable associated with the condition variable must
102     //+      be locked.
103     //! POST: The condition variable is locked.  If it was previously
104     //+       locked, the caller blocks until signaled.
105     //
106     //! RETURNS:  0 - Successful completion
107     //! RETURNS: -1 - Error
108     // -----------------------------------------------------------------------
109     int
110     wait (void) {
111         // ASSERT:  We have been locked
112
113         // If not locked ...
114         if ( mCondMutex->test() == 0 ) {
115             std::cerr << "vjCondPosix::wait: INCORRECT USAGE: Mutex was not "
116                       << "locked when wait invoked!!!\n";
117
118             return -1;
119         }
120
121         // The mutex variable must be locked when passed to
122         // pthread_cond_wait().
123         return pthread_cond_wait(&mCondVar, &(mCondMutex->mMutex));
124     }
125
126     // -----------------------------------------------------------------------
127     //: Signal a thread waiting on the condition variable.
128     //
129     //! PRE: The condition variable must be locked.
130     //! POST: The condition variable is unlocked, and a signal is sent to a
131     //+       thread waiting on it.
132     //
133     //! RETURNS:  0 - Successful completion
134     //! RETURNS: -1 - Error
135     // -----------------------------------------------------------------------
136     inline int
137     signal (void) {
138         // ASSERT:  We have been locked
139         return pthread_cond_signal(&mCondVar);
140     }
141
142     // -----------------------------------------------------------------------
143     //: Signal all waiting threads.
144     //
145     //! PRE: The mutex variable associated with the condition variable
146     //+      should be locked.
147     //! POST: The condition variable is unlocked, and all waiting threads
148     //+       are signaled of this event.
149     //
150     //! RETURNS:  0 - Successful completion
151     //! RETURNS: -1 - Error
152     // -----------------------------------------------------------------------
153     inline int
154     broadcast (void) {
155         // ASSERT:  We have been locked
156
157         // If not locked ...
158         if ( mCondMutex->test() == 0 ) {
159             std::cerr << "vjCondPosix::broadcast: Mutex was not locked when "
160                       << "broadcast called!!!\n";
161         }
162
163         return pthread_cond_broadcast(&mCondVar);
164     }
165
166     // -----------------------------------------------------------------------
167     //: Acquire a lock on the mutex variable associated with the condition
168     //+ variable.
169     //
170     //! PRE: None.
171     //! POST: A lock is acquired on the mutex variable associated with the
172     //+      condition variable.  If a lock is acquired, the caller controls
173     //+      the mutex variable.  If it was previously locked, the caller
174     //+      blocks until it is unlocked.
175     //
176     //! RETURNS:  0 - Successful completion
177     //! RETURNS: -1 - Error
178     // -----------------------------------------------------------------------
179     inline int
180     acquire (void) {
181         return mCondMutex->acquire();
182     }
183
184     // -----------------------------------------------------------------------
185     //: Try to acquire a lock on the mutex variable associated with the
186     //+ condition variable.
187     //
188     //! PRE: None.
189     //! POST: If the mutex variable is not already locked, the caller
190     //+       obtains a lock on it.  If it is already locked, the routine
191     //+       returns immediately to the caller.
192     //
193     //! RETURNS:  0 - Successful completion
194     //! RETURNS: -1 - Error
195     // -----------------------------------------------------------------------
196     inline int
197     tryAcquire (void) {
198         return mCondMutex->tryAcquire();
199     }
200
201     // -----------------------------------------------------------------------
202     //: Release the lock on the mutex variable associated with the condition
203     //+ variable.
204     //
205     //! PRE: None.
206     //! POST: The lock held by the caller on the mutex variable is released.
207     // -----------------------------------------------------------------------
208     inline int
209     release (void) {
210         return mCondMutex->release();
211     }
212
213     // -----------------------------------------------------------------------
214     //: Change the condition variable mutex to be the specifiec mutex
215     //+ variable.
216     //
217     //! PRE: The specified mutex variable must be initialized.
218     //! POST: The condition variable associated with the mutex variable is
219     //+       reset to the specified variable.
220     //
221     //! ARGS: mutex - Pointer to a vjMutexPosix variable that is used in
222     //+               association with the condition variable in this class.
223     //
224     //! NOTE: NEVER call except to initialize explicitly.
225     // -----------------------------------------------------------------------
226     inline void
227     setMutex (vjMutexPosix* mutex) {
228         // NOT exactly correct, but just make sure not to leave it locked
229         mutex->release();
230         mCondMutex = mutex;
231     }
232
233     // -----------------------------------------------------------------------
234     //: Print out information about the condition variable to stderr.
235     //
236     //! PRE: None.
237     //! POST: All important data and debugging information related to the
238     //+       condition variable and its mutex are dumped to stderr.
239     // -----------------------------------------------------------------------
240     void
241     dump (void) const {
242         std::cerr << "------------- vjCondPosix::Dump ---------\n"
243                   << "Not Implemented yet.\n";
244     }
245
246 private:
247     pthread_cond_t      mCondVar;       //: Condition variable
248     vjMutexPosix*       mCondMutex;     //: Mutex for the condition variable
249     vjMutexPosix        mDefaultMutex;  //: A default mutex variable
250
251     // = Prevent assignment and initialization.
252     void operator= (const vjCondPosix&) {}
253     vjCondPosix (const vjCondPosix &c) {}
254 };
255
256 #endif  /* _VJ_COND_POSIX_H_ */
Note: See TracBrowser for help on using the browser.