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