From bf61fd63d635493eeae0313704cc1c9230838dd3 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Thu, 1 Jul 1993 22:29:59 +0000 Subject: [PATCH] Change interfaces of OS_real_time_clock and OS_process_clock to return double instead of clock_t. This combined with changes in the implementations of these procedures should prevent wraparound of these clocks. --- v7/src/microcode/dosenv.c | 23 ++++++++++++++--------- v7/src/microcode/ntenv.c | 10 +++++----- v7/src/microcode/osenv.h | 6 +++--- v7/src/microcode/prosenv.c | 6 +++--- v7/src/microcode/uxenv.c | 32 +++++++++++++++++--------------- 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/v7/src/microcode/dosenv.c b/v7/src/microcode/dosenv.c index bec22c884..e76d02abf 100644 --- a/v7/src/microcode/dosenv.c +++ b/v7/src/microcode/dosenv.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: dosenv.c,v 1.3 1993/01/12 19:48:36 gjr Exp $ +$Id: dosenv.c,v 1.4 1993/07/01 22:29:56 cph Exp $ Copyright (c) 1992-1993 Massachusetts Institute of Technology @@ -83,18 +83,23 @@ DEFUN (OS_encode_time ,(buffer), struct time_structure * buffer) return (t); } -clock_t -DEFUN_VOID (OS_process_clock) +double +DEFUN_VOID (OS_real_time_clock) { - /* This must not signal an error in normal use. */ - /* Return answer in milliseconds, was in 1/100th seconds */ - return (clock()*((clock_t) (1000/CLOCKS_PER_SEC))); + /* Jump through hoops because `clock()' wraps around to zero after + one day and `time()' has at best resolution of one second. */ + return + (((((double) (((long) (OS_encoded_time ())) / 60L)) * 60.0) + + (((double) (((long) (clock ())) % (60L * ((long) CLOCKS_PER_SEC)))) + / ((double) CLOCKS_PER_SEC))) + * 1000.0); } -clock_t -DEFUN_VOID (OS_real_time_clock) +double +DEFUN_VOID (OS_process_clock) { - return (clock()*((clock_t) (1000/CLOCKS_PER_SEC))); + /* This must not signal an error in normal use. */ + return (OS_real_time_clock); } /* Timer adjustments */ diff --git a/v7/src/microcode/ntenv.c b/v7/src/microcode/ntenv.c index 11bac33ca..a6a3e53b8 100644 --- a/v7/src/microcode/ntenv.c +++ b/v7/src/microcode/ntenv.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: ntenv.c,v 1.2 1993/06/24 01:52:11 gjr Exp $ +$Id: ntenv.c,v 1.3 1993/07/01 22:29:57 cph Exp $ Copyright (c) 1992-1993 Massachusetts Institute of Technology @@ -87,18 +87,18 @@ DEFUN (OS_encode_time ,(buffer), struct time_structure * buffer) return (t); } -clock_t +double DEFUN_VOID (OS_process_clock) { /* This must not signal an error in normal use. */ /* Return answer in milliseconds, was in 1/100th seconds */ - return (clock()*((clock_t) (1000/CLOCKS_PER_SEC))); + return ((((double) (clock ())) * 1000.0) / ((double) CLOCKS_PER_SEC)); } -clock_t +double DEFUN_VOID (OS_real_time_clock) { - return (clock()*((clock_t) (1000/CLOCKS_PER_SEC))); + return ((((double) (clock ())) * 1000.0) / ((double) CLOCKS_PER_SEC)) } /* Timer adjustments */ diff --git a/v7/src/microcode/osenv.h b/v7/src/microcode/osenv.h index dd6edc2c2..41af186e9 100644 --- a/v7/src/microcode/osenv.h +++ b/v7/src/microcode/osenv.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: osenv.h,v 1.3 1993/01/12 19:48:12 gjr Exp $ +$Id: osenv.h,v 1.4 1993/07/01 22:29:57 cph Exp $ Copyright (c) 1990-1993 Massachusetts Institute of Technology @@ -51,8 +51,8 @@ struct time_structure extern time_t EXFUN (OS_encoded_time, ()); extern void EXFUN (OS_decode_time, (time_t, struct time_structure * ts)); extern time_t EXFUN (OS_encode_time, (struct time_structure * ts)); -extern clock_t EXFUN (OS_process_clock, (void)); -extern clock_t EXFUN (OS_real_time_clock, (void)); +extern double EXFUN (OS_process_clock, (void)); +extern double EXFUN (OS_real_time_clock, (void)); extern void EXFUN (OS_process_timer_set, (clock_t first, clock_t interval)); extern void EXFUN (OS_process_timer_clear, (void)); extern void EXFUN (OS_real_timer_set, (clock_t first, clock_t interval)); diff --git a/v7/src/microcode/prosenv.c b/v7/src/microcode/prosenv.c index 33098fe41..fd1270753 100644 --- a/v7/src/microcode/prosenv.c +++ b/v7/src/microcode/prosenv.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: prosenv.c,v 1.8 1993/01/12 19:48:19 gjr Exp $ +$Id: prosenv.c,v 1.9 1993/07/01 22:29:58 cph Exp $ Copyright (c) 1987-1993 Massachusetts Institute of Technology @@ -123,14 +123,14 @@ DEFINE_PRIMITIVE ("SYSTEM-CLOCK", Prim_system_clock, 0, 0, "Return the current process time in units of milliseconds.") { PRIMITIVE_HEADER (0); - PRIMITIVE_RETURN (ulong_to_integer (OS_process_clock ())); + PRIMITIVE_RETURN (double_to_integer (OS_process_clock ())); } DEFINE_PRIMITIVE ("REAL-TIME-CLOCK", Prim_real_time_clock, 0, 0, "Return the current real time in units of milliseconds.") { PRIMITIVE_HEADER (0); - PRIMITIVE_RETURN (ulong_to_integer (OS_real_time_clock ())); + PRIMITIVE_RETURN (double_to_integer (OS_real_time_clock ())); } DEFINE_PRIMITIVE ("PROCESS-TIMER-CLEAR", Prim_process_timer_clear, 0, 0, diff --git a/v7/src/microcode/uxenv.c b/v7/src/microcode/uxenv.c index 433b5ab23..e2bc44baa 100644 --- a/v7/src/microcode/uxenv.c +++ b/v7/src/microcode/uxenv.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: uxenv.c,v 1.10 1993/02/06 05:42:47 gjr Exp $ +$Id: uxenv.c,v 1.11 1993/07/01 22:29:59 cph Exp $ Copyright (c) 1990-1993 Massachusetts Institute of Technology @@ -104,10 +104,10 @@ DEFUN_VOID (initialize_process_clock) initial_process_clock = (buffer . tms_utime); } -clock_t +double DEFUN_VOID (OS_process_clock) { - clock_t ct = (UX_SC_CLK_TCK ()); + double ct = ((double) (UX_SC_CLK_TCK ())); struct tms buffer; /* Was STD_VOID_SYSTEM_CALL, but at least one version of Ultrix returns negative numbers other than -1 when there are no errors. */ @@ -115,8 +115,9 @@ DEFUN_VOID (OS_process_clock) if (errno != EINTR) error_system_call (errno, syscall_times); return - (((((buffer . tms_utime) - initial_process_clock) * 2000) + ct) / - (2 * ct)); + (((((double) ((buffer . tms_utime) - initial_process_clock)) * 2000.0) + + ct) + / (2.0 * ct)); } #else /* not HAVE_TIMES */ @@ -126,11 +127,11 @@ DEFUN_VOID (initialize_process_clock) { } -clock_t +double DEFUN_VOID (OS_process_clock) { /* This must not signal an error in normal use. */ - return (0); + return (0.0); } #endif /* HAVE_TIMES */ @@ -146,7 +147,7 @@ DEFUN_VOID (initialize_real_time_clock) UX_gettimeofday ((&initial_rtc), (&tz)); } -clock_t +double DEFUN_VOID (OS_real_time_clock) { struct timeval rtc; @@ -154,8 +155,9 @@ DEFUN_VOID (OS_real_time_clock) STD_VOID_SYSTEM_CALL (syscall_gettimeofday, (UX_gettimeofday ((&rtc), (&tz)))); return - ((((rtc . tv_sec) - (initial_rtc . tv_sec)) * 1000) + - ((((rtc . tv_usec) - (initial_rtc . tv_usec)) + 500) / 1000)); + ((((double) ((rtc . tv_sec) - (initial_rtc . tv_sec))) * 1000.0) + + ((((double) ((rtc . tv_usec) - (initial_rtc . tv_usec))) + 500.0) + / 1000.0)); } #else /* not HAVE_GETTIMEOFDAY */ @@ -170,10 +172,10 @@ DEFUN_VOID (initialize_real_time_clock) initial_rtc = (UX_times (&buffer)); } -clock_t +double DEFUN_VOID (OS_real_time_clock) { - clock_t ct = (UX_SC_CLK_TCK ()); + double ct = ((double) (UX_SC_CLK_TCK ())); struct tms buffer; clock_t t; /* Was STD_UINT_SYSTEM_CALL, but at least one version of Ultrix @@ -181,7 +183,7 @@ DEFUN_VOID (OS_real_time_clock) while ((t = (UX_times (&buffer))) == (-1)) if (errno != EINTR) error_system_call (errno, syscall_times); - return ((((t - initial_rtc) * 2000) + ct) / (2 * ct)); + return (((((double) (t - initial_rtc)) * 2000.0) + ct) / (2.0 * ct)); } #else /* not HAVE_TIMES */ @@ -194,12 +196,12 @@ DEFUN_VOID (initialize_real_time_clock) initial_rtc = (time (0)); } -clock_t +double DEFUN_VOID (OS_real_time_clock) { time_t t; STD_UINT_SYSTEM_CALL (syscall_time, t, (UX_time (0))); - return ((t - initial_rtc) * 1000); + return (((double) (t - initial_rtc)) * 1000.0); } #endif /* HAVE_TIMES */ -- 2.25.1