From 620bec9cae0507ff3e0e1f460b1f4b70e35bc9b1 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sun, 5 May 2019 17:38:56 -0700 Subject: [PATCH] wpiutil: uv: Add LoopClosing status to Handle (#1647) Useful for EventLoopRunner to know if a stop is requested, or close is happening for another reason. --- .../src/main/native/cpp/EventLoopRunner.cpp | 5 +++- .../src/main/native/include/wpi/uv/Handle.h | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/wpiutil/src/main/native/cpp/EventLoopRunner.cpp b/wpiutil/src/main/native/cpp/EventLoopRunner.cpp index b885385279..2ae6b47ac6 100644 --- a/wpiutil/src/main/native/cpp/EventLoopRunner.cpp +++ b/wpiutil/src/main/native/cpp/EventLoopRunner.cpp @@ -49,7 +49,10 @@ EventLoopRunner::~EventLoopRunner() { Stop(); } void EventLoopRunner::Stop() { ExecAsync([](uv::Loop& loop) { // close all handles; this will (eventually) stop the loop - loop.Walk([](uv::Handle& h) { h.Close(); }); + loop.Walk([](uv::Handle& h) { + h.SetLoopClosing(true); + h.Close(); + }); }); m_owner.Join(); } diff --git a/wpiutil/src/main/native/include/wpi/uv/Handle.h b/wpiutil/src/main/native/include/wpi/uv/Handle.h index 046c1551d0..dac1c4afc9 100644 --- a/wpiutil/src/main/native/include/wpi/uv/Handle.h +++ b/wpiutil/src/main/native/include/wpi/uv/Handle.h @@ -119,6 +119,29 @@ class Handle : public std::enable_shared_from_this { */ void Close() noexcept; + /** + * Set if the loop is closing. + * + * This is set during EventLoopRunner.Stop(), and can be used for other cases + * to indicate the loop should be closing. For instance for a uv_walk loop can + * use this to close existing handles. + * + * @param loopClosing true to set the loop currently in closing stages. + */ + void SetLoopClosing(bool loopClosing) noexcept { + m_loopClosing = loopClosing; + } + + /** + * Get the loop closing status. + * + * This can be used from closed() in order to tell if a closing loop is the + * reason for the close, or another reason. + * + * @return true if the loop is closing, otherwise false. + */ + bool IsLoopClosing() const noexcept { return m_loopClosing; } + /** * Reference the given handle. * @@ -237,6 +260,7 @@ class Handle : public std::enable_shared_from_this { std::shared_ptr m_self; uv_handle_t* m_uv_handle; bool m_closed = false; + bool m_loopClosing = false; std::function m_allocBuf{&Buffer::Allocate}; std::function m_freeBuf{&DefaultFreeBuf}; std::shared_ptr m_data;