Seastar
High performance C++ framework for concurrent servers
sleep.hh
Go to the documentation of this file.
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 #ifndef SEASTAR_MODULE
26 #include <chrono>
27 #include <functional>
28 
29 #include <seastar/core/abort_source.hh>
30 #include <seastar/core/future.hh>
31 #include <seastar/core/lowres_clock.hh>
32 #include <seastar/core/manual_clock.hh>
33 #include <seastar/core/timer.hh>
34 #include <seastar/util/modules.hh>
35 #endif
36 
37 namespace seastar {
38 
39 SEASTAR_MODULE_EXPORT_BEGIN
40 
42 
48 template <typename Clock = steady_clock_type, typename Rep, typename Period>
49 future<> sleep(std::chrono::duration<Rep, Period> dur) {
50  struct sleeper {
51  promise<> done;
52  timer<Clock> tmr;
53  sleeper(std::chrono::duration<Rep, Period> dur)
54  : tmr([this] { done.set_value(); })
55  {
56  tmr.arm(dur);
57  }
58  };
59  sleeper *s = new sleeper(dur);
60  future<> fut = s->done.get_future();
61  return fut.then([s] { delete s; });
62 }
63 
66 public:
68  virtual const char* what() const noexcept {
69  return "Sleep is aborted";
70  }
71 };
72 
79 template <typename Clock = steady_clock_type>
80 future<> sleep_abortable(typename Clock::duration dur);
81 
82 extern template future<> sleep_abortable<steady_clock_type>(typename steady_clock_type::duration);
83 extern template future<> sleep_abortable<lowres_clock>(typename lowres_clock::duration);
84 
93 template <typename Clock = steady_clock_type>
94 future<> sleep_abortable(typename Clock::duration dur, abort_source& as);
95 
96 extern template future<> sleep_abortable<steady_clock_type>(typename steady_clock_type::duration, abort_source&);
97 extern template future<> sleep_abortable<lowres_clock>(typename lowres_clock::duration, abort_source&);
98 extern template future<> sleep_abortable<manual_clock>(typename manual_clock::duration, abort_source&);
99 
100 SEASTAR_MODULE_EXPORT_END
101 }
Definition: abort_source.hh:49
Definition: abort_source.hh:59
Result then(Func &&func) noexcept
Schedule a block of code to run when the future is ready.
Definition: future.hh:1410
void set_value(A &&... a) noexcept
Sets the promises value.
Definition: future.hh:982
exception that is thrown when application is in process of been stopped
Definition: sleep.hh:65
virtual const char * what() const noexcept
Reports the exception reason.
Definition: sleep.hh:68
void arm(time_point until, std::optional< duration > period={}) noexcept
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
future sleep_abortable(typename Clock::duration dur)
future sleep(std::chrono::duration< Rep, Period > dur)
Definition: sleep.hh:49