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

  1 karl  1.3 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.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 karl  1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.2 //
 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 karl  1.3 // 
 21 mike  1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22           // 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 mike  1.4.2.4 // Author: Mike Brasher (m.brasher@inovadevelopment.com)
 33 mike  1.4.2.1 //
 34 mike  1.2     //%/////////////////////////////////////////////////////////////////////////////
 35               
 36               #ifndef Pegasus_Mutex_h
 37               #define Pegasus_Mutex_h
 38               
 39               #include <Pegasus/Common/Config.h>
 40               #include <Pegasus/Common/Linkage.h>
 41 mike  1.4.2.1 #include <Pegasus/Common/IPCExceptions.h>
 42 mike  1.4     #include <Pegasus/Common/Magic.h>
 43 mike  1.4.2.1 #include <Pegasus/Common/Threads.h>
 44 mike  1.2     
 45               PEGASUS_NAMESPACE_BEGIN
 46               
 47 mike  1.4.2.1 //==============================================================================
 48               //
 49               // MutexRep
 50               //
 51               //==============================================================================
 52               
 53               #if defined(PEGASUS_HAVE_PTHREADS)
 54 mike  1.4.2.2 typedef pthread_mutex_t MutexType;
 55 mike  1.4.2.1 struct MutexRep
 56               {
 57                   pthread_mutex_t mutex;
 58                   pthread_mutexattr_t attr;
 59               };
 60 mike  1.4.2.2 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
 61               inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
 62               # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
 63 mike  1.4.2.1 #endif
 64               
 65               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
 66 mike  1.4.2.2 typedef HANDLE MutexType;
 67 mike  1.4.2.1 struct MutexRep
 68               {
 69 mike  1.4.2.3     MutexType handle;
 70 mike  1.4.2.1     size_t count;
 71               };
 72 mike  1.4.2.3 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
 73               inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
 74               # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
 75 mike  1.4.2.1 #endif
 76               
 77               //==============================================================================
 78               //
 79               // Mutex
 80               //
 81               //==============================================================================
 82               
 83 mike  1.2     class PEGASUS_COMMON_LINKAGE Mutex
 84               {
 85               public:
 86               
 87                   Mutex();
 88               
 89                   ~Mutex();
 90               
 91 mike  1.4.2.4     void lock();
 92 mike  1.4.2.1 
 93 mike  1.4.2.4     void try_lock();
 94 mike  1.4.2.1 
 95 mike  1.4.2.4     void timed_lock(Uint32 milliseconds);
 96 mike  1.2     
 97                   void unlock();
 98               
 99               private:
100 mike  1.4.2.1     Mutex(const Mutex&);
101                   Mutex& operator=(const Mutex&);
102               
103               
104                   MutexRep _rep;
105                   Magic<0x57D11485> _magic;
106               
107                   friend class Condition;
108               };
109               
110               //==============================================================================
111               //
112               // AutoMutex
113               //
114               //==============================================================================
115               
116               class PEGASUS_COMMON_LINKAGE AutoMutex
117               {
118               public:
119               
120                   AutoMutex(Mutex& mutex, Boolean autoLock = true) : 
121 mike  1.4.2.1 	_mutex(mutex), _locked(autoLock)
122                   {
123                       if (autoLock)
124                           _mutex.lock();
125                   }
126               
127                   ~AutoMutex()
128 mike  1.2         {
129 mike  1.4.2.1         try
130                       {
131               	    if (_locked)
132               		unlock();
133                       }
134                       catch (...)
135                       {
136                           // Do not propagate exception from destructor
137                       }
138 mike  1.2         }
139               
140 mike  1.4.2.1     void lock()
141                   {
142                       if (_locked)
143 mike  1.4.2.4             throw AlreadyLocked(ThreadType());
144 mike  1.2     
145 mike  1.4.2.1         _mutex.lock();
146                       _locked = true;
147                   }
148 mike  1.4     
149 mike  1.4.2.1     void unlock()
150                   {
151                       if (!_locked)
152 mike  1.4.2.4             throw Permission(ThreadType());
153 mike  1.4.2.1 
154                       _mutex.unlock();
155                       _locked = false;
156                   }
157               
158                   Boolean isLocked() const
159                   {
160                       return _locked;
161                   }
162               
163               private:
164                   AutoMutex(); // Unimplemented
165                   AutoMutex(const AutoMutex& x); // Unimplemented
166                   AutoMutex& operator=(const AutoMutex& x); // Unimplemented
167               
168                   Mutex& _mutex;
169                   Boolean _locked;
170 mike  1.2     };
171               
172               PEGASUS_NAMESPACE_END
173               
174               #endif /* Pegasus_Mutex_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2