version 1.7, 2008/03/05 22:25:17
|
version 1.8, 2008/08/14 17:44:27
|
|
|
} | } |
} | } |
| |
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))) |
if (0 == (errorcode = pthread_rwlock_wrlock(&_rwlock.rwlock))) |
{ | { |
_readers++; |
_rwlock.owner = caller; |
|
_writers++; |
return; | return; |
} | } |
} | } |
else if (mode == PEG_SEM_WRITE) |
else |
{ | { |
if (0 == (errorcode = pthread_rwlock_wrlock(&_rwlock.rwlock))) |
if (0 == (errorcode = pthread_rwlock_rdlock(&_rwlock.rwlock))) |
{ | { |
_rwlock.owner = caller; |
_readers++; |
_writers++; |
|
return; | return; |
} | } |
} | } |
else |
|
throw(Permission(Threads::self())); |
|
| |
if (errorcode == EDEADLK) | if (errorcode == EDEADLK) |
throw(Deadlock(_rwlock.owner)); | throw(Deadlock(_rwlock.owner)); |
|
|
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); |
|
|
_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--; |
|
|
} | } |
| |
//--------------------------------------------------------------------- | //--------------------------------------------------------------------- |
void ReadWriteSem::wait(Uint32 mode, ThreadType caller) |
void ReadWriteSem::_wait(Boolean writeLock, ThreadType caller) |
{ | { |
//----------------------------------------------------------------- | //----------------------------------------------------------------- |
// 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 |
|
{ |
|
_rwlock._internal_lock.lock(); | _rwlock._internal_lock.lock(); |
} |
|
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 |
|
|
{ | { |
_rwlock._wlock.lock(); | _rwlock._wlock.lock(); |
} | } |
catch (const IPCException & e) |
catch (const IPCException&) |
{ | { |
_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 |
|
|
_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 |
{ | { |
//----------------------------------------------------------------- | //----------------------------------------------------------------- |
|
|
{ | { |
_rwlock._rlock.wait(); | _rwlock._rlock.wait(); |
} | } |
catch (const IPCException & e) |
catch (const IPCException&) |
{ | { |
_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, |
|
|
_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::unlock(Uint32 mode, ThreadType caller) |
void ReadWriteSem::_unlock(Boolean writeLock, 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(); |