WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
EventLoopRunner.hpp
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#pragma once
6
7#include <functional>
8#include <memory>
9
10#include "wpi/net/uv/Loop.hpp"
12
13namespace wpi::net {
14
15/**
16 * Executes an event loop on a separate thread.
17 */
19 public:
20 using LoopFunc = std::function<void(uv::Loop&)>;
21
24
25 /**
26 * Stop the loop. Once the loop is stopped it cannot be restarted.
27 * This function does not return until the loop has exited.
28 */
29 void Stop();
30
31 /**
32 * Run a function asynchronously (once) on the loop.
33 * This is safe to call from any thread, but is NOT safe to call from the
34 * provided function (it will deadlock).
35 * @param func function to execute on the loop
36 */
37 void ExecAsync(LoopFunc func);
38
39 /**
40 * Run a function synchronously (once) on the loop.
41 * This is safe to call from any thread, but is NOT safe to call from the
42 * provided function (it will deadlock).
43 * This does not return until the function finishes executing.
44 * @param func function to execute on the loop
45 */
46 void ExecSync(LoopFunc func);
47
48 /**
49 * Get the loop. If the loop thread is not running, returns nullptr.
50 * @return The loop
51 */
52 std::shared_ptr<uv::Loop> GetLoop();
53
54 private:
55 class Thread;
57};
58
59} // namespace wpi::net
std::shared_ptr< uv::Loop > GetLoop()
Get the loop.
void ExecSync(LoopFunc func)
Run a function synchronously (once) on the loop.
std::function< void(uv::Loop &)> LoopFunc
Definition EventLoopRunner.hpp:20
void Stop()
Stop the loop.
void ExecAsync(LoopFunc func)
Run a function asynchronously (once) on the loop.
Event loop.
Definition Loop.hpp:35
Definition SafeThread.hpp:123
Definition raw_socket_ostream.hpp:9