MySQL 8.3.0
Source Code Documentation
my_xp_util.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef MY_XP_UTIL_INCLUDED
24#define MY_XP_UTIL_INCLUDED
25
27
28#ifdef _WIN32
29#include <winsock2.h> // Must come before <windows.h>.
30
31#include <windows.h>
32#else
33#include <unistd.h>
34#endif
35
36#if defined(_WIN32)
37#include <Winsock2.h>
38#include <ws2tcpip.h>
39#ifndef INET_ADDRSTRLEN
40#define socklen_t int
41#endif
42#else
43#include <arpa/inet.h>
44#include <netinet/in.h>
45#include <netinet/tcp.h>
46#include <sys/socket.h>
47#include <sys/time.h>
48#include <sys/types.h>
49#include <time.h>
50#endif
51#include <errno.h>
52#include <stdint.h>
53#include <cassert>
54#include <iostream>
55
56#define INT_MAX32 0x7FFFFFFFL
57#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
58
59#ifdef _WIN32
60
61#define OFFSET_TO_EPOC ((__int64)134774 * 24 * 60 * 60 * 1000 * 1000 * 10)
62#define MS 10000000
63
64#include <time.h>
65
66#endif
67
68/**
69 @class My_xp_util
70
71 Class where cross platform utilities reside as static methods.
72*/
74 public:
75 /**
76 Current thread sleeps for the parameterized number of seconds.
77
78 @param seconds number of seconds for invoking thread to sleep
79 */
80
81 static void sleep_seconds(unsigned int seconds);
82
83 /**
84 Get the system's time.
85
86 @return system's time in 100s of nanoseconds
87 */
88
89 static uint64_t getsystime(void);
90
91 /**
92 Set the value of the timespec struct equal to the argument in nanoseconds.
93 */
94
95 static inline void set_timespec_nsec(struct timespec *abstime,
96 uint64_t nsec) {
97 uint64_t now = My_xp_util::getsystime() + (nsec / 100);
98 uint64_t tv_sec = now / 10000000ULL;
99#if SIZEOF_TIME_T < SIZEOF_LONG_LONG
100 /* Ensure that the number of seconds don't overflow. */
101 tv_sec = MY_MIN(tv_sec, ((uint64_t)INT_MAX32));
102#endif
103 abstime->tv_sec = (time_t)tv_sec;
104 abstime->tv_nsec = (now % 10000000ULL) * 100 + (nsec % 100);
105 }
106
107 /**
108 Set the value of the timespec struct equal to the argument in seconds.
109 */
110
111 static inline void set_timespec(struct timespec *abstime, uint64_t sec) {
112 My_xp_util::set_timespec_nsec(abstime, sec * 1000000000ULL);
113 }
114
115 /**
116 Compare two timespec structs.
117
118 @retval 1 If ts1 ends after ts2.
119 @retval -1 If ts1 ends before ts2.
120 @retval 0 If ts1 is equal to ts2.
121 */
122
123 static inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
124 if (ts1->tv_sec > ts2->tv_sec ||
125 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
126 return 1;
127 if (ts1->tv_sec < ts2->tv_sec ||
128 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
129 return -1;
130 return 0;
131 }
132
133 /**
134 Diff two timespec structs.
135 ts1 has to be larger than ts2, otherwise it will return unexpected value.
136
137 @return difference between the two arguments.
138 */
139
140 static inline uint64_t diff_timespec(struct timespec *ts1,
141 struct timespec *ts2) {
142 return static_cast<uint64_t>(ts1->tv_sec - ts2->tv_sec) * 1000000000ULL +
143 static_cast<uint64_t>(ts1->tv_nsec) -
144 static_cast<uint32_t>(ts2->tv_nsec);
145 }
146};
147
148/**
149 @class My_xp_socket_util
150
151 Interface for socket utility methods.
152*/
154 public:
155 /**
156 Disable Nagle algorithm in the specified socket.
157
158 @param fd file descriptor of the socket
159 */
160
161 virtual int disable_nagle_in_socket(int fd) = 0;
162
163 virtual ~My_xp_socket_util() = default;
164};
165
167 public:
168 int disable_nagle_in_socket(int fd) override;
169 explicit My_xp_socket_util_impl() = default;
170 ~My_xp_socket_util_impl() override = default;
171};
172
173#endif // MY_XP_UTIL_INCLUDED
Definition: my_xp_util.h:166
~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:40
Interface for socket utility methods.
Definition: my_xp_util.h:153
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:73
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:111
static uint64_t diff_timespec(struct timespec *ts1, struct timespec *ts2)
Diff two timespec structs.
Definition: my_xp_util.h:140
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:95
static int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_xp_util.h:123
static void sleep_seconds(unsigned int seconds)
Current thread sleeps for the parameterized number of seconds.
Definition: my_xp_util.cc:30
static uint64_t getsystime(void)
Get the system's time.
Definition: my_xp_util.cc:38
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:165
#define MY_MIN(a, b)
Definition: my_xp_util.h:57
#define INT_MAX32
Definition: my_xp_util.h:56
double seconds()
Definition: task.cc:309
Include file for Sun RPC to compile out of the box.