1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # Services to keep time of the wall clock time
14 module realtime
is ldflags
"-lrt"
17 #ifdef _POSIX_C_SOURCE
18 #undef _POSIX_C_SOURCE
20 #define _POSIX_C_SOURCE 199309L
27 #include <TargetConditionals.h>
28 #if defined(TARGET_OS_IPHONE) && __IPHONE_OS_VERSION_MIN_REQUIRED < 100000
29 // Preserve compatibility with pre-iOS 10 devices where there is no clock_get_time.
34 #if (defined(__MACH__) || defined(TARGET_OS_IPHONE)) && !defined(CLOCK_REALTIME)
35 /* OS X does not have clock_gettime, mascarade it and use clock_get_time
36 * cf http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
38 #include <mach/clock.h>
39 #include <mach/mach.h>
41 #undef CLOCK_MONOTONIC
42 #define CLOCK_REALTIME CALENDAR_CLOCK
43 #define CLOCK_MONOTONIC SYSTEM_CLOCK
44 void nit_clock_gettime(clock_t clock_name, struct timespec *ts) {
47 host_get_clock_service(mach_host_self(), clock_name, &cclock);
48 clock_get_time(cclock, &mts);
49 mach_port_deallocate(mach_task_self(), cclock);
50 ts->tv_sec = mts.tv_sec;
51 ts->tv_nsec = mts.tv_nsec;
54 #define nit_clock_gettime clock_gettime
58 # Elapsed time representation.
59 private extern class Timespec `{struct timespec*`}
61 # Init a new Timespec from `s
` seconds and `ns
` nanoseconds.
62 new ( s, ns : Int ) `{
63 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
64 tv-
>tv_sec
= s
; tv-
>tv_nsec
= ns
;
68 # Init a new Timespec from now.
70 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
71 nit_clock_gettime
( CLOCK_MONOTONIC, tv
);
75 # Init a new Timespec copied from another.
76 new copy_of( other : Timespec ) `{
77 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
78 tv-
>tv_sec
= other-
>tv_sec
;
79 tv-
>tv_nsec
= other-
>tv_nsec
;
83 # Update `self` clock.
85 nit_clock_gettime
(CLOCK_MONOTONIC, self);
88 # Subtract `other
` from `self`
89 fun -(other: Timespec): Timespec `{
90 time_t s
= self-
>tv_sec
- other-
>tv_sec
;
91 long ns
= self-
>tv_nsec
- other-
>tv_nsec
;
96 struct timespec
* tv
= malloc
(sizeof
(struct timespec
));
97 tv-
>tv_sec
= s
; tv-
>tv_nsec
= ns
;
101 # Set `self` to `a
` - `b
`
102 fun minus(a, b: Timespec) `{
103 time_t s
= a-
>tv_sec
- b-
>tv_sec
;
104 long ns
= a-
>tv_nsec
- b-
>tv_nsec
;
113 # Number of whole seconds of elapsed time.
118 # Rest of the elapsed time (a fraction of a second).
120 # Number of nanoseconds.
122 return self-
>tv_nsec
;
125 # Elapsed time in microseconds, with both whole seconds and the rest
127 # May cause an `Int` overflow, use only with a low number of seconds.
129 return self-
>tv_sec
*1000000 + self-
>tv_nsec
/1000;
132 # Elapsed time in milliseconds, with both whole seconds and the rest
134 # May cause an `Int` overflow, use only with a low number of seconds.
136 return self-
>tv_sec
*1000 + self-
>tv_nsec
/1000000;
139 # Number of seconds as a `Float`
141 # Incurs a loss of precision, but the result is pretty to print.
143 return (double
)self-
>tv_sec
+ 0.000000001 * self-
>tv_nsec
;
146 redef fun to_s do return "{to_f}s"
149 # Keeps track of real time
152 # var clock = new Clock
154 # # sleeping at least 1s
156 # assert clock.total >= 1.0
157 # assert clock.lapse >= 1.0
159 # # sleeping at least 5ms
161 # assert clock.total >= 1.005
162 # assert clock.lapse >= 0.005
165 super FinalizableOnce
167 # TODO use less mallocs
170 private var time_at_beginning = new Timespec.monotonic_now
172 # Time at last time a lapse method was called
173 private var time_at_last_lapse = new Timespec.monotonic_now
175 private var temp = new Timespec.monotonic_now
177 # Smallest time frame reported by clock
178 private fun resolution: Timespec `{
179 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
180 #if (defined(__MACH__) || defined(TARGET_OS_IPHONE)) && !defined(CLOCK_REALTIME)
183 mach_msg_type_number_t count
;
184 host_get_clock_service
(mach_host_self
(), SYSTEM_CLOCK, &cclock
);
185 clock_get_attributes
(cclock
, CLOCK_GET_TIME_RES, (clock_attr_t
)&nsecs
, &count
);
186 mach_port_deallocate
(mach_task_self
(), cclock
);
190 clock_getres
( CLOCK_MONOTONIC, tv
);
195 # Seconds since the creation of this instance
200 now.minus(now, time_at_beginning)
204 # Seconds since the last call to `lapse
`
207 var time_at_last_lapse = time_at_last_lapse
210 time_at_last_lapse.minus(now, time_at_last_lapse)
211 var r = time_at_last_lapse.to_f
213 self.temp = time_at_last_lapse
214 self.time_at_last_lapse = now
219 # Seconds since the last call to `lapse
`, without resetting the lapse counter
220 fun peek_lapse: Float
224 now.minus(now, time_at_last_lapse)
228 redef fun finalize_once
230 time_at_beginning.free
231 time_at_last_lapse.free