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

Revision 7539, 5.9 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 #ifndef _vjMutexWin32_h_
35 #define _vjMutexWin32_h_
36 //----------------------------------------------
37 // vjMutexWin32
38 //
39 // Purpose: (also called a lock)
40 //    Mutex wrapper for Win32 systems
41 //    Used for critical section protection
42 //
43 // Author:
44 //      Andy Himberger
45 //
46 // Date: 11-7-97
47 //-----------------------------------------------
48 #include <windows.h>
49 //#include <SharedMem/vjMemPool.h>
50 //#include <SharedMem/vjMemPoolWin32.h>
51
52 //: Mutex wrapper for Win32 systems.
53 //!PUBLIC_API:
54 class vjMutexWin32
55 {
56 public:
57     vjMutexWin32 ()
58     {       
59         // ----- Allocate the mutex ----- //
60         // NULL - No security
61         // FALSE - We don't want to qcquire it
62         // NULL - Unamed version
63         mutex = CreateMutex(NULL,FALSE,NULL);
64         mLocked = false;                  // We don't have it locked yet
65     }
66
67     ~vjMutexWin32(void)
68     {
69         // ---- Delete the mutex --- //
70         CloseHandle(mutex);
71     }
72
73     //---------------------------------------------------------
74     //: Lock the mutex.
75     //
76     //! RETURNS:  1 - Acquired
77     //! RETURNS: -1 - Error
78     //---------------------------------------------------------
79     int acquire()
80     {
81                 DWORD dw = WaitForSingleObject(mutex,INFINITE);
82                 if (dw == WAIT_OBJECT_0)
83                 {
84                         mLocked = true;
85          return 1;
86                 }
87       else
88       {
89         return -1;
90       }
91     }
92
93     //----------------------------------------------------------
94     //: Acquire a read mutex.
95     //----------------------------------------------------------
96     int acquireRead()
97     {
98         return this->acquire();     // No special "read" semaphore -- For now
99     }
100
101     //----------------------------------------------------------
102     //: Acquire a write mutex.
103     //----------------------------------------------------------
104     int acquireWrite()
105     {
106         return this->acquire();     // No special "write" semaphore -- For now
107     }
108
109     //---------------------------------------------------------
110     //: Try to acquire the lock.  Returns immediately even if
111     //+ we don't acquire the lock.
112     //
113     //! RETURNS: 1 - Acquired
114     //! RETURNS: 0 - Not Acquired
115     //---------------------------------------------------------
116     int tryAcquire ()
117     {
118       DWORD dw = WaitForSingleObject(mutex,0);
119                 if (dw == WAIT_OBJECT_0)
120                 {
121                         mLocked = true;
122          return 1;
123                 }
124                 return 0;       
125     }
126
127     //----------------------------------------------------------
128     //: Try to acquire a read mutex.
129     //----------------------------------------------------------
130     int tryAcquireRead ()
131     {
132         return this->tryAcquire();
133     }
134
135     //----------------------------------------------------------
136     //: Try to acquire a write mutex.
137     //----------------------------------------------------------
138     int tryAcquireWrite ()
139     {
140         return this->tryAcquire();
141     }
142
143     //---------------------------------------------------------
144     //: Release the mutex.
145     //
146     //! RETURNS:  0 - Success
147     //! RETURNS: -1 - Error
148     //---------------------------------------------------------
149     int release()
150     {
151        int ret_val = ReleaseMutex(mutex);
152        
153        if(ret_val == 1)
154           mLocked = false;       // If failure, I will keep it locked
155
156        return ret_val;
157     }
158
159     //------------------------------------------------------
160     //: Test the current lock status.
161     //
162     //! NOTE: Since Win32 allows the same thread to
163     //+        acqurie a lock multiple times, we need to do
164     //+        some extra stuff
165     //
166     //! RETURNS: 0 - Not locked
167     //! RETURNS: 1 - Locked
168     //------------------------------------------------------
169     int test() const
170     {
171        if(mLocked == true)
172           return 1;
173        else
174           return 0;
175     }
176
177
178     //---------------------------------------------------------
179     //: Dump the mutex debug stuff and current state.
180     //---------------------------------------------------------
181     void dump (FILE* dest = stderr, const char* message = "\n------ Mutex Dump -----\n") const
182     {
183         std::cout << "Mutex::dump\nNot implemented on Win32" << std::endl;
184     }
185
186
187 protected:
188     HANDLE mutex;
189    
190       // We need this variable because win32 allows multiple
191       // acquisitions by the same thread.  In order for us to
192       // determine if the thread is currently locked we need to
193       // test this variable
194       // Notice that the only place this variable is touched is
195       // in acquire and release where we implicitly have protection
196     bool   mLocked;     // Says wether the mutex is locked
197                         
198     // = Prevent assignment and initialization.
199     void operator= (const vjMutexWin32 &) {}
200     vjMutexWin32 (const vjMutexWin32 &) {}
201 };
202
203
204 #endif
Note: See TracBrowser for help on using the browser.