From 15668b9064bbc0aca524d9a74ece98aaa9260908 Mon Sep 17 00:00:00 2001 From: Francisco Zurita Date: Mon, 24 May 2021 18:34:53 -0400 Subject: [PATCH] Wait for buffer to be empty before reallocating memory --- .../ThreadedOpenGl/RingBufferPool.cpp | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Graphics/OpenGLContext/ThreadedOpenGl/RingBufferPool.cpp b/src/Graphics/OpenGLContext/ThreadedOpenGl/RingBufferPool.cpp index d0ed15e1..586ddf8c 100644 --- a/src/Graphics/OpenGLContext/ThreadedOpenGl/RingBufferPool.cpp +++ b/src/Graphics/OpenGLContext/ThreadedOpenGl/RingBufferPool.cpp @@ -136,8 +136,28 @@ PoolBufferPointer RingBufferPool::createPoolBuffer(const char* _buffer, size_t _ }); // Resize afterwards, we don't want to lose the validity of pointers - // pointing at the old data + // pointing at the old data, also wait until the memory pool is empty, + // we don't want anyone point to the old allocation. if (m_poolBuffer.size() < realBufferSize) { + + const size_t tempInUseStartLocal = m_inUseStartOffset; + const size_t remainingLocal = + tempInUseStartLocal > m_inUseEndOffset || m_full ? static_cast( + tempInUseStartLocal - m_inUseEndOffset) : + poolBufferSize - m_inUseEndOffset + tempInUseStartLocal; + + if (remainingLocal != realBufferSize) { + m_condition.wait(lock, [this, realBufferSize, poolBufferSize] { + const size_t tempInUseStartLocal = m_inUseStartOffset; + const size_t remainingLocal = + tempInUseStartLocal > m_inUseEndOffset || m_full ? static_cast( + tempInUseStartLocal - m_inUseEndOffset) : + poolBufferSize - m_inUseEndOffset + tempInUseStartLocal; + + return remainingLocal == realBufferSize; + }); + } + m_poolBuffer.resize(realBufferSize); } }