LoongArch: Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook
[gcc.git] / libgm2 / libm2iso / wrapclock.cc
blob31ddeb573f8a3503b56719c772a287cb369ecfbd
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)
11 any later version.
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/>. */
27 #include "config.h"
28 #include <m2rts.h>
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)
35 #include "stdlib.h"
36 #endif
38 #if defined(HAVE_UNISTD_H)
39 #include "unistd.h"
40 #endif
42 #if defined(HAVE_SYS_TYPES_H)
43 #include "sys/types.h"
44 #endif
46 #if defined(HAVE_SYS_TIME_H)
47 #include "sys/time.h"
48 #endif
50 #if defined(HAVE_TIME_H)
51 #include "time.h"
52 #endif
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)
58 #include <sys/time.h>
59 #endif
61 #if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL)
62 #include <unistd.h>
63 #include <sys/syscall.h>
64 #endif
66 #if defined(HAVE_MALLOC_H)
67 #include "malloc.h"
68 #endif
70 #if defined(HAVE_LIMITS_H)
71 #include "limits.h"
72 #endif
74 #if !defined(NULL)
75 #define NULL (void *)0
76 #endif
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)
86 extern "C" int
87 EXPORT(GetTimeRealtime) (struct timespec *ts)
89 timespec tp;
90 #if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL)
91 return syscall (SYS_clock_gettime, CLOCK_REALTIME, ts);
92 #else
93 return clock_gettime (CLOCK_REALTIME, ts);
94 #endif
97 #else
99 extern "C" int
100 EXPORT(GetTimeRealtime) (void *ts)
102 return 1;
104 #endif
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)
111 extern "C" int
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);
118 #else
119 return 1;
120 #endif
123 #else
125 extern "C" int
126 EXPORT(SetTimeRealtime) (void *ts)
128 return 1;
130 #endif
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));
140 #else
141 return NULL;
142 #endif
145 #else
147 extern "C" void *
148 EXPORT(InitTimespec) (void)
150 return NULL;
152 #endif
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)
161 free (ts);
162 #endif
163 return NULL;
166 #else
168 extern "C" void *
169 EXPORT(KillTimespec) (void *ts)
171 return NULL;
173 #endif
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)
179 extern "C" int
180 EXPORT(GetTimespec) (timespec *ts, longint_t *sec, longint_t *nano)
182 #if defined(HAVE_STRUCT_TIMESPEC)
183 *sec = ts->tv_sec;
184 *nano = ts->tv_nsec;
185 return 1;
186 #else
187 return 0;
188 #endif
191 #else
192 extern "C" int
193 EXPORT(GetTimespec) (void *ts, longint_t *sec, longint_t *nano)
195 return 0;
197 #endif
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)
203 extern "C" int
204 EXPORT(SetTimespec) (timespec *ts, longint_t sec, longint_t nano)
206 #if defined(HAVE_STRUCT_TIMESPEC)
207 ts->tv_sec = sec;
208 ts->tv_nsec = nano;
209 return 1;
210 #else
211 return 0;
212 #endif
215 #else
217 extern "C" int
218 EXPORT(SetTimespec) (void *ts, longint_t sec, longint_t nano)
220 return 0;
222 #endif
224 extern "C" longint_t
225 EXPORT(timezone) (void)
227 #if defined(HAVE_STRUCT_TIMESPEC)
228 struct tm result;
229 struct timespec ts;
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;
238 #endif
239 #endif
240 return 0;
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. */
247 extern "C" int
248 EXPORT(istimezone) (void)
250 #if defined(HAVE_STRUCT_TIMESPEC)
251 #if defined(HAVE_TM_TM_GMTOFF)
252 #if defined(_GLIBCXX_USE_CLOCK_REALTIME)
253 return 1;
254 #endif
255 #endif
256 #endif
257 return 0;
260 extern "C" int
261 EXPORT(daylight) (void)
263 #if defined(HAVE_DAYLIGHT)
264 return daylight;
265 #else
266 return 0;
267 #endif
270 /* isdst returns 1 if daylight saving time is currently in effect and
271 returns 0 if it is not. */
273 extern "C" int
274 EXPORT(isdst) (void)
276 #if defined(HAVE_STRUCT_TIMESPEC)
277 struct tm result;
278 struct timespec ts;
280 if (EXPORT(GetTimeRealtime) (&ts) == 0)
282 time_t time = ts.tv_sec;
283 localtime_r (&time, &result);
284 return result.tm_isdst;
286 else
287 return 0;
288 #else
289 return 0;
290 #endif
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
297 unavailable. */
299 extern "C" char *
300 EXPORT(tzname) (int daylight)
302 #if defined(HAVE_TZNAME)
303 return tzname[daylight];
304 #else
305 return NULL;
306 #endif
309 /* init - init/finish functions for the module */
311 /* GNU Modula-2 linking hooks. */
313 extern "C" void
314 M2EXPORT(init) (int, char **, char **)
318 extern "C" void
319 M2EXPORT(fini) (int, char **, char **)
323 extern "C" void
324 M2EXPORT(dep) (void)
328 extern "C" void __attribute__((__constructor__))
329 M2EXPORT(ctor) (void)
331 m2iso_M2RTS_RegisterModule ("wrapclock", M2LIBNAME,
332 M2EXPORT(init), M2EXPORT(fini),
333 M2EXPORT(dep));