Use mutex when enabling/disabling threaded-update

This commit is contained in:
Stephen Seo 2024-01-11 19:00:27 +09:00
parent 74341e83d4
commit 186f2edf0f
2 changed files with 25 additions and 4 deletions

View file

@ -276,6 +276,8 @@ public:
std::mutex atostrBufIndexMutex; std::mutex atostrBufIndexMutex;
std::uint32_t atostrBufIndex; std::uint32_t atostrBufIndex;
std::mutex setThreadedUpdateMutex;
}; // struct Context }; // struct Context
Context *verifyContext(UDPC_HContext ctx); Context *verifyContext(UDPC_HContext ctx);

View file

@ -257,7 +257,8 @@ peerPKWhitelistMutex(),
threadedSleepTime(std::chrono::milliseconds(UDPC_UPDATE_MS_DEFAULT)), threadedSleepTime(std::chrono::milliseconds(UDPC_UPDATE_MS_DEFAULT)),
keysSet(), keysSet(),
atostrBufIndexMutex(), atostrBufIndexMutex(),
atostrBufIndex(0) atostrBufIndex(0),
setThreadedUpdateMutex()
{ {
std::memset(atostrBuf, 0, UDPC_ATOSTR_SIZE); std::memset(atostrBuf, 0, UDPC_ATOSTR_SIZE);
@ -2207,7 +2208,13 @@ UDPC_HContext UDPC_init_threaded_update_ms(
int UDPC_enable_threaded_update(UDPC_HContext ctx) { int UDPC_enable_threaded_update(UDPC_HContext ctx) {
UDPC::Context *c = UDPC::verifyContext(ctx); UDPC::Context *c = UDPC::verifyContext(ctx);
if(!c || c->isAutoUpdating.load() || c->thread.joinable()) { if (!c) {
return 0;
}
std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
if(c->isAutoUpdating.load() || c->thread.joinable()) {
return 0; return 0;
} }
@ -2222,7 +2229,13 @@ int UDPC_enable_threaded_update(UDPC_HContext ctx) {
int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) { int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) {
UDPC::Context *c = UDPC::verifyContext(ctx); UDPC::Context *c = UDPC::verifyContext(ctx);
if(!c || c->isAutoUpdating.load() || c->thread.joinable()) { if (!c) {
return 0;
}
std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
if(c->isAutoUpdating.load() || c->thread.joinable()) {
return 0; return 0;
} }
@ -2243,7 +2256,13 @@ int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) {
int UDPC_disable_threaded_update(UDPC_HContext ctx) { int UDPC_disable_threaded_update(UDPC_HContext ctx) {
UDPC::Context *c = UDPC::verifyContext(ctx); UDPC::Context *c = UDPC::verifyContext(ctx);
if(!c || !c->isAutoUpdating.load() || !c->thread.joinable()) { if (!c) {
return 0;
}
std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
if(!c->isAutoUpdating.load() || !c->thread.joinable()) {
return 0; return 0;
} }