MySQL 8.0.39
Source Code Documentation
my_xp_util.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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_XP_UTIL_INCLUDED
25#define MY_XP_UTIL_INCLUDED
26
28
29#ifdef _WIN32
30#include <winsock2.h> // Must come before <windows.h>.
31
32#include <windows.h>
33#else
34#include <unistd.h>
35#endif
36
37#if defined(_WIN32)
38#include <Winsock2.h>
39#include <ws2tcpip.h>
40#ifndef INET_ADDRSTRLEN
41#define socklen_t int
42#endif
43#else
44#include <arpa/inet.h>
45#include <netinet/in.h>
46#include <netinet/tcp.h>
47#include <sys/socket.h>
48#include <sys/time.h>
49#include <sys/types.h>
50#include <time.h>
51#endif
52#include <errno.h>
53#include <stdint.h>
54#include <cassert>
55#include <iostream>
56
57#define INT_MAX32 0x7FFFFFFFL
58#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
59
60#ifdef _WIN32
61
62#define OFFSET_TO_EPOC ((__int64)134774 * 24 * 60 * 60 * 1000 * 1000 * 10)
63#define MS 10000000
64
65#include <time.h>
66
67#endif
68
69/**
70 @class My_xp_util
71
72 Class where cross platform utilities reside as static methods.
73*/
75 public:
76 /**
77 Current thread sleeps for the parameterized number of seconds.
78
79 @param seconds number of seconds for invoking thread to sleep
80 */
81
82 static void sleep_seconds(unsigned int seconds);
83
84 /**
85 Get the system's time.
86
87 @return system's time in 100s of nanoseconds
88 */
89
90 static uint64_t getsystime(void);
91
92 /**
93 Set the value of the timespec struct equal to the argument in nanoseconds.
94 */
95
96 static inline void set_timespec_nsec(struct timespec *abstime,
97 uint64_t nsec) {
98 uint64_t now = My_xp_util::getsystime() + (nsec / 100);
99 uint64_t tv_sec = now / 10000000ULL;
100#if SIZEOF_TIME_T < SIZEOF_LONG_LONG
101 /* Ensure that the number of seconds don't overflow. */
102 tv_sec = MY_MIN(tv_sec, ((uint64_t)INT_MAX32));
103#endif
104 abstime->tv_sec = (time_t)tv_sec;
105 abstime->tv_nsec = (now % 10000000ULL) * 100 + (nsec % 100);
106 }
107
108 /**
109 Set the value of the timespec struct equal to the argument in seconds.
110 */
111
112 static inline void set_timespec(struct timespec *abstime, uint64_t sec) {
113 My_xp_util::set_timespec_nsec(abstime, sec * 1000000000ULL);
114 }
115
116 /**
117 Compare two timespec structs.
118
119 @retval 1 If ts1 ends after ts2.
120 @retval -1 If ts1 ends before ts2.
121 @retval 0 If ts1 is equal to ts2.
122 */
123
124 static inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
125 if (ts1->tv_sec > ts2->tv_sec ||
126 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
127 return 1;
128 if (ts1->tv_sec < ts2->tv_sec ||
129 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
130 return -1;
131 return 0;
132 }
133
134 /**
135 Diff two timespec structs.
136 ts1 has to be larger than ts2, otherwise it will return unexpected value.
137
138 @return difference between the two arguments.
139 */
140
141 static inline uint64_t diff_timespec(struct timespec *ts1,
142 struct timespec *ts2) {
143 return static_cast<uint64_t>(ts1->tv_sec - ts2->tv_sec) * 1000000000ULL +
144 static_cast<uint64_t>(ts1->tv_nsec) -
145 static_cast<uint32_t>(ts2->tv_nsec);
146 }
147};
148
149/**
150 @class My_xp_socket_util
151
152 Interface for socket utility methods.
153*/
155 public:
156 /**
157 Disable Nagle algorithm in the specified socket.
158
159 @param fd file descriptor of the socket
160 */
161
162 virtual int disable_nagle_in_socket(int fd) = 0;
163
164 virtual ~My_xp_socket_util() = default;
165};
166
168 public:
169 int disable_nagle_in_socket(int fd) override;
170 explicit My_xp_socket_util_impl() = default;
171 ~My_xp_socket_util_impl() override = default;
172};
173
174#endif // MY_XP_UTIL_INCLUDED
Definition: my_xp_util.h:167
~My_xp_socket_util_impl() override=default
My_xp_socket_util_impl()=default
int disable_nagle_in_socket(int fd) override
Disable Nagle algorithm in the specified socket.
Definition: my_xp_util.cc:41
Interface for socket utility methods.
Definition: my_xp_util.h:154
virtual int disable_nagle_in_socket(int fd)=0
Disable Nagle algorithm in the specified socket.
virtual ~My_xp_socket_util()=default
Class where cross platform utilities reside as static methods.
Definition: my_xp_util.h:74
static void set_timespec(struct timespec *abstime, uint64_t sec)
Set the value of the timespec struct equal to the argument in seconds.
Definition: my_xp_util.h:112
static uint64_t diff_timespec(struct timespec *ts1, struct timespec *ts2)
Diff two timespec structs.
Definition: my_xp_util.h:141
static void set_timespec_nsec(struct timespec *abstime, uint64_t nsec)
Set the value of the timespec struct equal to the argument in nanoseconds.
Definition: my_xp_util.h:96
static int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_xp_util.h:124
static void sleep_seconds(unsigned int seconds)
Current thread sleeps for the parameterized number of seconds.
Definition: my_xp_util.cc:31
static uint64_t getsystime(void)
Get the system's time.
Definition: my_xp_util.cc:39
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:162
#define MY_MIN(a, b)
Definition: my_xp_util.h:58
#define INT_MAX32
Definition: my_xp_util.h:57
double seconds()
Definition: task.cc:310
Include file for Sun RPC to compile out of the box.