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

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