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

convert ReadWriteGuardMode to scoped enum

Change-Id: I21ae815d5bbd7b39cd690168738c21925558585e
Reviewed-on: https://gerrit.libreoffice.org/36452Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 05144427
...@@ -21,6 +21,17 @@ ...@@ -21,6 +21,17 @@
#define INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX #define INCLUDED_UNOTOOLS_READWRITEMUTEXGUARD_HXX
#include <osl/mutex.hxx> #include <osl/mutex.hxx>
#include <o3tl/typed_flags_set.hxx>
enum class ReadWriteGuardMode {
ReadOnly = 0x00,
Write = 0x01,
CriticalChange = 0x02 | Write,
BlockCritical = 0x04, // only a block, not a read, exclusive flag!
};
namespace o3tl {
template<> struct typed_flags<ReadWriteGuardMode> : is_typed_flags<ReadWriteGuardMode, 0x7> {};
}
namespace utl { namespace utl {
...@@ -41,12 +52,6 @@ public: ...@@ -41,12 +52,6 @@ public:
{} {}
}; };
namespace ReadWriteGuardMode {
const sal_Int32 nWrite = 0x01;
const sal_Int32 nCriticalChange = 0x02 | nWrite;
const sal_Int32 nBlockCritical = 0x04; // only a block, not a read, exclusive flag!
}
/** Enable multiple threads to read simultaneously, but a write blocks all /** Enable multiple threads to read simultaneously, but a write blocks all
other reads and writes, and a read blocks any write. other reads and writes, and a read blocks any write.
Used in I18N wrappers to be able to maintain a single instance of a wrapper Used in I18N wrappers to be able to maintain a single instance of a wrapper
...@@ -67,11 +72,11 @@ const sal_Int32 nBlockCritical = 0x04; // only a block, not a read, exclusi ...@@ -67,11 +72,11 @@ const sal_Int32 nBlockCritical = 0x04; // only a block, not a read, exclusi
class ReadWriteGuard class ReadWriteGuard
{ {
ReadWriteMutex& rMutex; ReadWriteMutex& rMutex;
sal_Int32 nMode; ReadWriteGuardMode nMode;
public: public:
ReadWriteGuard( ReadWriteGuard(
ReadWriteMutex& rMutex, ReadWriteMutex& rMutex,
sal_Int32 nRequestMode = 0 // read only ReadWriteGuardMode nRequestMode = ReadWriteGuardMode::ReadOnly // read only
); );
~ReadWriteGuard(); ~ReadWriteGuard();
......
...@@ -115,7 +115,7 @@ LocaleDataWrapper::~LocaleDataWrapper() ...@@ -115,7 +115,7 @@ LocaleDataWrapper::~LocaleDataWrapper()
void LocaleDataWrapper::setLanguageTag( const LanguageTag& rLanguageTag ) void LocaleDataWrapper::setLanguageTag( const LanguageTag& rLanguageTag )
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nCriticalChange ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::CriticalChange );
maLanguageTag = rLanguageTag; maLanguageTag = rLanguageTag;
invalidateData(); invalidateData();
} }
...@@ -1383,7 +1383,7 @@ sal_Unicode* LocaleDataWrapper::ImplAddFormatNum( sal_Unicode* pBuf, ...@@ -1383,7 +1383,7 @@ sal_Unicode* LocaleDataWrapper::ImplAddFormatNum( sal_Unicode* pBuf,
OUString LocaleDataWrapper::getDate( const Date& rDate ) const OUString LocaleDataWrapper::getDate( const Date& rDate ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
//!TODO: leading zeros et al //!TODO: leading zeros et al
sal_Unicode aBuf[128]; sal_Unicode aBuf[128];
sal_Unicode* pBuf = aBuf; sal_Unicode* pBuf = aBuf;
...@@ -1429,7 +1429,7 @@ OUString LocaleDataWrapper::getDate( const Date& rDate ) const ...@@ -1429,7 +1429,7 @@ OUString LocaleDataWrapper::getDate( const Date& rDate ) const
OUString LocaleDataWrapper::getTime( const tools::Time& rTime, bool bSec, bool b100Sec ) const OUString LocaleDataWrapper::getTime( const tools::Time& rTime, bool bSec, bool b100Sec ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
//!TODO: leading zeros et al //!TODO: leading zeros et al
sal_Unicode aBuf[128]; sal_Unicode aBuf[128];
sal_Unicode* pBuf = aBuf; sal_Unicode* pBuf = aBuf;
...@@ -1458,7 +1458,7 @@ OUString LocaleDataWrapper::getTime( const tools::Time& rTime, bool bSec, bool b ...@@ -1458,7 +1458,7 @@ OUString LocaleDataWrapper::getTime( const tools::Time& rTime, bool bSec, bool b
OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCal, OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCal,
bool bTwoDigitYear ) const bool bTwoDigitYear ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
using namespace css::i18n; using namespace css::i18n;
sal_Unicode aBuf[20]; sal_Unicode aBuf[20];
sal_Unicode* pBuf; sal_Unicode* pBuf;
...@@ -1500,7 +1500,7 @@ OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCa ...@@ -1500,7 +1500,7 @@ OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCa
OUString LocaleDataWrapper::getDuration( const tools::Time& rTime, bool bSec, bool b100Sec ) const OUString LocaleDataWrapper::getDuration( const tools::Time& rTime, bool bSec, bool b100Sec ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
sal_Unicode aBuf[128]; sal_Unicode aBuf[128];
sal_Unicode* pBuf = aBuf; sal_Unicode* pBuf = aBuf;
...@@ -1544,7 +1544,7 @@ inline size_t ImplGetNumberStringLengthGuess( const LocaleDataWrapper& rLoc, sal ...@@ -1544,7 +1544,7 @@ inline size_t ImplGetNumberStringLengthGuess( const LocaleDataWrapper& rLoc, sal
OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals, OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals,
bool bUseThousandSep, bool bTrailingZeros ) const bool bUseThousandSep, bool bTrailingZeros ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
sal_Unicode aBuf[128]; // big enough for 64-bit long and crazy grouping sal_Unicode aBuf[128]; // big enough for 64-bit long and crazy grouping
// check if digits and separators will fit into fixed buffer or allocate // check if digits and separators will fit into fixed buffer or allocate
size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals ); size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals );
...@@ -1563,7 +1563,7 @@ OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals, ...@@ -1563,7 +1563,7 @@ OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals,
OUString LocaleDataWrapper::getCurr( sal_Int64 nNumber, sal_uInt16 nDecimals, OUString LocaleDataWrapper::getCurr( sal_Int64 nNumber, sal_uInt16 nDecimals,
const OUString& rCurrencySymbol, bool bUseThousandSep ) const const OUString& rCurrencySymbol, bool bUseThousandSep ) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
sal_Unicode aBuf[192]; sal_Unicode aBuf[192];
sal_Unicode aNumBuf[128]; // big enough for 64-bit long and crazy grouping sal_Unicode aNumBuf[128]; // big enough for 64-bit long and crazy grouping
sal_Unicode cZeroChar = getCurrZeroChar(); sal_Unicode cZeroChar = getCurrZeroChar();
...@@ -1771,7 +1771,7 @@ LanguageTag LocaleDataWrapper::getLoadedLanguageTag() const ...@@ -1771,7 +1771,7 @@ LanguageTag LocaleDataWrapper::getLoadedLanguageTag() const
OUString LocaleDataWrapper::appendLocaleInfo(const OUString& rDebugMsg) const OUString LocaleDataWrapper::appendLocaleInfo(const OUString& rDebugMsg) const
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nBlockCritical ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical );
OUStringBuffer aDebugMsg(rDebugMsg); OUStringBuffer aDebugMsg(rDebugMsg);
aDebugMsg.append('\n'); aDebugMsg.append('\n');
aDebugMsg.append(maLanguageTag.getBcp47()); aDebugMsg.append(maLanguageTag.getBcp47());
...@@ -1872,7 +1872,7 @@ css::uno::Sequence< OUString > LocaleDataWrapper::getDateAcceptancePatterns() co ...@@ -1872,7 +1872,7 @@ css::uno::Sequence< OUString > LocaleDataWrapper::getDateAcceptancePatterns() co
void LocaleDataWrapper::setDateAcceptancePatterns( void LocaleDataWrapper::setDateAcceptancePatterns(
const css::uno::Sequence< OUString > & rPatterns ) const css::uno::Sequence< OUString > & rPatterns )
{ {
::utl::ReadWriteGuard aGuard( aMutex, ::utl::ReadWriteGuardMode::nWrite ); ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::Write );
if (!aDateAcceptancePatterns.getLength() || !rPatterns.getLength()) if (!aDateAcceptancePatterns.getLength() || !rPatterns.getLength())
{ {
......
...@@ -23,14 +23,14 @@ ...@@ -23,14 +23,14 @@
namespace utl { namespace utl {
ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP, ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
sal_Int32 nRequestMode ) ReadWriteGuardMode nRequestMode )
: rMutex( rMutexP ) : rMutex( 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.maWriteMutex ); ::osl::MutexGuard aGuard( rMutex.maWriteMutex );
nMode = nRequestMode; nMode = nRequestMode;
if ( nMode & ReadWriteGuardMode::nWrite ) if ( nMode & ReadWriteGuardMode::Write )
{ {
rMutex.maWriteMutex.acquire(); rMutex.maWriteMutex.acquire();
// wait for any read to complete // wait for any read to complete
...@@ -40,12 +40,12 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP, ...@@ -40,12 +40,12 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
{ {
rMutex.maMutex.acquire(); rMutex.maMutex.acquire();
bWait = (rMutex.nReadCount != 0); bWait = (rMutex.nReadCount != 0);
if ( nMode & ReadWriteGuardMode::nCriticalChange ) if ( nMode & ReadWriteGuardMode::CriticalChange )
bWait |= (rMutex.nBlockCriticalCount != 0); bWait |= (rMutex.nBlockCriticalCount != 0);
rMutex.maMutex.release(); rMutex.maMutex.release();
} while ( bWait ); } while ( bWait );
} }
else if ( nMode & ReadWriteGuardMode::nBlockCritical ) else if ( nMode & ReadWriteGuardMode::BlockCritical )
{ {
rMutex.maMutex.acquire(); rMutex.maMutex.acquire();
++rMutex.nBlockCriticalCount; ++rMutex.nBlockCriticalCount;
...@@ -61,9 +61,9 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP, ...@@ -61,9 +61,9 @@ ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP,
ReadWriteGuard::~ReadWriteGuard() ReadWriteGuard::~ReadWriteGuard()
{ {
if ( nMode & ReadWriteGuardMode::nWrite ) if ( nMode & ReadWriteGuardMode::Write )
rMutex.maWriteMutex.release(); rMutex.maWriteMutex.release();
else if ( nMode & ReadWriteGuardMode::nBlockCritical ) else if ( nMode & ReadWriteGuardMode::BlockCritical )
{ {
rMutex.maMutex.acquire(); rMutex.maMutex.acquire();
--rMutex.nBlockCriticalCount; --rMutex.nBlockCriticalCount;
...@@ -79,7 +79,7 @@ ReadWriteGuard::~ReadWriteGuard() ...@@ -79,7 +79,7 @@ ReadWriteGuard::~ReadWriteGuard()
void ReadWriteGuard::changeReadToWrite() void ReadWriteGuard::changeReadToWrite()
{ {
bool bOk = !(nMode & (ReadWriteGuardMode::nWrite | ReadWriteGuardMode::nBlockCritical)); bool bOk = !(nMode & (ReadWriteGuardMode::Write | ReadWriteGuardMode::BlockCritical));
DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" ); DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" );
if ( bOk ) if ( bOk )
{ {
...@@ -91,7 +91,7 @@ void ReadWriteGuard::changeReadToWrite() ...@@ -91,7 +91,7 @@ void ReadWriteGuard::changeReadToWrite()
rMutex.maMutex.release(); rMutex.maMutex.release();
rMutex.maWriteMutex.acquire(); rMutex.maWriteMutex.acquire();
nMode |= ReadWriteGuardMode::nWrite; nMode |= ReadWriteGuardMode::Write;
// 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;
......
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