source: libcfa/src/clock.hfa @ ca33b15

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ca33b15 was 4834563, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

refactor clock out of time because time.hfa too large for 32-bit build

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