Changeset e93f1d2 for src/libcfa/time


Ignore:
Timestamp:
Apr 13, 2018, 11:16:36 AM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
Children:
5f08961d, deaef5b
Parents:
01963df (diff), bd5cf7c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/time

    r01963df re93f1d2  
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Apr  9 13:10:23 2018
    13 // Update Count     : 616
     12// Last Modified On : Fri Apr 13 07:51:52 2018
     13// Update Count     : 634
    1414//
    1515
     
    2323#include <sys/time.h>                                                                   // timeval
    2424}
    25 #include <iostream>                                                                             // istype/ostype
     25#include <time_t.h>                                                                             // Duration/Time types
    2626
    2727enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
     
    3030//######################### Duration #########################
    3131
    32 struct Duration {                                                                               // private
    33         int64_t tv;                                                                                     // nanoseconds
    34 }; // Duration
    35 
    36 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
    3732static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
    3833
    39 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
    4034static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
    4135
     
    7569
    7670static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
    77 
    78 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
    7971
    8072static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
     
    143135
    144136
    145 //######################### C time #########################
    146 
    147 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
    148 static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
    149 static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
    150 static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
    151 static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
    152 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
    153 
    154 
    155137//######################### Time #########################
    156138
    157 struct Time {                                                                                   // private
    158         uint64_t tv;                                                                            // nanoseconds since UNIX epoch
    159 }; // Time
    160 
    161 static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast
    162 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow
    163 
    164 static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
    165 static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
    166 
    167 static inline void ?{}( Time & time, timeval t ) with( time ) {
    168         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
    169 } // Time
     139static inline void ?{}( Time & time, Time t ) with( time ) { tv = t.tv; }
     140void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 );
     141static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
     142
     143static inline Time ?=?( Time & time, zero_t ) { return time{ 0 }; }
    170144
    171145static inline Time ?=?( Time & time, timeval t ) with( time ) {
     
    214188size_t strftime( char * buf, size_t size, const char * fmt, Time time );
    215189
    216 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
    217 
    218190//------------------------- timeval (cont) -------------------------
    219191
     
    230202} // ?{}
    231203
    232 
    233 //######################### Clock #########################
    234 
    235 struct Clock {                                                                                  // private
    236         Duration offset;                                                                        // for virtual clock: contains offset from real-time
    237         int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
    238 };
    239 
    240 static inline void resetClock( Clock & clk ) with( clk ) {
    241         clocktype = CLOCK_REALTIME_COARSE;
    242 } // Clock::resetClock
    243 
    244 static 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 
    249 static inline void ?{}( Clock & clk ) {
    250         resetClock( clk );
    251 } // Clock
    252 
    253 static inline void ?{}( Clock & clk, Duration adj ) {
    254         resetClock( clk, adj );
    255 } // Clock
    256 
    257 static 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 
    263 static inline Time getTimeNsec() {                                              // with nanoseconds
    264         timespec curr;
    265         clock_gettime( CLOCK_REALTIME_COARSE, &curr );
    266         return (Time){ curr };
    267 } // getTime
    268 
    269 static 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 
    276 static inline Time getTime( Clock & clk ) with( clk ) {
    277         return getTime() + offset;
    278 } // getTime
    279 
    280 static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
    281         return getTime() + offset;
    282 } // getTime
    283 
    284 static inline timeval getTime( Clock & clk ) {
    285         return (timeval){ clk() };
    286 } // getTime
    287 
    288 static inline tm getTime( Clock & clk ) with( clk ) {
    289         tm ret;
    290         localtime_r( getTime( clk ).tv_sec, &ret );
    291         return ret;
    292 } // getTime
    293 
    294204// Local Variables: //
    295205// mode: c //
Note: See TracChangeset for help on using the changeset viewer.