Skip to content

Commit 15668b9

Browse files
fzuritagonetz
authored andcommitted
Wait for buffer to be empty before reallocating memory
1 parent e9d08c3 commit 15668b9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Diff for: src/Graphics/OpenGLContext/ThreadedOpenGl/RingBufferPool.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,28 @@ PoolBufferPointer RingBufferPool::createPoolBuffer(const char* _buffer, size_t _
136136
});
137137

138138
// Resize afterwards, we don't want to lose the validity of pointers
139-
// pointing at the old data
139+
// pointing at the old data, also wait until the memory pool is empty,
140+
// we don't want anyone point to the old allocation.
140141
if (m_poolBuffer.size() < realBufferSize) {
142+
143+
const size_t tempInUseStartLocal = m_inUseStartOffset;
144+
const size_t remainingLocal =
145+
tempInUseStartLocal > m_inUseEndOffset || m_full ? static_cast<size_t>(
146+
tempInUseStartLocal - m_inUseEndOffset) :
147+
poolBufferSize - m_inUseEndOffset + tempInUseStartLocal;
148+
149+
if (remainingLocal != realBufferSize) {
150+
m_condition.wait(lock, [this, realBufferSize, poolBufferSize] {
151+
const size_t tempInUseStartLocal = m_inUseStartOffset;
152+
const size_t remainingLocal =
153+
tempInUseStartLocal > m_inUseEndOffset || m_full ? static_cast<size_t>(
154+
tempInUseStartLocal - m_inUseEndOffset) :
155+
poolBufferSize - m_inUseEndOffset + tempInUseStartLocal;
156+
157+
return remainingLocal == realBufferSize;
158+
});
159+
}
160+
141161
m_poolBuffer.resize(realBufferSize);
142162
}
143163
}

0 commit comments

Comments
 (0)