From caee572ff94822ca2ed354fcb79ca04ed9adf388 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 5 Aug 2024 12:44:59 -0700 Subject: Perhaps fixed a Myriad race condition. If so, this will fix the issue where streams randomly truncate when accessed by multiple threads. --- src/stable/myriadstream.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/stable/myriadstream.cpp') diff --git a/src/stable/myriadstream.cpp b/src/stable/myriadstream.cpp index 50c6924..3c78bb0 100644 --- a/src/stable/myriadstream.cpp +++ b/src/stable/myriadstream.cpp @@ -19,7 +19,8 @@ using Bu::Fmt; #endif #include "bu/sio.h" -#define TRACE( x ) Bu::println("%1:%2: %3: %4 - %5").arg(__FILE__).arg( __LINE__ ).arg(__PRETTY_FUNCTION__).arg(rMyriad.sStore.getLocation()).arg(x) +// #define TRACE( x ) Bu::println("%1:%2: %3: %4 - %5").arg(__FILE__).arg( __LINE__ ).arg(__PRETTY_FUNCTION__).arg(rMyriad.sStore.getLocation()).arg(x) +#define TRACE( x ) (void)0 Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad, Bu::Myriad::Stream *pStream ) : @@ -55,8 +56,8 @@ Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes ) sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b." << sio.nl; #endif - if( nBytes > (Bu::size)pStream->iSize-iPos ) - nBytes = pStream->iSize-iPos; + if( nBytes > (Bu::size)pStream->getSize()-iPos ) + nBytes = pStream->getSize()-iPos; if( nBytes <= 0 ) return 0; int iLeft = nBytes; @@ -92,7 +93,7 @@ Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes ) rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, iLeft ), - pStream->iSize-iPos + pStream->getSize()-iPos ); #ifdef MYRIAD_STREAM_DEBUG sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: " @@ -166,14 +167,14 @@ Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) // There are two main writing modes when it comes down to it. // Overwrite mode and append mode. Append is what pretty much always // happens when creating a new stream. - if( iPos < pStream->iSize ) + if( iPos < pStream->getSize() ) { int iAmnt = Bu::buMin( Bu::buMin( rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, iLeft ), - pStream->iSize-iPos + pStream->getSize()-iPos ); #ifdef MYRIAD_STREAM_DEBUG sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: " @@ -208,9 +209,9 @@ Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) iAmnt ); iPos += iAmnt; - TRACE(Bu::String("Stream=%1 - pStream->iSize(%2) += iAmnt(%3)").arg(pStream->iId).arg( pStream->iSize ).arg(iAmnt).end()); - pStream->iSize += iAmnt; - TRACE(Bu::String("Stream=%1 - pStream->iSize = %2").arg(pStream->iId).arg( pStream->iSize ).end()); + TRACE(Bu::String("Stream=%1 - pStream->iSize(%2) += iAmnt(%3)").arg(pStream->iId).arg( pStream->getSize() ).arg(iAmnt).end()); + pStream->growTo( pStream->getSize()+iAmnt ); + TRACE(Bu::String("Stream=%1 - pStream->iSize = %2").arg(pStream->iId).arg( pStream->getSize() ).end()); rMyriad.headerChanged(); pBuf = &((char *)pBuf)[iAmnt]; iLeft -= iAmnt; @@ -237,12 +238,12 @@ void Bu::MyriadStream::setPos( Bu::size pos ) void Bu::MyriadStream::setPosEnd( Bu::size pos ) { - iPos = pStream->iSize-pos; + iPos = pStream->getSize()-pos; } bool Bu::MyriadStream::isEos() { - return iPos >= pStream->iSize; + return iPos >= pStream->getSize(); } bool Bu::MyriadStream::isOpen() @@ -299,7 +300,7 @@ void Bu::MyriadStream::setSize( Bu::size iSize ) Bu::size Bu::MyriadStream::getSize() const { - return pStream->iSize; + return pStream->getSize(); } Bu::size Bu::MyriadStream::getBlockSize() const -- cgit v1.2.3