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

Revision 7539, 6.1 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 _vjMutexSGI_h_
35 #define _vjMutexSGI_h_
36 //----------------------------------------------
37 //  vjMutexSGI
38 //
39 // Purpose: (also called a lock)
40 //    Mutex wrapper for the SGI systems
41 //    Used for critical section protection
42 //
43 // Author:
44 // Allen Bierbaum
45 //
46 // Date: 1-20-97
47 //-----------------------------------------------
48
49 #include <vjConfig.h>
50 #include <ulocks.h>
51 #include <SharedMem/vjMemPool.h>
52 #include <SharedMem/vjMemPoolSGI.h>
53 #include <Kernel/vjAssert.h>
54
55 //: Mutex wrapper for the SGI systems.  Used for critical section protection.
56 //!PUBLIC_API:
57 class  vjMutexSGI
58 {
59 public:
60      vjMutexSGI ()
61     {
62         // BUG: Possible race condition here
63         if(mutexPool == NULL) {
64             mutexPool = new vjMemPoolSGI(65536, 32, "/var/tmp/memMutexPoolSGIXXXXXX");
65             attachedCounter = static_cast<int*>(mutexPool->allocate(sizeof(int)));
66             *attachedCounter = 0;
67         }
68         *attachedCounter = *attachedCounter + 1;       // Track how many mutexes are allocated
69         //vjDEBUG << " vjMutexSGI:: vjMutexSGI: attachedCounter: " << *attachedCounter << endl << vjDEBUG_FLUSH;
70
71         // ----- Allocate the mutex ----- //
72         mutex = usnewlock(mutexPool->getArena());
73         vjASSERT( mutex != NULL && "in vjMutexSGI::vjMutexSGI() mutex is NULL" );
74     }
75
76     ~ vjMutexSGI(void)
77     {
78         // ---- Delete the mutex --- //
79         usfreelock(mutex, mutexPool->getArena());
80
81         // ---- Deal with the pool --- //
82         *attachedCounter = *attachedCounter - 1;       // Track how many mutexes are allocated
83
84         //vjDEBUG << " vjMutexSGI::~ vjMutexSGI: attachedCounter: " << *attachedCounter << endl << vjDEBUG_FLUSH;
85
86         if(*attachedCounter == 0)
87         {
88             mutexPool->deallocate(attachedCounter);
89             attachedCounter = NULL;
90             delete mutexPool;
91             mutexPool = NULL;
92         }
93
94     }
95
96     //---------------------------------------------------------
97     //: Lock the mutex.
98     //
99     //! RETURNS:  1 - Acquired
100     //! RETURNS: -1 - Error
101     //---------------------------------------------------------
102     int acquire() const
103     {
104        vjASSERT( mutex != NULL && "in vjMutexSGI::aquire() mutex is NULL" );
105         return ussetlock( mutex );
106     }
107
108     //----------------------------------------------------------
109     //: Acquire a read mutex.
110     //----------------------------------------------------------
111     int acquireRead() const
112     {
113         return this->acquire();      // No special "read" semaphore -- For now
114     }
115
116     //----------------------------------------------------------
117     //: Acquire a write mutex.
118     //----------------------------------------------------------
119     int acquireWrite() const
120     {
121         return this->acquire();      // No special "write" semaphore -- For now
122     }
123
124     //---------------------------------------------------------
125     //: Try to acquire the lock.  Returns immediately even if
126     //+ we don't acquire the lock.
127     //
128     //! RETURNS: 1 - Acquired
129     //! RETURNS: 0 - Not acquired
130     //---------------------------------------------------------
131     int tryAcquire () const
132     {
133         return uscsetlock(mutex, 100);     // Try 100 spins.
134     }
135
136     //----------------------------------------------------------
137     //: Try to acquire a read mutex.
138     //----------------------------------------------------------
139     int tryAcquireRead () const
140     {
141         return this->tryAcquire();
142     }
143
144     //----------------------------------------------------------
145     //: Try to acquire a write mutex.
146     //----------------------------------------------------------
147     int tryAcquireWrite () const
148     {
149         return this->tryAcquire();
150     }
151
152     //---------------------------------------------------------
153     //: Release the mutex.
154     //
155     //! RETURNS:  0 - Success
156     //! RETURNS: -1 - Error
157     //---------------------------------------------------------
158     int release() const
159     {
160         return usunsetlock(mutex);
161     }
162
163     //------------------------------------------------------
164     //: Test the current lock status.
165     //
166     //! RETURNS: 0 - Not locked
167     //! RETURNS: 1 - Locked
168     //------------------------------------------------------
169     int test()
170     {
171         return ustestlock(mutex);
172     }
173
174     //---------------------------------------------------------
175     //: Dump the mutex debug stuff and current state.
176     //---------------------------------------------------------
177     void dump (FILE* dest = stdout,
178                const char* message = "\n------ Mutex Dump -----\n") const
179     {
180         usdumplock(mutex, dest, message);
181     }
182
183 protected:
184     ulock_t mutex;
185
186     // = Prevent assignment and initialization.
187     void operator= (const  vjMutexSGI &) {}
188      vjMutexSGI (const  vjMutexSGI &) {}
189
190     static vjMemPoolSGI* mutexPool;
191     static int* attachedCounter;
192 };
193
194 #endif
Note: See TracBrowser for help on using the browser.