Debian Codegraph @

libgm2/libm2iso/wrapclock.cc

raw
1 /* wrapclock.cc provides access to time related system calls.
2
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
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.
12
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.
17
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.
21
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/>. */
26
27 #include "config.h"
28 #include <m2rts.h>
29
30 #define EXPORT(FUNC) m2iso ## _wrapclock_ ## FUNC
31 #define M2EXPORT(FUNC) m2iso ## _M2_wrapclock_ ## FUNC
32 #define M2LIBNAME "m2iso"
33
34 #if defined(HAVE_STDLIB_H)
35 #include "stdlib.h"
36 #endif
37
38 #if defined(HAVE_UNISTD_H)
39 #include "unistd.h"
40 #endif
41
42 #if defined(HAVE_SYS_TYPES_H)
43 #include "sys/types.h"
44 #endif
45
46 #if defined(HAVE_SYS_TIME_H)
47 #include "sys/time.h"
48 #endif
49
50 #if defined(HAVE_TIME_H)
51 #include "time.h"
52 #endif
53
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
60
61 #if defined(_GLIBCXX_USE_CLOCK_GETTIME_SYSCALL)
62 #include <unistd.h>
63 #include <sys/syscall.h>
64 #endif
65
66 #if defined(HAVE_MALLOC_H)
67 #include "malloc.h"
68 #endif
69
70 #if defined(HAVE_LIMITS_H)
71 #include "limits.h"
72 #endif
73
74 #if !defined(NULL)
75 #define NULL (void *)0
76 #endif
77
78 typedef long long int longint_t;
79
80
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. */
84
85 #if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME)
86 extern "C" int
87 EXPORT(GetTimeRealtime) (struct timespec *ts)
88 {
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
95 }
96
97 #else
98
99 extern "C" int
100 EXPORT(GetTimeRealtime) (void *ts)
101 {
102 return 1;
103 }
104 #endif
105
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. */
109
110 #if defined(HAVE_STRUCT_TIMESPEC) && defined(_GLIBCXX_USE_CLOCK_REALTIME)
111 extern "C" int
112 EXPORT(SetTimeRealtime) (struct timespec *ts)
113 {
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
121 }
122
123 #else
124
125 extern "C" int
126 EXPORT(SetTimeRealtime) (void *ts)
127 {
128 return 1;
129 }
130 #endif
131
132 /* InitTimespec returns a newly created opaque type. */
133
134 #if defined(HAVE_STRUCT_TIMESPEC)
135 extern "C" struct timespec *
136 EXPORT(InitTimespec) (void)
137 {
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
143 }
144
145 #else
146
147 extern "C" void *
148 EXPORT(InitTimespec) (void)
149 {
150 return NULL;
151 }
152 #endif
153
154 /* KillTimeval deallocates the memory associated with an opaque type. */
155
156 #if defined(HAVE_STRUCT_TIMESPEC)
157 extern "C" struct timespec *
158 EXPORT(KillTimespec) (void *ts)
159 {
160 #if defined(HAVE_MALLOC_H)
161 free (ts);
162 #endif
163 return NULL;
164 }
165
166 #else
167
168 extern "C" void *
169 EXPORT(KillTimespec) (void *ts)
170 {
171 return NULL;
172 }
173 #endif
174
175 /* GetTimespec retrieves the number of seconds and nanoseconds from the
176 timespec. 1 is returned if successful and 0 otherwise. */
177
178 #if defined(HAVE_STRUCT_TIMESPEC)
179 extern "C" int
180 EXPORT(GetTimespec) (timespec *ts, longint_t *sec, longint_t *nano)
181 {
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
189 }
190
191 #else
192 extern "C" int
193 EXPORT(GetTimespec) (void *ts, longint_t *sec, longint_t *nano)
194 {
195 return 0;
196 }
197 #endif
198
199 /* SetTimespec sets the number of seconds and nanoseconds into timespec.
200 1 is returned if successful and 0 otherwise. */
201
202 #if defined(HAVE_STRUCT_TIMESPEC)
203 extern "C" int
204 EXPORT(SetTimespec) (timespec *ts, longint_t sec, longint_t nano)
205 {
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
213 }
214
215 #else
216
217 extern "C" int
218 EXPORT(SetTimespec) (void *ts, longint_t sec, longint_t nano)
219 {
220 return 0;
221 }
222 #endif
223
224 extern "C" longint_t
225 EXPORT(timezone) (void)
226 {
227 #if defined(HAVE_STRUCT_TIMESPEC)
228 struct tm result;
229 struct timespec ts;
230
231 #if defined(HAVE_TM_TM_GMTOFF)
232 if (EXPORT(GetTimeRealtime) (&ts) == 0)
233 {
234 time_t time = ts.tv_sec;
235 localtime_r (&time, &result);
236 return result.tm_gmtoff;
237 }
238 #endif
239 #endif
240 return 0;
241 }
242
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. */
246
247 extern "C" int
248 EXPORT(istimezone) (void)
249 {
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;
258 }
259
260 extern "C" int
261 EXPORT(daylight) (void)
262 {
263 #if defined(HAVE_DAYLIGHT)
264 return daylight;
265 #else
266 return 0;
267 #endif
268 }
269
270 /* isdst returns 1 if daylight saving time is currently in effect and
271 returns 0 if it is not. */
272
273 extern "C" int
274 EXPORT(isdst) (void)
275 {
276 #if defined(HAVE_STRUCT_TIMESPEC)
277 struct tm result;
278 struct timespec ts;
279
280 if (EXPORT(GetTimeRealtime) (&ts) == 0)
281 {
282 time_t time = ts.tv_sec;
283 localtime_r (&time, &result);
284 return result.tm_isdst;
285 }
286 else
287 return 0;
288 #else
289 return 0;
290 #endif
291 }
292
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. */
298
299 extern "C" char *
300 EXPORT(tzname) (int daylight)
301 {
302 #if defined(HAVE_TZNAME)
303 return tzname[daylight];
304 #else
305 return NULL;
306 #endif
307 }
308
309 /* init - init/finish functions for the module */
310
311 /* GNU Modula-2 linking hooks. */
312
313 extern "C" void
314 M2EXPORT(init) (int, char **, char **)
315 {
316 }
317
318 extern "C" void
319 M2EXPORT(fini) (int, char **, char **)
320 {
321 }
322
323 extern "C" void
324 M2EXPORT(dep) (void)
325 {
326 }
327
328 extern "C" void __attribute__((__constructor__))
329 M2EXPORT(ctor) (void)
330 {
331 m2iso_M2RTS_RegisterModule ("wrapclock", M2LIBNAME,
332 M2EXPORT(init), M2EXPORT(fini),
333 M2EXPORT(dep));
334 }