diff options
author | Gaius Mulley <gaiusmod2@gmail.com> | 2023-08-12 18:17:41 +0100 |
---|---|---|
committer | Gaius Mulley <gaiusmod2@gmail.com> | 2023-08-12 18:17:41 +0100 |
commit | 63fb0bedb8077ac1e6b6337f198b4eae30813fbc (patch) | |
tree | 5a66a3f4f6f25ab7d12256efaa1afe1d2be13b13 /libgm2/libm2iso/wrapclock.cc | |
parent | 46905fcde00fd84eb06b6bd1a6e788171d32865b (diff) | |
download | gcc-63fb0bedb8077ac1e6b6337f198b4eae30813fbc.zip gcc-63fb0bedb8077ac1e6b6337f198b4eae30813fbc.tar.gz gcc-63fb0bedb8077ac1e6b6337f198b4eae30813fbc.tar.bz2 |
PR modula2/110779 SysClock can not read the clock (Darwin portability fixes)
This patch adds corrections to defensively check against glibc functions,
structures and contains fallbacks. These fixes were required under Darwin.
gcc/m2/ChangeLog:
PR modula2/110779
* gm2-libs-iso/SysClock.mod (EpochTime): New procedure.
(GetClock): Call EpochTime if the C time functions are
unavailable.
* gm2-libs-iso/wrapclock.def (istimezone): New function
definition.
libgm2/ChangeLog:
PR modula2/110779
* configure: Regenerate.
* configure.ac: Provide special case test for Darwin cross
configuration.
(GLIBCXX_CONFIGURE): New statement.
(GLIBCXX_CHECK_GETTIMEOFDAY): New statement.
(GLIBCXX_ENABLE_LIBSTDCXX_TIME): New statement.
* libm2iso/wrapclock.cc: New sys/time.h conditional include.
(sys/syscall.h): Conditional include.
(unistd.h): Conditional include.
(GetTimeRealtime): Re-implement.
(SetTimeRealtime): Re-implement.
(timezone): Re-implement.
(istimezone): New function.
(daylight): Re-implement.
(isdst): Re-implement.
(tzname): Re-implement.
Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
Diffstat (limited to 'libgm2/libm2iso/wrapclock.cc')
-rw-r--r-- | libgm2/libm2iso/wrapclock.cc | 201 |
1 files changed, 115 insertions, 86 deletions
diff --git a/libgm2/libm2iso/wrapclock.cc b/libgm2/libm2iso/wrapclock.cc index 91ac96f..1f4ca8c 100644 --- a/libgm2/libm2iso/wrapclock.cc +++ b/libgm2/libm2iso/wrapclock.cc @@ -51,6 +51,18 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include "time.h" #endif +// Conditional inclusion of sys/time.h for gettimeofday +#if !defined(_GLIBCXX_USE_CLOCK_MONOTONIC) && \ + !defined(_GLIBCXX_USE_CLOCK_REALTIME) && \ + defined(_GLIBCXX_USE_GETTIMEOFDAY) +#include <sys/time.h> +#endif + +#if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL) +#include <unistd.h> +#include <sys/syscall.h> +#endif + #if defined(HAVE_MALLOC_H) #include "malloc.h" #endif @@ -64,91 +76,19 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #endif -extern "C" long int -EXPORT(timezone) (void) -{ -#if defined(HAVE_STRUCT_TM) && defined(HAVE_STRUCT_TIMESPEC) - struct tm result; - struct timespec ts; - -#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_TM_TM_GMTOFF) - if (clock_gettime (CLOCK_REALTIME, &ts) == 0) - { - time_t time = ts.tv_sec; - localtime_r (&time, &result); - return result.tm_gmtoff; - } - else -#endif -#endif - return timezone; -} - - -extern "C" int -EXPORT(daylight) (void) -{ -#if defined(HAVE_DAYLIGHT) - return daylight; -#else - return 0; -#endif -} - - -/* isdst returns 1 if daylight saving time is currently in effect and - returns 0 if it is not. */ - -extern "C" int -EXPORT(isdst) (void) -{ -#if defined(HAVE_STRUCT_TM) && defined(HAVE_STRUCT_TIMESPEC) - struct tm result; - struct timespec ts; - -#if defined(HAVE_CLOCK_SETTIME) - if (clock_gettime (CLOCK_REALTIME, &ts) == 0) - { - time_t time = ts.tv_sec; - localtime_r (&time, &result); - return result.tm_isdst; - } - else -#endif -#endif - return 0; -} - - -/* tzname returns the string associated with the local timezone. - The daylight value is 0 or 1. The value 0 returns the non - daylight saving timezone string and the value of 1 returns the - daylight saving timezone string. It returns NULL if tzname is - unavailable. */ - -extern "C" char * -EXPORT(tzname) (int daylight) -{ -#if defined(HAVE_TZNAME) - return tzname[daylight]; -#else - return NULL; -#endif -} - - /* GetTimeRealtime performs return gettime (CLOCK_REALTIME, ts). gettime returns 0 on success and -1 on failure. If the underlying system does not have gettime then GetTimeRealtime returns 1. */ -#if defined(HAVE_STRUCT_TIMESPEC) +#if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME) extern "C" int EXPORT(GetTimeRealtime) (struct timespec *ts) { -#if defined(HAVE_CLOCK_GETTIME) - return clock_gettime (CLOCK_REALTIME, ts); + timespec tp; +#if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL) + return syscall (SYS_clock_gettime, CLOCK_REALTIME, ts); #else - return 1; + return clock_gettime (CLOCK_REALTIME, ts); #endif } @@ -161,16 +101,17 @@ EXPORT(GetTimeRealtime) (void *ts) } #endif - /* SetTimeRealtime performs return settime (CLOCK_REALTIME, ts). gettime returns 0 on success and -1 on failure. If the underlying system does not have gettime then GetTimeRealtime returns 1. */ -#if defined(HAVE_STRUCT_TIMESPEC) +#if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME) extern "C" int EXPORT(SetTimeRealtime) (struct timespec *ts) { -#if defined(HAVE_CLOCK_GETTIME) +#if defined(_GLIBCXX_USE_CLOCK_SETTIME_SYSCALL) + return syscall (SYS_clock_settime, CLOCK_REALTIME, ts); +#elif defined(HAVE_CLOCK_SETTIME) return clock_settime (CLOCK_REALTIME, ts); #else return 1; @@ -186,7 +127,6 @@ EXPORT(SetTimeRealtime) (void *ts) } #endif - /* InitTimespec returns a newly created opaque type. */ #if defined(HAVE_STRUCT_TIMESPEC) @@ -209,7 +149,6 @@ EXPORT(InitTimespec) (void) } #endif - /* KillTimeval deallocates the memory associated with an opaque type. */ #if defined(HAVE_STRUCT_TIMESPEC) @@ -231,9 +170,8 @@ EXPORT(KillTimespec) (void *ts) } #endif - /* GetTimespec retrieves the number of seconds and nanoseconds from the - timespec. */ + timespec. 1 is returned if successful and 0 otherwise. */ #if defined(HAVE_STRUCT_TIMESPEC) extern "C" int @@ -256,8 +194,8 @@ EXPORT(GetTimespec) (void *ts, unsigned long *sec, unsigned long *nano) } #endif - -/* SetTimespec sets the number of seconds and nanoseconds into timespec. */ +/* SetTimespec sets the number of seconds and nanoseconds into timespec. + 1 is returned if successful and 0 otherwise. */ #if defined(HAVE_STRUCT_TIMESPEC) extern "C" int @@ -281,6 +219,97 @@ EXPORT(SetTimespec) (void *ts, unsigned long sec, unsigned long nano) } #endif +extern "C" long int +EXPORT(timezone) (void) +{ +#if defined(HAVE_STRUCT_TIMESPEC) + struct tm result; + struct timespec ts; + +#if defined(HAVE_TM_TM_GMTOFF) + if (EXPORT(GetTimeRealtime) (&ts) == 0) + { + time_t time = ts.tv_sec; + localtime_r (&time, &result); + return result.tm_gmtoff; + } + else +#endif +#endif + { +#if defined(HAVE_TIMEZONE) + return timezone; +#else + return 0; +#endif + } +} + +/* istimezone returns 1 if timezone in wrapclock.cc can resolve the + timezone value using the timezone C library call or by using + clock_gettime, localtime_r and tm_gmtoff. */ + +extern "C" int +EXPORT(istimezone) (void) +{ +#if defined(HAVE_STRUCT_TIMESPEC) +#if defined(HAVE_TM_TM_GMTOFF) +#if defined(_GLIBCXX_USE_CLOCK_REALTIME) + return 1; +#endif +#endif +#endif + return 0; +} + +extern "C" int +EXPORT(daylight) (void) +{ +#if defined(HAVE_DAYLIGHT) + return daylight; +#else + return 0; +#endif +} + +/* isdst returns 1 if daylight saving time is currently in effect and + returns 0 if it is not. */ + +extern "C" int +EXPORT(isdst) (void) +{ +#if defined(HAVE_STRUCT_TIMESPEC) + struct tm result; + struct timespec ts; + + if (EXPORT(GetTimeRealtime) (&ts) == 0) + { + time_t time = ts.tv_sec; + localtime_r (&time, &result); + return result.tm_isdst; + } + else + return 0; +#else + return 0; +#endif +} + +/* tzname returns the string associated with the local timezone. + The daylight value is 0 or 1. The value 0 returns the non + daylight saving timezone string and the value of 1 returns the + daylight saving timezone string. It returns NULL if tzname is + unavailable. */ + +extern "C" char * +EXPORT(tzname) (int daylight) +{ +#if defined(HAVE_TZNAME) + return tzname[daylight]; +#else + return NULL; +#endif +} /* init - init/finish functions for the module */ |