Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/EventLoopRunner.h" |
| 6 | |
| 7 | #include "wpi/SmallVector.h" |
| 8 | #include "wpi/condition_variable.h" |
| 9 | #include "wpi/mutex.h" |
| 10 | #include "wpi/uv/AsyncFunction.h" |
| 11 | #include "wpi/uv/Loop.h" |
| 12 | |
| 13 | using namespace wpi; |
| 14 | |
| 15 | class EventLoopRunner::Thread : public SafeThread { |
| 16 | public: |
| 17 | using UvExecFunc = uv::AsyncFunction<void(LoopFunc)>; |
| 18 | |
| 19 | Thread() : m_loop(uv::Loop::Create()) { |
| 20 | // set up async handles |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 21 | if (!m_loop) { |
| 22 | return; |
| 23 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | |
| 25 | // run function |
| 26 | m_doExec = UvExecFunc::Create( |
| 27 | m_loop, [loop = m_loop.get()](auto out, LoopFunc func) { |
| 28 | func(*loop); |
| 29 | out.set_value(); |
| 30 | }); |
| 31 | } |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | void Main() override { |
| 34 | if (m_loop) { |
| 35 | m_loop->Run(); |
| 36 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // the loop |
| 40 | std::shared_ptr<uv::Loop> m_loop; |
| 41 | |
| 42 | // run function |
| 43 | std::weak_ptr<UvExecFunc> m_doExec; |
| 44 | }; |
| 45 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 46 | EventLoopRunner::EventLoopRunner() { |
| 47 | m_owner.Start(); |
| 48 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 50 | EventLoopRunner::~EventLoopRunner() { |
| 51 | Stop(); |
| 52 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | |
| 54 | void EventLoopRunner::Stop() { |
| 55 | ExecAsync([](uv::Loop& loop) { |
| 56 | // close all handles; this will (eventually) stop the loop |
| 57 | loop.Walk([](uv::Handle& h) { |
| 58 | h.SetLoopClosing(true); |
| 59 | h.Close(); |
| 60 | }); |
| 61 | }); |
| 62 | m_owner.Join(); |
| 63 | } |
| 64 | |
| 65 | void EventLoopRunner::ExecAsync(LoopFunc func) { |
| 66 | if (auto thr = m_owner.GetThread()) { |
| 67 | if (auto doExec = thr->m_doExec.lock()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 68 | doExec->Call(std::move(func)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void EventLoopRunner::ExecSync(LoopFunc func) { |
| 74 | wpi::future<void> f; |
| 75 | if (auto thr = m_owner.GetThread()) { |
| 76 | if (auto doExec = thr->m_doExec.lock()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 77 | f = doExec->Call(std::move(func)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 80 | if (f.valid()) { |
| 81 | f.wait(); |
| 82 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | std::shared_ptr<uv::Loop> EventLoopRunner::GetLoop() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | if (auto thr = m_owner.GetThread()) { |
| 87 | return thr->m_loop; |
| 88 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |