|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "mrc/coroutines/scheduler.hpp" |
| 19 | +#include "mrc/coroutines/task.hpp" |
| 20 | + |
| 21 | +#include <chrono> |
| 22 | +#include <coroutine> |
| 23 | +#include <queue> |
| 24 | +#include <utility> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +namespace mrc::coroutines { |
| 30 | + |
| 31 | +class TestScheduler : public Scheduler |
| 32 | +{ |
| 33 | + private: |
| 34 | + struct Operation |
| 35 | + { |
| 36 | + public: |
| 37 | + Operation(TestScheduler* self, std::chrono::time_point<std::chrono::steady_clock> time); |
| 38 | + |
| 39 | + static constexpr bool await_ready() |
| 40 | + { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + void await_suspend(std::coroutine_handle<> handle); |
| 45 | + |
| 46 | + void await_resume() {} |
| 47 | + |
| 48 | + private: |
| 49 | + TestScheduler* m_self; |
| 50 | + std::chrono::time_point<std::chrono::steady_clock> m_time; |
| 51 | + }; |
| 52 | + |
| 53 | + using item_t = std::pair<std::coroutine_handle<>, std::chrono::time_point<std::chrono::steady_clock>>; |
| 54 | + struct ItemCompare |
| 55 | + { |
| 56 | + bool operator()(item_t& lhs, item_t& rhs); |
| 57 | + }; |
| 58 | + |
| 59 | + std::priority_queue<item_t, std::vector<item_t>, ItemCompare> m_queue; |
| 60 | + std::chrono::time_point<std::chrono::steady_clock> m_time = std::chrono::steady_clock::now(); |
| 61 | + |
| 62 | + public: |
| 63 | + /** |
| 64 | + * @brief Enqueue's the coroutine handle to be resumed at the current logical time. |
| 65 | + */ |
| 66 | + void resume(std::coroutine_handle<> handle) noexcept override; |
| 67 | + |
| 68 | + /** |
| 69 | + * Suspends the current function and enqueue's it to be resumed at the current logical time. |
| 70 | + */ |
| 71 | + mrc::coroutines::Task<> yield() override; |
| 72 | + |
| 73 | + /** |
| 74 | + * Suspends the current function and enqueue's it to be resumed at the current logica time + the given duration. |
| 75 | + */ |
| 76 | + mrc::coroutines::Task<> yield_for(std::chrono::milliseconds time) override; |
| 77 | + |
| 78 | + /** |
| 79 | + * Suspends the current function and enqueue's it to be resumed at the given logical time. |
| 80 | + */ |
| 81 | + mrc::coroutines::Task<> yield_until(std::chrono::time_point<std::chrono::steady_clock> time) override; |
| 82 | + |
| 83 | + /** |
| 84 | + * Immediately resumes the next-in-queue coroutine handle. |
| 85 | + * |
| 86 | + * @return true if more coroutines exist in the queue after resuming, false otherwise. |
| 87 | + */ |
| 88 | + bool resume_next(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Immediately resumes next-in-queue coroutines up to the current logical time + the given duration, in-order. |
| 92 | + * |
| 93 | + * @return true if more coroutines exist in the queue after resuming, false otherwise. |
| 94 | + */ |
| 95 | + bool resume_for(std::chrono::milliseconds time); |
| 96 | + |
| 97 | + /** |
| 98 | + * Immediately resumes next-in-queue coroutines up to the given logical time. |
| 99 | + * |
| 100 | + * @return true if more coroutines exist in the queue after resuming, false otherwise. |
| 101 | + */ |
| 102 | + bool resume_until(std::chrono::time_point<std::chrono::steady_clock> time); |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace mrc::coroutines |
0 commit comments