From d6413efc0aa61fd98a94da7b6c6fb774833fdec9 Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Tue, 6 Dec 2022 22:10:51 -0500 Subject: [PATCH] Isolate interrupt requests to single thread --- src/util/Interrupt.cpp | 6 ++-- tests/unit/capi/GEOSInterruptTest.cpp | 50 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/util/Interrupt.cpp b/src/util/Interrupt.cpp index 0bc988221b..ce337e2ed2 100644 --- a/src/util/Interrupt.cpp +++ b/src/util/Interrupt.cpp @@ -16,10 +16,8 @@ #include // for inheritance namespace { -/* Could these be portably stored in thread-specific space ? */ -bool requested = false; - -geos::util::Interrupt::Callback* callback = nullptr; +thread_local bool requested = false; +thread_local geos::util::Interrupt::Callback* callback = nullptr; } namespace geos { diff --git a/tests/unit/capi/GEOSInterruptTest.cpp b/tests/unit/capi/GEOSInterruptTest.cpp index 0bd0301143..5b0e9ec71e 100644 --- a/tests/unit/capi/GEOSInterruptTest.cpp +++ b/tests/unit/capi/GEOSInterruptTest.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace tut { // @@ -18,6 +19,7 @@ namespace tut { // Common data used in test cases. struct test_capiinterrupt_data { static int numcalls; + static int numcalls2; static GEOSInterruptCallback* nextcb; static void @@ -56,9 +58,19 @@ struct test_capiinterrupt_data { } } + static void + countCalls2() + { + ++numcalls2; + if(nextcb) { + (*nextcb)(); + } + } + }; int test_capiinterrupt_data::numcalls = 0; +int test_capiinterrupt_data::numcalls2 = 0; GEOSInterruptCallback* test_capiinterrupt_data::nextcb = nullptr; typedef test_group group; @@ -221,5 +233,43 @@ void object::test<5> } +// Test callback is thread-local +template<> +template<> +void object::test<6> +() +{ + numcalls = 0; + numcalls2 = 0; + nextcb = nullptr; + + initGEOS(notice, notice); + + auto buffer = [](GEOSInterruptCallback* cb) { + GEOSGeometry* geom1 = GEOSGeomFromWKT("LINESTRING (0 0, 1 0)"); + + GEOS_interruptRegisterCallback(cb); + + GEOSGeometry* geom2 = GEOSBuffer(geom1, 1, 8); + GEOSGeom_destroy(geom2); + GEOSGeom_destroy(geom1); + + GEOS_interruptRegisterCallback(nullptr); + }; + + std::thread t1(buffer, countCalls); + std::thread t2(buffer, countCalls2); + + t1.join(); + t2.join(); + + ensure("numcalls > 0", numcalls > 0); + ensure("numcalls2 > 0", numcalls2 > 0); + ensure_equals(numcalls, numcalls2); + + finishGEOS(); +} + + } // namespace tut