(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.9 and 1.15.8.1

version 1.9, 2008/08/15 23:14:01 version 1.15.8.1, 2013/06/03 22:35:13
Line 1 
Line 1 
 //%2006////////////////////////////////////////////////////////////////////////  //%LICENSE////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development  // Licensed to The Open Group (TOG) under one or more contributor license
 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.  // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;  // this work for additional information regarding copyright ownership.
 // IBM Corp.; EMC Corporation, The Open Group.  // Each contributor licenses this file to you under the OpenPegasus Open
 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;  // Source License; you may not use this file except in compliance with the
 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.  // License.
 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;  //
 // EMC Corporation; VERITAS Software Corporation; The Open Group.  // Permission is hereby granted, free of charge, to any person obtaining a
 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;  // copy of this software and associated documentation files (the "Software"),
 // EMC Corporation; Symantec Corporation; The Open Group.  // to deal in the Software without restriction, including without limitation
 //  // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 // Permission is hereby granted, free of charge, to any person obtaining a copy  // and/or sell copies of the Software, and to permit persons to whom the
 // of this software and associated documentation files (the "Software"), to  // Software is furnished to do so, subject to the following conditions:
 // deal in the Software without restriction, including without limitation the  //
 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  // The above copyright notice and this permission notice shall be included
 // sell copies of the Software, and to permit persons to whom the Software is  // in all copies or substantial portions of the Software.
 // furnished to do so, subject to the following conditions:  //
 //  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN  // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR  // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT  // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN  // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
 // //
 //==============================================================================  //////////////////////////////////////////////////////////////////////////
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
Line 35 
Line 33 
 #include "Time.h" #include "Time.h"
 #include "PegasusAssert.h" #include "PegasusAssert.h"
 #include "Threads.h" #include "Threads.h"
   #include "Exception.h"
   #include "System.h"
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
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);
     Threads::clear(_rwlock.owner);  
 } }
  
 ReadWriteSem::~ReadWriteSem() ReadWriteSem::~ReadWriteSem()
Line 62 
Line 61 
     }     }
 } }
  
 void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller)  void ReadWriteSem::waitRead()
 {  
     if (writeLock)  
     {  
         if (0 == pthread_rwlock_wrlock(&_rwlock.rwlock))  
         {         {
             _rwlock.owner = caller;      int r = pthread_rwlock_rdlock(&_rwlock.rwlock);
             _writers++;  
             return;      if (r != 0)
         }  
     }  
     else  
     {     {
         if (0 == pthread_rwlock_rdlock(&_rwlock.rwlock))          if (r != -1)
         {         {
             _readers++;              // Special behavior for Single UNIX Specification, Version 3
             return;              errno = r;
         }  
     }     }
  
     throw WaitFailed(Threads::self());          throw Exception(MessageLoaderParms(
               "Common.InternalException.READ_LOCK_FAILED",
               "Failed to acquire read lock: $0",
               PEGASUS_SYSTEM_ERRORMSG_NLS));
       }
 } }
  
 void ReadWriteSem::_unlock(Boolean writeLock, ThreadType caller)  void ReadWriteSem::waitWrite()
 { {
     ThreadType owner;      int r = pthread_rwlock_wrlock(&_rwlock.rwlock);
  
     if (writeLock)      if (r != 0)
     {     {
         owner = _rwlock.owner;          if (r != -1)
         Threads::clear(_rwlock.owner);  
     }  
     if (0 != pthread_rwlock_unlock(&_rwlock.rwlock))  
     {     {
         _rwlock.owner = owner;              // Special behavior for Single UNIX Specification, Version 3
         throw(Permission(Threads::self()));              errno = r;
           }
   
           throw Exception(MessageLoaderParms(
               "Common.InternalException.WRITE_LOCK_FAILED",
               "Failed to acquire write lock: $0",
               PEGASUS_SYSTEM_ERRORMSG_NLS));
     }     }
     if (!writeLock && _readers.get() != 0)  
         _readers--;  
     else if (_writers.get() != 0)  
         _writers--;  
 } }
  
 int ReadWriteSem::read_count() const  void ReadWriteSem::unlockRead()
 { {
     return _readers.get();      // All documented error codes represent coding errors.
       PEGASUS_FCT_EXECUTE_AND_ASSERT(0, pthread_rwlock_unlock(&_rwlock.rwlock));
 } }
  
 int ReadWriteSem::write_count() const  void ReadWriteSem::unlockWrite()
 { {
     return _writers.get();      // All documented error codes represent coding errors.
       PEGASUS_FCT_EXECUTE_AND_ASSERT(0, pthread_rwlock_unlock(&_rwlock.rwlock));
 } }
  
 #endif /* PEGASUS_USE_POSIX_RWLOCK */ #endif /* PEGASUS_USE_POSIX_RWLOCK */
Line 130 
Line 126 
 // 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 137 
     {     {
         _rwlock._internal_lock.lock();         _rwlock._internal_lock.lock();
     }     }
     catch (IPCException &)      catch (...)
     {     {
         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&)  
         {      // Wait for a reader slot to open up.
             _rwlock._internal_lock.unlock();  
             throw;  
         }  
 //-----------------------------------------------------------------  
 // Write Lock Step 3: set the writer count to one, unlock the object  
 //   There are no readers and we are the only writer !  
 //-----------------------------------------------------------------  
         _writers = 1;  
         // set the owner  
         _rwlock._owner = Threads::self();  
         // 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 && _writers.get() != 0)  
     {     {
         _writers = 0;      // Lock the internal mutex to ensure only one waiter is processed at a time.
         _rwlock._wlock.unlock();      AutoMutex lock(_rwlock._internal_lock);
     }  
     else if (_readers.get() != 0)      // Allow all the readers to exit.
       while (_rwlock._readers.get() > 0)
     {     {
         _readers--;          Threads::yield();
         _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.9  
changed lines
  Added in v.1.15.8.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2