2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-07-17 01:06:24 -07:00
|
|
|
|
|
|
|
|
#include "wpi/uv/Loop.h"
|
|
|
|
|
|
|
|
|
|
using namespace wpi::uv;
|
|
|
|
|
|
|
|
|
|
Loop::Loop(const private_init&) noexcept {
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
// Ignore SIGPIPE (see https://github.com/joyent/libuv/issues/1254)
|
|
|
|
|
static bool once = []() {
|
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
return true;
|
|
|
|
|
}();
|
|
|
|
|
(void)once;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Loop::~Loop() noexcept {
|
|
|
|
|
if (m_loop) {
|
|
|
|
|
m_loop->data = nullptr;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Loop> Loop::Create() {
|
|
|
|
|
auto loop = std::make_shared<Loop>(private_init{});
|
2020-12-28 12:58:06 -08:00
|
|
|
if (uv_loop_init(&loop->m_loopStruct) < 0) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
loop->m_loop = &loop->m_loopStruct;
|
|
|
|
|
loop->m_loop->data = loop.get();
|
|
|
|
|
return loop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Loop> Loop::GetDefault() {
|
|
|
|
|
static std::shared_ptr<Loop> loop = std::make_shared<Loop>(private_init{});
|
|
|
|
|
loop->m_loop = uv_default_loop();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!loop->m_loop) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
loop->m_loop->data = loop.get();
|
|
|
|
|
return loop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Loop::Close() {
|
|
|
|
|
int err = uv_loop_close(m_loop);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (err < 0) {
|
|
|
|
|
ReportError(err);
|
|
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 23:24:59 -07:00
|
|
|
void Loop::Walk(function_ref<void(Handle&)> callback) {
|
2020-06-27 20:39:00 -07:00
|
|
|
uv_walk(
|
|
|
|
|
m_loop,
|
|
|
|
|
[](uv_handle_t* handle, void* func) {
|
|
|
|
|
auto& h = *static_cast<Handle*>(handle->data);
|
2021-10-20 23:24:59 -07:00
|
|
|
auto& f = *static_cast<function_ref<void(Handle&)>*>(func);
|
2020-06-27 20:39:00 -07:00
|
|
|
f(h);
|
|
|
|
|
},
|
|
|
|
|
&callback);
|
2018-07-17 01:06:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Loop::Fork() {
|
|
|
|
|
int err = uv_loop_fork(m_loop);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (err < 0) {
|
|
|
|
|
ReportError(err);
|
|
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
}
|