source: src/libcfa/time @ 1031c7e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 1031c7e was 8eb2018, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

cleanup, remove conversion of timeval/timespec to duration

  • Property mode set to 100644
File size: 14.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// time --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed Mar 14 23:18:57 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Apr  1 20:03:16 2018
13// Update Count     : 581
14//
15
16#pragma once
17
18// http://en.cppreference.com/w/cpp/header/chrono
19// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc
20
21#include <time.h>                                                                               // timespec
22extern "C" {
23#include <sys/time.h>                                                                   // timeval
24}
25#include <iostream>                                                                             // istype/ostype
26
27enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
28
29
30//######################### timeval #########################
31
32static inline void ?{}( timeval & t ) {}
33static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
34static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
35static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; }
36static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
37static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
38static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
39static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
40static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
41
42
43//######################### timespec #########################
44
45static inline void ?{}( timespec & t ) {}
46static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
47static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
48static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; }
49static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
50static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
51static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
52static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
53static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
54
55
56//######################### C time #########################
57
58static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
59static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
60static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
61static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
62static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
63static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
64
65//######################### Duration #########################
66
67struct Duration {
68        int64_t tv;
69};
70
71static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
72static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
73static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
74static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
75
76#if 0
77static inline void ?{}( Duration & dur, timeval t ) with( dur ) {
78        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
79} // Duration
80
81static inline void ?{}( Duration & dur, timespec t ) with( dur ) {
82        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
83} // Duration
84
85static inline Duration ?=?( Duration & dur, timeval t ) with( dur ) {
86        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
87        return dur;
88} // ?=?
89
90static inline Duration ?=?( Duration & dur, timespec t ) with( dur ) {
91        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
92        return dur;
93} // ?=? timespec
94#endif
95
96//static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
97
98static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
99static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
100static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
101
102static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
103static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
104static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
105
106static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
107static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
108static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
109
110static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
111static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
112static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
113
114static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
115
116static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
117static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
118static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
119static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
120static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
121static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
122
123static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
124static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
125static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
126static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
127static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
128static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
129
130static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; }
131
132forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
133
134static inline Duration ?`ns( int64_t nsec  ) { return (Duration)@{ nsec }; }
135static inline Duration ?`us( int64_t usec  ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
136static inline Duration ?`ms( int64_t msec  ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
137static inline Duration ?`s ( int64_t sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
138static inline Duration ?`s ( double  sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
139static inline Duration ?`m ( int64_t min   ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
140static inline Duration ?`m ( double  min   ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
141static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600LL * TIMEGRAN) }; }
142static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (3600LL * TIMEGRAN) }; }
143static inline Duration ?`d ( int64_t days  ) { return (Duration)@{ days * (24LL * 3600LL * TIMEGRAN) }; }
144static inline Duration ?`d ( double  days  ) { return (Duration)@{ days * (24LL * 3600LL * TIMEGRAN) }; }
145static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 3600LL * TIMEGRAN) }; }
146//static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 3600LL * TIMEGRAN) }; }
147
148static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
149static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
150static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
151static inline int64_t ?`s  ( Duration dur ) { return dur.tv / TIMEGRAN; }
152static inline int64_t ?`m  ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
153static inline int64_t ?`h  ( Duration dur ) { return dur.tv / (3600LL * TIMEGRAN); }
154static inline int64_t ?`d  ( Duration dur ) { return dur.tv / (24LL * 3600LL * TIMEGRAN); }
155static inline int64_t ?`w  ( Duration dur ) { return dur.tv / (7LL * 24LL * 3600LL * TIMEGRAN); }
156static inline int64_t ?`f  ( Duration dur ) { return dur.tv / (14LL * 24LL * 3600LL * TIMEGRAN); }
157
158//------------------------- timeval (cont) -------------------------
159
160static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
161        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
162        t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
163} // ?{}
164
165//------------------------- timespec (cont) -------------------------
166
167static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
168        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
169        t.tv_nsec = tv % TIMEGRAN;                                                      // nanoseconds
170} // Timespec
171
172
173//######################### Time #########################
174
175
176struct Time {
177        uint64_t tv;
178};
179
180static inline void ?{}( Time & t ) with( t ) {
181        tv = 0;
182} // Time
183
184void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
185
186static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
187        time{ year, month, day, hour, min, sec, 0 };
188} // Time
189
190static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
191        time{ year, month, day, hour, min, 0, 0 };
192} // Time
193
194static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
195        time{ year, month, day, hour, 0, 0, 0 };
196} // Time
197
198static inline void ?{}( Time & time, int year, int month, int day ) {
199        time{ year, month, day, 0, 0, 0, 0 };
200} // Time
201
202static inline void ?{}( Time & time, int year, int month ) {
203        time{ year, month, 0, 0, 0, 0, 0 };
204} // Time
205
206static inline void ?{}( Time & time, int year ) {
207        time{ year, 0, 0, 0, 0, 0, 0 };
208} // Time
209
210static inline void ?{}( Time & time, timeval t ) with( time ) {
211        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
212} // Time
213
214static inline void ?{}( Time & time, timespec t ) with( time ) {
215        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
216} // Time
217
218static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
219static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
220
221static inline Time ?=?( Time & time, timeval t ) with( time ) {
222        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
223        return time;
224} // ?=?
225
226static inline Time ?=?( Time & time, timespec t ) with( time ) {
227        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
228        return time;
229} // ?=?
230
231static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
232static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
233static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
234
235static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
236static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; }
237static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
238static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; }
239static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; }
240static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; }
241static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; }
242static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; }
243static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
244
245char * yy_mm_dd( Time time, char * buf );
246static inline char * ?`ymd( Time time, char * buf ) {
247        return yy_mm_dd( time, buf );
248} // ymd
249
250char * mm_dd_yy( Time time, char * buf );
251static inline char * ?`mdy( Time time, char * buf ) {
252        return mm_dd_yy( time, buf );
253} // mdy
254
255char * dd_mm_yy( Time time, char * buf );
256static inline char * ?`dmy( Time time, char * buf ) {
257        return dd_mm_yy( time, buf );;
258} // dmy
259
260size_t strftime( char * buf, size_t size, const char * fmt, Time time );
261
262forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
263
264//------------------------- timeval (cont) -------------------------
265
266static inline void ?{}( timeval & t, Time time ) with( t, time ) {
267        tv_sec = tv / TIMEGRAN;                                                         // seconds
268        tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
269} // ?{}
270
271//------------------------- timespec (cont) -------------------------
272
273static inline void ?{}( timespec & t, Time time ) with( t, time ) {
274        tv_sec = tv / TIMEGRAN;                                                         // seconds
275        tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds
276} // ?{}
277
278
279//######################### Clock #########################
280
281
282struct Clock {
283        Duration offset;                                                                        // for virtual clock: contains offset from real-time
284        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
285};
286
287static inline void resetClock( Clock & clk ) with( clk ) {
288        clocktype = CLOCK_REALTIME_COARSE;
289} // Clock::resetClock
290
291static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
292        clocktype = -1;
293        offset = adj + timezone`s;                                                      // timezone is (UTC - local time) in seconds
294} // resetClock
295
296static inline void ?{}( Clock & clk ) {
297        resetClock( clk );
298} // Clock
299
300static inline void ?{}( Clock & clk, Duration adj ) {
301        resetClock( clk, adj );
302} // Clock
303
304static inline Duration getRes() {
305        struct timespec res;
306        clock_getres( CLOCK_REALTIME_COARSE, &res );
307        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
308} // getRes
309
310static inline Time getTimeNsec() {                                              // with nanoseconds
311        timespec curr;
312        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
313        return (Time){ curr };
314} // getTime
315
316static inline Time getTime() {                                                  // without nanoseconds
317        timespec curr;
318        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
319        curr.tv_nsec = 0;
320        return (Time){ curr };
321} // getTime
322
323static inline Time getTime( Clock & clk ) with( clk ) {
324        return getTime() + offset;
325} // getTime
326
327static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
328        return getTime() + offset;
329} // getTime
330
331static inline timeval getTime( Clock & clk ) {
332        return (timeval){ clk() };
333} // getTime
334
335static inline tm getTime( Clock & clk ) with( clk ) {
336        tm ret;
337        localtime_r( getTime( clk ).tv_sec, &ret );
338        return ret;
339} // getTime
340
341// Local Variables: //
342// mode: c //
343// tab-width: 4 //
344// End: //
Note: See TracBrowser for help on using the repository browser.