Changeset 2a84d06d


Ignore:
Timestamp:
Mar 27, 2018, 5:22:58 PM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
43725bd
Parents:
af1ed1ad
Message:

second draft of time package and incorporation into runtime kernel

Location:
src/libcfa
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/Makefile.am

    raf1ed1ad r2a84d06d  
    1111## Created On       : Sun May 31 08:54:01 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Thu Mar 22 17:14:15 2018
    14 ## Update Count     : 224
     13## Last Modified On : Tue Mar 27 14:27:05 2018
     14## Update Count     : 225
    1515###############################################################################
    1616
     
    4646CC = ${abs_top_srcdir}/src/driver/cfa
    4747
    48 headers = fstream iostream iterator limits rational stdlib \
     48headers = fstream iostream iterator limits rational time stdlib \
    4949          containers/maybe containers/pair containers/result containers/vector
    5050
     
    9999        ${stdhdr}                       \
    100100        math                            \
    101         time                            \
    102101        gmp                             \
    103102        bits/align.h            \
  • src/libcfa/Makefile.in

    raf1ed1ad r2a84d06d  
    150150am__libcfa_d_a_SOURCES_DIST = libcfa-prelude.c interpose.c \
    151151        bits/debug.c fstream.c iostream.c iterator.c limits.c \
    152         rational.c stdlib.c containers/maybe.c containers/pair.c \
    153         containers/result.c containers/vector.c \
     152        rational.c time.c stdlib.c containers/maybe.c \
     153        containers/pair.c containers/result.c containers/vector.c \
    154154        concurrency/coroutine.c concurrency/thread.c \
    155155        concurrency/kernel.c concurrency/monitor.c assert.c \
     
    165165        libcfa_d_a-iostream.$(OBJEXT) libcfa_d_a-iterator.$(OBJEXT) \
    166166        libcfa_d_a-limits.$(OBJEXT) libcfa_d_a-rational.$(OBJEXT) \
    167         libcfa_d_a-stdlib.$(OBJEXT) \
     167        libcfa_d_a-time.$(OBJEXT) libcfa_d_a-stdlib.$(OBJEXT) \
    168168        containers/libcfa_d_a-maybe.$(OBJEXT) \
    169169        containers/libcfa_d_a-pair.$(OBJEXT) \
     
    184184libcfa_a_LIBADD =
    185185am__libcfa_a_SOURCES_DIST = libcfa-prelude.c interpose.c bits/debug.c \
    186         fstream.c iostream.c iterator.c limits.c rational.c stdlib.c \
    187         containers/maybe.c containers/pair.c containers/result.c \
    188         containers/vector.c concurrency/coroutine.c \
    189         concurrency/thread.c concurrency/kernel.c \
    190         concurrency/monitor.c assert.c exception.c virtual.c \
    191         concurrency/CtxSwitch-@MACHINE_TYPE@.S concurrency/alarm.c \
    192         concurrency/invoke.c concurrency/preemption.c
     186        fstream.c iostream.c iterator.c limits.c rational.c time.c \
     187        stdlib.c containers/maybe.c containers/pair.c \
     188        containers/result.c containers/vector.c \
     189        concurrency/coroutine.c concurrency/thread.c \
     190        concurrency/kernel.c concurrency/monitor.c assert.c \
     191        exception.c virtual.c concurrency/CtxSwitch-@MACHINE_TYPE@.S \
     192        concurrency/alarm.c concurrency/invoke.c \
     193        concurrency/preemption.c
    193194@BUILD_CONCURRENCY_TRUE@am__objects_5 = concurrency/libcfa_a-coroutine.$(OBJEXT) \
    194195@BUILD_CONCURRENCY_TRUE@        concurrency/libcfa_a-thread.$(OBJEXT) \
     
    197198am__objects_6 = libcfa_a-fstream.$(OBJEXT) libcfa_a-iostream.$(OBJEXT) \
    198199        libcfa_a-iterator.$(OBJEXT) libcfa_a-limits.$(OBJEXT) \
    199         libcfa_a-rational.$(OBJEXT) libcfa_a-stdlib.$(OBJEXT) \
    200         containers/libcfa_a-maybe.$(OBJEXT) \
     200        libcfa_a-rational.$(OBJEXT) libcfa_a-time.$(OBJEXT) \
     201        libcfa_a-stdlib.$(OBJEXT) containers/libcfa_a-maybe.$(OBJEXT) \
    201202        containers/libcfa_a-pair.$(OBJEXT) \
    202203        containers/libcfa_a-result.$(OBJEXT) \
     
    260261  esac
    261262am__nobase_cfa_include_HEADERS_DIST = fstream iostream iterator limits \
    262         rational stdlib containers/maybe containers/pair \
     263        rational time stdlib containers/maybe containers/pair \
    263264        containers/result containers/vector concurrency/coroutine \
    264265        concurrency/thread concurrency/kernel concurrency/monitor \
    265         ${shell find stdhdr -type f -printf "%p "} math time gmp \
     266        ${shell find stdhdr -type f -printf "%p "} math gmp \
    266267        bits/align.h bits/cfatime.h bits/containers.h bits/defs.h \
    267268        bits/debug.h bits/locks.h concurrency/invoke.h
     
    419420EXTRA_FLAGS = -g -Wall -Wno-unused-function -imacros libcfa-prelude.c @CFA_FLAGS@
    420421AM_CCASFLAGS = @CFA_FLAGS@
    421 headers = fstream iostream iterator limits rational stdlib \
     422headers = fstream iostream iterator limits rational time stdlib \
    422423        containers/maybe containers/pair containers/result \
    423424        containers/vector $(am__append_3)
     
    435436        ${stdhdr}                       \
    436437        math                            \
    437         time                            \
    438438        gmp                             \
    439439        bits/align.h            \
     
    612612@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-rational.Po@am__quote@
    613613@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-stdlib.Po@am__quote@
     614@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-time.Po@am__quote@
    614615@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-virtual.Po@am__quote@
    615616@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-assert.Po@am__quote@
     
    623624@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-rational.Po@am__quote@
    624625@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-stdlib.Po@am__quote@
     626@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-time.Po@am__quote@
    625627@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-virtual.Po@am__quote@
    626628@AMDEP_TRUE@@am__include@ @am__quote@bits/$(DEPDIR)/libcfa_a-debug.Po@am__quote@
     
    787789@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libcfa_d_a-rational.obj `if test -f 'rational.c'; then $(CYGPATH_W) 'rational.c'; else $(CYGPATH_W) '$(srcdir)/rational.c'; fi`
    788790
     791libcfa_d_a-time.o: time.c
     792@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT libcfa_d_a-time.o -MD -MP -MF $(DEPDIR)/libcfa_d_a-time.Tpo -c -o libcfa_d_a-time.o `test -f 'time.c' || echo '$(srcdir)/'`time.c
     793@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) $(DEPDIR)/libcfa_d_a-time.Tpo $(DEPDIR)/libcfa_d_a-time.Po
     794@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='time.c' object='libcfa_d_a-time.o' libtool=no @AMDEPBACKSLASH@
     795@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     796@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libcfa_d_a-time.o `test -f 'time.c' || echo '$(srcdir)/'`time.c
     797
     798libcfa_d_a-time.obj: time.c
     799@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT libcfa_d_a-time.obj -MD -MP -MF $(DEPDIR)/libcfa_d_a-time.Tpo -c -o libcfa_d_a-time.obj `if test -f 'time.c'; then $(CYGPATH_W) 'time.c'; else $(CYGPATH_W) '$(srcdir)/time.c'; fi`
     800@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) $(DEPDIR)/libcfa_d_a-time.Tpo $(DEPDIR)/libcfa_d_a-time.Po
     801@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='time.c' object='libcfa_d_a-time.obj' libtool=no @AMDEPBACKSLASH@
     802@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     803@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libcfa_d_a-time.obj `if test -f 'time.c'; then $(CYGPATH_W) 'time.c'; else $(CYGPATH_W) '$(srcdir)/time.c'; fi`
     804
    789805libcfa_d_a-stdlib.o: stdlib.c
    790806@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT libcfa_d_a-stdlib.o -MD -MP -MF $(DEPDIR)/libcfa_d_a-stdlib.Tpo -c -o libcfa_d_a-stdlib.o `test -f 'stdlib.c' || echo '$(srcdir)/'`stdlib.c
     
    10801096@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    10811097@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libcfa_a-rational.obj `if test -f 'rational.c'; then $(CYGPATH_W) 'rational.c'; else $(CYGPATH_W) '$(srcdir)/rational.c'; fi`
     1098
     1099libcfa_a-time.o: time.c
     1100@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT libcfa_a-time.o -MD -MP -MF $(DEPDIR)/libcfa_a-time.Tpo -c -o libcfa_a-time.o `test -f 'time.c' || echo '$(srcdir)/'`time.c
     1101@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) $(DEPDIR)/libcfa_a-time.Tpo $(DEPDIR)/libcfa_a-time.Po
     1102@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='time.c' object='libcfa_a-time.o' libtool=no @AMDEPBACKSLASH@
     1103@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     1104@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libcfa_a-time.o `test -f 'time.c' || echo '$(srcdir)/'`time.c
     1105
     1106libcfa_a-time.obj: time.c
     1107@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT libcfa_a-time.obj -MD -MP -MF $(DEPDIR)/libcfa_a-time.Tpo -c -o libcfa_a-time.obj `if test -f 'time.c'; then $(CYGPATH_W) 'time.c'; else $(CYGPATH_W) '$(srcdir)/time.c'; fi`
     1108@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) $(DEPDIR)/libcfa_a-time.Tpo $(DEPDIR)/libcfa_a-time.Po
     1109@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='time.c' object='libcfa_a-time.obj' libtool=no @AMDEPBACKSLASH@
     1110@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     1111@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libcfa_a-time.obj `if test -f 'time.c'; then $(CYGPATH_W) 'time.c'; else $(CYGPATH_W) '$(srcdir)/time.c'; fi`
    10821112
    10831113libcfa_a-stdlib.o: stdlib.c
  • src/libcfa/concurrency/alarm.c

    raf1ed1ad r2a84d06d  
    1010// Created On       : Fri Jun 2 11:31:25 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:35:18 2017
    13 // Update Count     : 1
     12// Last Modified On : Tue Mar 27 14:12:11 2018
     13// Update Count     : 41
    1414//
    1515
     
    2727
    2828
    29 static inline void ?{}( itimerval & this, __cfa_time_t * alarm ) with( this ) {
    30         it_value.tv_sec = alarm->val / (1`cfa_s).val;                   // seconds
    31         it_value.tv_usec = max( (alarm->val % (1`cfa_s).val) / (1`cfa_us).val, 1000 ); // microseconds
    32         it_interval.tv_sec = 0;
    33         it_interval.tv_usec = 0;
    34 }
    35 
    36 static inline void ?{}( __cfa_time_t & this, timespec * curr ) {
    37         uint64_t secs  = curr->tv_sec;
    38         uint64_t nsecs = curr->tv_nsec;
    39         this.val = from_s(secs).val + nsecs;
     29static inline void ?{}( itimerval & this, Duration alarm ) with( this ) {
     30        it_value   { alarm };                                                           // seconds, microseconds
     31        it_interval{ 0 };
    4032}
    4133
     
    4436//=============================================================================================
    4537
    46 __cfa_time_t __kernel_get_time() {
     38Time __kernel_get_time() {
    4739        timespec curr;
    48         clock_gettime( CLOCK_REALTIME, &curr );
    49         return (__cfa_time_t){ &curr };
     40        clock_gettime( CLOCK_MONOTONIC_RAW, &curr );            // CLOCK_REALTIME
     41        return (Time){ curr };
    5042}
    5143
    52 void __kernel_set_timer( __cfa_time_t alarm ) {
    53         itimerval val = { &alarm };
    54         setitimer( ITIMER_REAL, &val, NULL );
     44void __kernel_set_timer( Duration alarm ) {
     45        setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL );
    5546}
    5647
     
    5950//=============================================================================================
    6051
    61 void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
     52void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) {
    6253        this.thrd = thrd;
    6354        this.alarm = alarm;
     
    6859}
    6960
    70 void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s ) with( this ) {
     61void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period ) with( this ) {
    7162        this.proc = proc;
    7263        this.alarm = alarm;
  • src/libcfa/concurrency/alarm.h

    raf1ed1ad r2a84d06d  
    1010// Created On       : Fri Jun 2 11:31:25 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 22 09:59:27 2017
    13 // Update Count     : 3
     12// Last Modified On : Mon Mar 26 16:25:41 2018
     13// Update Count     : 11
    1414//
    1515
     
    2121#include <assert.h>
    2222
    23 #include "bits/cfatime.h"
     23#include "time"
    2424
    2525struct thread_desc;
     
    3030//=============================================================================================
    3131
    32 __cfa_time_t __kernel_get_time();
    33 void __kernel_set_timer( __cfa_time_t alarm );
     32Time __kernel_get_time();
     33void __kernel_set_timer( Duration alarm );
    3434
    3535//=============================================================================================
     
    3838
    3939struct alarm_node_t {
    40         __cfa_time_t alarm;             // time when alarm goes off
    41         __cfa_time_t period;            // if > 0 => period of alarm
     40        Time alarm;                             // time when alarm goes off
     41        Duration period;                        // if > 0 => period of alarm
    4242        alarm_node_t * next;            // intrusive link list field
    4343
     
    5353typedef alarm_node_t ** __alarm_it_t;
    5454
    55 void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s );
    56 void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s );
     55void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period );
     56void ?{}( alarm_node_t & this, processor   * proc, Time alarm, Duration period );
    5757void ^?{}( alarm_node_t & this );
    5858
  • src/libcfa/concurrency/kernel

    raf1ed1ad r2a84d06d  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 22 09:58:39 2017
    13 // Update Count     : 2
     12// Last Modified On : Fri Mar 23 17:08:20 2018
     13// Update Count     : 3
    1414//
    1515
     
    1919
    2020#include "invoke.h"
    21 #include "bits/cfatime.h"
     21#include "time"
    2222
    2323extern "C" {
     
    4949
    5050        // Preemption rate on this cluster
    51         __cfa_time_t preemption_rate;
     51        Duration preemption_rate;
    5252};
    5353
    54 extern __cfa_time_t default_preemption();
     54extern Duration default_preemption();
    5555
    5656void ?{} (cluster & this);
  • src/libcfa/concurrency/preemption.c

    raf1ed1ad r2a84d06d  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  9 16:38:13 2018
    13 // Update Count     : 14
     12// Last Modified On : Tue Mar 27 11:28:51 2018
     13// Update Count     : 24
    1414//
    1515
     
    2727
    2828#if !defined(__CFA_DEFAULT_PREEMPTION__)
    29 #define __CFA_DEFAULT_PREEMPTION__ 10`cfa_ms
     29#define __CFA_DEFAULT_PREEMPTION__ 10`ms
    3030#endif
    3131
    32 __cfa_time_t default_preemption() __attribute__((weak)) {
     32Duration default_preemption() __attribute__((weak)) {
    3333        return __CFA_DEFAULT_PREEMPTION__;
    3434}
     
    7878
    7979// Get next expired node
    80 static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
     80static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) {
    8181        if( !alarms->head ) return NULL;                          // If no alarms return null
    8282        if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
     
    8888        alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
    8989        alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
    90         __cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
     90        Time currtime = __kernel_get_time();                    // Check current time once so we everything "happens at once"
    9191
    9292        //Loop throught every thing expired
     
    102102
    103103                // Check if this is a periodic alarm
    104                 __cfa_time_t period = node->period;
     104                Duration period = node->period;
    105105                if( period > 0 ) {
    106106                        node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
     
    117117
    118118// Update the preemption of a processor and notify interested parties
    119 void update_preemption( processor * this, __cfa_time_t duration ) {
     119void update_preemption( processor * this, Duration duration ) {
    120120        alarm_node_t * alarm = this->preemption_alarm;
    121121
    122122        // Alarms need to be enabled
    123         if ( duration > 0 && !alarm->set ) {
     123        if ( duration > 0 && ! alarm->set ) {
    124124                alarm->alarm = __kernel_get_time() + duration;
    125125                alarm->period = duration;
     
    291291// Used by thread to control when they want to receive preemption signals
    292292void ?{}( preemption_scope & this, processor * proc ) {
    293         (this.alarm){ proc, 0`cfa_s, 0`cfa_s };
     293        (this.alarm){ proc, (Time){ 0 }, 0`s };
    294294        this.proc = proc;
    295295        this.proc->preemption_alarm = &this.alarm;
     
    301301        disable_interrupts();
    302302
    303         update_preemption( this.proc, 0`cfa_s );
     303        update_preemption( this.proc, 0`s );
    304304}
    305305
  • src/libcfa/concurrency/preemption.h

    raf1ed1ad r2a84d06d  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:34:25 2017
    13 // Update Count     : 1
     12// Last Modified On : Fri Mar 23 17:18:53 2018
     13// Update Count     : 2
    1414//
    1515
     
    2121void kernel_start_preemption();
    2222void kernel_stop_preemption();
    23 void update_preemption( processor * this, __cfa_time_t duration );
     23void update_preemption( processor * this, Duration duration );
    2424void tick_preemption();
    2525
  • src/libcfa/time

    raf1ed1ad r2a84d06d  
    11//
    2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // calendar --
     7// time --
    88//
    99// Author           : Peter A. Buhr
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 22 17:11:19 2018
    13 // Update Count     : 495
     12// Last Modified On : Tue Mar 27 16:37:37 2018
     13// Update Count     : 564
    1414//
    1515
     
    1919// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc
    2020
    21 #include <time.h>
     21#include <time.h>                                                                               // timespec
    2222extern "C" {
    23 #include <sys/time.h>
    24 int snprintf( char * buf, size_t size, const char * fmt, ... );
     23#include <sys/time.h>                                                                   // timeval
    2524}
    26 #include <fstream>
    27 
    28 enum {
    29         CLOCKGRAN = 15_000_000L,                                                        // ALWAYS in nanoseconds, MUST BE less than 1 second
    30         TIMEGRAN = 1_000_000_000L                                                       // nanosecond granularity, except for timeval
    31 };
    32 
    33 
    34 #if defined( REALTIME_POSIX )
    35 #define tv_XSEC tv_nsec
    36 #else
    37 #define tv_XSEC tv_usec
    38 #endif
    39 
    40 
    41 #if defined( __linux__ )
    42 // fake a few things
    43 #define CLOCK_REALTIME  0                                                               // real (clock on the wall) time
    44 #endif
    45 
    46 // conversions for existing time types
     25#include <iostream>                                                                             // istype/ostype
     26
     27enum { TIMEGRAN = 1_000_000_000L };                                             // nanosecond granularity, except for timeval
     28
     29
     30//######################### timeval #########################
     31
    4732static inline void ?{}( timeval & t ) {}
    4833static inline void ?{}( timeval & t, time_t sec ) { t.tv_sec = sec; t.tv_usec = 0; }
    4934static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
     35static inline void ?{}( timeval & t, zero_t ) { t.tv_sec = 0; t.tv_usec = 0; }
     36static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
     37static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
     38static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
     39static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
     40static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
     41
     42
     43//######################### timespec #########################
    5044
    5145static inline void ?{}( timespec & t ) {}
    5246static inline void ?{}( timespec & t, time_t sec ) { t.tv_sec = sec; t.tv_nsec = 0; }
    5347static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
     48static inline void ?{}( timespec & t, zero_t ) { t.tv_sec = 0; t.tv_nsec = 0; }
     49static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
     50static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
     51static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
     52static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
     53static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
     54
     55
     56//######################### C time #########################
    5457
    5558static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
     
    6063static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
    6164
    62 
    6365//######################### Duration #########################
    6466
     
    6971static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
    7072static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
     73static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
     74static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
    7175
    7276static inline void ?{}( Duration & dur, timeval t ) with( dur ) {
     
    8892} // ?=? timespec
    8993
     94static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
     95
     96static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
     97static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
     98static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
     99
     100static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
     101static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
     102static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
     103
     104static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
     105static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
     106static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
     107
     108static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
     109static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
     110static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
     111
     112static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
     113
     114static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
     115static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
     116static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
     117static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
     118static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
     119static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
     120
     121static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
     122static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
     123static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
     124static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
     125static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
     126static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
     127
     128static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; }
     129
     130forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
     131
     132static inline Duration ?`ns( int64_t nsec  ) { return (Duration)@{ nsec }; }
     133static inline Duration ?`us( int64_t usec  ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000L) }; }
     134static inline Duration ?`ms( int64_t msec  ) { return (Duration)@{ msec * (TIMEGRAN / 1_000L) }; }
     135static inline Duration ?`s ( int64_t sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
     136static inline Duration ?`s ( double  sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
     137static inline Duration ?`m ( int64_t min   ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
     138static inline Duration ?`m ( double  min   ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
     139static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
     140static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
     141static inline Duration ?`d ( int64_t days  ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
     142static inline Duration ?`d ( double  days  ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
     143static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; }
     144static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; }
     145
     146static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
     147static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000L); }
     148static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000L); }
     149static inline int64_t ?`s  ( Duration dur ) { return dur.tv / TIMEGRAN; }
     150static inline int64_t ?`m  ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); }
     151static inline int64_t ?`h  ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); }
     152static inline int64_t ?`d  ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); }
     153static inline int64_t ?`w  ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); }
     154static inline int64_t ?`f  ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); }
     155
     156//------------------------- timeval (cont) -------------------------
     157
    90158static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
    91159        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
    92         t.tv_usec = tv % TIMEGRAN / ( TIMEGRAN / 1000000L ); // microseconds
     160        t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000L); // microseconds
    93161} // ?{}
     162
     163//------------------------- timespec (cont) -------------------------
    94164
    95165static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
     
    98168} // Timespec
    99169
    100 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
    101 
    102 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
    103 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
    104 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
    105 
    106 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
    107 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
    108 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
    109 
    110 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
    111 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
    112 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
    113 
    114 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
    115 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
    116 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
    117 
    118 static inline Duration ?%?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv % rhs }; }
    119 static inline int64_t ?%?( int64_t lhs, Duration rhs ) { return lhs % (rhs.tv / TIMEGRAN); }
    120 static inline int64_t ?%?( Duration lhs, Duration rhs ) { return lhs.tv % rhs.tv; }
    121 
    122 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
    123 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
    124 static inline _Bool ?<?( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; }
    125 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
    126 static inline _Bool ?>?( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; }
    127 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
    128 
    129 static inline Duration abs( Duration lhs ) {
    130         return lhs.tv >= 0 ? lhs : -lhs;
    131 } // abs
    132 
    133 static inline forall( dtype ostype | ostream( ostype ) )
    134 ostype & ?|?( ostype & os, Duration dur ) with( dur ) {
    135         os | tv / TIMEGRAN;
    136         char buf[16];
    137         snprintf( buf, 16, "%09ld", ((tv < 0 ? -tv : tv) % TIMEGRAN) );
    138         int i;
    139         for ( i = 8; i >= 0 && buf[i] == '0' ; i -= 1 );        // find least significant digit
    140         if ( i != -1 ) { buf[i + 1] = '\0';     os | '.' | buf; }
    141         return os;
    142 }
    143 
    144 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
    145 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000l) }; }
    146 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000_000l) }; }
    147 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    148 static inline Duration ?`s ( double  sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    149 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
    150 static inline Duration ?`m ( double  min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
    151 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
    152 static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
    153 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
    154 static inline Duration ?`d ( double  days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
    155 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; }
    156 static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; }
    157 
    158 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; }
    159 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); }
    160 static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); }
    161 static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); }
    162 static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); }
    163 static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); }
    164 
    165170
    166171//######################### Time #########################
     
    171176};
    172177
    173 #ifdef __CFA_DEBUG__
    174 #define CreateFmt "Attempt to create Time( year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d, nsec=%d ), " \
    175         "which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038."
    176 #endif // __CFA_DEBUG__
    177 
    178 void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) {
    179         tm tm;
    180 
    181 //      tzset();                                                                                        // initialize time global variables
    182         tm.tm_isdst = -1;                                                                       // let mktime determine if alternate timezone is in effect
    183         tm.tm_year = year - 1900;                                                       // mktime uses 1900 as its starting point
    184         tm.tm_mon = month - 1;
    185         tm.tm_mday = day;                                                                       // mktime uses range 1-31
    186         tm.tm_hour = hour;
    187         tm.tm_min = min;
    188         tm.tm_sec = sec;
    189         time_t epochsec = mktime( &tm );
    190 #ifdef __CFA_DEBUG__
    191         if ( epochsec == (time_t)-1 ) {
    192                 abort( CreateFmt, year, month, day, hour, min, sec, nsec );
    193         } // if
    194 #endif // __CFA_DEBUG__
    195         tv = (int64_t)(epochsec) * TIMEGRAN + nsec;                     // convert to nanoseconds
    196 #ifdef __CFA_DEBUG__
    197         if ( tv > 2147483647LL * TIMEGRAN ) {                           // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    198                 abort( CreateFmt, year, month, day, hour, min, sec, nsec );
    199         } // if
    200 #endif // __CFA_DEBUG__
    201 } // mktime
     178void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
    202179
    203180static inline void ?{}( Time & t ) with( t ) {
     
    205182} // Time
    206183
    207 // These two constructors must not call mktime because it calls malloc. The malloc calls lead to recursion problems
    208 // because Time values are created from the sigalrm handler in composing the next context switch event.
    209 
    210 static inline void ?{}( Time & t, int sec ) with( t ) {
    211 #ifdef __CFA_DEBUG__
    212         if ( tv < 0 || tv > 2147483647LL ) {                            // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    213             abort( CreateFmt, 1970, 0, 0, 0, 0, sec, 0 );
    214         } // if
    215 #endif // __CFA_DEBUG__
    216         tv = (int64_t)sec * TIMEGRAN;
    217 } // Time
    218 
    219 static inline void ?{}( Time & t, int sec, int nsec ) with( t ) {
    220 #ifdef __U_DEBUG__
    221         if ( tv < 0 || tv > 2147483647LL || nsec < 0 ) {        // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    222             abort( CreateFmt, 1970, 0, 0, 0, 0, sec, nsec );
    223         } // if
    224 #endif // __U_DEBUG__
    225         tv = (int64_t)sec * TIMEGRAN + nsec;
    226 } // Time
    227 
    228 static inline void ?{}( Time & time, int min, int sec, long int nsec ) {
    229         mktime( time, 1970, 1, 1, 0, min, sec, nsec );
    230 } // Time
    231 
    232 static inline void ?{}( Time & time, int hour, int min, int sec, long int nsec ) {
    233         mktime( time, 1970, 1, 1, hour, min, sec, nsec );
    234 } // Time
    235 
    236 static inline void ?{}( Time & time, int day, int hour, int min, int sec, long int nsec ) {
    237         mktime( time, 1970, 1, day, hour, min, sec, nsec );
    238 } // Time
    239 
    240 static inline void ?{}( Time & time, int month, int day, int hour, int min, int sec, long int nsec ) {
    241         mktime( time, 1970, month, day, hour, min, sec, nsec );
    242 } // Time
    243 
    244 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, long int nsec ) {
     184static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) {
    245185        mktime( time, year, month, day, hour, min, sec, nsec );
     186} // Time
     187
     188static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
     189        mktime( time, year, month, day, hour, min, sec, 0 );
     190} // Time
     191
     192static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
     193        mktime( time, year, month, day, hour, min, 0, 0 );
     194} // Time
     195
     196static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
     197        mktime( time, year, month, day, hour, 0, 0, 0 );
     198} // Time
     199
     200static inline void ?{}( Time & time, int year, int month, int day ) {
     201        mktime( time, year, month, day, 0, 0, 0, 0 );
     202} // Time
     203
     204static inline void ?{}( Time & time, int year, int month ) {
     205        mktime( time, year, month, 0, 0, 0, 0, 0 );
     206} // Time
     207
     208static inline void ?{}( Time & time, int year ) {
     209        mktime( time, year, 0, 0, 0, 0, 0, 0 );
    246210} // Time
    247211
     
    254218} // Time
    255219
     220static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
     221static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
     222
    256223static inline Time ?=?( Time & time, timeval t ) with( time ) {
    257         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
     224        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000L);
    258225        return time;
    259226} // ?=?
     
    273240        t.tv_nsec = tv % TIMEGRAN;                                                      // nanoseconds
    274241} // ?{}
    275 
    276 static inline int64_t nsec( Time time ) with( time ) { return tv; }
    277242
    278243static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
     
    290255static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
    291256
    292 static inline char * yymmd( Time time, char * buf ) with( time ) {
    293         tm tm;
    294         time_t s = tv / TIMEGRAN;
    295         gmtime_r( &s, &tm );
    296         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon, tm.tm_mday );
    297         return buf;
    298 } // yymmd
    299 
    300 static inline char * mmyyd( Time time, char * buf ) with( time ) {
    301         tm tm;
    302         time_t s = tv / TIMEGRAN;
    303         gmtime_r( &s, &tm );
    304         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon, tm.tm_year % 99, tm.tm_mday );
    305         return buf;
    306 } // yymmd
    307 
    308 static inline char * dmmyy( Time time, char * buf ) with( time ) {
    309         tm tm;
    310         time_t s = tv / TIMEGRAN;
    311         gmtime_r( &s, &tm );
    312         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon, tm.tm_year % 99 );
    313         return buf;
    314 } // yymmd
    315 
    316 static inline forall( dtype ostype | ostream( ostype ) )
    317 ostype & ?|?( ostype & os, Time time ) with( time ) {
    318         char buf[32];                                                                           // at least 26
    319         time_t s = tv / TIMEGRAN;
    320         tm tm;
    321         gmtime_r( &s, &tm );                                                            // ctime_r adjusts for timezone
    322         asctime_r( &tm, (char *)&buf );
    323         buf[24] = '\0';                                                                         // remove trailing '\n'
    324         long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;
    325         if ( ns == 0 ) {
    326                 os | buf;
    327         } else {
    328                 buf[19] = '\0';
    329                 os | buf;
    330                 char buf2[16];
    331                 snprintf( buf2, 16, "%09ld", ns );
    332                 int i;
    333                 for ( i = 8; i >= 0 && buf2[i] == '0' ; i -= 1 ); // find least significant digit
    334                 if ( i != -1 ) { buf2[i + 1] = '\0'; os | '.' | buf2; }
    335                 os | ' ' | &buf[20];
    336         } // if
    337         return os;
    338 } // ?|?
     257char * yy_mm_dd( Time time, char * buf );
     258static inline char * ?`ymd( Time time, char * buf ) {
     259        return yy_mm_dd( time, buf );
     260} // ymd
     261
     262char * mm_dd_yy( Time time, char * buf );
     263static inline char * ?`mdy( Time time, char * buf ) {
     264        return mm_dd_yy( time, buf );
     265} // mdy
     266
     267char * dd_mm_yy( Time time, char * buf );
     268static inline char * ?`dmy( Time time, char * buf ) {
     269        return dd_mm_yy( time, buf );;
     270} // dmy
     271
     272size_t strftime( char * buf, size_t size, const char * fmt, Time time );
     273
     274forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
     275
    339276
    340277//######################### Clock #########################
     
    346283};
    347284
    348 void resetClock( Clock & clk ) with( clk ) {
    349         clocktype = CLOCK_REALTIME;
     285static inline void resetClock( Clock & clk ) with( clk ) {
     286        clocktype = CLOCK_REALTIME_COARSE;
    350287} // Clock::resetClock
    351288
    352 void resetClock( Clock & clk, Duration adj ) with( clk ) {
     289static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
    353290        clocktype = -1;
    354291        Duration tz = (timeval){ timezone, 0 };
     
    356293} // resetClock
    357294
    358 void ?{}( Clock & clk ) {
     295static inline void ?{}( Clock & clk ) {
    359296        resetClock( clk );
    360297} // Clock
    361298
    362 void ?{}( Clock & clk, Duration adj ) {
     299static inline void ?{}( Clock & clk, Duration adj ) {
    363300        resetClock( clk, adj );
    364301} // Clock
    365302
    366 Duration getRes() {
     303static inline Duration getRes() {
    367304        struct timespec res;
    368         clock_getres( CLOCK_REALTIME, &res );
     305        clock_getres( CLOCK_REALTIME_COARSE, &res );
    369306        return (Duration){ res };
    370307} // getRes
    371308
    372 Time getTime() {
     309static inline Time getTimeNsec() {                                              // with nanoseconds
    373310        timespec curr;
    374311        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     
    376313} // getTime
    377314
    378 Time getTime( Clock & clk ) with( clk ) {
     315static inline Time getTime() {                                                  // without nanoseconds
     316        timespec curr;
     317        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     318        curr.tv_nsec = 0;
     319        return (Time){ curr };
     320} // getTime
     321
     322static inline Time getTime( Clock & clk ) with( clk ) {
    379323        return getTime() + offset;
    380324} // getTime
    381325
    382 Time ?()( Clock & clk ) with( clk ) {                                   // alternative syntax
     326static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
    383327        return getTime() + offset;
    384328} // getTime
    385329
    386 timeval getTime( Clock & clk ) {
     330static inline timeval getTime( Clock & clk ) {
    387331        return (timeval){ clk() };
    388332} // getTime
    389333
    390 tm getTime( Clock & clk ) with( clk ) {
     334static inline tm getTime( Clock & clk ) with( clk ) {
    391335        tm ret;
    392336        localtime_r( getTime( clk ).tv_sec, &ret );
     
    398342// tab-width: 4 //
    399343// End: //
    400 
Note: See TracChangeset for help on using the changeset viewer.