From c5a03233c0a49e2bfa73aa0c74609abce87bae7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 6 Apr 2015 18:22:15 -0700 Subject: [PATCH] Fixed shutdown sequence. --- src/bgfx.cpp | 13 ++++++++++--- src/bgfx_p.h | 5 ++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 815452a5..3b712d3a 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -819,6 +819,10 @@ namespace bgfx BGFX_CHECK_RENDER_THREAD(); if (s_ctx->renderFrame() ) { + Context* ctx = s_ctx; + ctx->gameSemWait(); + s_ctx = NULL; + ctx->renderSemPost(); return RenderFrame::Exiting; } @@ -1054,15 +1058,18 @@ namespace bgfx m_declRef.shutdown(m_vertexDeclHandle); #if BGFX_CONFIG_MULTITHREADED + // Render thread shutdown sequence. + renderSemWait(); // Wait for previous frame. + gameSemPost(); // OK to set context to NULL. + // s_ctx is NULL here. + renderSemWait(); // In RenderFrame::Exiting state. + if (m_thread.isRunning() ) { m_thread.shutdown(); } #endif // BGFX_CONFIG_MULTITHREADED - s_ctx = NULL; // Can't be used by renderFrame at this point. - renderSemWait(); - m_submit->destroy(); m_render->destroy(); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 9ae0887b..4b7cc4d3 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1934,11 +1934,10 @@ namespace bgfx { } - static int32_t renderThread(void* _userData) + static int32_t renderThread(void* /*_userData*/) { BX_TRACE("render thread start"); - Context* ctx = (Context*)_userData; - while (!ctx->renderFrame() ) {}; + while (RenderFrame::Exiting != bgfx::renderFrame() ) {}; BX_TRACE("render thread exit"); return EXIT_SUCCESS; }