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

  1 martin 1.14 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.15 //
  3 martin 1.14 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.15 //
 10 martin 1.14 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.15 //
 17 martin 1.14 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.15 //
 20 martin 1.14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.15 //
 28 martin 1.14 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32             #include "ReadWriteSem.h"
 33             #include "Time.h"
 34             #include "PegasusAssert.h"
 35             #include "Threads.h"
 36 kumpf  1.13 #include "Exception.h"
 37             #include "System.h"
 38 mike   1.2  
 39             PEGASUS_NAMESPACE_BEGIN
 40             
 41             //==============================================================================
 42             //
 43             // PEGASUS_USE_POSIX_RWLOCK
 44             //
 45             //==============================================================================
 46             
 47             #ifdef PEGASUS_USE_POSIX_RWLOCK
 48             
 49 kumpf  1.12 ReadWriteSem::ReadWriteSem()
 50 mike   1.2  {
 51                 pthread_rwlock_init(&_rwlock.rwlock, NULL);
 52             }
 53             
 54             ReadWriteSem::~ReadWriteSem()
 55             {
 56 kumpf  1.5      int r = 0;
 57 mike   1.6      while ((r = pthread_rwlock_destroy(&_rwlock.rwlock)) == EBUSY ||
 58 kumpf  1.5             (r == -1 && errno == EBUSY))
 59 mike   1.2      {
 60                     Threads::yield();
 61                 }
 62             }
 63             
 64 kumpf  1.12 void ReadWriteSem::waitRead()
 65 mike   1.2  {
 66 kumpf  1.13     int r = pthread_rwlock_rdlock(&_rwlock.rwlock);
 67             
 68                 if (r != 0)
 69 mike   1.2      {
 70 kumpf  1.13         if (r != -1)
 71                     {
 72                         // Special behavior for Single UNIX Specification, Version 3
 73                         errno = r;
 74                     }
 75             
 76                     throw Exception(MessageLoaderParms(
 77                         "Common.InternalException.READ_LOCK_FAILED",
 78                         "Failed to acquire read lock: $0",
 79                         PEGASUS_SYSTEM_ERRORMSG_NLS));
 80 mike   1.2      }
 81             }
 82             
 83 kumpf  1.12 void ReadWriteSem::waitWrite()
 84 mike   1.2  {
 85 kumpf  1.13     int r = pthread_rwlock_wrlock(&_rwlock.rwlock);
 86             
 87                 if (r != 0)
 88 mike   1.2      {
 89 kumpf  1.13         if (r != -1)
 90                     {
 91                         // Special behavior for Single UNIX Specification, Version 3
 92                         errno = r;
 93                     }
 94             
 95                     throw Exception(MessageLoaderParms(
 96                         "Common.InternalException.WRITE_LOCK_FAILED",
 97                         "Failed to acquire write lock: $0",
 98                         PEGASUS_SYSTEM_ERRORMSG_NLS));
 99 mike   1.2      }
100 kumpf  1.12 }
101 kumpf  1.10 
102 kumpf  1.12 void ReadWriteSem::unlockRead()
103             {
104 kumpf  1.10     // All documented error codes represent coding errors.
105 marek  1.16     PEGASUS_FCT_EXECUTE_AND_ASSERT(0, pthread_rwlock_unlock(&_rwlock.rwlock));
106 mike   1.2  }
107             
108 kumpf  1.12 void ReadWriteSem::unlockWrite()
109 mike   1.2  {
110 kumpf  1.12     // All documented error codes represent coding errors.
111 marek  1.16     PEGASUS_FCT_EXECUTE_AND_ASSERT(0, pthread_rwlock_unlock(&_rwlock.rwlock));
112 mike   1.2  }
113             
114             #endif /* PEGASUS_USE_POSIX_RWLOCK */
115             
116             //==============================================================================
117             //
118             // PEGASUS_USE_SEMAPHORE_RWLOCK
119             //
120             //==============================================================================
121             
122             #if defined(PEGASUS_USE_SEMAPHORE_RWLOCK)
123             
124             // // If i get cancelled, I MUST ensure:
125             // 1) I do not hold the internal mutex
126             // 2) I do not hold the write lock
127             // 3) I am not using a reader slot
128             
129 kumpf  1.12 ReadWriteSem::ReadWriteSem() : _rwlock()
130 mike   1.2  {
131             }
132             
133             ReadWriteSem::~ReadWriteSem()
134             {
135                 // lock everyone out of this object
136                 try
137                 {
138                     _rwlock._internal_lock.lock();
139                 }
140 kumpf  1.13     catch (...)
141 mike   1.2      {
142                     PEGASUS_ASSERT(0);
143                 }
144 kumpf  1.12     while (_rwlock._readers.get() > 0 || _rwlock._writers.get() > 0)
145 mike   1.2      {
146                     Threads::yield();
147                 }
148                 _rwlock._internal_lock.unlock();
149             }
150             
151 kumpf  1.12 void ReadWriteSem::waitRead()
152 mike   1.2  {
153 kumpf  1.12     // Lock the internal mutex to ensure only one waiter is processed at a time.
154                 AutoMutex lock(_rwlock._internal_lock);
155             
156                 // Wait for the existing writer (if any) to clear.
157                 while (_rwlock._writers.get() > 0)
158                 {
159                     Threads::yield();
160 mike   1.2      }
161 kumpf  1.12 
162                 // Wait for a reader slot to open up.
163                 _rwlock._rlock.wait();
164             
165                 // Increment the number of readers.
166                 _rwlock._readers++;
167             }
168             
169             void ReadWriteSem::waitWrite()
170             {
171                 // Lock the internal mutex to ensure only one waiter is processed at a time.
172                 AutoMutex lock(_rwlock._internal_lock);
173             
174                 // Allow all the readers to exit.
175                 while (_rwlock._readers.get() > 0)
176                 {
177                     Threads::yield();
178                 }
179             
180                 // Obtain the write mutex.
181                 _rwlock._wlock.lock();
182 kumpf  1.12 
183                 // Set the writer count to one.
184                 _rwlock._writers = 1;
185 mike   1.2  }
186             
187 kumpf  1.12 void ReadWriteSem::unlockRead()
188 mike   1.2  {
189 kumpf  1.12     PEGASUS_ASSERT(_rwlock._readers.get() > 0);
190                 _rwlock._readers--;
191                 _rwlock._rlock.signal();
192 mike   1.2  }
193             
194 kumpf  1.12 void ReadWriteSem::unlockWrite()
195 mike   1.2  {
196 kumpf  1.12     PEGASUS_ASSERT(_rwlock._writers.get() == 1);
197                 _rwlock._writers = 0;
198                 _rwlock._wlock.unlock();
199 mike   1.2  }
200             
201             #endif /* !PEGASUS_USE_SEMAPHORE_RWLOCK */
202             
203             PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2