Skip to content

Commit

Permalink
Bug#29041740: libmytime: Create separate convenience library of my_ti…
Browse files Browse the repository at this point in the history
…me.cc and link mysys and other libraries/executables with it.

- Refactor time functions into my_time and my_systime.
- Re-implement functions with std::chrono where possible/desirable.
- Move the mysql_field_type enum to include/field_types.h and make it fwd-declarable in C++11 and up.

Change-Id: Iebca7c694827607bc18f04b1105988d7c7e4e7f4
  • Loading branch information
Dyre Tjeldvoll authored and Steinar H. Gunderson committed Jan 11, 2019
1 parent 6f61591 commit b8be8ee
Show file tree
Hide file tree
Showing 138 changed files with 905 additions and 892 deletions.
3 changes: 2 additions & 1 deletion client/mysqldump.cc
@@ -1,5 +1,5 @@
/*
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -47,6 +47,7 @@
#include "my_io.h"
#include "my_macros.h"
#include "my_sys.h"
#include "my_systime.h" // GETDATE_DATE_TIME
#include "my_user.h"
#include "mysql.h"
#include "mysql/service_mysql_alloc.h"
Expand Down
1 change: 1 addition & 0 deletions client/mysqltest.cc
Expand Up @@ -83,6 +83,7 @@
#include "my_macros.h"
#include "my_pointer_arithmetic.h"
#include "my_stacktrace.h"
#include "my_systime.h" // my_sleep()
#include "my_thread_local.h"
#include "prealloced_array.h"
#include "print_version.h"
Expand Down
5 changes: 3 additions & 2 deletions components/mysql_server/log_builtins_filter.cc
@@ -1,4 +1,4 @@
/* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
/* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -78,7 +78,8 @@
#include "log_builtins_filter_imp.h"
#include "log_builtins_imp.h"
#include "my_atomic.h"
#include "mysys_err.h" // EE_ERROR_LAST for globerrs
#include "my_systime.h" // my_micro_time()
#include "mysys_err.h" // EE_ERROR_LAST for globerrs
#include "sql/derror.h"
#include "sql/log.h"
// for the default rules
Expand Down
4 changes: 2 additions & 2 deletions include/CMakeLists.txt
@@ -1,4 +1,4 @@
# Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -35,7 +35,7 @@ SET(HEADERS
mysql_time.h
${CMAKE_CURRENT_BINARY_DIR}/mysql_version.h
${CMAKE_CURRENT_BINARY_DIR}/mysqld_error.h
../libbinlogevents/export/binary_log_types.h
field_types.h
)
SET(HEADERS_MYSQL_DIR
mysql/client_plugin.h
Expand Down
29 changes: 17 additions & 12 deletions libbinlogevents/export/binary_log_types.h → include/field_types.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
/* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -26,7 +26,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */

/**
@file binary_log_types.h
@file field_types.h
@brief This file contains the field type.
Expand All @@ -35,12 +35,12 @@
definitions have to be constructed to support this.
*/

#ifndef BINARY_LOG_TYPES_INCLUDED
#define BINARY_LOG_TYPES_INCLUDED
#ifndef FIELD_TYPES_INCLUDED
#define FIELD_TYPES_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

/*
* Constants exported from this package.
Expand All @@ -49,8 +49,12 @@ extern "C" {
/**
Column types for MySQL
*/
typedef enum enum_field_types {
MYSQL_TYPE_DECIMAL,
enum enum_field_types
#if defined(__cplusplus) && __cplusplus > 201103L
// N2764: Forward enum declarations, added in C++11
: int
#endif /* __cplusplus */
{ MYSQL_TYPE_DECIMAL,
MYSQL_TYPE_TINY,
MYSQL_TYPE_SHORT,
MYSQL_TYPE_LONG,
Expand Down Expand Up @@ -80,11 +84,12 @@ typedef enum enum_field_types {
MYSQL_TYPE_BLOB = 252,
MYSQL_TYPE_VAR_STRING = 253,
MYSQL_TYPE_STRING = 254,
MYSQL_TYPE_GEOMETRY = 255
} enum_field_types;
MYSQL_TYPE_GEOMETRY = 255 };

#ifdef __cplusplus
}
#endif // __cplusplus
} // extern "C"
#else
typedef enum enum_field_types enum_field_types;
#endif /* __cplusplus */

#endif /* BINARY_LOG_TYPES_INCLUDED */
#endif /* FIELD_TYPES_INCLUDED */
26 changes: 1 addition & 25 deletions include/my_sys.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -174,15 +174,6 @@ struct MEM_ROOT;
#define MY_ERRNO_EDOM 33
#define MY_ERRNO_ERANGE 34

/* Bits for get_date timeflag */
#define GETDATE_DATE_TIME 1
#define GETDATE_SHORT_DATE 2
#define GETDATE_HHMMSSTIME 4
#define GETDATE_GMT 8
#define GETDATE_FIXEDLENGTH 16
#define GETDATE_T_DELIMITER 32
#define GETDATE_SHORT_DATE_FULL_YEAR 64

/* defines when allocating data */
extern void *my_multi_malloc(PSI_memory_key key, myf flags, ...);

Expand Down Expand Up @@ -733,7 +724,6 @@ extern char *my_load_path(char *to, const char *path,
const char *own_path_prefix);
extern bool array_append_string_unique(const char *str, const char **array,
size_t size);
extern void get_date(char *to, int timeflag, time_t use_time);

void my_store_ptr(uchar *buff, size_t pack_length, my_off_t pos);
my_off_t my_get_ptr(uchar *ptr, size_t pack_length);
Expand Down Expand Up @@ -821,23 +811,9 @@ extern uchar *my_compress_alloc(const uchar *packet, size_t *len,
size_t *complen);
extern ha_checksum my_checksum(ha_checksum crc, const uchar *mem, size_t count);

/* Wait a given number of microseconds */
static inline void my_sleep(time_t m_seconds) {
#if defined(_WIN32)
Sleep((DWORD)m_seconds / 1000 + 1); /* Sleep() has millisecond arg */
#else
struct timeval t;
t.tv_sec = m_seconds / 1000000L;
t.tv_usec = m_seconds % 1000000L;
select(0, 0, 0, 0, &t); /* sleep */
#endif
}

extern uint my_set_max_open_files(uint files);
void my_free_open_file_info(void);

extern time_t my_time(myf flags);
extern ulonglong my_micro_time();
extern bool my_gethwaddr(uchar *to);

#ifdef HAVE_SYS_MMAN_H
Expand Down
118 changes: 104 additions & 14 deletions include/my_systime.h
@@ -1,5 +1,5 @@
/*
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand Down Expand Up @@ -31,38 +31,94 @@
std::chrono::steady_clock instead.
*/

#include <time.h>
#include <time.h> // time_t, struct timespec (C11/C++17)
#include <chrono> // system_clock, high_resolution_clock
#include <cstdint> // std::int64_t
#include <limits> // std::numeric_limits
#include <thread> // std::this_thread::wait_for

#include "my_config.h"
#include "my_inttypes.h"
#include "my_macros.h"

using UTC_clock = std::chrono::system_clock;

/* Bits for get_date timeflag */
constexpr const int GETDATE_DATE_TIME = 1;
constexpr const int GETDATE_SHORT_DATE = 2;
constexpr const int GETDATE_HHMMSSTIME = 4;
constexpr const int GETDATE_GMT = 8;
constexpr const int GETDATE_FIXEDLENGTH = 16;
constexpr const int GETDATE_T_DELIMITER = 32;
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64;

/**
Wait a given number of microseconds.
@param m_seconds number of microseconds to wait.
*/
inline void my_sleep(time_t m_seconds) {
std::this_thread::sleep_for(std::chrono::microseconds{m_seconds});
}

#ifdef _WIN32

#include <windows.h>

static inline void sleep(unsigned long seconds) { Sleep(seconds * 1000); }

/****************************************************************************
** Replacements for localtime_r and gmtime_r
****************************************************************************/

static inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
localtime_s(tmp, timep);
return tmp;
}

static inline struct tm *gmtime_r(const time_t *clock, struct tm *res) {
inline struct tm *gmtime_r(const time_t *clock, struct tm *res) {
gmtime_s(res, clock);
return res;
}

/**
Sleep the given number of seconds. POSIX compatibility.
@param seconds number of seconds to wait
*/
inline void sleep(unsigned long seconds) {
std::this_thread::sleep_for(std::chrono::seconds{seconds});
}

#endif /* _WIN32 */

ulonglong my_getsystime(void);
/**
Get high-resolution time. Forwards to std::chrono.
@deprecated New code should use std::chrono directly.
@return current high-resolution time in multiples of 100 nanoseconds.
*/
inline unsigned long long int my_getsystime() {
return std::chrono::duration_cast<
std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>>(
UTC_clock::now().time_since_epoch())
.count();
}

/**
The maximum timespec value used to represent "inifinity" (as when
requesting an "inifinite" timeout.
*/
constexpr const timespec TIMESPEC_POSINF = {
std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999};

/** Type alias to reduce chance of coversion errors on timeout values. */
using Timeout_type = std::uint64_t;

void set_timespec_nsec(struct timespec *abstime, ulonglong nsec);
/** Value representing "infinite" timeout. */
constexpr const Timeout_type TIMEOUT_INF =
std::numeric_limits<Timeout_type>::max() - 1;

void set_timespec(struct timespec *abstime, ulonglong sec);
void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec);
void set_timespec(struct timespec *abstime, Timeout_type sec);
timespec timespec_now();

/**
Compare two timespec structs.
Expand All @@ -71,7 +127,7 @@ void set_timespec(struct timespec *abstime, ulonglong sec);
@retval -1 If ts1 ends before ts2.
@retval 0 If ts1 is equal to ts2.
*/
static inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
if (ts1->tv_sec > ts2->tv_sec ||
(ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
return 1;
Expand All @@ -81,10 +137,44 @@ static inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
return 0;
}

static inline ulonglong diff_timespec(struct timespec *ts1,
struct timespec *ts2) {
/**
Calculate the diff between two timespec values.
@return difference in nanoseconds between ts1 and ts2
*/
inline unsigned long long int diff_timespec(struct timespec *ts1,
struct timespec *ts2) {
return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL + ts1->tv_nsec -
ts2->tv_nsec;
}

/**
Return current time. Takes an int argument
for backward compatibility. This argument is ignored.
@deprecated New code should use std::time() directly.
@retval current time.
*/
inline time_t my_time(int) { return time(nullptr); }

/**
Return time in microseconds. Uses std::chrono::high_resolution_clock
@remark This function is to be used to measure performance in
micro seconds.
@deprecated New code should use std::chrono directly.
@retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000
(UTC)
*/
inline unsigned long long int my_micro_time() {
return std::chrono::duration_cast<std::chrono::microseconds>(
UTC_clock::now().time_since_epoch())
.count();
}

void get_date(char *to, int flag, time_t date);

#endif // MY_SYSTIME_INCLUDED

0 comments on commit b8be8ee

Please sign in to comment.