root/juggler/tags/1.0.5/Sync/vjRWMutex.h
| Revision 2828, 4.8 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 _VJRWMutex_h_ |
| 35 | #define _VJRWMutex_h_ |
| 36 | |
| 37 | #include <vjConfig.h> |
| 38 | #include <Sync/vjSemaphore.h> |
| 39 | #include <Sync/vjCond.h> |
| 40 | #include <Sync/vjMutex.h> |
| 41 | |
| 42 | //---------------------------------------------- |
| 43 | // vjRWMutex |
| 44 | // |
| 45 | // Purpose: |
| 46 | //: vjRWMutex wrapper |
| 47 | // |
| 48 | // |
| 49 | // Author: |
| 50 | // Allen Bierbaum |
| 51 | // |
| 52 | // Date: 1-31-97 |
| 53 | //----------------------------------------------- |
| 54 | //!PUBLIC_API: |
| 55 | class vjRWMutex |
| 56 | { |
| 57 | public: |
| 58 | vjRWMutex () : waitingReaders(&stateLock), |
| 59 | numWaitingReaders(0), |
| 60 | waitingWriters(&stateLock), |
| 61 | numWaitingWriters(0), |
| 62 | refCount(0) |
| 63 | {} |
| 64 | |
| 65 | ~vjRWMutex(void) |
| 66 | {} |
| 67 | |
| 68 | //--------------------------------------------------------- |
| 69 | //: Lock the mutex. |
| 70 | // |
| 71 | //! RETURNS: 1 - Acquired |
| 72 | //! RETURNS: -1 - Error |
| 73 | //--------------------------------------------------------- |
| 74 | int acquire() |
| 75 | { |
| 76 | return acquireWrite(); |
| 77 | } |
| 78 | |
| 79 | //---------------------------------------------------------- |
| 80 | //: Acquire a read mutex. |
| 81 | //---------------------------------------------------------- |
| 82 | int acquireRead(void); |
| 83 | |
| 84 | //---------------------------------------------------------- |
| 85 | //: Acquire a write mutex. |
| 86 | //---------------------------------------------------------- |
| 87 | int acquireWrite(void); |
| 88 | |
| 89 | //--------------------------------------------------------- |
| 90 | //: Trys to acquire the mutex. |
| 91 | // Wait until the semaphore value is greater than 0. |
| 92 | // Then decrement by 1 and return. |
| 93 | // P operation. |
| 94 | // |
| 95 | //! RETURNS: 1 - Acquired |
| 96 | //! RETURNS: 0 - Not acquired |
| 97 | //--------------------------------------------------------- |
| 98 | int tryAcquire () |
| 99 | { |
| 100 | return tryAcquireWrite(); |
| 101 | } |
| 102 | |
| 103 | //---------------------------------------------------------- |
| 104 | //: Try to acquire a read mutex. |
| 105 | //---------------------------------------------------------- |
| 106 | int tryAcquireRead (void); |
| 107 | |
| 108 | //---------------------------------------------------------- |
| 109 | //: Try to acquire a write mutex. |
| 110 | //---------------------------------------------------------- |
| 111 | int tryAcquireWrite (void); |
| 112 | |
| 113 | //--------------------------------------------------------- |
| 114 | //: Release the mutex. |
| 115 | // |
| 116 | //! RETURNS: 0 - Success |
| 117 | //! RETURNS: -1 - Error |
| 118 | //--------------------------------------------------------- |
| 119 | int release(void); |
| 120 | |
| 121 | //------------------------------------------------------ |
| 122 | //: Test the current lock status. |
| 123 | // |
| 124 | //! RETURNS: 0 - Not locked |
| 125 | //! RETURNS: 1 - Locked |
| 126 | //------------------------------------------------------ |
| 127 | int test() |
| 128 | { |
| 129 | return stateLock.test(); |
| 130 | } |
| 131 | |
| 132 | //--------------------------------------------------------- |
| 133 | //: Dump the mutex debug stuff and current state. |
| 134 | //--------------------------------------------------------- |
| 135 | void dump (FILE* dest = stderr, |
| 136 | const char* message = "\n------ Mutex Dump -----\n") const |
| 137 | { |
| 138 | stateLock.dump(); |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | vjMutex stateLock; //: Serialize access to internal state. |
| 143 | vjCond waitingReaders; //: Reader threads waiting to acquire the lock. |
| 144 | int numWaitingReaders; //: Number of waiting readers. |
| 145 | |
| 146 | vjCond waitingWriters; //: Writer threads waiting to acquire the lock. |
| 147 | int numWaitingWriters; //: Number of waiting writers. |
| 148 | |
| 149 | // Value is -1 if writer has the lock, else this keeps track of the |
| 150 | // number of readers holding the lock. |
| 151 | int refCount; |
| 152 | |
| 153 | // = Prevent assignment and initialization. |
| 154 | void operator= (const vjRWMutex &) {} |
| 155 | vjRWMutex (const vjRWMutex &) {} |
| 156 | }; |
| 157 | |
| 158 | #endif |
Note: See TracBrowser for help on using the browser.
