Kaydet (Commit) 2841cf6c authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:inlinefields in utl::ReadWriteMutex

Change-Id: Ibd31d1c0ec154be886f9fd4d2e7a439b2d7f5f4a
Reviewed-on: https://gerrit.libreoffice.org/36267Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 52117bb8
...@@ -31,21 +31,14 @@ class ReadWriteMutex ...@@ -31,21 +31,14 @@ class ReadWriteMutex
sal_uInt32 nReadCount; sal_uInt32 nReadCount;
sal_uInt32 nBlockCriticalCount; sal_uInt32 nBlockCriticalCount;
::osl::Mutex* pMutex; ::osl::Mutex maMutex;
::osl::Mutex* pWriteMutex; ::osl::Mutex maWriteMutex;
public: public:
ReadWriteMutex() ReadWriteMutex()
: nReadCount(0) : nReadCount(0)
, nBlockCriticalCount(0) , nBlockCriticalCount(0)
, pMutex( new ::osl::Mutex )
, pWriteMutex( new ::osl::Mutex )
{} {}
~ReadWriteMutex()
{
delete pMutex;
delete pWriteMutex;
}
}; };
namespace ReadWriteGuardMode { namespace ReadWriteGuardMode {
......
...@@ -28,52 +28,52 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP, ...@@ -28,52 +28,52 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
{ {
// don't do anything until a pending write completed (or another // don't do anything until a pending write completed (or another
// ReadWriteGuard leaves the ctor phase) // ReadWriteGuard leaves the ctor phase)
::osl::MutexGuard aGuard( rMutex.pWriteMutex ); ::osl::MutexGuard aGuard( rMutex.maWriteMutex );
nMode = nRequestMode; nMode = nRequestMode;
if ( nMode & ReadWriteGuardMode::nWrite ) if ( nMode & ReadWriteGuardMode::nWrite )
{ {
rMutex.pWriteMutex->acquire(); rMutex.maWriteMutex.acquire();
// wait for any read to complete // wait for any read to complete
// TODO: set up a waiting thread instead of a loop // TODO: set up a waiting thread instead of a loop
bool bWait = true; bool bWait = true;
do do
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
bWait = (rMutex.nReadCount != 0); bWait = (rMutex.nReadCount != 0);
if ( nMode & ReadWriteGuardMode::nCriticalChange ) if ( nMode & ReadWriteGuardMode::nCriticalChange )
bWait |= (rMutex.nBlockCriticalCount != 0); bWait |= (rMutex.nBlockCriticalCount != 0);
rMutex.pMutex->release(); rMutex.maMutex.release();
} while ( bWait ); } while ( bWait );
} }
else if ( nMode & ReadWriteGuardMode::nBlockCritical ) else if ( nMode & ReadWriteGuardMode::nBlockCritical )
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
++rMutex.nBlockCriticalCount; ++rMutex.nBlockCriticalCount;
rMutex.pMutex->release(); rMutex.maMutex.release();
} }
else else
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
++rMutex.nReadCount; ++rMutex.nReadCount;
rMutex.pMutex->release(); rMutex.maMutex.release();
} }
} }
ReadWriteGuard::~ReadWriteGuard() ReadWriteGuard::~ReadWriteGuard()
{ {
if ( nMode & ReadWriteGuardMode::nWrite ) if ( nMode & ReadWriteGuardMode::nWrite )
rMutex.pWriteMutex->release(); rMutex.maWriteMutex.release();
else if ( nMode & ReadWriteGuardMode::nBlockCritical ) else if ( nMode & ReadWriteGuardMode::nBlockCritical )
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
--rMutex.nBlockCriticalCount; --rMutex.nBlockCriticalCount;
rMutex.pMutex->release(); rMutex.maMutex.release();
} }
else else
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
--rMutex.nReadCount; --rMutex.nReadCount;
rMutex.pMutex->release(); rMutex.maMutex.release();
} }
} }
...@@ -86,20 +86,20 @@ void ReadWriteGuard::changeReadToWrite() ...@@ -86,20 +86,20 @@ void ReadWriteGuard::changeReadToWrite()
// MUST release read before acquiring write mutex or dead lock would // MUST release read before acquiring write mutex or dead lock would
// occur if there was a write in another thread waiting for this read // occur if there was a write in another thread waiting for this read
// to complete. // to complete.
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
--rMutex.nReadCount; --rMutex.nReadCount;
rMutex.pMutex->release(); rMutex.maMutex.release();
rMutex.pWriteMutex->acquire(); rMutex.maWriteMutex.acquire();
nMode |= ReadWriteGuardMode::nWrite; nMode |= ReadWriteGuardMode::nWrite;
// wait for any other read to complete // wait for any other read to complete
// TODO: set up a waiting thread instead of a loop // TODO: set up a waiting thread instead of a loop
bool bWait = true; bool bWait = true;
do do
{ {
rMutex.pMutex->acquire(); rMutex.maMutex.acquire();
bWait = (rMutex.nReadCount != 0); bWait = (rMutex.nReadCount != 0);
rMutex.pMutex->release(); rMutex.maMutex.release();
} while ( bWait ); } while ( bWait );
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment