Skip to content

Commit

Permalink
Fix race condition in ShaderCompilerService (#7147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado authored Sep 8, 2023
1 parent dc818ab commit a57225c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions filament/backend/src/opengl/ShaderCompilerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ struct ShaderCompilerService::OpenGLProgramToken : ProgramToken {
return programData;
}

void wait() const noexcept {
std::unique_lock l(lock);
cond.wait(l, [this](){ return signaled; });
}

// Checks if the programBinary is ready.
// This is similar to std::future::wait_for(0s)
bool isReady() const noexcept {
Expand Down Expand Up @@ -338,6 +343,22 @@ GLuint ShaderCompilerService::getProgram(ShaderCompilerService::program_token_t&

token->compiler.cancelTickOp(token);

if (token->compiler.mShaderCompilerThreadCount) {
auto job = token->compiler.mCompilerThreadPool.dequeue(token);
if (!job) {
// The job is being executed right now. We need to wait for it to finish to avoid a
// race.
token->wait();
} else {
// The job has not been executed, but we still need to inform the callback manager in
// order for future callbacks to be successfully called.
token->compiler.mCallbackManager.put(token->handle);
}
} else {
// Since the tick op was canceled, we need to .put the token here.
token->compiler.mCallbackManager.put(token->handle);
}

for (GLuint& shader: token->gl.shaders) {
if (shader) {
if (token->gl.program) {
Expand Down

0 comments on commit a57225c

Please sign in to comment.