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
|
|
|
|
2022-05-07 10:54:14 -07:00
|
|
|
#include "wpinet/uv/Work.h"
|
2018-07-17 01:06:24 -07:00
|
|
|
|
2022-05-07 10:54:14 -07:00
|
|
|
#include "wpinet/uv/Loop.h"
|
2018-07-17 01:06:24 -07:00
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace wpi::uv {
|
2018-07-17 01:06:24 -07:00
|
|
|
|
|
|
|
|
WorkReq::WorkReq() {
|
|
|
|
|
error = [this](Error err) { GetLoop().error(err); };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QueueWork(Loop& loop, const std::shared_ptr<WorkReq>& req) {
|
2020-06-27 20:39:00 -07:00
|
|
|
int err = uv_queue_work(
|
|
|
|
|
loop.GetRaw(), req->GetRaw(),
|
|
|
|
|
[](uv_work_t* req) {
|
|
|
|
|
auto& h = *static_cast<WorkReq*>(req->data);
|
|
|
|
|
h.work();
|
|
|
|
|
},
|
|
|
|
|
[](uv_work_t* req, int status) {
|
|
|
|
|
auto& h = *static_cast<WorkReq*>(req->data);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (status < 0) {
|
2020-06-27 20:39:00 -07:00
|
|
|
h.ReportError(status);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-06-27 20:39:00 -07:00
|
|
|
h.afterWork();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-06-27 20:39:00 -07:00
|
|
|
h.Release(); // this is always a one-shot
|
|
|
|
|
});
|
2020-12-28 12:58:06 -08:00
|
|
|
if (err < 0) {
|
2018-07-17 01:06:24 -07:00
|
|
|
loop.ReportError(err);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-07-17 01:06:24 -07:00
|
|
|
req->Keep();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QueueWork(Loop& loop, std::function<void()> work,
|
|
|
|
|
std::function<void()> afterWork) {
|
|
|
|
|
auto req = std::make_shared<WorkReq>();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (work) {
|
2021-10-20 23:24:59 -07:00
|
|
|
req->work.connect(std::move(work));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (afterWork) {
|
2021-10-20 23:24:59 -07:00
|
|
|
req->afterWork.connect(std::move(afterWork));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
QueueWork(loop, req);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace wpi::uv
|