Debian Codegraph @

src/c/time.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * time.d - time routines
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <limits.h>
16 #include <time.h>
17 #ifndef _MSC_VER
18 # include <unistd.h>
19 # include <errno.h>
20 #endif
21 #if defined(_MSC_VER) || defined(__MINGW32__)
22 # include <windows.h>
23 # include <winsock.h>
24 #endif
25
26 #define ECL_INCLUDE_MATH_H
27 #include <ecl/ecl.h>
28 #include <ecl/internal.h>
29 #include <ecl/number.h>
30 #ifdef HAVE_TIMES
31 # include <sys/times.h>
32 #endif
33 #ifdef HAVE_GETRUSAGE
34 # include <sys/time.h>
35 # include <sys/resource.h>
36 #endif
37 #ifdef HAVE_GETTIMEOFDAY
38 # include <sys/time.h>
39 #endif
40 #include <ecl/impl/math_fenv.h>
41
42 static struct ecl_timeval beginning;
43
44 void
45 ecl_get_internal_real_time(struct ecl_timeval *tv)
46 {
47 #if defined(ECL_MS_WINDOWS_HOST)
48 union {
49 FILETIME filetime;
50 DWORDLONG hundred_ns;
51 } system_time;
52 GetSystemTimeAsFileTime(&system_time.filetime);
53 system_time.hundred_ns /= 10;
54 tv->tv_sec = system_time.hundred_ns / 1000000;
55 tv->tv_usec = system_time.hundred_ns % 1000000;
56 #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
57 struct timespec ts;
58 clock_gettime(CLOCK_MONOTONIC, &ts);
59 tv->tv_usec = ts.tv_nsec / 1000;
60 tv->tv_sec = ts.tv_sec;
61 #elif defined(HAVE_GETTIMEOFDAY)
62 struct timeval aux;
63 gettimeofday(&aux, NULL);
64 tv->tv_usec = aux.tv_usec;
65 tv->tv_sec = aux.tv_sec;
66 #else
67 tv->tv_sec = time(NULL);
68 tv->tv_usec = 0;
69 #endif
70 }
71
72 void
73 ecl_get_internal_run_time(struct ecl_timeval *tv)
74 {
75 #ifdef HAVE_GETRUSAGE
76 struct rusage r;
77 getrusage(RUSAGE_SELF, &r);
78 tv->tv_usec = r.ru_utime.tv_usec;
79 tv->tv_sec = r.ru_utime.tv_sec;
80 #else
81 # ifdef HAVE_TIMES
82 struct tms buf;
83 times(&buf);
84 tv->tv_sec = buf.tms_utime / CLK_TCK;
85 tv->tv_usec = ((buf.tms_utime % CLK_TCK) * 1000000) / CLK_TCK;
86 # else
87 # if defined(ECL_MS_WINDOWS_HOST)
88 union {
89 FILETIME filetime;
90 DWORDLONG hundred_ns;
91 } kernel_time, user_time, creation_time, exit_time;
92 if (!GetProcessTimes(GetCurrentProcess(),
93 &creation_time.filetime,
94 &exit_time.filetime,
95 &kernel_time.filetime,
96 &user_time.filetime))
97 FEwin32_error("GetProcessTimes() failed", 0);
98 kernel_time.hundred_ns += user_time.hundred_ns;
99 kernel_time.hundred_ns /= 10;
100 tv->tv_sec = kernel_time.hundred_ns / 1000000;
101 tv->tv_usec = kernel_time.hundred_ns % 1000000;
102 # else
103 ecl_get_internal_real_time(tv);
104 # endif
105 # endif
106 #endif
107 }
108
109 void
110 ecl_musleep(double time)
111 {
112 #ifdef HAVE_NANOSLEEP
113 struct timespec tm;
114 int code;
115 tm.tv_sec = (time_t)floor(time);
116 tm.tv_nsec = (long)((time - floor(time)) * 1e9);
117 AGAIN:
118 code = nanosleep(&tm, &tm);
119 {
120 int old_errno = errno;
121 if (code < 0 && old_errno == EINTR) {
122 goto AGAIN;
123 }
124 }
125 #else
126 #if defined (ECL_MS_WINDOWS_HOST)
127 /* Maximum waiting time that fits in SleepEx. This is the
128 * largest integer that fits safely in DWORD in milliseconds
129 * and has to be converted to 100ns (1e-3 / 100e-9 = 1e4) */
130 const DWORDLONG maxtime = (DWORDLONG)0xfffffff * (DWORDLONG)10000;
131 DWORDLONG wait = time * 1e7;
132 union {
133 FILETIME filetime;
134 DWORDLONG hundred_ns;
135 } end, now;
136 GetSystemTimeAsFileTime(&end.filetime);
137 end.hundred_ns += wait;
138 do {
139 DWORDLONG interval;
140 if (wait > maxtime) {
141 interval = maxtime;
142 wait -= maxtime;
143 } else {
144 interval = wait;
145 wait = 0;
146 }
147 if (SleepEx(interval/10000, 1) != 0) {
148 GetSystemTimeAsFileTime(&now.filetime);
149 if (now.hundred_ns >= end.hundred_ns)
150 break;
151 else
152 wait = end.hundred_ns - now.hundred_ns;
153 }
154 } while (wait);
155 #else
156 int t = (int)time;
157 for (t = (time + 0.5); t > 1000; t -= 1000)
158 sleep(1000);
159 sleep(t);
160 #endif
161 #endif
162 }
163
164 cl_fixnum
165 ecl_runtime(void)
166 {
167 struct ecl_timeval tv;
168 ecl_get_internal_run_time(&tv);
169 return tv.tv_sec * 1000000 + tv.tv_usec;
170 }
171
172 cl_object
173 cl_sleep(cl_object z)
174 {
175 double time;
176 /* INV: ecl_minusp() makes sure `z' is real */
177 if (ecl_minusp(z))
178 cl_error(9, @'simple-type-error', @':format-control',
179 @"Not a non-negative number ~S",
180 @':format-arguments', cl_list(1, z),
181 @':expected-type', @'real', @':datum', z);
182 /* Compute time without overflows */
183 ECL_WITHOUT_FPE_BEGIN {
184 time = ecl_to_double(z);
185 if (isnan(time) || !isfinite(time) || (time > INT_MAX)) {
186 time = INT_MAX;
187 } else if (time < 1e-9) {
188 time = 1e-9;
189 }
190 } ECL_WITHOUT_FPE_END;
191 ecl_musleep(time);
192 @(return ECL_NIL);
193 }
194
195 static cl_object
196 timeval_to_time(long sec, long usec)
197 {
198 cl_object milliseconds = ecl_plus(ecl_times(ecl_make_integer(sec),
199 ecl_make_fixnum(1000000)),
200 ecl_make_integer(usec));
201 @(return milliseconds);
202 }
203
204 cl_object
205 cl_get_internal_run_time()
206 {
207 struct ecl_timeval tv;
208 ecl_get_internal_run_time(&tv);
209 return timeval_to_time(tv.tv_sec, tv.tv_usec);
210 }
211
212 cl_object
213 cl_get_internal_real_time()
214 {
215 struct ecl_timeval tv;
216 ecl_get_internal_real_time(&tv);
217 return timeval_to_time(tv.tv_sec - beginning.tv_sec,
218 tv.tv_usec - beginning.tv_usec);
219 }
220
221 cl_object
222 cl_get_universal_time()
223 {
224 cl_object utc = ecl_make_integer(time(0));
225 @(return ecl_plus(utc, cl_core.Jan1st1970UT));
226 }
227
228 void
229 init_unixtime(void)
230 {
231 ecl_get_internal_real_time(&beginning);
232
233 ECL_SET(@'internal-time-units-per-second', ecl_make_fixnum(1000000));
234
235 cl_core.Jan1st1970UT =
236 ecl_times(ecl_make_fixnum(24 * 60 * 60),
237 ecl_make_fixnum(17 + 365 * 70));
238 }