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 <chrono>
30#endif
31
32namespace seastar {
33
34SEASTAR_MODULE_EXPORT_BEGIN
35
36//
37// Forward declarations.
38//
39
40class lowres_clock;
41class lowres_system_clock;
42
44
45//
56class lowres_clock final {
57public:
58 using rep = std::chrono::steady_clock::rep;
59 using period = std::chrono::steady_clock::period;
60 using duration = std::chrono::steady_clock::duration;
61 using time_point = std::chrono::time_point<lowres_clock, duration>;
62 static constexpr bool is_steady = true;
63private:
64#ifdef SEASTAR_BUILD_SHARED_LIBS
65 static thread_local time_point _now;
66#else
67 // Use inline variable to prevent the compiler from introducing an initialization guard
68 inline static thread_local time_point _now;
69#endif
70public:
74 static time_point now() noexcept {
75 return _now;
76 }
77
78 static void update() noexcept;
79};
80
91public:
92 using rep = std::chrono::system_clock::rep;
93 using period = std::chrono::system_clock::period;
94 using duration = std::chrono::system_clock::duration;
95 using time_point = std::chrono::time_point<lowres_system_clock, duration>;
96 static constexpr bool is_steady = false;
97private:
98#ifdef SEASTAR_BUILD_SHARED_LIBS
99 static thread_local time_point _now;
100#else
101 // Use inline variable to prevent the compiler from introducing an initialization guard
102 inline static thread_local time_point _now;
103#endif
104 friend class lowres_clock; // for updates
105public:
109 static time_point now() noexcept {
110 return _now;
111 }
112
113 static std::time_t to_time_t(time_point t) noexcept {
114 return std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count();
115 }
116
117 static time_point from_time_t(std::time_t t) noexcept {
118 return time_point(std::chrono::duration_cast<duration>(std::chrono::seconds(t)));
119 }
120};
121
122extern template class timer<lowres_clock>;
123
124SEASTAR_MODULE_EXPORT_END
125}
126
Low-resolution and efficient steady clock.
Definition: lowres_clock.hh:56
static time_point now() noexcept
Definition: lowres_clock.hh:74
Low-resolution and efficient system clock.
Definition: lowres_clock.hh:90
static time_point now() noexcept
Definition: lowres_clock.hh:109
Seastar API namespace.
Definition: abort_on_ebadf.hh:26