source: src/libcfa/time @ 8ad6533

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerwith_gc
Last change on this file since 8ad6533 was 8ad6533, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove cfatime.h, move itimerval constructor to "time", update concurrent examples to use Duration

  • Property mode set to 100644
File size: 13.4 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 : Mon Apr  9 13:10:23 2018
13// Update Count     : 616
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//######################### Duration #########################
31
32struct Duration {                                                                               // private
33        int64_t tv;                                                                                     // nanoseconds
34}; // Duration
35
36static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
37static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
38
39static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
40static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
41
42static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
43static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
44static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
45
46static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
47static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
48static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
49
50static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
51static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
52static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
53
54static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
55static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
56static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
57static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; }
58
59static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
60static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
61
62static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
63static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
64static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
65static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
66static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
67static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
68
69static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
70static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
71static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
72static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
73static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
74static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
75
76static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
77
78forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
79
80static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
81static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
82static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
83static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
84static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
85static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
86static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
87static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
88static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
89static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
90static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
91static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
92static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
93
94static inline int64_t ?`ns( Duration dur ) { return dur.tv; }
95static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
96static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
97static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; }
98static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
99static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
100static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
101static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
102
103
104//######################### C timeval #########################
105
106static inline void ?{}( timeval & t ) {}
107static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
108static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
109static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; }
110static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
111static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
112static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
113static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
114static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
115
116
117//######################### C timespec #########################
118
119static inline void ?{}( timespec & t ) {}
120static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
121static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
122static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; }
123static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
124static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
125static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
126static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
127static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
128
129
130//######################### C itimerval #########################
131
132static inline void ?{}( itimerval & itv, Duration alarm ) with( itv ) {
133        // itimerval contains durations but but uses time data-structure timeval.
134        it_value{ alarm`s, (alarm % 1`s)`us };                          // seconds, microseconds
135        it_interval{ 0 };                                                                       // 0 seconds, 0 microseconds
136} // itimerval
137
138static inline void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) {
139        // itimerval contains durations but but uses time data-structure timeval.
140        it_value{ alarm`s, (alarm % 1`s)`us };                          // seconds, microseconds
141        it_interval{ interval`s, interval`us };                         // seconds, microseconds
142} // itimerval
143
144
145//######################### C time #########################
146
147static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
148static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
149static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
150static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
151static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
152static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
153
154
155//######################### Time #########################
156
157struct Time {                                                                                   // private
158        uint64_t tv;                                                                            // nanoseconds since UNIX epoch
159}; // Time
160
161static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast
162void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow
163
164static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
165static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
166
167static inline void ?{}( Time & time, timeval t ) with( time ) {
168        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
169} // Time
170
171static inline Time ?=?( Time & time, timeval t ) with( time ) {
172        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
173        return time;
174} // ?=?
175
176static inline void ?{}( Time & time, timespec t ) with( time ) {
177        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
178} // Time
179
180static inline Time ?=?( Time & time, timespec t ) with( time ) {
181        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
182        return time;
183} // ?=?
184
185static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
186static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
187static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
188
189static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
190static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; }
191static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
192static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; }
193static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; }
194static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; }
195static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; }
196static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; }
197static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
198
199char * yy_mm_dd( Time time, char * buf );
200static inline char * ?`ymd( Time time, char * buf ) {   // short form
201        return yy_mm_dd( time, buf );
202} // ymd
203
204char * mm_dd_yy( Time time, char * buf );
205static inline char * ?`mdy( Time time, char * buf ) {   // short form
206        return mm_dd_yy( time, buf );
207} // mdy
208
209char * dd_mm_yy( Time time, char * buf );
210static inline char * ?`dmy( Time time, char * buf ) {   // short form
211        return dd_mm_yy( time, buf );;
212} // dmy
213
214size_t strftime( char * buf, size_t size, const char * fmt, Time time );
215
216forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
217
218//------------------------- timeval (cont) -------------------------
219
220static inline void ?{}( timeval & t, Time time ) with( t, time ) {
221        tv_sec = tv / TIMEGRAN;                                                         // seconds
222        tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
223} // ?{}
224
225//------------------------- timespec (cont) -------------------------
226
227static inline void ?{}( timespec & t, Time time ) with( t, time ) {
228        tv_sec = tv / TIMEGRAN;                                                         // seconds
229        tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds
230} // ?{}
231
232
233//######################### Clock #########################
234
235struct Clock {                                                                                  // private
236        Duration offset;                                                                        // for virtual clock: contains offset from real-time
237        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
238};
239
240static inline void resetClock( Clock & clk ) with( clk ) {
241        clocktype = CLOCK_REALTIME_COARSE;
242} // Clock::resetClock
243
244static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
245        clocktype = -1;
246        offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
247} // resetClock
248
249static inline void ?{}( Clock & clk ) {
250        resetClock( clk );
251} // Clock
252
253static inline void ?{}( Clock & clk, Duration adj ) {
254        resetClock( clk, adj );
255} // Clock
256
257static inline Duration getRes() {
258        struct timespec res;
259        clock_getres( CLOCK_REALTIME_COARSE, &res );
260        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
261} // getRes
262
263static inline Time getTimeNsec() {                                              // with nanoseconds
264        timespec curr;
265        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
266        return (Time){ curr };
267} // getTime
268
269static inline Time getTime() {                                                  // without nanoseconds
270        timespec curr;
271        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
272        curr.tv_nsec = 0;
273        return (Time){ curr };
274} // getTime
275
276static inline Time getTime( Clock & clk ) with( clk ) {
277        return getTime() + offset;
278} // getTime
279
280static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
281        return getTime() + offset;
282} // getTime
283
284static inline timeval getTime( Clock & clk ) {
285        return (timeval){ clk() };
286} // getTime
287
288static inline tm getTime( Clock & clk ) with( clk ) {
289        tm ret;
290        localtime_r( getTime( clk ).tv_sec, &ret );
291        return ret;
292} // getTime
293
294// Local Variables: //
295// mode: c //
296// tab-width: 4 //
297// End: //
Note: See TracBrowser for help on using the repository browser.