WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
Work.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_WORK_H_
6#define WPINET_UV_WORK_H_
7
8#include <uv.h>
9
10#include <functional>
11#include <memory>
12#include <utility>
13
14#include <wpi/Signal.h>
15
16#include "wpinet/uv/Request.h"
17
18namespace wpi::uv {
19
20class Loop;
21
22/**
23 * Work request.
24 * For use with `QueueWork()` function family.
25 */
26class WorkReq : public RequestImpl<WorkReq, uv_work_t> {
27 public:
29
30 Loop& GetLoop() const { return *static_cast<Loop*>(GetRaw()->loop->data); }
31
32 /**
33 * Function(s) that will be run on the thread pool.
34 */
36
37 /**
38 * Function(s) that will be run on the loop thread after the work on the
39 * thread pool has been completed by the work callback.
40 */
42};
43
44/**
45 * Initializes a work request which will run on the thread pool.
46 *
47 * @param loop Event loop
48 * @param req request
49 */
50void QueueWork(Loop& loop, const std::shared_ptr<WorkReq>& req);
51
52/**
53 * Initializes a work request which will run on the thread pool.
54 *
55 * @param loop Event loop
56 * @param req request
57 */
58inline void QueueWork(const std::shared_ptr<Loop>& loop,
59 const std::shared_ptr<WorkReq>& req) {
60 QueueWork(*loop, req);
61}
62
63/**
64 * Initializes a work request which will run on the thread pool. The work
65 * callback will be run on a thread from the thread pool, and the afterWork
66 * callback will be called on the loop thread after the work completes. Any
67 * errors are forwarded to the loop.
68 *
69 * @param loop Event loop
70 * @param work Work callback (called from separate thread)
71 * @param afterWork After work callback (called on loop thread)
72 */
73void QueueWork(Loop& loop, std::function<void()> work,
74 std::function<void()> afterWork);
75
76/**
77 * Initializes a work request which will run on the thread pool. The work
78 * callback will be run on a thread from the thread pool, and the afterWork
79 * callback will be called on the loop thread after the work completes. Any
80 * errors are forwarded to the loop.
81 *
82 * @param loop Event loop
83 * @param work Work callback (called from separate thread)
84 * @param afterWork After work callback (called on loop thread)
85 */
86inline void QueueWork(const std::shared_ptr<Loop>& loop,
87 std::function<void()> work,
88 std::function<void()> afterWork) {
89 QueueWork(*loop, std::move(work), std::move(afterWork));
90}
91
92} // namespace wpi::uv
93
94#endif // WPINET_UV_WORK_H_
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition Signal.h:495
Event loop.
Definition Loop.h:37
Request.
Definition Request.h:135
uv_work_t * GetRaw() noexcept
Definition Request.h:150
Work request.
Definition Work.h:26
sig::Signal afterWork
Function(s) that will be run on the loop thread after the work on the thread pool has been completed ...
Definition Work.h:41
Loop & GetLoop() const
Definition Work.h:30
sig::Signal work
Function(s) that will be run on the thread pool.
Definition Work.h:35
Definition Loop.h:22
void QueueWork(Loop &loop, const std::shared_ptr< WorkReq > &req)
Initializes a work request which will run on the thread pool.
void * data
Definition uv.h:1907
UV_REQ_FIELDS uv_loop_t * loop
Definition uv.h:1161