Ignore:
Timestamp:
Jan 6, 2020, 2:13:42 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
90cb6f7
Parents:
e0c235c
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/time.hfa

    re0c235c r4834563  
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jan  5 19:01:07 2020
    13 // Update Count     : 652
     12// Last Modified On : Mon Jan  6 12:50:16 2020
     13// Update Count     : 653
    1414//
    1515
     
    207207} // ?{}
    208208
    209 //######################### C time #########################
    210 
    211 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
    212 static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
    213 static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
    214 static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
    215 static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
    216 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
    217 
    218 //######################### Clock #########################
    219 
    220 struct Clock {                                                                                  // private
    221         Duration offset;                                                                        // for virtual clock: contains offset from real-time
    222 };
    223 
    224 static inline {
    225         void resetClock( Clock & clk, Duration adj ) with( clk ) {
    226                 offset = adj + __timezone`s;                                    // timezone (global) is (UTC - local time) in seconds
    227         } // resetClock
    228 
    229         void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }
    230 
    231         Duration getResNsec() {
    232                 struct timespec res;
    233                 clock_getres( CLOCK_REALTIME, &res );
    234                 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
    235         } // getRes
    236 
    237         Duration getRes() {
    238                 struct timespec res;
    239                 clock_getres( CLOCK_REALTIME_COARSE, &res );
    240                 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
    241         } // getRes
    242 
    243         Time getTimeNsec() {                                                            // with nanoseconds
    244                 timespec curr;
    245                 clock_gettime( CLOCK_REALTIME, &curr );
    246                 return (Time){ curr };
    247         } // getTimeNsec
    248 
    249         Time getTime() {                                                                        // without nanoseconds
    250                 timespec curr;
    251                 clock_gettime( CLOCK_REALTIME_COARSE, &curr );
    252                 curr.tv_nsec = 0;
    253                 return (Time){ curr };
    254         } // getTime
    255 
    256         Time getTime( Clock & clk ) with( clk ) {
    257                 return getTime() + offset;
    258         } // getTime
    259 
    260         Time ?()( Clock & clk ) with( clk ) {                           // alternative syntax
    261                 return getTime() + offset;
    262         } // getTime
    263 
    264         timeval getTime( Clock & clk ) {
    265                 return (timeval){ clk() };
    266         } // getTime
    267 
    268         tm getTime( Clock & clk ) with( clk ) {
    269                 tm ret;
    270                 localtime_r( getTime( clk ).tv_sec, &ret );
    271                 return ret;
    272         } // getTime
    273 
    274         Time getCPUTime() {
    275                 timespec ts;
    276                 clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
    277                 return (Time){ ts };
    278     } // getCPUTime
    279 } // distribution
    280 
    281209// Local Variables: //
    282210// mode: c //
Note: See TracChangeset for help on using the changeset viewer.