(file) Return to Mutex.cpp 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           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #include "Mutex.h"
 35 mike  1.4 #include "Time.h"
 36           #include "PegasusAssert.h"
 37           #include "Once.h"
 38 mike  1.2 
 39           PEGASUS_NAMESPACE_BEGIN
 40           
 41 mike  1.4 //==============================================================================
 42           //
 43           // PEGASUS_HAVE_PTHREADS
 44           //
 45           //==============================================================================
 46           
 47           #if defined(PEGASUS_HAVE_PTHREADS)
 48           
 49           static Once _once = PEGASUS_ONCE_INITIALIZER;
 50           static pthread_mutexattr_t _attr;
 51           
 52           static void _init_attr()
 53           {
 54               pthread_mutexattr_init(&_attr);
 55               pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
 56           }
 57           
 58           Mutex::Mutex()
 59           {
 60               once(&_once, _init_attr);
 61               pthread_mutex_init(&_rep.mutex, &_attr);
 62 mike  1.4 #if defined(PEGASUS_DEBUG)
 63               _rep.count = 0;
 64           #endif
 65           }
 66           
 67 mike  1.6 Mutex::Mutex(RecursiveTag)
 68           {
 69               once(&_once, _init_attr);
 70               pthread_mutex_init(&_rep.mutex, &_attr);
 71           #if defined(PEGASUS_DEBUG)
 72               _rep.count = 0;
 73           #endif
 74           }
 75           
 76           Mutex::Mutex(NonRecursiveTag)
 77           {
 78               pthread_mutex_init(&_rep.mutex, NULL);
 79           #if defined(PEGASUS_DEBUG)
 80               _rep.count = 0;
 81           #endif
 82           }
 83           
 84 mike  1.4 Mutex::~Mutex()
 85           {
 86               PEGASUS_DEBUG_ASSERT(_magic);
 87               pthread_mutex_destroy(&_rep.mutex);
 88           }
 89           
 90           void Mutex::lock()
 91           {
 92               PEGASUS_DEBUG_ASSERT(_magic);
 93           
 94               switch (pthread_mutex_lock(&_rep.mutex))
 95               {
 96                   case 0:
 97           #if defined(PEGASUS_DEBUG)
 98                       _rep.count++;
 99           #endif
100                       break;
101           
102                   default:
103                       throw WaitFailed(Threads::self());
104               }
105 mike  1.4 }
106           
107           void Mutex::try_lock()
108           {
109               PEGASUS_DEBUG_ASSERT(_magic);
110           
111 marek 1.5     int r = pthread_mutex_trylock(&_rep.mutex);
112               if (r == -1)
113                   r=errno;
114               switch (r)
115 mike  1.4     {
116                   case 0:
117           #if defined(PEGASUS_DEBUG)
118                       _rep.count++;
119           #endif
120                       break;
121           
122                   case EBUSY:
123                       throw AlreadyLocked(Threads::self());
124           
125                   default:
126                       throw WaitFailed(Threads::self());
127               }
128           }
129           
130 kumpf 1.11 Boolean Mutex::timed_lock(Uint32 milliseconds)
131 mike  1.4  {
132                PEGASUS_DEBUG_ASSERT(_magic);
133            
134                struct timeval now;
135                struct timeval finish;
136                struct timeval remaining;
137                {
138                    Uint32 usec;
139                    gettimeofday(&finish, NULL);
140                    finish.tv_sec += (milliseconds / 1000 );
141                    milliseconds %= 1000;
142                    usec = finish.tv_usec + ( milliseconds * 1000 );
143                    finish.tv_sec += (usec / 1000000);
144                    finish.tv_usec = usec % 1000000;
145                }
146            
147                for (;;)
148                {
149 marek 1.5          int r=pthread_mutex_trylock(&_rep.mutex);
150                    if (r == -1)
151                        r = errno;
152 kumpf 1.11 
153                    if (r == 0)
154                    {
155                        break;
156                    }
157                    else if (r == EBUSY)
158 mike  1.4          {
159 kumpf 1.11             gettimeofday(&now, NULL);
160 mike  1.4  
161 kumpf 1.11             if (Time::subtract(&remaining, &finish, &now))
162 mike  1.4              {
163 kumpf 1.11                 return false;
164 mike  1.4              }
165            
166 kumpf 1.11             Threads::yield();
167                    }
168                    else
169                    {
170                        throw WaitFailed(Threads::self());
171 mike  1.4          }
172                }
173 kumpf 1.11 
174            #if defined(PEGASUS_DEBUG)
175                _rep.count++;
176            #endif
177                return true;
178 mike  1.4  }
179            
180            void Mutex::unlock()
181            {
182                PEGASUS_DEBUG_ASSERT(_magic);
183                PEGASUS_DEBUG_ASSERT(_rep.count > 0);
184            
185            #if defined(PEGASUS_DEBUG)
186                _rep.count--;
187            #endif
188            
189                if (pthread_mutex_unlock(&_rep.mutex) != 0)
190                    throw Permission(Threads::self());
191            }
192            
193 thilo.boehm 1.10 #if defined(PEGASUS_OS_LINUX) || \
194                      (defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000))
195 kumpf       1.8  void Mutex::reinitialize()
196                  {
197                      pthread_mutex_init(&_rep.mutex, &_attr);
198                  #if defined(PEGASUS_DEBUG)
199                      _rep.count = 0;
200                  #endif
201                  }
202                  #endif
203                  
204 mike        1.4  #endif /* PEGASUS_HAVE_PTHREADS */
205                  
206                  //==============================================================================
207                  //
208                  // PEGASUS_HAVE_WINDOWS_THREADS
209                  //
210                  //==============================================================================
211                  
212                  #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
213                  
214 mike        1.7  static inline void _initialize(MutexRep& rep)
215                  {
216                      rep.handle = CreateMutex(NULL, FALSE, NULL);
217                      rep.count = 0;
218                  }
219                  
220 mike        1.4  Mutex::Mutex()
221                  {
222 mike        1.7      _initialize(_rep);
223                  }
224                  
225 mike        1.6  Mutex::Mutex(RecursiveTag)
226                  {
227 mike        1.7      _initialize(_rep);
228 mike        1.6  }
229                  
230                  Mutex::Mutex(NonRecursiveTag)
231                  {
232 mike        1.7      _initialize(_rep);
233 mike        1.4  }
234                  
235                  Mutex::~Mutex()
236                  {
237                      PEGASUS_DEBUG_ASSERT(_magic);
238                  
239                      WaitForSingleObject(_rep.handle, INFINITE);
240                      CloseHandle(_rep.handle);
241                  }
242                  
243                  void Mutex::lock()
244                  {
245                      PEGASUS_DEBUG_ASSERT(_magic);
246                  
247                      DWORD rc = WaitForSingleObject(_rep.handle, INFINITE);
248                  
249                      if (rc == WAIT_FAILED)
250                          throw WaitFailed(Threads::self());
251                  
252                      _rep.count++;
253                  }
254 mike        1.4  
255                  void Mutex::try_lock()
256                  {
257                      PEGASUS_DEBUG_ASSERT(_magic);
258                  
259                      DWORD rc = WaitForSingleObject(_rep.handle, 0);
260                  
261                      if (rc == WAIT_TIMEOUT)
262                          throw AlreadyLocked(Threads::self());
263                  
264                      if (rc == WAIT_FAILED)
265                          throw WaitFailed(Threads::self());
266                  
267                      _rep.count++;
268                  }
269                  
270 kumpf       1.11 Boolean Mutex::timed_lock(Uint32 milliseconds)
271 mike        1.4  {
272                      PEGASUS_DEBUG_ASSERT(_magic);
273                  
274                      DWORD rc = WaitForSingleObject(_rep.handle, milliseconds);
275                  
276                      if (rc == WAIT_TIMEOUT)
277 kumpf       1.11         return false;
278 mike        1.4  
279                      if (rc == WAIT_FAILED)
280                          throw WaitFailed(Threads::self());
281                  
282                      _rep.count++;
283 kumpf       1.11     return true;
284 mike        1.4  }
285                  
286                  void Mutex::unlock()
287                  {
288                      PEGASUS_DEBUG_ASSERT(_magic);
289                      PEGASUS_DEBUG_ASSERT(_rep.count > 0);
290                  
291                      _rep.count--;
292                      ReleaseMutex(_rep.handle);
293                  }
294                  
295                  #endif /* PEGASUS_HAVE_WINDOWS_THREADS */
296                  
297 mike        1.2  PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2