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

Revision 8789, 9.8 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • 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, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 /*
36  * --------------------------------------------------------------------------
37  * Author:
38  *   Patrick Hartling (based on vjSemaphoreSGI by Allen Bierbaum).
39  * --------------------------------------------------------------------------
40  * NOTES:
41  *    - This file (vjSemaphorePosix.h) must be included by vjSemaphore.h,
42  *      not the other way around.
43  * --------------------------------------------------------------------------
44  */
45
46 #ifndef _VJ_SEMAPHORE_POSIX_H_
47 #define _VJ_SEMAPHORE_POSIX_H_
48
49 #include <vjConfig.h>
50 #include <unistd.h>
51 #include <limits.h>
52 #include <semaphore.h>
53 #include <errno.h>
54 #include <stdio.h>
55
56 //: Semaphore wrapper for POSIX.4-compliant systems.
57
58 //!PUBLIC_API:
59 class vjSemaphorePosix {
60 public:
61     // -----------------------------------------------------------------------
62     //: Custructor for vjSemaphorePosix class.
63     //
64     //! PRE: None.
65     //! POST: The semaphore variable for the class is initilized as an
66     //+       unnamed semaphore.
67     //
68     //! ARGS: initialValue - The initial number of resources controlled by
69     //+                      the semaphore.  If not specified, the default
70     //+                      value is 1.
71     // -----------------------------------------------------------------------
72     vjSemaphorePosix (int initialValue = 1) {
73         // ----- Allocate the unnamed semaphore ----- //
74         mSema = (sem_t*) malloc(sizeof(sem_t));
75
76         if ( sem_init(mSema, 0, initialValue) != 0 ) {
77             perror("sem_init() error");
78         }
79     }
80
81     // -----------------------------------------------------------------------
82     //: Destructor for vjSemaphorePosix class.
83     //
84     //! PRE: None.
85     //! POST: The resources used by the semaphore variable are freed.
86     // -----------------------------------------------------------------------
87     ~vjSemaphorePosix (void) {
88         // ---- Delete the semaphore --- //
89         if ( sem_destroy(mSema) != 0 ) {
90             perror("sem_destroy() error");
91         }
92
93         free(mSema);
94     }
95
96     // -----------------------------------------------------------------------
97     //: Lock the semaphore.
98     //
99     //! PRE: None.
100     //! POST: The calling thread either acquires the semaphore until
101     //+       release() is called, or the caller is put at the tail of a wait
102     //+       and is suspended until such time as it can be freed and allowed
103     //+       to acquire the semaphore itself.
104     //
105     //! RETURNS:  1 - Lock acquired
106     //! RETURNS: -1 - Error
107     // -----------------------------------------------------------------------
108     inline int
109     acquire (void) const {
110         if ( sem_wait(mSema) == 0 ) {
111             return 1;
112         } else {
113             perror("sem_wait() error");
114             return -1;
115         }
116     }
117
118     // -----------------------------------------------------------------------
119     //: Acquire and lock a read semaphore.
120     //
121     //! PRE: None.
122     //! POST: The calling thread either acquires the semaphore until
123     //+       release() is called, or the caller is put at the tail of a wait
124     //+       and is suspended until such time as it can be freed and allowed
125     //+       to acquire the semaphore itself.
126     //
127     //! RETURNS:  1 - Lock acquired
128     //! RETURNS: -1 - Error
129     //
130     //! NOTE: There is no special read semaphore for now.
131     // -----------------------------------------------------------------------
132     inline int
133     acquireRead (void) const {
134         return this->acquire();
135     }
136
137     // -----------------------------------------------------------------------
138     //: Acquire and lock a write semaphore.
139     //
140     //! PRE: None.
141     //! POST: The calling thread either acquires the semaphore until
142     //+       release() is called, or the caller is put at the tail of a wait
143     //+       and is suspended until such time as it can be freed and allowed
144     //+       to acquire the semaphore itself.
145     //
146     //! RETURNS:  1 - Lock acquired
147     //! RETURNS: -1 - Error
148     //
149     //! NOTE: There is no special write semaphore for now.
150     // -----------------------------------------------------------------------
151     inline int
152     acquireWrite (void) const {
153         return this->acquire();
154     }
155
156     // -----------------------------------------------------------------------
157     //: Try to acquire the semaphore immediately (does not block).
158     //
159     //! PRE: None.
160     //! POST: If the semaphore could be acquired by the caller, the caller
161     //+       gets control of the semaphore.  If the semaphore was already
162     //+       locked, the routine returns immediately without suspending the
163     //+       calling thread.
164     //
165     //! RETURNS: 1 - Acquired
166     //! RETURNS: 0 - Not acquired
167     // -----------------------------------------------------------------------
168     inline int
169     tryAcquire (void) const {
170         return sem_trywait(mSema);
171     }
172
173     // -----------------------------------------------------------------------
174     //: Try to acquire a read semaphore (does not block).
175     //
176     //! PRE: None.
177     //! POST: If the semaphore could be acquired by the caller, the caller
178     //+       gets control of the semaphore.  If the semaphore was already
179     //+       locked, the routine returns immediately without suspending the
180     //+       calling thread.
181     //
182     //! RETURNS: 1 - Acquired
183     //! RETURNS: 0 - Not acquired
184     // -----------------------------------------------------------------------
185     inline int
186     tryAcquireRead (void) const {
187         return this->tryAcquire();
188     }
189
190     // -----------------------------------------------------------------------
191     //: Try to acquire a write semaphore (does not block).
192     //
193     //! PRE: None.
194     //! POST: If the semaphore could be acquired by the caller, the caller
195     //+       gets control of the semaphore.  If the semaphore was already
196     //+       locked, the routine returns immediately without suspending the
197     //+       calling thread.
198     //
199     //! RETURNS: 1 - Acquired
200     //! RETURNS: 0 - Not acquired
201     // -----------------------------------------------------------------------
202     inline int
203     tryAcquireWrite (void) const {
204         return this->tryAcquire();
205     }
206
207     // -----------------------------------------------------------------------
208     //: Release the semaphore.
209     //
210     //! PRE: The semaphore should have been locked before being released.
211     //! POST: The semaphore is released and the thread at the haed of the
212     //+       wait queue is allowed to execute again.
213     //
214     //! RETURNS:  0 - Success
215     //! RETURNS: -1 - Error
216     // -----------------------------------------------------------------------
217     inline int
218     release (void) const {
219         return sem_post(mSema);
220     }
221
222     // -----------------------------------------------------------------------
223     //: Reset the semaphore.
224     //
225     //! PRE: None.
226     //! POST: The semaphore's count is set to the specified value.
227     //
228     //! ARGS: val - The value to which the semaphore is reset.
229     //
230     //! RETURNS:  0 - Success
231     //! RETURNS: -1 - Error
232     //
233     //! NOTE: If processes are waiting on the semaphore, the results are
234     //+       undefined.
235     // -----------------------------------------------------------------------
236     inline int
237     reset (int val) {
238         // First destroy the current semaphore.
239         sem_destroy(mSema);
240
241         // Now recreate it with the new value in val.
242         return sem_init(mSema, 0, val);
243     }
244
245     // -----------------------------------------------------------------------
246     //: Dump the semaphore debug stuff and current state.
247     //
248     //! PRE: None.
249     //! POST: All important data and debugging information related to the
250     //+       semaphore is dumped to the specified file descriptor (or to
251     //+       stderr 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------ Semaphore Dump -----\n") const
260     {
261         int value;
262
263         sem_getvalue(mSema, &value);
264
265         fprintf(dest, "%s", message);
266         fprintf(dest, "Current semaphore value: %d", value);
267     }
268
269 protected:
270     sem_t* mSema;       //: Semaphore variable for the class.
271
272     // Prevent assignment and initialization.
273     void operator= (const vjSemaphorePosix &) {}
274     vjSemaphorePosix (const vjSemaphorePosix &) {}
275 };
276
277 #endif  /* _VJ_SEMAPHORE_POSIX_H_ */
Note: See TracBrowser for help on using the browser.