From 2a2a2752d8351f044b1fdc69beb8104f6a575245 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 1 Mar 2024 00:24:15 +0800 Subject: [PATCH] feat times: added initial functions Signed-off-by: John Sanpe --- CMakeLists.txt | 1 + src/core/xdbd_times.c | 31 +++++++++++++++++++++++++++++++ src/core/xdbd_times.h | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/core/xdbd_times.c create mode 100644 src/core/xdbd_times.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e8dea9..180438c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(SRCS ${PROJECT_SOURCE_DIR}/src/connection/connection.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_buf.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c + ${PROJECT_SOURCE_DIR}/src/core/xdbd_times.c ${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c ${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c ${PROJECT_SOURCE_DIR}/src/event/xdbd_timer.c diff --git a/src/core/xdbd_times.c b/src/core/xdbd_times.c new file mode 100644 index 0000000..22d47a0 --- /dev/null +++ b/src/core/xdbd_times.c @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#include +#include +#include +#include + +volatile xdbd_msec_t xdbd_current_msec; + +static xdbd_msec_t +xdbd_monotonic_time(void) +{ + struct timespec ts; + time_t sec; + long msec; + + clock_gettime(CLOCK_MONOTONIC, &ts); + sec = ts.tv_sec; + msec = ts.tv_nsec / 1000000; + + return (xdbd_msec_t) sec * 1000 + msec; +} + +void +xdbd_time_update(void) +{ + xdbd_current_msec = xdbd_monotonic_time(); +} diff --git a/src/core/xdbd_times.h b/src/core/xdbd_times.h new file mode 100644 index 0000000..e605dd6 --- /dev/null +++ b/src/core/xdbd_times.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef __xdbd_times__H__ +#define __xdbd_times__H__ + +#include + +extern volatile xdbd_msec_t xdbd_current_msec; + +void +xdbd_time_update(void); + +#endif /* __xdbd_times__H__ */