Enforce thread-safety on destroy context

This commit is contained in:
Stephen Seo 2024-01-11 19:18:12 +09:00
parent 186f2edf0f
commit 1ab9e73d08

View file

@ -2281,12 +2281,23 @@ int UDPC_is_valid_context(UDPC_HContext ctx) {
void UDPC_destroy(UDPC_HContext ctx) { void UDPC_destroy(UDPC_HContext ctx) {
UDPC::Context *UDPC_ctx = UDPC::verifyContext(ctx); UDPC::Context *UDPC_ctx = UDPC::verifyContext(ctx);
if(UDPC_ctx) { if(UDPC_ctx) {
// stop thread if threaded {
if(UDPC_ctx->isAutoUpdating.load()) { // Acquire lock so that this code does not run at the same time as
// enabling/disabling threaded-update.
std::lock_guard<std::mutex>
setThreadedLock(UDPC_ctx->setThreadedUpdateMutex);
// Stop thread if threaded.
// Set atomic bool to false always so that the thread will always
// stop at this point.
UDPC_ctx->threadRunning.store(false); UDPC_ctx->threadRunning.store(false);
if(UDPC_ctx->isAutoUpdating.load()) {
UDPC_ctx->thread.join(); UDPC_ctx->thread.join();
} }
// Drop lock at this point before destructing the context.
}
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
WSACleanup(); WSACleanup();
#endif #endif