wpiutil: uv::Async: Keep weak reference to loop

Other handles can only be used within the loop itself, but Async is intended
to be used from another thread.  This introduces the possibility of a race
condition between the loop being destroyed and the Async being destroyed.
Change Async to keep a weak reference to a loop and check it before performing
libuv operations.
This commit is contained in:
Peter Johnson
2018-09-16 15:36:19 -07:00
parent 1a7a0db1ff
commit 172e438cd6
4 changed files with 100 additions and 66 deletions

View File

@@ -104,7 +104,9 @@ class Handle : public std::enable_shared_from_this<Handle> {
*
* @return True if the handle is closing or closed, false otherwise.
*/
bool IsClosing() const noexcept { return uv_is_closing(m_uv_handle) != 0; }
bool IsClosing() const noexcept {
return m_closed || uv_is_closing(m_uv_handle) != 0;
}
/**
* Request handle to be closed.
@@ -219,6 +221,7 @@ class Handle : public std::enable_shared_from_this<Handle> {
void Keep() noexcept { m_self = shared_from_this(); }
void Release() noexcept { m_self.reset(); }
void ForceClosed() noexcept { m_closed = true; }
static void AllocBuf(uv_handle_t* handle, size_t size, uv_buf_t* buf);
static void DefaultFreeBuf(Buffer& buf);