root/juggler/tags/1.0.5/Sync/vjSemaphoreSGI.h
| Revision 2828, 6.5 kB (checked in by patrickh, 8 years ago) | |
|---|---|
| |
| 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 _vjSemaphoreSGI_h_ |
| 35 | #define _vjSemaphoreSGI_h_ |
| 36 | |
| 37 | #include <vjConfig.h> |
| 38 | #include <ulocks.h> |
| 39 | #include <SharedMem/vjMemPool.h> |
| 40 | #include <SharedMem/vjMemPoolSGI.h> |
| 41 | |
| 42 | //---------------------------------------------- |
| 43 | //: Semaphore wrapper for the SGI systems |
| 44 | // |
| 45 | // This class encapsulates the behavior of a semaphore variable. |
| 46 | // |
| 47 | // Author: |
| 48 | // Allen Bierbaum |
| 49 | // |
| 50 | // Date: 1-20-97 |
| 51 | //----------------------------------------------- |
| 52 | //!PUBLIC_API: |
| 53 | class vjSemaphoreSGI |
| 54 | { |
| 55 | public: |
| 56 | //--------------------------------------------------------- |
| 57 | //: Constructor |
| 58 | // Default to initial Value = 1 |
| 59 | // That means taht semaphore initialy is available. |
| 60 | //--------------------------------------------------------- |
| 61 | vjSemaphoreSGI (int initialValue = 1) |
| 62 | { |
| 63 | // BUG: |
| 64 | if (semaphorePool == NULL) |
| 65 | { |
| 66 | semaphorePool = new vjMemPoolSGI(65536, 32, "/var/tmp/memSemaphorePoolSGIXXXXXX"); |
| 67 | attachedCounter = static_cast<int*>(semaphorePool->allocate(sizeof(int))); |
| 68 | *attachedCounter = 0; |
| 69 | } |
| 70 | *attachedCounter = *attachedCounter + 1; // Track how many semaphores are allocated |
| 71 | |
| 72 | //DebugLock.acquire(); |
| 73 | //vjDEBUG << vjThread::self() << " vjSemaphoreSGI::vjSemaphoreSGI: attachedCounter: " << *attachedCounter << endl << vjDEBUG_FLUSH; |
| 74 | //DebugLock.release(); |
| 75 | |
| 76 | // ----- Allocate the semaphore ----- // |
| 77 | sema = usnewsema(semaphorePool->getArena(), initialValue); |
| 78 | } |
| 79 | |
| 80 | ~vjSemaphoreSGI(void) |
| 81 | { |
| 82 | // ---- Delete the semaphore --- // |
| 83 | usfreesema(sema, semaphorePool->getArena()); |
| 84 | |
| 85 | // ---- Deal with the pool --- // |
| 86 | *attachedCounter = *attachedCounter - 1; // Track how many Semaphore are allocated |
| 87 | |
| 88 | //DebugLock.acquire(); |
| 89 | //vjDEBUG << vjThread::self() << " vjSemaphoreSGI::~vjSemaphoreSGI: attachedCounter: " << *attachedCounter << endl << vjDEBUG_FLUSH; |
| 90 | //DebugLock.release(); |
| 91 | |
| 92 | if (*attachedCounter == 0) |
| 93 | { |
| 94 | semaphorePool->deallocate(attachedCounter); |
| 95 | attachedCounter = NULL; |
| 96 | delete semaphorePool; |
| 97 | semaphorePool = NULL; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | //--------------------------------------------------------- |
| 102 | //: Lock the semaphore. |
| 103 | // |
| 104 | //! RETURNS: 1 - Acquired |
| 105 | //! RETURNS: -1 - Error |
| 106 | //--------------------------------------------------------- |
| 107 | int acquire() const |
| 108 | { |
| 109 | int ret_val = uspsema(sema); |
| 110 | if(ret_val < 0) |
| 111 | std::cerr << "vjSemphoreSGI::ERROR:" << std::endl; |
| 112 | |
| 113 | return ret_val; |
| 114 | } |
| 115 | |
| 116 | //---------------------------------------------------------- |
| 117 | //: Acquire a read semaphore. |
| 118 | //---------------------------------------------------------- |
| 119 | int acquireRead() const |
| 120 | { |
| 121 | return this->acquire(); // No special "read" semaphore -- For now |
| 122 | } |
| 123 | |
| 124 | //---------------------------------------------------------- |
| 125 | //: Acquire a write semaphore. |
| 126 | //---------------------------------------------------------- |
| 127 | int acquireWrite() const |
| 128 | { |
| 129 | return this->acquire(); // No special "write" semaphore -- For now |
| 130 | } |
| 131 | |
| 132 | //--------------------------------------------------------- |
| 133 | //: Try to acquire the semaphore immediately. Does not |
| 134 | //+ block. |
| 135 | // |
| 136 | //! RETURNS: 1 - Acquired |
| 137 | //! RETURNS: 0 - Not acquired |
| 138 | //--------------------------------------------------------- |
| 139 | int tryAcquire () const |
| 140 | { |
| 141 | return uscpsema(sema); |
| 142 | } |
| 143 | |
| 144 | //---------------------------------------------------------- |
| 145 | //: Try to acquire a read semaphore. |
| 146 | //---------------------------------------------------------- |
| 147 | int tryAcquireRead () const |
| 148 | { |
| 149 | return this->tryAcquire(); |
| 150 | } |
| 151 | |
| 152 | //---------------------------------------------------------- |
| 153 | //: Try to acquire a write semaphore. |
| 154 | //---------------------------------------------------------- |
| 155 | int tryAcquireWrite () const |
| 156 | { |
| 157 | return this->tryAcquire(); |
| 158 | } |
| 159 | |
| 160 | //--------------------------------------------------------- |
| 161 | //: Release the semaphore. |
| 162 | // |
| 163 | //! RETURNS: 0 - Success |
| 164 | //! RETURNS: -1 - Error |
| 165 | //--------------------------------------------------------- |
| 166 | int release() const |
| 167 | { |
| 168 | int ret_val = usvsema(sema); |
| 169 | if(ret_val < 0) |
| 170 | std::cerr << "vjSemaphoreSGI::ERROR:" << std::endl; |
| 171 | |
| 172 | return ret_val; |
| 173 | } |
| 174 | |
| 175 | //--------------------------------------------------------- |
| 176 | //: Reset the semaphore. |
| 177 | // |
| 178 | //! RETURNS: 0 - Success |
| 179 | //! RETURNS: -1 - Error |
| 180 | // |
| 181 | //! NOTE: If processes are waiting on the semaphore, |
| 182 | //+ the results are undefined. |
| 183 | //--------------------------------------------------------- |
| 184 | int reset(int val) |
| 185 | { |
| 186 | return usinitsema(sema, val); |
| 187 | } |
| 188 | |
| 189 | //--------------------------------------------------------- |
| 190 | //: Dump the semaphore debug stuff and current state. |
| 191 | //--------------------------------------------------------- |
| 192 | void dump (FILE* dest = stderr, |
| 193 | const char* message = "\n------ Semaphore Dump -----\n") const |
| 194 | { |
| 195 | usdumpsema(sema, dest, message); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | protected: |
| 200 | usema_t* sema; |
| 201 | |
| 202 | // = Prevent assignment and initialization. |
| 203 | void operator= (const vjSemaphoreSGI &) {} |
| 204 | vjSemaphoreSGI (const vjSemaphoreSGI &) {} |
| 205 | |
| 206 | // Problem here. Fork will not like these. |
| 207 | static vjMemPoolSGI* semaphorePool; |
| 208 | static int* attachedCounter; |
| 209 | }; |
| 210 | |
| 211 | #endif |
Note: See TracBrowser for help on using the browser.
