Seastar
High performance C++ framework for concurrent servers
lowres_clock.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  * Copyright (C) 2016 ScyllaDB
20  */
21 
22 #pragma once
23 
24 #ifndef SEASTAR_MODULE
25 #include <seastar/core/cacheline.hh>
26 #include <seastar/core/timer.hh>
27 #include <seastar/util/modules.hh>
28 
29 #include <cstdint>
30 
31 #include <atomic>
32 #include <chrono>
33 #endif
34 
35 namespace seastar {
36 
37 SEASTAR_MODULE_EXPORT_BEGIN
38 
39 //
40 // Forward declarations.
41 //
42 
43 class lowres_clock;
44 class lowres_system_clock;
45 
47 
48 //
59 class lowres_clock final {
60 public:
61  using rep = std::chrono::steady_clock::rep;
62  using period = std::chrono::steady_clock::period;
63  using duration = std::chrono::steady_clock::duration;
64  using time_point = std::chrono::time_point<lowres_clock, duration>;
65  static constexpr bool is_steady = true;
66 private:
67 #ifdef SEASTAR_BUILD_SHARED_LIBS
68  static thread_local time_point _now;
69 #else
70  // Use inline variable to prevent the compiler from introducing an initialization guard
71  inline static thread_local time_point _now;
72 #endif
73 public:
77  static time_point now() noexcept {
78  return _now;
79  }
80 
81  static void update() noexcept;
82 };
83 
93 class lowres_system_clock final {
94 public:
95  using rep = std::chrono::system_clock::rep;
96  using period = std::chrono::system_clock::period;
97  using duration = std::chrono::system_clock::duration;
98  using time_point = std::chrono::time_point<lowres_system_clock, duration>;
99  static constexpr bool is_steady = false;
100 private:
101 #ifdef SEASTAR_BUILD_SHARED_LIBS
102  static thread_local time_point _now;
103 #else
104  // Use inline variable to prevent the compiler from introducing an initialization guard
105  inline static thread_local time_point _now;
106 #endif
107  friend class lowres_clock; // for updates
108 public:
112  static time_point now() noexcept {
113  return _now;
114  }
115 
116  static std::time_t to_time_t(time_point t) noexcept {
117  return std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count();
118  }
119 
120  static time_point from_time_t(std::time_t t) noexcept {
121  return time_point(std::chrono::duration_cast<duration>(std::chrono::seconds(t)));
122  }
123 };
124 
125 extern template class timer<lowres_clock>;
126 
127 SEASTAR_MODULE_EXPORT_END
128 }
129 
Low-resolution and efficient steady clock.
Definition: lowres_clock.hh:59
static time_point now() noexcept
Definition: lowres_clock.hh:77
Low-resolution and efficient system clock.
Definition: lowres_clock.hh:93
static time_point now() noexcept
Definition: lowres_clock.hh:112
Seastar API namespace.
Definition: abort_on_ebadf.hh:26