(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 marek 1.6.6.1 /**
 49                   Every platform should decide which implementation for read/write locks
 50                   should be used in OpenPegasus by defining one of the following:
 51                   1.) PEGASUS_USE_POSIX_RWLOCK - POSIX standard based implementation
 52                   2.) PEGASUS_USE_SEMAPHORE_RWLOCK - mutex based implementation
 53                
 54                   The definition for each platform can be found in the according platform
 55                   header file: pegasus/src/Pegasus/Common/Platform_<Platform>.h
 56               */
 57 mike  1.2     
 58 marek 1.6.6.1 #if !defined(PEGASUS_USE_POSIX_RWLOCK) && !defined(PEGASUS_USE_SEMAPHORE_RWLOCK)
 59               # error "Unsupported platform: ReadWriteSem.h implementation type missing"
 60 karl  1.6.6.2 
 61 mike  1.2     #endif
 62               
 63               //==============================================================================
 64               //
 65               // ReadWriteSemRep
 66               //
 67               //==============================================================================
 68               
 69               #ifdef PEGASUS_USE_POSIX_RWLOCK
 70               struct ReadWriteSemRep
 71               {
 72                   pthread_rwlock_t rwlock;
 73                   ThreadType owner;
 74               };
 75               #endif /* PEGASUS_USE_POSIX_RWLOCK */
 76               
 77               #ifdef PEGASUS_USE_SEMAPHORE_RWLOCK
 78               struct ReadWriteSemRep
 79               {
 80                   Semaphore _rlock;
 81                   Mutex _wlock;
 82 mike  1.2         Mutex _internal_lock;
 83                   ThreadType _owner;
 84 kumpf 1.3         ReadWriteSemRep() :
 85 mike  1.2             _rlock(10), _wlock(), _internal_lock(), _owner(Threads::self())
 86                   {
 87                   }
 88               };
 89               #endif /* PEGASUS_USE_POSIX_RWLOCK */
 90               
 91               //==============================================================================
 92               //
 93               // ReadWriteSem
 94               //
 95               //==============================================================================
 96               
 97               class PEGASUS_COMMON_LINKAGE ReadWriteSem
 98               {
 99               public:
100               
101                   ReadWriteSem();
102               
103                   ~ReadWriteSem();
104               
105                   // @exception Deadlock
106 mike  1.2         // @exception Permission
107                   // @exception WaitFailed
108                   inline void wait_read(ThreadType caller)
109                   {
110                       wait(PEG_SEM_READ, caller );
111                   }
112               
113                   // @exception Deadlock
114                   // @exception Permission
115                   // @exception WaitFailed
116                   inline void wait_write(ThreadType caller)
117                   {
118                       wait(PEG_SEM_WRITE, caller);
119                   }
120               
121                   // @exception Permission
122                   inline void unlock_read(ThreadType caller)
123                   {
124                       unlock(PEG_SEM_READ, caller);
125                   }
126               
127 mike  1.2         // @exception Permission
128                   inline void unlock_write(ThreadType caller)
129                   {
130                       unlock(PEG_SEM_WRITE, caller);
131                   }
132               
133                   int read_count() const;
134                   int write_count() const;
135               
136                   // @exception Deadlock
137                   // @exception Permission
138                   // @exception WaitFailed
139                   // @exception TooManyReaders
140                   void wait(Uint32 mode, ThreadType caller);
141               
142                   // @exception Permission
143                   void unlock(Uint32 mode, ThreadType caller);
144               
145               private:
146                   AtomicInt _readers;
147                   AtomicInt _writers;
148 mike  1.2         ReadWriteSemRep _rwlock;
149                   friend void extricate_read_write(void *);
150               };
151               
152               //==============================================================================
153               //
154               // ReadLock
155               //
156               //==============================================================================
157               
158               class ReadLock
159               {
160               public:
161               
162                   ReadLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
163                   {
164                       _rwsem.wait_read(Threads::self());
165                   }
166               
167                   ~ReadLock()
168                   {
169 mike  1.2             _rwsem.unlock_read(Threads::self());
170                   }
171               
172               private:
173                   ReadWriteSem & _rwsem;
174               };
175               
176               //==============================================================================
177               //
178               // WriteLock
179               //
180               //==============================================================================
181               
182               class WriteLock
183               {
184               public:
185               
186                   WriteLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
187                   {
188                       _rwsem.wait_write(Threads::self());
189                   }
190 mike  1.2     
191                   ~WriteLock()
192                   {
193                       _rwsem.unlock_write(Threads::self());
194                   }
195               
196               private:
197                   ReadWriteSem & _rwsem;
198               };
199               
200               PEGASUS_NAMESPACE_END
201               
202               #endif /* Pegasus_ReadWriteSem_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2