From c03eae1c15670244f5300605084eb98fb636b6d1 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 11 Jan 2024 20:07:25 +0900 Subject: [PATCH] UnitTest for start/stop threaded update and fix --- src/UDPConnection.cpp | 4 +++- src/test/TestUDPC.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 37dab7f..617778f 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -2326,7 +2326,9 @@ void UDPC_destroy(UDPC_HContext ctx) { // After lock has been dropped, wait in case there are enable/disable // threaded update functions waiting on the lock. Do this via a // atomic-int-based spin-lock. - while(UDPC_ctx->enableDisableFuncRunningCount.load() != 0) {} + while(UDPC_ctx->enableDisableFuncRunningCount.load() != 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS WSACleanup(); diff --git a/src/test/TestUDPC.cpp b/src/test/TestUDPC.cpp index 1cdd83e..7d75416 100644 --- a/src/test/TestUDPC.cpp +++ b/src/test/TestUDPC.cpp @@ -434,3 +434,29 @@ TEST(UDPC, free_packet_ptr) { UDPC_free_PacketInfo_ptr(&pinfo); UDPC_free_PacketInfo_ptr(nullptr); } + +TEST(UDPC, enableDisableThreadedUpdate_StressTest) { + UDPC_ConnectionId id = UDPC_create_id_anyaddr(0); + UDPC_HContext ctx = UDPC_init(id, 0, 0); + + std::array thread_array; + for (int i = 0; i < 100; ++i) { + if (i % 2 == 0) { + thread_array[i] = std::thread([] (UDPC_HContext ctx) { + UDPC_enable_threaded_update(ctx); + }, ctx); + } else { + thread_array[i] = std::thread([] (UDPC_HContext ctx) { + UDPC_disable_threaded_update(ctx); + }, ctx); + } + } + + thread_array[0].join(); + + UDPC_destroy(ctx); + + for (int i = 1; i < 100; ++i) { + thread_array[i].join(); + } +}