source: src/libcfa/time @ 2a84d06d

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 2a84d06d was 2a84d06d, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

second draft of time package and incorporation into runtime kernel

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