(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.5.4.1 and 1.8

version 1.5.4.1, 2008/02/21 06:33:19 version 1.8, 2008/08/14 17:44:27
Line 62 
Line 62 
     }     }
 } }
  
 void ReadWriteSem::wait(Uint32 mode, ThreadType caller)  void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller)
 { {
     int errorcode;     int errorcode;
     if (mode == PEG_SEM_READ)      if (writeLock)
     {  
         if (0 == (errorcode = pthread_rwlock_rdlock(&_rwlock.rwlock)))  
         {  
             _readers++;  
             return;  
         }  
     }  
     else if (mode == PEG_SEM_WRITE)  
     {     {
         if (0 == (errorcode = pthread_rwlock_wrlock(&_rwlock.rwlock)))         if (0 == (errorcode = pthread_rwlock_wrlock(&_rwlock.rwlock)))
         {         {
Line 83 
Line 75 
         }         }
     }     }
     else     else
         throw(Permission(Threads::self()));  
   
     if (errorcode == EDEADLK)  
         throw(Deadlock(_rwlock.owner));  
     else  
         throw(WaitFailed(Threads::self()));  
 }  
   
 void ReadWriteSem::try_wait(Uint32 mode, ThreadType caller)  
 {  
     int errorcode = 0;  
     if (mode == PEG_SEM_READ)  
     {     {
         if (0 == (errorcode = pthread_rwlock_tryrdlock(&_rwlock.rwlock)))          if (0 == (errorcode = pthread_rwlock_rdlock(&_rwlock.rwlock)))
         {  
             _readers++;  
             return;  
         }  
     }  
     else if (mode == PEG_SEM_WRITE)  
     {  
         if (0 == (errorcode = pthread_rwlock_trywrlock(&_rwlock.rwlock)))  
         {  
             _writers++;  
             _rwlock.owner = caller;  
             return;  
         }  
     }  
     else  
         throw(Permission(Threads::self()));  
     if (errorcode == -1)  
         errorcode = errno;  
     if (errorcode == EBUSY)  
         throw(AlreadyLocked(_rwlock.owner));  
     else if (errorcode == EDEADLK)  
         throw(Deadlock(_rwlock.owner));  
     else  
         throw(WaitFailed(Threads::self()));  
 }  
   
   
 // timedrdlock and timedwrlock are not supported on HPUX  
 // mdday Sun Aug  5 14:21:00 2001  
 void ReadWriteSem::timed_wait(Uint32 mode,  
                               ThreadType caller, int milliseconds)  
 {  
     int errorcode = 0, timeout = 0;  
     struct timeval now, finish, remaining;  
     Uint32 usec;  
   
     gettimeofday(&finish, NULL);  
     finish.tv_sec += (milliseconds / 1000);  
     milliseconds %= 1000;  
     usec = finish.tv_usec + (milliseconds * 1000);  
     finish.tv_sec += (usec / 1000000);  
     finish.tv_usec = usec % 1000000;  
   
     if (mode == PEG_SEM_READ)  
     {  
         do  
         {  
             errorcode = pthread_rwlock_tryrdlock(&_rwlock.rwlock);  
             if (errorcode == -1)  
                 errorcode = errno;  
             gettimeofday(&now, NULL);  
         }  
         while (errorcode == EBUSY &&  
                (0 == (timeout = Time::subtract(&remaining, &finish, &now))));  
         if (0 == errorcode)  
         {         {
             _readers++;             _readers++;
             return;             return;
         }         }
     }     }
     else if (mode == PEG_SEM_WRITE)  
     {  
         do  
         {  
             errorcode = pthread_rwlock_trywrlock(&_rwlock.rwlock);  
             if (errorcode == -1)  
                 errorcode = errno;  
             gettimeofday(&now, NULL);  
         }  
         while (errorcode == EBUSY &&  
                (0 == (timeout = Time::subtract(&remaining, &finish, &now))));  
  
         if (0 == errorcode)      if (errorcode == EDEADLK)
         {  
             _writers++;  
             _rwlock.owner = caller;  
             return;  
         }  
     }  
     else  
         throw(Permission(Threads::self()));  
     if (timeout != 0)  
         throw(TimeOut(_rwlock.owner));  
     else if (errorcode == EDEADLK)  
         throw(Deadlock(_rwlock.owner));         throw(Deadlock(_rwlock.owner));
     else     else
         throw(WaitFailed(Threads::self()));         throw(WaitFailed(Threads::self()));
 } }
  
 void ReadWriteSem::unlock(Uint32 mode, ThreadType caller)  void ReadWriteSem::_unlock(Boolean writeLock, ThreadType caller)
 { {
     ThreadType owner;     ThreadType owner;
  
     if (mode == PEG_SEM_WRITE)      if (writeLock)
     {     {
         owner = _rwlock.owner;         owner = _rwlock.owner;
         Threads::clear(_rwlock.owner);         Threads::clear(_rwlock.owner);
Line 200 
Line 103 
         _rwlock.owner = owner;         _rwlock.owner = owner;
         throw(Permission(Threads::self()));         throw(Permission(Threads::self()));
     }     }
     if (mode == PEG_SEM_READ && _readers.get() != 0)      if (!writeLock && _readers.get() != 0)
         _readers--;         _readers--;
     else if (_writers.get() != 0)     else if (_writers.get() != 0)
         _writers--;         _writers--;
Line 231 
Line 134 
 // 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
  
 #if 0  
 void extricate_read_write(void *parm)  
 {  
     ReadWriteSem *rws = (ReadWriteSem *) parm;  
     ThreadType myself = Threads::self();  
   
     if (Threads::equal(rws->_rwlock._wlock.get_owner(), myself))  
         rws->_rwlock._wlock.unlock();  
     else if (rws->_readers.get() > 0)  
         rws->_rwlock._rlock.signal();  
   
     if (Threads::equal(rws->_rwlock._internal_lock.get_owner(), myself))  
         rws->_rwlock._internal_lock.unlock();  
 }  
 #endif  
   
   
 ReadWriteSem::ReadWriteSem():_readers(0), _writers(0), _rwlock() ReadWriteSem::ReadWriteSem():_readers(0), _writers(0), _rwlock()
 { {
 } }
Line 274 
Line 160 
     _rwlock._internal_lock.unlock();     _rwlock._internal_lock.unlock();
 } }
  
   //---------------------------------------------------------------------
   void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller)
   
   
   
 //-----------------------------------------------------------------  
 // if milliseconds == -1, wait indefinately  
 // if milliseconds == 0, fast wait  
 //-----------------------------------------------------------------  
 void ReadWriteSem::timed_wait(Uint32 mode, ThreadType caller,  
                               int milliseconds)  
 { {
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Lock this object to maintain integrity while we decide // Lock this object to maintain integrity while we decide
 // exactly what to do next. // exactly what to do next.
 //----------------------------------------------------------------- //-----------------------------------------------------------------
     // AutoPtr<IPCException> caught;  
     // IPCException caught((ThreadType)0);  
     // WaitFailed caughtWaitFailed((ThreadType)0);  
     // TimeOut caughtTimeOut((ThreadType)0);  
     // TooManyReaders caughtTooManyReaders((ThreadType)0);  
   
     ThreadType zero;  
     IPCException caught(zero);  
     WaitFailed caughtWaitFailed(zero);  
     TimeOut caughtTimeOut(zero);  
     TooManyReaders caughtTooManyReaders(zero);  
   
     // cleanup stack frame  
     {  
         // Threads::cleanup_push(extricate_read_write, this);  
   
         try  
         {  
             if (milliseconds == 0)  
                 _rwlock._internal_lock.try_lock();  
             else if (milliseconds == -1)  
                 _rwlock._internal_lock.lock();                 _rwlock._internal_lock.lock();
             else  
                 _rwlock._internal_lock.timed_lock(milliseconds);  
         }  
         catch (const IPCException & e)  
         {  
             caught = e;  
             goto throw_from_here;  
         }  
  
         if (mode == PEG_SEM_WRITE)      if (writeLock)
         {         {
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Write Lock Step 1: lock the object and allow all the readers to exit // Write Lock Step 1: lock the object and allow all the readers to exit
 //----------------------------------------------------------------- //-----------------------------------------------------------------
   
   
             if (milliseconds == 0)      // fast wait  
             {  
                 if (_readers.get() > 0)  
                 {  
                     _rwlock._internal_lock.unlock();  
                     // caught.reset(new WaitFailed(Threads::self()));  
                     caughtWaitFailed = WaitFailed(Threads::self());  
                     goto throw_from_here;  
                 }  
             }  
             else if (milliseconds == -1)        // infinite wait  
             {  
                 while (_readers.get() > 0)                 while (_readers.get() > 0)
                     Threads::yield();                     Threads::yield();
             }  
             else                // timed wait  
             {  
                 struct timeval start, now;  
                 Time::gettimeofday(&start);  
                 start.tv_usec += (1000 * milliseconds);  
                 while (_readers.get() > 0)  
                 {  
                     Time::gettimeofday(&now);  
                     if ((now.tv_usec > start.tv_usec) ||  
                         now.tv_sec > start.tv_sec)  
                     {  
                         _rwlock._internal_lock.unlock();  
                         // caught.reset(new TimeOut(Threads::self()));  
                         caughtTimeOut = TimeOut(Threads::self());  
                         goto throw_from_here;  
                     }  
                     Threads::yield();  
                 }  
             }  
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Write Lock Step 2: Obtain the Write Mutex // Write Lock Step 2: Obtain the Write Mutex
 //  Although there are no readers, there may be a writer //  Although there are no readers, there may be a writer
 //----------------------------------------------------------------- //-----------------------------------------------------------------
             if (milliseconds == 0)      // fast wait  
             {  
                 try  
                 {  
                     _rwlock._wlock.try_lock();  
                 }  
                 catch (IPCException & e)  
                 {  
                     _rwlock._internal_lock.unlock();  
                     caught = e;  
                     goto throw_from_here;  
                 }  
             }  
             else if (milliseconds == -1)        // infinite wait  
             {  
                 try                 try
                 {                 {
                     _rwlock._wlock.lock();                     _rwlock._wlock.lock();
                 }                 }
                 catch (const IPCException & e)          catch (const IPCException&)
                 {  
                     _rwlock._internal_lock.unlock();  
                     caught = e;  
                     goto throw_from_here;  
                 }  
             }  
             else                // timed wait  
             {  
                 try  
                 {  
                     _rwlock._wlock.timed_lock(milliseconds);  
                 }  
                 catch (const IPCException & e)  
                 {                 {
                     _rwlock._internal_lock.unlock();                     _rwlock._internal_lock.unlock();
                     caught = e;              throw;
                     goto throw_from_here;  
                 }                 }
             }  
   
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Write Lock Step 3: set the writer count to one, unlock the object // Write Lock Step 3: set the writer count to one, unlock the object
 //   There are no readers and we are the only writer ! //   There are no readers and we are the only writer !
Line 415 
Line 198 
             _rwlock._owner = Threads::self();             _rwlock._owner = Threads::self();
             // unlock the object             // unlock the object
             _rwlock._internal_lock.unlock();             _rwlock._internal_lock.unlock();
         }                       // PEG_SEM_WRITE      }
         else         else
         {         {
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Read Lock Step 1: Wait for the existing writer (if any) to clear // Read Lock Step 1: Wait for the existing writer (if any) to clear
 //----------------------------------------------------------------- //-----------------------------------------------------------------
             if (milliseconds == 0)      // fast wait  
             {  
                 if (_writers.get() > 0)  
                 {  
                     _rwlock._internal_lock.unlock();  
                     // caught.reset(new WaitFailed(Threads::self()));  
                     caughtWaitFailed = WaitFailed(Threads::self());  
                     goto throw_from_here;  
                 }  
             }  
             else if (milliseconds == -1)        // infinite wait  
             {  
                 while (_writers.get() > 0)                 while (_writers.get() > 0)
                     Threads::yield();                     Threads::yield();
             }  
             else                // timed wait  
             {  
                 struct timeval start, now;  
                 Time::gettimeofday(&start);  
                 start.tv_usec += (milliseconds * 1000);  
   
                 while (_writers.get() > 0)  
                 {  
                     Time::gettimeofday(&now);  
                     if ((now.tv_usec > start.tv_usec) ||  
                         (now.tv_sec > start.tv_sec))  
                     {  
                         _rwlock._internal_lock.unlock();  
                         // caught.reset(new TimeOut(Threads::self()));  
                         caughtTimeOut = TimeOut(Threads::self());  
                         goto throw_from_here;  
                     }  
                     Threads::yield();  
                     Time::gettimeofday(&now);  
                 }  
             }  
   
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Read Lock Step 2: wait for a reader slot to open up, then return // 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 //  At this point there are no writers, but there may be too many
 //  readers. //  readers.
 //----------------------------------------------------------------- //-----------------------------------------------------------------
             if (milliseconds == 0)      // fast wait  
             {  
                 try  
                 {  
                     _rwlock._rlock.try_wait();  
                 }  
                 catch (const IPCException &)  
                 {  
                     // the wait failed, there must be too many readers  
                     // already.  
                     // unlock the object  
                     caughtTooManyReaders = TooManyReaders(Threads::self());  
                     _rwlock._internal_lock.unlock();  
                     // caught.reset(new TooManyReaders(Threads::self()));  
                 }  
             }  
             else if (milliseconds == -1)        // infinite wait  
             {  
                 try                 try
                 {                 {
                     _rwlock._rlock.wait();                     _rwlock._rlock.wait();
                 }                 }
                 catch (const IPCException & e)          catch (const IPCException&)
                 {  
                     _rwlock._internal_lock.unlock();  
                     caught = e;  
                     goto throw_from_here;  
                 }  
             }  
             else                // timed wait  
             {  
                 try  
                 {  
                     _rwlock._rlock.time_wait(milliseconds);  
                 }  
                 catch (const IPCException & e)  
                 {                 {
                     _rwlock._internal_lock.unlock();                     _rwlock._internal_lock.unlock();
                     caught = e;              throw;
                     goto throw_from_here;  
                 }  
             }             }
   
 //----------------------------------------------------------------- //-----------------------------------------------------------------
 // Read Lock Step 3: increment the number of readers, unlock the object, // Read Lock Step 3: increment the number of readers, unlock the object,
 // return // return
Line 513 
Line 227 
             _readers++;             _readers++;
             _rwlock._internal_lock.unlock();             _rwlock._internal_lock.unlock();
         }         }
       throw_from_here:  
         // ATTN:  
         Threads::cleanup_pop(0);  
     }  
   
     if (!Threads::null(caught.get_owner()))  
         throw caught;  
     if (!Threads::null(caughtWaitFailed.get_owner()))  
         throw caughtWaitFailed;  
     if (!Threads::null(caughtTimeOut.get_owner()))  
         throw caughtTimeOut;  
     if (!Threads::null(caughtTooManyReaders.get_owner()))  
   
         throw caughtTooManyReaders;  
     return;  
 }  
   
 //---------------------------------------------------------------------  
 void ReadWriteSem::wait(Uint32 mode, ThreadType caller)  
 {  
     timed_wait(mode, caller, -1);  
 }  
   
 void ReadWriteSem::try_wait(Uint32 mode, ThreadType caller)  
 {  
     timed_wait(mode, caller, 0);  
 } }
  
   void ReadWriteSem::_unlock(Boolean writeLock, ThreadType caller)
 void ReadWriteSem::unlock(Uint32 mode, ThreadType caller)  
 { {
     if (mode == PEG_SEM_WRITE && _writers.get() != 0)      if (writeLock && _writers.get() != 0)
     {     {
         _writers = 0;         _writers = 0;
         _rwlock._wlock.unlock();         _rwlock._wlock.unlock();


Legend:
Removed from v.1.5.4.1  
changed lines
  Added in v.1.8

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2