Files
allwpilib/wpinet/src/main/native/include/wpi/net/EventLoopRunner.hpp

60 lines
1.5 KiB
C++
Raw Normal View History

// 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.
#pragma once
#include <functional>
#include <memory>
2025-11-07 19:56:21 -05:00
#include "wpi/net/uv/Loop.hpp"
2025-11-07 19:57:55 -05:00
#include "wpi/util/SafeThread.hpp"
2025-11-07 20:00:05 -05:00
namespace wpi::net {
/**
* Executes an event loop on a separate thread.
*/
class EventLoopRunner {
public:
using LoopFunc = std::function<void(uv::Loop&)>;
2018-08-14 21:01:20 -07:00
EventLoopRunner();
virtual ~EventLoopRunner();
/**
* Stop the loop. Once the loop is stopped it cannot be restarted.
* This function does not return until the loop has exited.
*/
void Stop();
/**
* Run a function asynchronously (once) on the loop.
* This is safe to call from any thread, but is NOT safe to call from the
* provided function (it will deadlock).
* @param func function to execute on the loop
*/
2018-08-14 21:01:20 -07:00
void ExecAsync(LoopFunc func);
/**
* Run a function synchronously (once) on the loop.
* This is safe to call from any thread, but is NOT safe to call from the
* provided function (it will deadlock).
* This does not return until the function finishes executing.
* @param func function to execute on the loop
*/
2018-08-14 21:01:20 -07:00
void ExecSync(LoopFunc func);
/**
* Get the loop. If the loop thread is not running, returns nullptr.
* @return The loop
*/
std::shared_ptr<uv::Loop> GetLoop();
private:
class Thread;
2025-11-07 20:00:05 -05:00
wpi::util::SafeThreadOwner<Thread> m_owner;
};
2025-11-07 20:00:05 -05:00
} // namespace wpi::net