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

Revision 2828, 9.8 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 /*
35  * --------------------------------------------------------------------------
36  * Author:
37  *   Patrick Hartling (based on vjSemaphoreSGI by Allen Bierbaum).
38  * --------------------------------------------------------------------------
39  * NOTES:
40  *    - This file (vjSemaphorePosix.h) must be included by vjSemaphore.h,
41  *      not the other way around.
42  * --------------------------------------------------------------------------
43  */
44
45 #ifndef _VJ_SEMAPHORE_POSIX_H_
46 #define _VJ_SEMAPHORE_POSIX_H_
47
48 #include <vjConfig.h>
49 #include <unistd.h>
50 #include <limits.h>
51 #include <semaphore.h>
52 #include <errno.h>
53 #include <stdio.h>
54
55 //: Semaphore wrapper for POSIX.4-compliant systems.
56
57 //!PUBLIC_API:
58 class vjSemaphorePosix {
59 public:
60     // -----------------------------------------------------------------------
61     //: Custructor for vjSemaphorePosix class.
62     //
63     //! PRE: None.
64     //! POST: The semaphore variable for the class is initilized as an
65     //+       unnamed semaphore.
66     //
67     //! ARGS: initialValue - The initial number of resources controlled by
68     //+                      the semaphore.  If not specified, the default
69     //+                      value is 1.
70     // -----------------------------------------------------------------------
71     vjSemaphorePosix (int initialValue = 1) {
72         // ----- Allocate the unnamed semaphore ----- //
73         mSema = (sem_t*) malloc(sizeof(sem_t));
74
75         if ( sem_init(mSema, 0, initialValue) != 0 ) {
76             perror("sem_init() error");
77         }
78     }
79
80     // -----------------------------------------------------------------------
81     //: Destructor for vjSemaphorePosix class.
82     //
83     //! PRE: None.
84     //! POST: The resources used by the semaphore variable are freed.
85     // -----------------------------------------------------------------------
86     ~vjSemaphorePosix (void) {
87         // ---- Delete the semaphore --- //
88         if ( sem_destroy(mSema) != 0 ) {
89             perror("sem_destroy() error");
90         }
91
92         free(mSema);
93     }
94
95     // -----------------------------------------------------------------------
96     //: Lock the semaphore.
97     //
98     //! PRE: None.
99     //! POST: The calling thread either acquires the semaphore until
100     //+       release() is called, or the caller is put at the tail of a wait
101     //+       and is suspended until such time as it can be freed and allowed
102     //+       to acquire the semaphore itself.
103     //
104     //! RETURNS:  1 - Lock acquired
105     //! RETURNS: -1 - Error
106     // -----------------------------------------------------------------------
107     inline int
108     acquire (void) const {
109         if ( sem_wait(mSema) == 0 ) {
110             return 1;
111         } else {
112             perror("sem_wait() error");
113             return -1;
114         }
115     }
116
117     // -----------------------------------------------------------------------
118     //: Acquire and lock a read semaphore.
119     //
120     //! PRE: None.
121     //! POST: The calling thread either acquires the semaphore until
122     //+       release() is called, or the caller is put at the tail of a wait
123     //+       and is suspended until such time as it can be freed and allowed
124     //+       to acquire the semaphore itself.
125     //
126     //! RETURNS:  1 - Lock acquired
127     //! RETURNS: -1 - Error
128     //
129     //! NOTE: There is no special read semaphore for now.
130     // -----------------------------------------------------------------------
131     inline int
132     acquireRead (void) const {
133         return this->acquire();
134     }
135
136     // -----------------------------------------------------------------------
137     //: Acquire and lock a write semaphore.
138     //
139     //! PRE: None.
140     //! POST: The calling thread either acquires the semaphore until
141     //+       release() is called, or the caller is put at the tail of a wait
142     //+       and is suspended until such time as it can be freed and allowed
143     //+       to acquire the semaphore itself.
144     //
145     //! RETURNS:  1 - Lock acquired
146     //! RETURNS: -1 - Error
147     //
148     //! NOTE: There is no special write semaphore for now.
149     // -----------------------------------------------------------------------
150     inline int
151     acquireWrite (void) const {
152         return this->acquire();
153     }
154
155     // -----------------------------------------------------------------------
156     //: Try to acquire the semaphore immediately (does not block).
157     //
158     //! PRE: None.
159     //! POST: If the semaphore could be acquired by the caller, the caller
160     //+       gets control of the semaphore.  If the semaphore was already
161     //+       locked, the routine returns immediately without suspending the
162     //+       calling thread.
163     //
164     //! RETURNS: 1 - Acquired
165     //! RETURNS: 0 - Not acquired
166     // -----------------------------------------------------------------------
167     inline int
168     tryAcquire (void) const {
169         return sem_trywait(mSema);
170     }
171
172     // -----------------------------------------------------------------------
173     //: Try to acquire a read semaphore (does not block).
174     //
175     //! PRE: None.
176     //! POST: If the semaphore could be acquired by the caller, the caller
177     //+       gets control of the semaphore.  If the semaphore was already
178     //+       locked, the routine returns immediately without suspending the
179     //+       calling thread.
180     //
181     //! RETURNS: 1 - Acquired
182     //! RETURNS: 0 - Not acquired
183     // -----------------------------------------------------------------------
184     inline int
185     tryAcquireRead (void) const {
186         return this->tryAcquire();
187     }
188
189     // -----------------------------------------------------------------------
190     //: Try to acquire a write semaphore (does not block).
191     //
192     //! PRE: None.
193     //! POST: If the semaphore could be acquired by the caller, the caller
194     //+       gets control of the semaphore.  If the semaphore was already
195     //+       locked, the routine returns immediately without suspending the
196     //+       calling thread.
197     //
198     //! RETURNS: 1 - Acquired
199     //! RETURNS: 0 - Not acquired
200     // -----------------------------------------------------------------------
201     inline int
202     tryAcquireWrite (void) const {
203         return this->tryAcquire();
204     }
205
206     // -----------------------------------------------------------------------
207     //: Release the semaphore.
208     //
209     //! PRE: The semaphore should have been locked before being released.
210     //! POST: The semaphore is released and the thread at the haed of the
211     //+       wait queue is allowed to execute again.
212     //
213     //! RETURNS:  0 - Success
214     //! RETURNS: -1 - Error
215     // -----------------------------------------------------------------------
216     inline int
217     release (void) const {
218         return sem_post(mSema);
219     }
220
221     // -----------------------------------------------------------------------
222     //: Reset the semaphore.
223     //
224     //! PRE: None.
225     //! POST: The semaphore's count is set to the specified value.
226     //
227     //! ARGS: val - The value to which the semaphore is reset.
228     //
229     //! RETURNS:  0 - Success
230     //! RETURNS: -1 - Error
231     //
232     //! NOTE: If processes are waiting on the semaphore, the results are
233     //+       undefined.
234     // -----------------------------------------------------------------------
235     inline int
236     reset (int val) {
237         // First destroy the current semaphore.
238         sem_destroy(mSema);
239
240         // Now recreate it with the new value in val.
241         return sem_init(mSema, 0, val);
242     }
243
244     // -----------------------------------------------------------------------
245     //: Dump the semaphore debug stuff and current state.
246     //
247     //! PRE: None.
248     //! POST: All important data and debugging information related to the
249     //+       semaphore is dumped to the specified file descriptor (or to
250     //+       stderr if none is given).
251     //
252     //! ARGS: dest - File descriptor to which the output will be written.
253     //+              It defaults to stderr if no descriptor is specified.
254     //! ARGS: message - Message printed out before the output is dumped.
255     // -----------------------------------------------------------------------
256     void
257     dump (FILE* dest = stderr,
258           const char* message = "\n------ Semaphore Dump -----\n") const
259     {
260         int value;
261
262         sem_getvalue(mSema, &value);
263
264         fprintf(dest, "%s", message);
265         fprintf(dest, "Current semaphore value: %d", value);
266     }
267
268 protected:
269     sem_t* mSema;       //: Semaphore variable for the class.
270
271     // Prevent assignment and initialization.
272     void operator= (const vjSemaphorePosix &) {}
273     vjSemaphorePosix (const vjSemaphorePosix &) {}
274 };
275
276 #endif  /* _VJ_SEMAPHORE_POSIX_H_ */
Note: See TracBrowser for help on using the browser.