From: ljsebald <ljs...@us...> - 2023-08-19 19:24:45
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via c6ddbb13e8bb9d5feb8f992a992845c9500a4eea (commit) via 76d520850f13b77fef2ccfa45c4cd357d1214349 (commit) from 89148902083e680415afe1976458551d3ed321a7 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit c6ddbb13e8bb9d5feb8f992a992845c9500a4eea Author: Falco Girgis <gyr...@gm...> Date: Sat Aug 19 14:21:43 2023 -0500 Removing NULL check warning from posix_memalign (#270) Removing NULL check warning from posix_memalign commit 76d520850f13b77fef2ccfa45c4cd357d1214349 Author: Falco Girgis <gyr...@gm...> Date: Sat Aug 19 14:20:39 2023 -0500 Implement POSIX clock_gettime() and friends (#266) * Initial impl of POSIX clock_gettime() and friends - added POSIX preprocessor defines to time.h for compile-time feature checks - added undefined clock types to time.h - added clock_gettime(), clock_settime(), and clock_getres() to time.h - created subdirectory for POSIX-related functionality - added clock_gettime.c * Corrected file path in Makefile heading * Fixed incorrect filename in clock_gettime.c header * Updated changelog * Removed C11 version check - Removed C11 verson check, and am only checking for !ANSI - This means the implementation, clock_gettime.c, must now be compiled with gnu extendsions in the Makefile (duh). - Changed parameter names to match timespec_get() * Update include/kos/time.h Co-authored-by: Lawrence Sebald <ljs...@us...> * Update include/kos/time.h Co-authored-by: Lawrence Sebald <ljs...@us...> * Update kernel/libc/posix/clock_gettime.c Co-authored-by: Lawrence Sebald <ljs...@us...> * Addressed code review requests - Spacing/indentation fixed - Move inner switch/case scope variables to main function scope * Rearranged #ifdefs in time.h, addressing feedback --------- Co-authored-by: Lawrence Sebald <ljs...@us...> ----------------------------------------------------------------------- Summary of changes: doc/CHANGELOG | 1 + include/kos/time.h | 32 +++++++++++++++-- kernel/libc/posix/Makefile | 4 +-- kernel/libc/posix/clock_gettime.c | 71 ++++++++++++++++++++++++++++++++++++++ kernel/libc/posix/posix_memalign.c | 5 +++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 kernel/libc/posix/clock_gettime.c diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 68bd32c..70f838c 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -179,6 +179,7 @@ KallistiOS version 2.1.0 ----------------------------------------------- - DC Add example for rumble accessory use [DH] - *** Split init flags from <arch/arch.h> into <kos/init.h> [LS] - *** Implemented posix_memalign() from stdlib.h [FG] +- *** Added clock_gettime(), clock_settime(), clock_getres() [FG] KallistiOS version 2.0.0 ----------------------------------------------- - DC Broadband Adapter driver fixes [Megan Potter == MP] diff --git a/include/kos/time.h b/include/kos/time.h index d32da92..738230c 100644 --- a/include/kos/time.h +++ b/include/kos/time.h @@ -14,12 +14,12 @@ #ifndef __KOS_TIME_H #define __KOS_TIME_H -#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L) || (__cplusplus >= 201703L) - #include <kos/cdefs.h> __BEGIN_DECLS +#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L) || (__cplusplus >= 201703L) + /* Forward declaration. */ struct timespec; @@ -27,7 +27,33 @@ struct timespec; extern int timespec_get(struct timespec *ts, int base); +#endif /* !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L) || (__cplusplus >= 201703L) */ + +#if !defined(__STRICT_ANSI__) || (_POSIX_C_SOURCE >= 199309L) + +#ifndef _POSIX_TIMERS +#define _POSIX_TIMERS 1 +#endif +#ifdef _POSIX_MONOTONIC_CLOCK +#define _POSIX_MONOTONIC_CLOCK 1 +#endif +#ifdef _POSIX_CPUTIME +#define _POSIX_CPUTIME 1 +#endif +#ifdef _POSIX_THREAD_CPU_TIME +#define _POSIX_THREAD_CPUTIME 1 +#endif + +#define CLOCK_MONOTONIC (CLOCK_REALTIME + 1) +#define CLOCK_PROCESS_CPUTIME_ID (CLOCK_REALTIME + 2) +#define CLOCK_THREAD_CPUTIME_ID (CLOCK_REALTIME + 3) + +extern int clock_getres(__clockid_t clk_id, struct timespec *res); +extern int clock_gettime(__clockid_t clk_id, struct timespec *tp); +extern int clock_settime(__clockid_t clk_id, const struct timespec *tp); + +#endif /* !defined(__STRICT_ANSI__) || (_POSIX_C_SOURCE >= 199309L) */ + __END_DECLS -#endif /* !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 201112L) || (__cplusplus >= 201703L) */ #endif /* !__KOS_TIME_H */ diff --git a/kernel/libc/posix/Makefile b/kernel/libc/posix/Makefile index 8e07e19..24587fa 100644 --- a/kernel/libc/posix/Makefile +++ b/kernel/libc/posix/Makefile @@ -10,7 +10,7 @@ # are not provided as part of Newlib. # -CFLAGS += -std=c11 -OBJS = posix_memalign.o +CFLAGS += -std=gnu11 +OBJS = posix_memalign.o clock_gettime.o include $(KOS_BASE)/Makefile.prefab diff --git a/kernel/libc/posix/clock_gettime.c b/kernel/libc/posix/clock_gettime.c new file mode 100644 index 0000000..c76eba8 --- /dev/null +++ b/kernel/libc/posix/clock_gettime.c @@ -0,0 +1,71 @@ +/* KallistiOS ##version## + + clock_gettime.c + Copyright (C) 2023 Falco Girgis +*/ + +#include <time.h> +#include <arch/timer.h> +#include <arch/rtc.h> +#include <errno.h> + +int clock_getres(clockid_t clk_id, struct timespec *ts) { + switch(clk_id) { + case CLOCK_REALTIME: + case CLOCK_MONOTONIC: + case CLOCK_PROCESS_CPUTIME_ID: + case CLOCK_THREAD_CPUTIME_ID: + if(!ts) { + errno = EFAULT; + return -1; + } + ts->tv_sec = 0; + ts->tv_nsec = 1000 * 1000; + return 0; + + default: + errno = EINVAL; + return -1; + } +} + +int clock_gettime(clockid_t clk_id, struct timespec *ts) { + uint32_t secs, msecs; + + if(!ts) { + errno = EFAULT; + return -1; + } + + switch(clk_id) { + case CLOCK_REALTIME: + return timespec_get(ts, TIME_UTC) == TIME_UTC ? 0 : -1; + + case CLOCK_MONOTONIC: + case CLOCK_PROCESS_CPUTIME_ID: + case CLOCK_THREAD_CPUTIME_ID: + timer_ms_gettime(&secs, &msecs); + ts->tv_sec = secs; + ts->tv_nsec = msecs * 1000 * 1000; + return 0; + + default: + errno = EINVAL; + return -1; + } +} + +int clock_settime(clockid_t clk_id, const struct timespec *ts) { + switch(clk_id) { + case CLOCK_REALTIME: + if(!ts) { + errno = EFAULT; + return -1; + } + return rtc_set_unix_secs(ts->tv_sec); + + default: + errno = EINVAL; + return -1; + } +} diff --git a/kernel/libc/posix/posix_memalign.c b/kernel/libc/posix/posix_memalign.c index a5b2591..8a5b6c4 100644 --- a/kernel/libc/posix/posix_memalign.c +++ b/kernel/libc/posix/posix_memalign.c @@ -9,6 +9,9 @@ #include <stdlib.h> #include <assert.h> +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnonnull-compare" + static inline int is_power_of_two(size_t x) { return (x & (x - 1)) == 0; } @@ -43,3 +46,5 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) { return *memptr ? 0 : ENOMEM; } + +#pragma GCC diagnostic pop hooks/post-receive -- A pseudo Operating System for the Dreamcast. |