Spin when waiting for decompressed data instead of using lock + cv.

This commit is contained in:
Bartosz Taudul 2024-06-02 15:06:16 +02:00
parent ce240ddfc1
commit 277ec25a8d
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -101,15 +101,15 @@ class FileRead
{ {
struct StreamHandle struct StreamHandle
{ {
StreamHandle( uint8_t type ) : stream( type ) {} StreamHandle( uint8_t type ) : stream( type ), outputReady( false ) {}
ReadStream stream; ReadStream stream;
const char* src; const char* src;
uint32_t size; uint32_t size;
bool inputReady = false; bool inputReady = false;
bool outputReady = false;
bool exit = false; bool exit = false;
alignas(64) std::atomic<bool> outputReady;
std::mutex signalLock; std::mutex signalLock;
std::condition_variable signal; std::condition_variable signal;
@ -504,19 +504,16 @@ private:
static void Worker( StreamHandle* hnd ) static void Worker( StreamHandle* hnd )
{ {
std::unique_lock lock( hnd->signalLock );
for(;;) for(;;)
{ {
std::unique_lock lock( hnd->signalLock );
hnd->signal.wait( lock, [&] { return hnd->inputReady || hnd->exit; } ); hnd->signal.wait( lock, [&] { return hnd->inputReady || hnd->exit; } );
if( hnd->exit ) return; if( hnd->exit ) return;
lock.unlock(); lock.unlock();
hnd->stream.Decompress( hnd->src, hnd->size ); hnd->stream.Decompress( hnd->src, hnd->size );
hnd->inputReady = false; hnd->inputReady = false;
hnd->outputReady.store( true, std::memory_order_release );
lock.lock();
hnd->outputReady = true;
hnd->signal.notify_one();
} }
} }
@ -567,18 +564,15 @@ private:
void GetNextDataBlock() void GetNextDataBlock()
{ {
auto& hnd = *m_streams[m_streamId]; auto& hnd = *m_streams[m_streamId];
std::unique_lock lock( hnd.signalLock ); while( hnd.outputReady.load( std::memory_order_acquire ) == false ) { YieldThread(); }
hnd.signal.wait( lock, [&hnd]{ return hnd.outputReady; } ); hnd.outputReady.store( false, std::memory_order_relaxed );
lock.unlock();
hnd.outputReady = false;
m_buf = hnd.stream.GetBuffer(); m_buf = hnd.stream.GetBuffer();
m_offset = 0; m_offset = 0;
if( m_dataOffset < m_dataSize ) if( m_dataOffset < m_dataSize )
{ {
const auto sz = ReadBlockSize(); const auto sz = ReadBlockSize();
lock.lock(); std::unique_lock lock( hnd.signalLock );
hnd.src = m_data + m_dataOffset; hnd.src = m_data + m_dataOffset;
hnd.size = sz; hnd.size = sz;
hnd.inputReady = true; hnd.inputReady = true;