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/future.h" |
| 6 | |
| 7 | namespace wpi { |
| 8 | namespace detail { |
| 9 | |
| 10 | PromiseFactoryBase::~PromiseFactoryBase() { |
| 11 | m_active = false; |
| 12 | m_resultCv.notify_all(); // wake up any waiters |
| 13 | } |
| 14 | |
| 15 | void PromiseFactoryBase::IgnoreResult(uint64_t request) { |
| 16 | std::unique_lock lock(m_resultMutex); |
| 17 | EraseRequest(request); |
| 18 | } |
| 19 | |
| 20 | uint64_t PromiseFactoryBase::CreateRequest() { |
| 21 | std::unique_lock lock(m_resultMutex); |
| 22 | uint64_t req = ++m_uid; |
| 23 | m_requests.push_back(req); |
| 24 | return req; |
| 25 | } |
| 26 | |
| 27 | bool PromiseFactoryBase::EraseRequest(uint64_t request) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | if (request == 0) { |
| 29 | return false; |
| 30 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | auto it = std::find_if(m_requests.begin(), m_requests.end(), |
| 32 | [=](auto r) { return r == request; }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | if (it == m_requests.end()) { |
| 34 | return false; // no waiters |
| 35 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | m_requests.erase(it); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | } // namespace detail |
| 41 | |
| 42 | future<void> PromiseFactory<void>::MakeReadyFuture() { |
| 43 | std::unique_lock lock(GetResultMutex()); |
| 44 | uint64_t req = CreateErasedRequest(); |
| 45 | m_results.emplace_back(req); |
| 46 | return future<void>{this, req}; |
| 47 | } |
| 48 | |
| 49 | void PromiseFactory<void>::SetValue(uint64_t request) { |
| 50 | std::unique_lock lock(GetResultMutex()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 51 | if (!EraseRequest(request)) { |
| 52 | return; |
| 53 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | auto it = std::find_if(m_thens.begin(), m_thens.end(), |
| 55 | [=](const auto& x) { return x.request == request; }); |
| 56 | if (it != m_thens.end()) { |
| 57 | uint64_t outRequest = it->outRequest; |
| 58 | ThenFunction func = std::move(it->func); |
| 59 | m_thens.erase(it); |
| 60 | lock.unlock(); |
| 61 | return func(outRequest); |
| 62 | } |
| 63 | m_results.emplace_back(request); |
| 64 | Notify(); |
| 65 | } |
| 66 | |
| 67 | void PromiseFactory<void>::SetThen(uint64_t request, uint64_t outRequest, |
| 68 | ThenFunction func) { |
| 69 | std::unique_lock lock(GetResultMutex()); |
| 70 | auto it = std::find_if(m_results.begin(), m_results.end(), |
| 71 | [=](const auto& r) { return r == request; }); |
| 72 | if (it != m_results.end()) { |
| 73 | m_results.erase(it); |
| 74 | lock.unlock(); |
| 75 | return func(outRequest); |
| 76 | } |
| 77 | m_thens.emplace_back(request, outRequest, func); |
| 78 | } |
| 79 | |
| 80 | bool PromiseFactory<void>::IsReady(uint64_t request) noexcept { |
| 81 | std::unique_lock lock(GetResultMutex()); |
| 82 | auto it = std::find_if(m_results.begin(), m_results.end(), |
| 83 | [=](const auto& r) { return r == request; }); |
| 84 | return it != m_results.end(); |
| 85 | } |
| 86 | |
| 87 | void PromiseFactory<void>::GetResult(uint64_t request) { |
| 88 | // wait for response |
| 89 | std::unique_lock lock(GetResultMutex()); |
| 90 | while (IsActive()) { |
| 91 | // Did we get a response to *our* request? |
| 92 | auto it = std::find_if(m_results.begin(), m_results.end(), |
| 93 | [=](const auto& r) { return r == request; }); |
| 94 | if (it != m_results.end()) { |
| 95 | // Yes, remove it from the vector and we're done. |
| 96 | m_results.erase(it); |
| 97 | return; |
| 98 | } |
| 99 | // No, keep waiting for a response |
| 100 | Wait(lock); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void PromiseFactory<void>::WaitResult(uint64_t request) { |
| 105 | // wait for response |
| 106 | std::unique_lock lock(GetResultMutex()); |
| 107 | while (IsActive()) { |
| 108 | // Did we get a response to *our* request? |
| 109 | auto it = std::find_if(m_results.begin(), m_results.end(), |
| 110 | [=](const auto& r) { return r == request; }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 111 | if (it != m_results.end()) { |
| 112 | return; |
| 113 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 114 | // No, keep waiting for a response |
| 115 | Wait(lock); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | PromiseFactory<void>& PromiseFactory<void>::GetInstance() { |
| 120 | static PromiseFactory<void> inst; |
| 121 | return inst; |
| 122 | } |
| 123 | |
| 124 | } // namespace wpi |