mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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:
@@ -12,14 +12,21 @@
|
||||
namespace wpi {
|
||||
namespace uv {
|
||||
|
||||
std::shared_ptr<Async<>> Async<>::Create(Loop& loop) {
|
||||
auto h = std::make_shared<Async>(private_init{});
|
||||
int err = uv_async_init(loop.GetRaw(), h->GetRaw(), [](uv_async_t* handle) {
|
||||
Async<>::~Async() noexcept {
|
||||
if (auto loop = m_loop.lock())
|
||||
Close();
|
||||
else
|
||||
ForceClosed();
|
||||
}
|
||||
|
||||
std::shared_ptr<Async<>> Async<>::Create(const std::shared_ptr<Loop>& loop) {
|
||||
auto h = std::make_shared<Async>(loop, private_init{});
|
||||
int err = uv_async_init(loop->GetRaw(), h->GetRaw(), [](uv_async_t* handle) {
|
||||
Async& h = *static_cast<Async*>(handle->data);
|
||||
h.wakeup();
|
||||
});
|
||||
if (err < 0) {
|
||||
loop.ReportError(err);
|
||||
loop->ReportError(err);
|
||||
return nullptr;
|
||||
}
|
||||
h->Keep();
|
||||
|
||||
Reference in New Issue
Block a user