source: src/libcfa/clock @ 22cf65e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 22cf65e was 22cf65e, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add getResNsec, make getTimeNsec use more accurate clock, temporary fix for ambiguous timezone

  • Property mode set to 100644
File size: 2.9 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// clock --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Apr 12 14:36:06 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jun 26 14:11:44 2018
13// Update Count     : 6
14//
15
16#include <time>
17
18
19//######################### C time #########################
20
21static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
22static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
23static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
24static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
25static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
26static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
27
28
29//######################### Clock #########################
30
31struct Clock {                                                                                  // private
32        Duration offset;                                                                        // for virtual clock: contains offset from real-time
33        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
34};
35
36static inline void resetClock( Clock & clk ) with( clk ) {
37        clocktype = CLOCK_REALTIME_COARSE;
38} // Clock::resetClock
39
40static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
41        clocktype = -1;
42        offset = adj + __timezone`s;                                                    // timezone (global) is (UTC - local time) in seconds
43} // resetClock
44
45static inline void ?{}( Clock & clk ) { resetClock( clk ); }
46static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }
47
48static inline Duration getResNsec() {
49        struct timespec res;
50        clock_getres( CLOCK_REALTIME, &res );
51        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
52} // getRes
53
54static inline Duration getRes() {
55        struct timespec res;
56        clock_getres( CLOCK_REALTIME_COARSE, &res );
57        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
58} // getRes
59
60static inline Time getTimeNsec() {                                              // with nanoseconds
61        timespec curr;
62        clock_gettime( CLOCK_REALTIME, &curr );
63        return (Time){ curr };
64} // getTime
65
66static inline Time getTime() {                                                  // without nanoseconds
67        timespec curr;
68        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
69        curr.tv_nsec = 0;
70        return (Time){ curr };
71} // getTime
72
73static inline Time getTime( Clock & clk ) with( clk ) {
74        return getTime() + offset;
75} // getTime
76
77static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
78        return getTime() + offset;
79} // getTime
80
81static inline timeval getTime( Clock & clk ) {
82        return (timeval){ clk() };
83} // getTime
84
85static inline tm getTime( Clock & clk ) with( clk ) {
86        tm ret;
87        localtime_r( getTime( clk ).tv_sec, &ret );
88        return ret;
89} // getTime
90
91// Local Variables: //
92// mode: c //
93// tab-width: 4 //
94// End: //
Note: See TracBrowser for help on using the repository browser.