MySQL  8.0.18
Source Code Documentation
my_systime.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation. The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23 
24 #ifndef MY_SYSTIME_INCLUDED
25 #define MY_SYSTIME_INCLUDED
26 
27 /**
28  @file include/my_systime.h
29  Defines for getting and processing the current system type programmatically.
30 */
31 
32 #include <time.h> // time_t, struct timespec (C11/C++17)
33 #include <chrono> // std::chrono::microseconds
34 #include <cstdint> // std::int64_t
35 #include <limits> // std::numeric_limits
36 #include <thread> // std::this_thread::wait_for
37 
38 #include "my_config.h"
39 
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h> // clock_gettime()
42 #endif /* HAVE_SYS_TIME_H */
43 
44 using UTC_clock = std::chrono::system_clock;
45 
46 /* Bits for get_date timeflag */
47 constexpr const int GETDATE_DATE_TIME = 1;
48 constexpr const int GETDATE_SHORT_DATE = 2;
49 constexpr const int GETDATE_HHMMSSTIME = 4;
50 constexpr const int GETDATE_GMT = 8;
51 constexpr const int GETDATE_FIXEDLENGTH = 16;
52 constexpr const int GETDATE_T_DELIMITER = 32;
53 constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64;
54 
55 /**
56  Wait a given number of microseconds.
57 
58  @param m_seconds number of microseconds to wait.
59 */
60 inline void my_sleep(time_t m_seconds) {
61  std::this_thread::sleep_for(std::chrono::microseconds{m_seconds});
62 }
63 
64 #ifdef _WIN32
65 
66 #include <windows.h>
67 
68 /****************************************************************************
69 ** Replacements for localtime_r and gmtime_r
70 ****************************************************************************/
71 
72 inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
73  localtime_s(tmp, timep);
74  return tmp;
75 }
76 
77 inline struct tm *gmtime_r(const time_t *clock, struct tm *res) {
78  gmtime_s(res, clock);
79  return res;
80 }
81 
82 /**
83  Sleep the given number of seconds. POSIX compatibility.
84 
85  @param seconds number of seconds to wait
86 */
87 inline void sleep(unsigned long seconds) {
88  std::this_thread::sleep_for(std::chrono::seconds{seconds});
89 }
90 
91 #endif /* _WIN32 */
92 
93 /**
94  Get high-resolution time. Forwards to std::chrono.
95 
96  @deprecated New code should use std::chrono directly.
97 
98  @return current high-resolution time in multiples of 100 nanoseconds.
99 */
100 inline unsigned long long int my_getsystime() {
101 #ifdef HAVE_CLOCK_GETTIME
102  // Performance regression testing showed this to be preferable
103  struct timespec tp;
104  clock_gettime(CLOCK_REALTIME, &tp);
105  return (static_cast<unsigned long long int>(tp.tv_sec) * 10000000 +
106  static_cast<unsigned long long int>(tp.tv_nsec) / 100);
107 #else
108  return std::chrono::duration_cast<
109  std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>>(
110  UTC_clock::now().time_since_epoch())
111  .count();
112 #endif /* HAVE_CLOCK_GETTIME */
113 }
114 
115 /**
116  The maximum timespec value used to represent "inifinity" (as when
117  requesting an "inifinite" timeout.
118  */
119 constexpr const timespec TIMESPEC_POSINF = {
120  std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999};
121 
122 /** Type alias to reduce chance of coversion errors on timeout values. */
123 using Timeout_type = std::uint64_t;
124 
125 /** Value representing "infinite" timeout. */
126 constexpr const Timeout_type TIMEOUT_INF =
127  std::numeric_limits<Timeout_type>::max() - 1;
128 
129 void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec);
130 void set_timespec(struct timespec *abstime, Timeout_type sec);
131 timespec timespec_now();
132 
133 /**
134  Compare two timespec structs.
135 
136  @retval 1 If ts1 ends after ts2.
137  @retval -1 If ts1 ends before ts2.
138  @retval 0 If ts1 is equal to ts2.
139 */
140 inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
141  if (ts1->tv_sec > ts2->tv_sec ||
142  (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
143  return 1;
144  if (ts1->tv_sec < ts2->tv_sec ||
145  (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
146  return -1;
147  return 0;
148 }
149 
150 /**
151  Calculate the diff between two timespec values.
152 
153  @return difference in nanoseconds between ts1 and ts2
154 */
155 inline unsigned long long int diff_timespec(struct timespec *ts1,
156  struct timespec *ts2) {
157  return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL + ts1->tv_nsec -
158  ts2->tv_nsec;
159 }
160 
161 /**
162  Return current time. Takes an int argument
163  for backward compatibility. This argument is ignored.
164 
165  @deprecated New code should use std::time() directly.
166 
167  @retval current time.
168 */
169 inline time_t my_time(int) { return time(nullptr); }
170 
171 /**
172  Return time in microseconds. Uses std::chrono::high_resolution_clock
173 
174  @remark This function is to be used to measure performance in
175  micro seconds.
176 
177  @deprecated New code should use std::chrono directly.
178 
179  @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000
180  (UTC)
181 */
182 inline unsigned long long int my_micro_time() {
183 #ifdef _WIN32
184  return std::chrono::duration_cast<std::chrono::microseconds>(
185  UTC_clock::now().time_since_epoch())
186  .count();
187 #else
188  struct timeval t;
189  /*
190  The following loop is here because gettimeofday may fail on some systems
191  */
192  while (gettimeofday(&t, nullptr) != 0) {
193  }
194  return (static_cast<unsigned long long int>(t.tv_sec) * 1000000 + t.tv_usec);
195 #endif /* _WIN32 */
196 }
197 
198 void get_date(char *to, int flag, time_t date);
199 
200 #endif // MY_SYSTIME_INCLUDED
ssize_t count
Definition: memcached.c:386
Include file for Sun RPC to compile out of the box.
timespec timespec_now()
void set_timespec(struct timespec *abstime, Timeout_type sec)
Set the value of a timespec object to the current time plus a number of seconds using seconds...
Definition: my_systime.cc:82
constexpr const int GETDATE_HHMMSSTIME
Definition: my_systime.h:49
int sleep(int seconds)
Definition: win32.c:256
unsigned long long int diff_timespec(struct timespec *ts1, struct timespec *ts2)
Calculate the diff between two timespec values.
Definition: my_systime.h:155
std::uint64_t Timeout_type
Type alias to reduce chance of coversion errors on timeout values.
Definition: my_systime.h:123
constexpr const Timeout_type TIMEOUT_INF
Value representing "infinite" timeout.
Definition: my_systime.h:126
constexpr const int GETDATE_DATE_TIME
Definition: my_systime.h:47
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:100
time_t my_time(int)
Return current time.
Definition: my_systime.h:169
void my_sleep(time_t m_seconds)
Wait a given number of microseconds.
Definition: my_systime.h:60
constexpr const timespec TIMESPEC_POSINF
The maximum timespec value used to represent "inifinity" (as when requesting an "inifinite" timeout...
Definition: my_systime.h:119
unsigned long long int my_micro_time()
Return time in microseconds.
Definition: my_systime.h:182
double seconds()
Definition: task.c:298
static int flag
Definition: hp_test1.cc:39
void get_date(char *to, int flag, time_t date)
Store textual representation of date in a character array.
Definition: my_systime.cc:107
std::chrono::system_clock UTC_clock
Definition: my_systime.h:44
constexpr const int GETDATE_FIXEDLENGTH
Definition: my_systime.h:51
int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_systime.h:140
void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec)
Set the value of a timespec object to the current time plus a number of nanosconds.
Definition: my_systime.cc:56
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR
Definition: my_systime.h:53
constexpr const int GETDATE_SHORT_DATE
Definition: my_systime.h:48
static time_t abstime(const rel_time_t exptime)
Convert the relative time to an absolute time (relative to EPOC ;) )
Definition: memcached.c:271
constexpr const int GETDATE_GMT
Definition: my_systime.h:50
constexpr const int GETDATE_T_DELIMITER
Definition: my_systime.h:52
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:157