|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/internal/rest_pure_completion_queue_impl.h" |
| 16 | +#include "google/cloud/internal/timer_queue.h" |
| 17 | + |
| 18 | +namespace google { |
| 19 | +namespace cloud { |
| 20 | +namespace rest_internal { |
| 21 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 22 | + |
| 23 | +RestPureCompletionQueue::RestPureCompletionQueue() |
| 24 | + : impl_(std::make_shared<RestPureCompletionQueueImpl>()) {} |
| 25 | + |
| 26 | +RestPureCompletionQueueImpl::RestPureCompletionQueueImpl() |
| 27 | + : tq_(internal::TimerQueue::Create()) {} |
| 28 | + |
| 29 | +void RestPureCompletionQueueImpl::Run() { tq_->Service(); } |
| 30 | + |
| 31 | +void RestPureCompletionQueueImpl::Shutdown() { tq_->Shutdown(); } |
| 32 | + |
| 33 | +void RestPureCompletionQueueImpl::CancelAll() {} |
| 34 | + |
| 35 | +future<StatusOr<std::chrono::system_clock::time_point>> |
| 36 | +RestPureCompletionQueueImpl::MakeDeadlineTimer( |
| 37 | + std::chrono::system_clock::time_point deadline) { |
| 38 | + return tq_->Schedule(deadline); |
| 39 | +} |
| 40 | + |
| 41 | +future<StatusOr<std::chrono::system_clock::time_point>> |
| 42 | +RestPureCompletionQueueImpl::MakeRelativeTimer( |
| 43 | + std::chrono::nanoseconds duration) { |
| 44 | + using std::chrono::system_clock; |
| 45 | + auto d = std::chrono::duration_cast<system_clock::duration>(duration); |
| 46 | + if (d < duration) d += system_clock::duration{1}; |
| 47 | + return MakeDeadlineTimer(system_clock::now() + d); |
| 48 | +} |
| 49 | + |
| 50 | +// Use an "immediately" expiring timer in order to get the thread(s) servicing |
| 51 | +// the TimerQueue to execute the function. However, if the timer |
| 52 | +// expires before .then() is invoked, the lambda will be immediately called and |
| 53 | +// the passing of execution to the queue servicing thread will not occur. |
| 54 | +void RestPureCompletionQueueImpl::RunAsync( |
| 55 | + std::unique_ptr<internal::RunAsyncBase> function) { |
| 56 | + ++run_async_counter_; |
| 57 | + tq_->Schedule([f = std::move(function)](auto) { f->exec(); }); |
| 58 | +} |
| 59 | + |
| 60 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 61 | +} // namespace rest_internal |
| 62 | +} // namespace cloud |
| 63 | +} // namespace google |
0 commit comments