(file) Return to ReadWriteSem.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.2 //%2006////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11           // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13           //
 14           // Permission is hereby granted, free of charge, to any person obtaining a copy
 15           // of this software and associated documentation files (the "Software"), to
 16           // deal in the Software without restriction, including without limitation the
 17           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18           // sell copies of the Software, and to permit persons to whom the Software is
 19           // furnished to do so, subject to the following conditions:
 20           // 
 21           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29           //
 30           //==============================================================================
 31           //
 32           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #ifndef Pegasus_ReadWriteSem_h
 35           #define Pegasus_ReadWriteSem_h
 36           
 37           #include <Pegasus/Common/Config.h>
 38           #include <Pegasus/Common/Linkage.h>
 39           #include <Pegasus/Common/Semaphore.h>
 40           #include <Pegasus/Common/Mutex.h>
 41           #include <Pegasus/Common/AtomicInt.h>
 42           
 43 mike  1.2 #define PEG_SEM_READ 1
 44           #define PEG_SEM_WRITE 2
 45           
 46           PEGASUS_NAMESPACE_BEGIN
 47           
 48           //==============================================================================
 49           //
 50           // Select the read-write-lock implementation for your platform:
 51           //
 52           //     PEGASUS_USE_POSIX_RWLOCK
 53           //     PEGASUS_USE_SEMAPHORE_RWLOCK
 54           //
 55           //==============================================================================
 56           
 57           #if defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX)
 58           # define PEGASUS_USE_POSIX_RWLOCK
 59 ouyang.jian 1.5 #elif defined(PEGASUS_PLATFORM_PASE_ISERIES_IBMCXX)
 60                 # define PEGASUS_USE_POSIX_RWLOCK
 61 mike        1.2 #elif defined(PEGASUS_PLATFORM_HPUX_ACC)
 62                 # define PEGASUS_USE_POSIX_RWLOCK
 63 mike        1.6.2.3 #elif defined(PEGASUS_OS_SOLARIS)
 64 mike        1.6.2.1 # define PEGASUS_USE_POSIX_RWLOCK
 65 mike        1.2     #elif defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX)
 66                     # define PEGASUS_USE_POSIX_RWLOCK
 67                     #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
 68                     # define PEGASUS_USE_POSIX_RWLOCK
 69                     #elif defined(PEGASUS_PLATFORM_VMS_IA64_DECCXX)
 70                     # define PEGASUS_USE_POSIX_RWLOCK
 71                     #elif defined(PEGASUS_PLATFORM_VMS_ALPHA_DECCXX)
 72                     # define PEGASUS_USE_POSIX_RWLOCK
 73                     #elif defined(PEGASUS_PLATFORM_LINUX_X86_64_GNU)
 74                     # define PEGASUS_USE_POSIX_RWLOCK
 75                     #else
 76                     # define PEGASUS_USE_SEMAPHORE_RWLOCK
 77                     #endif
 78                     
 79                     //==============================================================================
 80                     //
 81                     // ReadWriteSemRep
 82                     //
 83                     //==============================================================================
 84                     
 85                     #ifdef PEGASUS_USE_POSIX_RWLOCK
 86 mike        1.2     struct ReadWriteSemRep
 87                     {
 88                         pthread_rwlock_t rwlock;
 89                         ThreadType owner;
 90                     };
 91                     #endif /* PEGASUS_USE_POSIX_RWLOCK */
 92                     
 93                     #ifdef PEGASUS_USE_SEMAPHORE_RWLOCK
 94                     struct ReadWriteSemRep
 95                     {
 96                         Semaphore _rlock;
 97                         Mutex _wlock;
 98                         Mutex _internal_lock;
 99                         ThreadType _owner;
100 kumpf       1.3         ReadWriteSemRep() :
101 mike        1.2             _rlock(10), _wlock(), _internal_lock(), _owner(Threads::self())
102                         {
103                         }
104                     };
105                     #endif /* PEGASUS_USE_POSIX_RWLOCK */
106                     
107                     //==============================================================================
108                     //
109                     // ReadWriteSem
110                     //
111                     //==============================================================================
112                     
113                     class PEGASUS_COMMON_LINKAGE ReadWriteSem
114                     {
115                     public:
116                     
117                         ReadWriteSem();
118                     
119                         ~ReadWriteSem();
120                     
121                         // @exception Deadlock
122 mike        1.2         // @exception Permission
123                         // @exception WaitFailed
124                         inline void wait_read(ThreadType caller)
125                         {
126                             wait(PEG_SEM_READ, caller );
127                         }
128                     
129                         // @exception Deadlock
130                         // @exception Permission
131                         // @exception WaitFailed
132                         inline void wait_write(ThreadType caller)
133                         {
134                             wait(PEG_SEM_WRITE, caller);
135                         }
136                     
137                         // @exception Permission
138                         inline void unlock_read(ThreadType caller)
139                         {
140                             unlock(PEG_SEM_READ, caller);
141                         }
142                     
143 mike        1.2         // @exception Permission
144                         inline void unlock_write(ThreadType caller)
145                         {
146                             unlock(PEG_SEM_WRITE, caller);
147                         }
148                     
149                         int read_count() const;
150                         int write_count() const;
151                     
152                         // @exception Deadlock
153                         // @exception Permission
154                         // @exception WaitFailed
155                         // @exception TooManyReaders
156                         void wait(Uint32 mode, ThreadType caller);
157                     
158                         // @exception Permission
159                         void unlock(Uint32 mode, ThreadType caller);
160                     
161                     private:
162                         AtomicInt _readers;
163                         AtomicInt _writers;
164 mike        1.2         ReadWriteSemRep _rwlock;
165                         friend void extricate_read_write(void *);
166                     };
167                     
168                     //==============================================================================
169                     //
170                     // ReadLock
171                     //
172                     //==============================================================================
173                     
174                     class ReadLock
175                     {
176                     public:
177                     
178                         ReadLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
179                         {
180                             _rwsem.wait_read(Threads::self());
181                         }
182                     
183                         ~ReadLock()
184                         {
185 mike        1.2             _rwsem.unlock_read(Threads::self());
186                         }
187                     
188                     private:
189                         ReadWriteSem & _rwsem;
190                     };
191                     
192                     //==============================================================================
193                     //
194                     // WriteLock
195                     //
196                     //==============================================================================
197                     
198                     class WriteLock
199                     {
200                     public:
201                     
202                         WriteLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
203                         {
204                             _rwsem.wait_write(Threads::self());
205                         }
206 mike        1.2     
207                         ~WriteLock()
208                         {
209                             _rwsem.unlock_write(Threads::self());
210                         }
211                     
212                     private:
213                         ReadWriteSem & _rwsem;
214                     };
215                     
216                     PEGASUS_NAMESPACE_END
217                     
218                     #endif /* Pegasus_ReadWriteSem_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2