Changeset 2a84d06d
- Timestamp:
- Mar 27, 2018, 5:22:58 PM (5 years ago)
- Branches:
- aaron-thesis, arm-eh, 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
- Location:
- src/libcfa
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/Makefile.am
raf1ed1ad r2a84d06d 11 11 ## Created On : Sun May 31 08:54:01 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : T hu Mar 22 17:14:15 201814 ## Update Count : 22 413 ## Last Modified On : Tue Mar 27 14:27:05 2018 14 ## Update Count : 225 15 15 ############################################################################### 16 16 … … 46 46 CC = ${abs_top_srcdir}/src/driver/cfa 47 47 48 headers = fstream iostream iterator limits rational stdlib \48 headers = fstream iostream iterator limits rational time stdlib \ 49 49 containers/maybe containers/pair containers/result containers/vector 50 50 … … 99 99 ${stdhdr} \ 100 100 math \ 101 time \102 101 gmp \ 103 102 bits/align.h \ -
src/libcfa/Makefile.in
raf1ed1ad r2a84d06d 150 150 am__libcfa_d_a_SOURCES_DIST = libcfa-prelude.c interpose.c \ 151 151 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 \ 154 154 concurrency/coroutine.c concurrency/thread.c \ 155 155 concurrency/kernel.c concurrency/monitor.c assert.c \ … … 165 165 libcfa_d_a-iostream.$(OBJEXT) libcfa_d_a-iterator.$(OBJEXT) \ 166 166 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) \ 168 168 containers/libcfa_d_a-maybe.$(OBJEXT) \ 169 169 containers/libcfa_d_a-pair.$(OBJEXT) \ … … 184 184 libcfa_a_LIBADD = 185 185 am__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 193 194 @BUILD_CONCURRENCY_TRUE@am__objects_5 = concurrency/libcfa_a-coroutine.$(OBJEXT) \ 194 195 @BUILD_CONCURRENCY_TRUE@ concurrency/libcfa_a-thread.$(OBJEXT) \ … … 197 198 am__objects_6 = libcfa_a-fstream.$(OBJEXT) libcfa_a-iostream.$(OBJEXT) \ 198 199 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) \ 201 202 containers/libcfa_a-pair.$(OBJEXT) \ 202 203 containers/libcfa_a-result.$(OBJEXT) \ … … 260 261 esac 261 262 am__nobase_cfa_include_HEADERS_DIST = fstream iostream iterator limits \ 262 rational stdlib containers/maybe containers/pair \263 rational time stdlib containers/maybe containers/pair \ 263 264 containers/result containers/vector concurrency/coroutine \ 264 265 concurrency/thread concurrency/kernel concurrency/monitor \ 265 ${shell find stdhdr -type f -printf "%p "} math timegmp \266 ${shell find stdhdr -type f -printf "%p "} math gmp \ 266 267 bits/align.h bits/cfatime.h bits/containers.h bits/defs.h \ 267 268 bits/debug.h bits/locks.h concurrency/invoke.h … … 419 420 EXTRA_FLAGS = -g -Wall -Wno-unused-function -imacros libcfa-prelude.c @CFA_FLAGS@ 420 421 AM_CCASFLAGS = @CFA_FLAGS@ 421 headers = fstream iostream iterator limits rational stdlib \422 headers = fstream iostream iterator limits rational time stdlib \ 422 423 containers/maybe containers/pair containers/result \ 423 424 containers/vector $(am__append_3) … … 435 436 ${stdhdr} \ 436 437 math \ 437 time \438 438 gmp \ 439 439 bits/align.h \ … … 612 612 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-rational.Po@am__quote@ 613 613 @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@ 614 615 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_a-virtual.Po@am__quote@ 615 616 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-assert.Po@am__quote@ … … 623 624 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-rational.Po@am__quote@ 624 625 @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@ 625 627 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-virtual.Po@am__quote@ 626 628 @AMDEP_TRUE@@am__include@ @am__quote@bits/$(DEPDIR)/libcfa_a-debug.Po@am__quote@ … … 787 789 @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` 788 790 791 libcfa_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 798 libcfa_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 789 805 libcfa_d_a-stdlib.o: stdlib.c 790 806 @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 … … 1080 1096 @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 1081 1097 @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 1099 libcfa_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 1106 libcfa_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` 1082 1112 1083 1113 libcfa_a-stdlib.o: stdlib.c -
src/libcfa/concurrency/alarm.c
raf1ed1ad r2a84d06d 10 10 // Created On : Fri Jun 2 11:31:25 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:35:18 201713 // Update Count : 112 // Last Modified On : Tue Mar 27 14:12:11 2018 13 // Update Count : 41 14 14 // 15 15 … … 27 27 28 28 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; 29 static inline void ?{}( itimerval & this, Duration alarm ) with( this ) { 30 it_value { alarm }; // seconds, microseconds 31 it_interval{ 0 }; 40 32 } 41 33 … … 44 36 //============================================================================================= 45 37 46 __cfa_time_t__kernel_get_time() {38 Time __kernel_get_time() { 47 39 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 }; 50 42 } 51 43 52 void __kernel_set_timer( __cfa_time_t alarm ) { 53 itimerval val = { &alarm }; 54 setitimer( ITIMER_REAL, &val, NULL ); 44 void __kernel_set_timer( Duration alarm ) { 45 setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL ); 55 46 } 56 47 … … 59 50 //============================================================================================= 60 51 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 ) {52 void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) { 62 53 this.thrd = thrd; 63 54 this.alarm = alarm; … … 68 59 } 69 60 70 void ?{}( alarm_node_t & this, processor * proc, __cfa_time_t alarm = 0`cfa_s, __cfa_time_t period = 0`cfa_s) with( this ) {61 void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) { 71 62 this.proc = proc; 72 63 this.alarm = alarm; -
src/libcfa/concurrency/alarm.h
raf1ed1ad r2a84d06d 10 10 // Created On : Fri Jun 2 11:31:25 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jul 22 09:59:27 201713 // Update Count : 312 // Last Modified On : Mon Mar 26 16:25:41 2018 13 // Update Count : 11 14 14 // 15 15 … … 21 21 #include <assert.h> 22 22 23 #include " bits/cfatime.h"23 #include "time" 24 24 25 25 struct thread_desc; … … 30 30 //============================================================================================= 31 31 32 __cfa_time_t__kernel_get_time();33 void __kernel_set_timer( __cfa_time_talarm );32 Time __kernel_get_time(); 33 void __kernel_set_timer( Duration alarm ); 34 34 35 35 //============================================================================================= … … 38 38 39 39 struct alarm_node_t { 40 __cfa_time_t alarm;// time when alarm goes off41 __cfa_time_t period;// if > 0 => period of alarm40 Time alarm; // time when alarm goes off 41 Duration period; // if > 0 => period of alarm 42 42 alarm_node_t * next; // intrusive link list field 43 43 … … 53 53 typedef alarm_node_t ** __alarm_it_t; 54 54 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);55 void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ); 56 void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ); 57 57 void ^?{}( alarm_node_t & this ); 58 58 -
src/libcfa/concurrency/kernel
raf1ed1ad r2a84d06d 10 10 // Created On : Tue Jan 17 12:27:26 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jul 22 09:58:39 201713 // Update Count : 212 // Last Modified On : Fri Mar 23 17:08:20 2018 13 // Update Count : 3 14 14 // 15 15 … … 19 19 20 20 #include "invoke.h" 21 #include " bits/cfatime.h"21 #include "time" 22 22 23 23 extern "C" { … … 49 49 50 50 // Preemption rate on this cluster 51 __cfa_time_tpreemption_rate;51 Duration preemption_rate; 52 52 }; 53 53 54 extern __cfa_time_tdefault_preemption();54 extern Duration default_preemption(); 55 55 56 56 void ?{} (cluster & this); -
src/libcfa/concurrency/preemption.c
raf1ed1ad r2a84d06d 10 10 // Created On : Mon Jun 5 14:20:42 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Feb 9 16:38:13201813 // Update Count : 1412 // Last Modified On : Tue Mar 27 11:28:51 2018 13 // Update Count : 24 14 14 // 15 15 … … 27 27 28 28 #if !defined(__CFA_DEFAULT_PREEMPTION__) 29 #define __CFA_DEFAULT_PREEMPTION__ 10` cfa_ms29 #define __CFA_DEFAULT_PREEMPTION__ 10`ms 30 30 #endif 31 31 32 __cfa_time_tdefault_preemption() __attribute__((weak)) {32 Duration default_preemption() __attribute__((weak)) { 33 33 return __CFA_DEFAULT_PREEMPTION__; 34 34 } … … 78 78 79 79 // Get next expired node 80 static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_tcurrtime ) {80 static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) { 81 81 if( !alarms->head ) return NULL; // If no alarms return null 82 82 if( alarms->head->alarm >= currtime ) return NULL; // If alarms head not expired return null … … 88 88 alarm_node_t * node = NULL; // Used in the while loop but cannot be declared in the while condition 89 89 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" 91 91 92 92 //Loop throught every thing expired … … 102 102 103 103 // Check if this is a periodic alarm 104 __cfa_time_tperiod = node->period;104 Duration period = node->period; 105 105 if( period > 0 ) { 106 106 node->alarm = currtime + period; // Alarm is periodic, add currtime to it (used cached current time) … … 117 117 118 118 // Update the preemption of a processor and notify interested parties 119 void update_preemption( processor * this, __cfa_time_tduration ) {119 void update_preemption( processor * this, Duration duration ) { 120 120 alarm_node_t * alarm = this->preemption_alarm; 121 121 122 122 // Alarms need to be enabled 123 if ( duration > 0 && ! alarm->set ) {123 if ( duration > 0 && ! alarm->set ) { 124 124 alarm->alarm = __kernel_get_time() + duration; 125 125 alarm->period = duration; … … 291 291 // Used by thread to control when they want to receive preemption signals 292 292 void ?{}( preemption_scope & this, processor * proc ) { 293 (this.alarm){ proc, 0`cfa_s, 0`cfa_s };293 (this.alarm){ proc, (Time){ 0 }, 0`s }; 294 294 this.proc = proc; 295 295 this.proc->preemption_alarm = &this.alarm; … … 301 301 disable_interrupts(); 302 302 303 update_preemption( this.proc, 0` cfa_s );303 update_preemption( this.proc, 0`s ); 304 304 } 305 305 -
src/libcfa/concurrency/preemption.h
raf1ed1ad r2a84d06d 10 10 // Created On : Mon Jun 5 14:20:42 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:34:25 201713 // Update Count : 112 // Last Modified On : Fri Mar 23 17:18:53 2018 13 // Update Count : 2 14 14 // 15 15 … … 21 21 void kernel_start_preemption(); 22 22 void kernel_stop_preemption(); 23 void update_preemption( processor * this, __cfa_time_tduration );23 void update_preemption( processor * this, Duration duration ); 24 24 void tick_preemption(); 25 25 -
src/libcfa/time
raf1ed1ad r2a84d06d 1 1 // 2 // Cforall Version 1.0.0 Copyright (C) 201 6University of Waterloo2 // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo 3 3 // 4 4 // The contents of this file are covered under the licence agreement in the 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // calendar--7 // time -- 8 8 // 9 9 // Author : Peter A. Buhr 10 10 // Created On : Wed Mar 14 23:18:57 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Mar 22 17:11:19201813 // Update Count : 49512 // Last Modified On : Tue Mar 27 16:37:37 2018 13 // Update Count : 564 14 14 // 15 15 … … 19 19 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc 20 20 21 #include <time.h> 21 #include <time.h> // timespec 22 22 extern "C" { 23 #include <sys/time.h> 24 int snprintf( char * buf, size_t size, const char * fmt, ... ); 23 #include <sys/time.h> // timeval 25 24 } 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 27 enum { TIMEGRAN = 1_000_000_000L }; // nanosecond granularity, except for timeval 28 29 30 //######################### timeval ######################### 31 47 32 static inline void ?{}( timeval & t ) {} 48 33 static inline void ?{}( timeval & t, time_t sec ) { t.tv_sec = sec; t.tv_usec = 0; } 49 34 static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; } 35 static inline void ?{}( timeval & t, zero_t ) { t.tv_sec = 0; t.tv_usec = 0; } 36 static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; } 37 static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; } 38 static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; } 39 static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; } 40 static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; } 41 42 43 //######################### timespec ######################### 50 44 51 45 static inline void ?{}( timespec & t ) {} 52 46 static inline void ?{}( timespec & t, time_t sec ) { t.tv_sec = sec; t.tv_nsec = 0; } 53 47 static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; } 48 static inline void ?{}( timespec & t, zero_t ) { t.tv_sec = 0; t.tv_nsec = 0; } 49 static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; } 50 static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; } 51 static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; } 52 static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; } 53 static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; } 54 55 56 //######################### C time ######################### 54 57 55 58 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; } … … 60 63 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); } 61 64 62 63 65 //######################### Duration ######################### 64 66 … … 69 71 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; } 70 72 static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; } 73 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; } 74 static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; } 71 75 72 76 static inline void ?{}( Duration & dur, timeval t ) with( dur ) { … … 88 92 } // ?=? timespec 89 93 94 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; } 95 96 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; } 97 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; } 98 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } 99 100 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; } 101 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } 102 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } 103 104 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; } 105 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; } 106 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; } 107 108 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; } 109 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; } 110 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; } 111 112 static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; } 113 114 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; } 115 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; } 116 static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; } 117 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; } 118 static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; } 119 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; } 120 121 static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; } 122 static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; } 123 static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv < 0; } 124 static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; } 125 static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv > 0; } 126 static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; } 127 128 static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; } 129 130 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 131 132 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; } 133 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000L) }; } 134 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000L) }; } 135 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; } 136 static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; } 137 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; } 138 static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; } 139 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; } 140 static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; } 141 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; } 142 static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; } 143 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; } 144 static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; } 145 146 static inline int64_t ?`ns ( Duration dur ) { return dur.tv; } 147 static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000L); } 148 static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000L); } 149 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; } 150 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); } 151 static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); } 152 static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); } 153 static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); } 154 static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); } 155 156 //------------------------- timeval (cont) ------------------------- 157 90 158 static inline void ?{}( timeval & t, Duration dur ) with( dur ) { 91 159 t.tv_sec = tv / TIMEGRAN; // seconds 92 t.tv_usec = tv % TIMEGRAN / ( TIMEGRAN / 1000000L); // microseconds160 t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000L); // microseconds 93 161 } // ?{} 162 163 //------------------------- timespec (cont) ------------------------- 94 164 95 165 static inline void ?{}( timespec & t, Duration dur ) with( dur ) { … … 98 168 } // Timespec 99 169 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 } // abs132 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 digit140 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 165 170 166 171 //######################### Time ######################### … … 171 176 }; 172 177 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 178 void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ); 202 179 203 180 static inline void ?{}( Time & t ) with( t ) { … … 205 182 } // Time 206 183 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 ) { 184 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) { 245 185 mktime( time, year, month, day, hour, min, sec, nsec ); 186 } // Time 187 188 static 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 192 static 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 196 static 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 200 static inline void ?{}( Time & time, int year, int month, int day ) { 201 mktime( time, year, month, day, 0, 0, 0, 0 ); 202 } // Time 203 204 static inline void ?{}( Time & time, int year, int month ) { 205 mktime( time, year, month, 0, 0, 0, 0, 0 ); 206 } // Time 207 208 static inline void ?{}( Time & time, int year ) { 209 mktime( time, year, 0, 0, 0, 0, 0, 0 ); 246 210 } // Time 247 211 … … 254 218 } // Time 255 219 220 static inline void ?{}( Time & t, zero_t ) { t.tv = 0; } 221 static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; } 222 256 223 static 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); 258 225 return time; 259 226 } // ?=? … … 273 240 t.tv_nsec = tv % TIMEGRAN; // nanoseconds 274 241 } // ?{} 275 276 static inline int64_t nsec( Time time ) with( time ) { return tv; }277 242 278 243 static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } … … 290 255 static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } 291 256 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 } // ?|? 257 char * yy_mm_dd( Time time, char * buf ); 258 static inline char * ?`ymd( Time time, char * buf ) { 259 return yy_mm_dd( time, buf ); 260 } // ymd 261 262 char * mm_dd_yy( Time time, char * buf ); 263 static inline char * ?`mdy( Time time, char * buf ) { 264 return mm_dd_yy( time, buf ); 265 } // mdy 266 267 char * dd_mm_yy( Time time, char * buf ); 268 static inline char * ?`dmy( Time time, char * buf ) { 269 return dd_mm_yy( time, buf );; 270 } // dmy 271 272 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 273 274 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 275 339 276 340 277 //######################### Clock ######################### … … 346 283 }; 347 284 348 void resetClock( Clock & clk ) with( clk ) {349 clocktype = CLOCK_REALTIME ;285 static inline void resetClock( Clock & clk ) with( clk ) { 286 clocktype = CLOCK_REALTIME_COARSE; 350 287 } // Clock::resetClock 351 288 352 void resetClock( Clock & clk, Duration adj ) with( clk ) {289 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) { 353 290 clocktype = -1; 354 291 Duration tz = (timeval){ timezone, 0 }; … … 356 293 } // resetClock 357 294 358 void ?{}( Clock & clk ) {295 static inline void ?{}( Clock & clk ) { 359 296 resetClock( clk ); 360 297 } // Clock 361 298 362 void ?{}( Clock & clk, Duration adj ) {299 static inline void ?{}( Clock & clk, Duration adj ) { 363 300 resetClock( clk, adj ); 364 301 } // Clock 365 302 366 Duration getRes() {303 static inline Duration getRes() { 367 304 struct timespec res; 368 clock_getres( CLOCK_REALTIME , &res );305 clock_getres( CLOCK_REALTIME_COARSE, &res ); 369 306 return (Duration){ res }; 370 307 } // getRes 371 308 372 Time getTime() { 309 static inline Time getTimeNsec() { // with nanoseconds 373 310 timespec curr; 374 311 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); … … 376 313 } // getTime 377 314 378 Time getTime( Clock & clk ) with( clk ) { 315 static 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 322 static inline Time getTime( Clock & clk ) with( clk ) { 379 323 return getTime() + offset; 380 324 } // getTime 381 325 382 Time ?()( Clock & clk ) with( clk ) {// alternative syntax326 static inline Time ?()( Clock & clk ) with( clk ) { // alternative syntax 383 327 return getTime() + offset; 384 328 } // getTime 385 329 386 timeval getTime( Clock & clk ) {330 static inline timeval getTime( Clock & clk ) { 387 331 return (timeval){ clk() }; 388 332 } // getTime 389 333 390 tm getTime( Clock & clk ) with( clk ) {334 static inline tm getTime( Clock & clk ) with( clk ) { 391 335 tm ret; 392 336 localtime_r( getTime( clk ).tv_sec, &ret ); … … 398 342 // tab-width: 4 // 399 343 // End: // 400
Note: See TracChangeset
for help on using the changeset viewer.