1 /* wrapclock.cc provides access to time related system calls.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
30 #define EXPORT(FUNC) m2iso ## _wrapclock_ ## FUNC
31 #define M2EXPORT(FUNC) m2iso ## _M2_wrapclock_ ## FUNC
32 #define M2LIBNAME "m2iso"
34 #if defined(HAVE_STDLIB_H)
38 #if defined(HAVE_UNISTD_H)
42 #if defined(HAVE_SYS_TYPES_H)
43 #include "sys/types.h"
46 #if defined(HAVE_SYS_TIME_H)
50 #if defined(HAVE_TIME_H)
54 // Conditional inclusion of sys/time.h for gettimeofday
55 #if !defined(_GLIBCXX_USE_CLOCK_MONOTONIC) && \
56 !defined(_GLIBCXX_USE_CLOCK_REALTIME) && \
57 defined(_GLIBCXX_USE_GETTIMEOFDAY)
61 #if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL)
63 #include <sys/syscall.h>
66 #if defined(HAVE_MALLOC_H)
70 #if defined(HAVE_LIMITS_H)
75 #define NULL (void *)0
78 typedef long long int longint_t
;
81 /* GetTimeRealtime performs return gettime (CLOCK_REALTIME, ts).
82 gettime returns 0 on success and -1 on failure. If the underlying
83 system does not have gettime then GetTimeRealtime returns 1. */
85 #if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME)
87 EXPORT(GetTimeRealtime
) (struct timespec
*ts
)
90 #if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL)
91 return syscall (SYS_clock_gettime
, CLOCK_REALTIME
, ts
);
93 return clock_gettime (CLOCK_REALTIME
, ts
);
100 EXPORT(GetTimeRealtime
) (void *ts
)
106 /* SetTimeRealtime performs return settime (CLOCK_REALTIME, ts).
107 gettime returns 0 on success and -1 on failure. If the underlying
108 system does not have gettime then GetTimeRealtime returns 1. */
110 #if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME)
112 EXPORT(SetTimeRealtime
) (struct timespec
*ts
)
114 #if defined(_GLIBCXX_USE_CLOCK_SETTIME_SYSCALL)
115 return syscall (SYS_clock_settime
, CLOCK_REALTIME
, ts
);
116 #elif defined(HAVE_CLOCK_SETTIME)
117 return clock_settime (CLOCK_REALTIME
, ts
);
126 EXPORT(SetTimeRealtime
) (void *ts
)
132 /* InitTimespec returns a newly created opaque type. */
134 #if defined(HAVE_STRUCT_TIMESPEC)
135 extern "C" struct timespec
*
136 EXPORT(InitTimespec
) (void)
138 #if defined(HAVE_STRUCT_TIMESPEC) && defined(HAVE_MALLOC_H)
139 return (struct timespec
*)malloc (sizeof (struct timespec
));
148 EXPORT(InitTimespec
) (void)
154 /* KillTimeval deallocates the memory associated with an opaque type. */
156 #if defined(HAVE_STRUCT_TIMESPEC)
157 extern "C" struct timespec
*
158 EXPORT(KillTimespec
) (void *ts
)
160 #if defined(HAVE_MALLOC_H)
169 EXPORT(KillTimespec
) (void *ts
)
175 /* GetTimespec retrieves the number of seconds and nanoseconds from the
176 timespec. 1 is returned if successful and 0 otherwise. */
178 #if defined(HAVE_STRUCT_TIMESPEC)
180 EXPORT(GetTimespec
) (timespec
*ts
, longint_t
*sec
, longint_t
*nano
)
182 #if defined(HAVE_STRUCT_TIMESPEC)
193 EXPORT(GetTimespec
) (void *ts
, longint_t
*sec
, longint_t
*nano
)
199 /* SetTimespec sets the number of seconds and nanoseconds into timespec.
200 1 is returned if successful and 0 otherwise. */
202 #if defined(HAVE_STRUCT_TIMESPEC)
204 EXPORT(SetTimespec
) (timespec
*ts
, longint_t sec
, longint_t nano
)
206 #if defined(HAVE_STRUCT_TIMESPEC)
218 EXPORT(SetTimespec
) (void *ts
, longint_t sec
, longint_t nano
)
225 EXPORT(timezone
) (void)
227 #if defined(HAVE_STRUCT_TIMESPEC)
231 #if defined(HAVE_TM_TM_GMTOFF)
232 if (EXPORT(GetTimeRealtime
) (&ts
) == 0)
234 time_t time
= ts
.tv_sec
;
235 localtime_r (&time
, &result
);
236 return result
.tm_gmtoff
;
243 /* istimezone returns 1 if timezone in wrapclock.cc can resolve the
244 timezone value using the timezone C library call or by using
245 clock_gettime, localtime_r and tm_gmtoff. */
248 EXPORT(istimezone
) (void)
250 #if defined(HAVE_STRUCT_TIMESPEC)
251 #if defined(HAVE_TM_TM_GMTOFF)
252 #if defined(_GLIBCXX_USE_CLOCK_REALTIME)
261 EXPORT(daylight
) (void)
263 #if defined(HAVE_DAYLIGHT)
270 /* isdst returns 1 if daylight saving time is currently in effect and
271 returns 0 if it is not. */
276 #if defined(HAVE_STRUCT_TIMESPEC)
280 if (EXPORT(GetTimeRealtime
) (&ts
) == 0)
282 time_t time
= ts
.tv_sec
;
283 localtime_r (&time
, &result
);
284 return result
.tm_isdst
;
293 /* tzname returns the string associated with the local timezone.
294 The daylight value is 0 or 1. The value 0 returns the non
295 daylight saving timezone string and the value of 1 returns the
296 daylight saving timezone string. It returns NULL if tzname is
300 EXPORT(tzname
) (int daylight
)
302 #if defined(HAVE_TZNAME)
303 return tzname
[daylight
];
309 /* init - init/finish functions for the module */
311 /* GNU Modula-2 linking hooks. */
314 M2EXPORT(init
) (int, char **, char **)
319 M2EXPORT(fini
) (int, char **, char **)
328 extern "C" void __attribute__((__constructor__
))
329 M2EXPORT(ctor
) (void)
331 m2iso_M2RTS_RegisterModule ("wrapclock", M2LIBNAME
,
332 M2EXPORT(init
), M2EXPORT(fini
),