(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           // Author: Mike Day (mdday@us.ibm.com)
 33           //
 34           // Modified By: Markus Mueller
 35           //              Ramnath Ravindran (Ramnath.Ravindran@compaq.com)
 36           //              David Eger (dteger@us.ibm.com)
 37           //              Amit K Arora, IBM (amita@in.ibm.com) for PEP#101
 38           //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
 39           //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 40           //              David Dillard, VERITAS Software Corp.
 41           //                  (david.dillard@veritas.com)
 42 mike  1.2 //              Aruran, IBM (ashanmug@in.ibm.com) for BUG# 3518
 43           //
 44 mike  1.4.2.1 // Reworked By: Mike Brasher (m.brasher@inovadevelopment.com)
 45               //
 46 mike  1.2     //%/////////////////////////////////////////////////////////////////////////////
 47               
 48               #ifndef Pegasus_Mutex_h
 49               #define Pegasus_Mutex_h
 50               
 51               #include <Pegasus/Common/Config.h>
 52               #include <Pegasus/Common/Linkage.h>
 53 mike  1.4.2.1 #include <Pegasus/Common/IPCExceptions.h>
 54 mike  1.4     #include <Pegasus/Common/Magic.h>
 55 mike  1.4.2.1 #include <Pegasus/Common/Threads.h>
 56 mike  1.2     
 57               PEGASUS_NAMESPACE_BEGIN
 58               
 59 mike  1.4.2.1 //==============================================================================
 60               //
 61               // MutexRep
 62               //
 63               //==============================================================================
 64               
 65               #if defined(PEGASUS_HAVE_PTHREADS)
 66               struct MutexRep
 67               {
 68                   pthread_mutex_t mutex;
 69                   pthread_mutexattr_t attr;
 70                   pthread_t owner;
 71               };
 72               #endif
 73               
 74               #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
 75               struct MutexRep
 76               {
 77                   HANDLE handle;
 78                   HANDLE owner;
 79                   size_t count;
 80 mike  1.4.2.1 };
 81               #endif
 82               
 83               //==============================================================================
 84               //
 85               // Mutex
 86               //
 87               //==============================================================================
 88               
 89 mike  1.2     class PEGASUS_COMMON_LINKAGE Mutex
 90               {
 91               public:
 92               
 93                   Mutex();
 94               
 95                   ~Mutex();
 96               
 97 mike  1.4.2.1     void lock(ThreadType caller = Threads::self());
 98               
 99                   void try_lock(ThreadType caller = Threads::self());
100               
101                   void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self());
102 mike  1.2     
103                   void unlock();
104               
105 mike  1.4.2.1     ThreadType get_owner() { return _rep.owner; }
106               
107                   void set_owner(ThreadType caller) { _rep.owner = caller; }
108 mike  1.2     
109               private:
110 mike  1.4.2.1     Mutex(const Mutex&);
111                   Mutex& operator=(const Mutex&);
112               
113               
114                   MutexRep _rep;
115                   Magic<0x57D11485> _magic;
116               
117                   friend class Condition;
118               };
119               
120               //==============================================================================
121               //
122               // AutoMutex
123               //
124               //==============================================================================
125               
126               class PEGASUS_COMMON_LINKAGE AutoMutex
127               {
128               public:
129               
130                   AutoMutex(Mutex& mutex, Boolean autoLock = true) : 
131 mike  1.4.2.1 	_mutex(mutex), _locked(autoLock)
132                   {
133                       if (autoLock)
134                           _mutex.lock();
135                   }
136               
137                   ~AutoMutex()
138 mike  1.2         {
139 mike  1.4.2.1         try
140                       {
141               	    if (_locked)
142               		unlock();
143                       }
144                       catch (...)
145                       {
146                           // Do not propagate exception from destructor
147                       }
148 mike  1.2         }
149               
150 mike  1.4.2.1     void lock()
151                   {
152                       if (_locked)
153                           throw AlreadyLocked(Threads::self());
154 mike  1.2     
155 mike  1.4.2.1         _mutex.lock();
156                       _locked = true;
157                   }
158 mike  1.4     
159 mike  1.4.2.1     void unlock()
160                   {
161                       if (!_locked)
162                           throw Permission(Threads::self());
163               
164                       _mutex.unlock();
165                       _locked = false;
166                   }
167               
168                   Boolean isLocked() const
169                   {
170                       return _locked;
171                   }
172               
173               private:
174                   AutoMutex(); // Unimplemented
175                   AutoMutex(const AutoMutex& x); // Unimplemented
176                   AutoMutex& operator=(const AutoMutex& x); // Unimplemented
177               
178                   Mutex& _mutex;
179                   Boolean _locked;
180 mike  1.2     };
181               
182               PEGASUS_NAMESPACE_END
183               
184               #endif /* Pegasus_Mutex_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2