]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/sleep.hh
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / seastar / include / seastar / core / sleep.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
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,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19 /*
20 * Copyright (C) 2015 Cloudius Systems, Ltd.
21 */
22
23 #pragma once
24
25 #include <chrono>
26 #include <functional>
27
28 #include <seastar/core/abort_source.hh>
29 #include <seastar/core/future.hh>
30 #include <seastar/core/lowres_clock.hh>
31 #include <seastar/core/timer.hh>
32
33 namespace seastar {
34
35 /// \file
36
37 /// Returns a future which completes after a specified time has elapsed.
38 ///
39 /// \param dur minimum amount of time before the returned future becomes
40 /// ready.
41 /// \return A \ref future which becomes ready when the sleep duration elapses.
42 template <typename Clock = steady_clock_type, typename Rep, typename Period>
43 future<> sleep(std::chrono::duration<Rep, Period> dur) {
44 struct sleeper {
45 promise<> done;
46 timer<Clock> tmr;
47 sleeper(std::chrono::duration<Rep, Period> dur)
48 : tmr([this] { done.set_value(); })
49 {
50 tmr.arm(dur);
51 }
52 };
53 sleeper *s = new sleeper(dur);
54 future<> fut = s->done.get_future();
55 return fut.then([s] { delete s; });
56 }
57
58 /// exception that is thrown when application is in process of been stopped
59 class sleep_aborted : public abort_requested_exception {
60 public:
61 /// Reports the exception reason.
62 virtual const char* what() const noexcept {
63 return "Sleep is aborted";
64 }
65 };
66
67 /// Returns a future which completes after a specified time has elapsed
68 /// or throws \ref sleep_aborted exception if application is aborted
69 ///
70 /// \param dur minimum amount of time before the returned future becomes
71 /// ready.
72 /// \return A \ref future which becomes ready when the sleep duration elapses.
73 template <typename Clock = steady_clock_type>
74 future<> sleep_abortable(typename Clock::duration dur);
75
76 extern template future<> sleep_abortable<steady_clock_type>(typename steady_clock_type::duration);
77 extern template future<> sleep_abortable<lowres_clock>(typename lowres_clock::duration);
78
79 /// Returns a future which completes after a specified time has elapsed
80 /// or throws \ref sleep_aborted exception if the sleep is aborted.
81 ///
82 /// \param dur minimum amount of time before the returned future becomes
83 /// ready.
84 /// \param as the \ref abort_source that eventually notifies that the sleep
85 /// should be aborted.
86 /// \return A \ref future which becomes ready when the sleep duration elapses.
87 template <typename Clock = steady_clock_type>
88 future<> sleep_abortable(typename Clock::duration dur, abort_source& as);
89
90 extern template future<> sleep_abortable<steady_clock_type>(typename steady_clock_type::duration, abort_source&);
91 extern template future<> sleep_abortable<lowres_clock>(typename lowres_clock::duration, abort_source&);
92
93 }