diff --git a/smocore/deviceManager/deviceReattacher.cpp b/smocore/deviceManager/deviceReattacher.cpp index 5b7a679..dc2bd72 100644 --- a/smocore/deviceManager/deviceReattacher.cpp +++ b/smocore/deviceManager/deviceReattacher.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -28,37 +27,23 @@ timer(ioThread->getIoService()) void DeviceReattacher::start() { - shouldContinue.store(true); + shouldContinue = true; scheduleNextTimeout(); } void DeviceReattacher::stop() { - shouldContinue.store(false); + { + SpinLock::Guard lock(shouldContinueLock); + shouldContinue = false; + } + timer.cancel(); - - // Set up a timeout bridge using the provided ioThread's io_service - auto& ioService = ioThread->getIoService(); - boost::asio::deadline_timer timeoutTimer(ioService); - AsynchronousBridge bridge(ioService); - - // Set up the timeout for ~10ms - timeoutTimer.expires_from_now(boost::posix_time::milliseconds(20)); - timeoutTimer.async_wait( - [&bridge](const boost::system::error_code& error) - { - (void)error; - - // Always signal complete, whether timeout expired or was cancelled - bridge.setAsyncOperationComplete(); - }); - - bridge.waitForAsyncOperationCompleteOrIoServiceStopped(); } void DeviceReattacher::scheduleNextTimeout() { - if (!shouldContinue.load()) { + if (!shouldContinue) { return; } @@ -85,7 +70,8 @@ void DeviceReattacher::onTimeout(const boost::system::error_code& error) return; } - if (!shouldContinue.load()) { + SpinLock::Guard lock(shouldContinueLock); + if (!shouldContinue) { return; } diff --git a/smocore/include/deviceManager/deviceReattacher.h b/smocore/include/deviceManager/deviceReattacher.h index f652318..0ae487f 100644 --- a/smocore/include/deviceManager/deviceReattacher.h +++ b/smocore/include/deviceManager/deviceReattacher.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace smo { @@ -35,7 +36,8 @@ private: DeviceManager &parent; std::shared_ptr ioThread; - std::atomic shouldContinue; + SpinLock shouldContinueLock; + bool shouldContinue; boost::asio::deadline_timer timer; };