root/juggler/tags/1.0.5/Sync/vjSemaphoreWin32.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 _vjSemaphoreWin32_h_ |
| 35 | #define _vjSemaphoreWin32_h_ |
| 36 | //---------------------------------------------- |
| 37 | // vjSemaphoreWin32 |
| 38 | // |
| 39 | // Purpose: |
| 40 | //: Semaphore wrapper for the Win32 systems |
| 41 | // |
| 42 | // |
| 43 | // Author: |
| 44 | // Andy Himberger |
| 45 | // |
| 46 | // Date: 11-9-97 |
| 47 | //----------------------------------------------- |
| 48 | #include <windows.h> |
| 49 | #include <SharedMem/vjMemPool.h> |
| 50 | #include <SharedMem/vjMemPoolWin32.h> |
| 51 | #include <Kernel/vjDebug.h> |
| 52 | #include <Threads/vjThread.h> |
| 53 | |
| 54 | //!PUBLIC_API: |
| 55 | class vjSemaphoreWin32 |
| 56 | { |
| 57 | public: |
| 58 | vjSemaphoreWin32 (int initialValue = 1) |
| 59 | { |
| 60 | // // BUG: |
| 61 | // if (semaphorePool == NULL) |
| 62 | // { |
| 63 | // semaphorePool = new vjMemPoolWin32(65536, 32, "memSemaphorePoolWin32XXXXXX"); |
| 64 | // attachedCounter = static_cast<int*>(semaphorePool->allocate(sizeof(int))); |
| 65 | // *attachedCounter = 0; |
| 66 | // } |
| 67 | // *attachedCounter = *attachedCounter + 1; // Track how many semaphores are allocated |
| 68 | |
| 69 | //DebugLock.acquire(); |
| 70 | //std::cerr << vjThread::self() << " vjSemaphoreWin32::vjSemaphoreWin32: attachedCounter: " << *attachedCounter << std::endl; |
| 71 | //DebugLock.release(); |
| 72 | |
| 73 | // ----- Allocate the semaphore ----- // |
| 74 | sema = CreateSemaphore(NULL,initialValue,99,NULL); |
| 75 | } |
| 76 | |
| 77 | ~vjSemaphoreWin32(void) |
| 78 | { |
| 79 | // ---- Delete the semaphore --- // |
| 80 | CloseHandle(sema); |
| 81 | |
| 82 | // ---- Deal with the pool --- // |
| 83 | // *attachedCounter = *attachedCounter - 1; // Track how many Semaphore are allocated |
| 84 | |
| 85 | //DebugLock.acquire(); |
| 86 | //std::cerr << vjThread::self() << " vjSemaphoreWin32::~vjSemaphoreWin32: attachedCounter: " << *attachedCounter << std::endl; |
| 87 | //DebugLock.release(); |
| 88 | |
| 89 | // if (*attachedCounter == 0) |
| 90 | // { |
| 91 | // semaphorePool->deallocate(attachedCounter); |
| 92 | // attachedCounter = NULL; |
| 93 | // delete semaphorePool; |
| 94 | // semaphorePool = NULL; |
| 95 | // } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | //--------------------------------------------------------- |
| 100 | //: Lock the semaphore. |
| 101 | // |
| 102 | //! RETURNS: 1 - Acquired |
| 103 | //! RETURNS: -1 - Error |
| 104 | //--------------------------------------------------------- |
| 105 | int acquire() const |
| 106 | { |
| 107 | DWORD dw = WaitForSingleObject(sema,INFINITE); |
| 108 | if (dw == WAIT_OBJECT_0) |
| 109 | { |
| 110 | return 1; |
| 111 | } |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | //---------------------------------------------------------- |
| 116 | //: Acquire a read semaphore. |
| 117 | //---------------------------------------------------------- |
| 118 | int acquireRead() const |
| 119 | { |
| 120 | return this->acquire(); // No special "read" semaphore -- For now |
| 121 | } |
| 122 | |
| 123 | //---------------------------------------------------------- |
| 124 | //: Acquire a write semaphore. |
| 125 | //---------------------------------------------------------- |
| 126 | int acquireWrite() const |
| 127 | { |
| 128 | return this->acquire(); // No special "write" semaphore -- For now |
| 129 | } |
| 130 | |
| 131 | //--------------------------------------------------------- |
| 132 | //: Try to acquire the semaphore immediately. Does not |
| 133 | //+ block. |
| 134 | // |
| 135 | //! RETURNS: 1 - Acquired |
| 136 | //! RETURNS: 0 - Not acquired |
| 137 | //--------------------------------------------------------- |
| 138 | int tryAcquire () const |
| 139 | { |
| 140 | DWORD dw = WaitForSingleObject(sema,0); |
| 141 | if (dw == WAIT_OBJECT_0) |
| 142 | { |
| 143 | return 1; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | //---------------------------------------------------------- |
| 149 | //: Try to acquire a read semaphore. |
| 150 | //---------------------------------------------------------- |
| 151 | int tryAcquireRead () const |
| 152 | { |
| 153 | return this->tryAcquire(); |
| 154 | } |
| 155 | |
| 156 | //---------------------------------------------------------- |
| 157 | //: Try to acquire a write semaphore. |
| 158 | //---------------------------------------------------------- |
| 159 | int tryAcquireWrite () const |
| 160 | { |
| 161 | return this->tryAcquire(); |
| 162 | } |
| 163 | |
| 164 | //--------------------------------------------------------- |
| 165 | //: Release the semaphore. |
| 166 | // |
| 167 | //! RETURNS: 0 - Success |
| 168 | //! RETURNS: -1 - Error |
| 169 | //--------------------------------------------------------- |
| 170 | int release() const |
| 171 | { |
| 172 | return ReleaseSemaphore(sema,1,NULL); |
| 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, the |
| 182 | //+ results are undefined. |
| 183 | //--------------------------------------------------------- |
| 184 | int reset(int val) |
| 185 | { |
| 186 | if (!CloseHandle(sema)) return -1; |
| 187 | sema = CreateSemaphore(NULL,val,99,NULL); |
| 188 | if (sema == NULL) return -1; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | //--------------------------------------------------------- |
| 193 | //: Dump the semaphore debug stuff and current state. |
| 194 | //--------------------------------------------------------- |
| 195 | void dump (FILE* dest = stderr, const char* message = "\n------ Semaphore Dump -----\n") const |
| 196 | { |
| 197 | std::cout << "vjSemaphoreWin32::dump() \nNot implemented on Win32" |
| 198 | << std::endl; |
| 199 | } |
| 200 | |
| 201 | protected: |
| 202 | HANDLE sema; |
| 203 | int initVal; |
| 204 | |
| 205 | // = Prevent assignment and initialization. |
| 206 | void operator= (const vjSemaphoreWin32 &) {} |
| 207 | vjSemaphoreWin32 (const vjSemaphoreWin32 &) {} |
| 208 | |
| 209 | // // Problem here. Fork will not like these. |
| 210 | // static vjMemPoolWin32* semaphorePool; |
| 211 | // static int* attachedCounter; |
| 212 | }; |
| 213 | |
| 214 | //vjMemPoolWin32* vjSemaphoreWin32::semaphorePool = NULL; |
| 215 | //int* vjSemaphoreWin32::attachedCounter = NULL; |
| 216 | |
| 217 | #endif |
Note: See TracBrowser for help on using the browser.
