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

Diff for /pegasus/src/Pegasus/Common/ReadWriteSem.cpp between version 1.11 and 1.12

version 1.11, 2008/09/05 20:37:24 version 1.12, 2008/09/09 17:38:27
Line 46 
Line 46 
  
 #ifdef PEGASUS_USE_POSIX_RWLOCK #ifdef PEGASUS_USE_POSIX_RWLOCK
  
 ReadWriteSem::ReadWriteSem():_readers(0), _writers(0)  ReadWriteSem::ReadWriteSem()
 { {
     pthread_rwlock_init(&_rwlock.rwlock, NULL);     pthread_rwlock_init(&_rwlock.rwlock, NULL);
 } }
Line 61 
Line 61 
     }     }
 } }
  
 void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller)  void ReadWriteSem::waitRead()
 { {
     if (writeLock)      if (pthread_rwlock_rdlock(&_rwlock.rwlock) != 0)
     {     {
         if (0 == pthread_rwlock_wrlock(&_rwlock.rwlock))  
         {  
             _writers++;  
             return;  
         }  
     }  
     else  
     {  
         if (0 == pthread_rwlock_rdlock(&_rwlock.rwlock))  
         {  
             _readers++;  
             return;  
         }  
     }  
   
     throw WaitFailed(Threads::self());     throw WaitFailed(Threads::self());
 } }
   }
  
 void ReadWriteSem::_unlock(Boolean writeLock, ThreadType caller)  void ReadWriteSem::waitWrite()
 { {
     if (writeLock)      if (pthread_rwlock_wrlock(&_rwlock.rwlock) != 0)
     {     {
         PEGASUS_ASSERT(_writers.get() == 1);          throw WaitFailed(Threads::self());
         _writers = 0;  
     }     }
     else  
     {  
         PEGASUS_ASSERT(_readers.get() > 0);  
         _readers--;  
     }     }
  
   void ReadWriteSem::unlockRead()
   {
     int rc = pthread_rwlock_unlock(&_rwlock.rwlock);     int rc = pthread_rwlock_unlock(&_rwlock.rwlock);
     // All documented error codes represent coding errors.     // All documented error codes represent coding errors.
     PEGASUS_ASSERT(rc == 0);     PEGASUS_ASSERT(rc == 0);
 } }
  
 int ReadWriteSem::read_count() const  void ReadWriteSem::unlockWrite()
 { {
     return _readers.get();      int rc = pthread_rwlock_unlock(&_rwlock.rwlock);
 }      // All documented error codes represent coding errors.
       PEGASUS_ASSERT(rc == 0);
 int ReadWriteSem::write_count() const  
 {  
     return _writers.get();  
 } }
  
 #endif /* PEGASUS_USE_POSIX_RWLOCK */ #endif /* PEGASUS_USE_POSIX_RWLOCK */
Line 126 
Line 106 
 // 2) I do not hold the write lock // 2) I do not hold the write lock
 // 3) I am not using a reader slot // 3) I am not using a reader slot
  
 ReadWriteSem::ReadWriteSem():_readers(0), _writers(0), _rwlock()  ReadWriteSem::ReadWriteSem() : _rwlock()
 { {
 } }
  
Line 141 
Line 121 
     {     {
         PEGASUS_ASSERT(0);         PEGASUS_ASSERT(0);
     }     }
     while (_readers.get() > 0 || _writers.get() > 0)      while (_rwlock._readers.get() > 0 || _rwlock._writers.get() > 0)
     {     {
         Threads::yield();         Threads::yield();
     }     }
     _rwlock._internal_lock.unlock();     _rwlock._internal_lock.unlock();
 } }
  
 //---------------------------------------------------------------------  void ReadWriteSem::waitRead()
 void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller)  
 { {
 //-----------------------------------------------------------------      // Lock the internal mutex to ensure only one waiter is processed at a time.
 // Lock this object to maintain integrity while we decide      AutoMutex lock(_rwlock._internal_lock);
 // exactly what to do next.  
 //-----------------------------------------------------------------  
     _rwlock._internal_lock.lock();  
  
     if (writeLock)      // Wait for the existing writer (if any) to clear.
       while (_rwlock._writers.get() > 0)
     {     {
 //-----------------------------------------------------------------  
 // Write Lock Step 1: lock the object and allow all the readers to exit  
 //-----------------------------------------------------------------  
         while (_readers.get() > 0)  
             Threads::yield();             Threads::yield();
 //-----------------------------------------------------------------  
 // Write Lock Step 2: Obtain the Write Mutex  
 //  Although there are no readers, there may be a writer  
 //-----------------------------------------------------------------  
         try  
         {  
             _rwlock._wlock.lock();  
         }  
         catch (const IPCException&)  
         {  
             _rwlock._internal_lock.unlock();  
             throw;  
         }         }
 //-----------------------------------------------------------------  
 // Write Lock Step 3: set the writer count to one, unlock the object      // Wait for a reader slot to open up.
 //   There are no readers and we are the only writer !  
 //-----------------------------------------------------------------  
         _writers = 1;  
         // unlock the object  
         _rwlock._internal_lock.unlock();  
     }  
     else  
     {  
 //-----------------------------------------------------------------  
 // Read Lock Step 1: Wait for the existing writer (if any) to clear  
 //-----------------------------------------------------------------  
         while (_writers.get() > 0)  
             Threads::yield();  
 //-----------------------------------------------------------------  
 // Read Lock Step 2: wait for a reader slot to open up, then return  
 //  At this point there are no writers, but there may be too many  
 //  readers.  
 //-----------------------------------------------------------------  
         try  
         {  
             _rwlock._rlock.wait();             _rwlock._rlock.wait();
         }  
         catch (const IPCException&)      // Increment the number of readers.
         {      _rwlock._readers++;
             _rwlock._internal_lock.unlock();  
             throw;  
         }  
 //-----------------------------------------------------------------  
 // Read Lock Step 3: increment the number of readers, unlock the object,  
 // return  
 //-----------------------------------------------------------------  
         _readers++;  
         _rwlock._internal_lock.unlock();  
     }  
 } }
  
 void ReadWriteSem::_unlock(Boolean writeLock, ThreadType caller)  void ReadWriteSem::waitWrite()
 {  
     if (writeLock)  
     {     {
         PEGASUS_ASSERT(_writers.get() == 1);      // Lock the internal mutex to ensure only one waiter is processed at a time.
         _writers = 0;      AutoMutex lock(_rwlock._internal_lock);
         _rwlock._wlock.unlock();  
     }      // Allow all the readers to exit.
     else      while (_rwlock._readers.get() > 0)
     {     {
         PEGASUS_ASSERT(_readers.get() > 0);          Threads::yield();
         _readers--;  
         _rwlock._rlock.signal();  
     }     }
   
       // Obtain the write mutex.
       _rwlock._wlock.lock();
   
       // Set the writer count to one.
       _rwlock._writers = 1;
 } }
  
 int ReadWriteSem::read_count() const  void ReadWriteSem::unlockRead()
 { {
     return _readers.get();      PEGASUS_ASSERT(_rwlock._readers.get() > 0);
       _rwlock._readers--;
       _rwlock._rlock.signal();
 } }
  
 int ReadWriteSem::write_count() const  void ReadWriteSem::unlockWrite()
 { {
     return _writers.get();      PEGASUS_ASSERT(_rwlock._writers.get() == 1);
       _rwlock._writers = 0;
       _rwlock._wlock.unlock();
 } }
  
 #endif /* !PEGASUS_USE_SEMAPHORE_RWLOCK */ #endif /* !PEGASUS_USE_SEMAPHORE_RWLOCK */


Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2