This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
use sysconf(_SC_CLK_TCK) for times()
authorJarkko Hietaniemi <jhi@iki.fi>
Sun, 1 Sep 2002 19:23:55 +0000 (22:23 +0300)
committerhv <hv@crypt.org>
Wed, 4 Sep 2002 12:55:54 +0000 (12:55 +0000)
Message-ID: <20020901162355.GH12536@lyta.hut.fi>

p4raw-id: //depot/perl@17833

embedvar.h
intrpvar.h
perl.c
perlapi.h
pp_sys.c

index 1d76394..dbc4d18 100644 (file)
 #define PL_bufptr              (PERL_GET_INTERP->Ibufptr)
 #define PL_checkav             (PERL_GET_INTERP->Icheckav)
 #define PL_checkav_save                (PERL_GET_INTERP->Icheckav_save)
+#define PL_clocktick           (PERL_GET_INTERP->Iclocktick)
 #define PL_collation_ix                (PERL_GET_INTERP->Icollation_ix)
 #define PL_collation_name      (PERL_GET_INTERP->Icollation_name)
 #define PL_collation_standard  (PERL_GET_INTERP->Icollation_standard)
 #define PL_bufptr              (vTHX->Ibufptr)
 #define PL_checkav             (vTHX->Icheckav)
 #define PL_checkav_save                (vTHX->Icheckav_save)
+#define PL_clocktick           (vTHX->Iclocktick)
 #define PL_collation_ix                (vTHX->Icollation_ix)
 #define PL_collation_name      (vTHX->Icollation_name)
 #define PL_collation_standard  (vTHX->Icollation_standard)
 #define PL_Ibufptr             PL_bufptr
 #define PL_Icheckav            PL_checkav
 #define PL_Icheckav_save       PL_checkav_save
+#define PL_Iclocktick          PL_clocktick
 #define PL_Icollation_ix       PL_collation_ix
 #define PL_Icollation_name     PL_collation_name
 #define PL_Icollation_standard PL_collation_standard
index a957e5b..c64b838 100644 (file)
@@ -524,6 +524,8 @@ PERLVAR(Isort_RealCmp,  SVCOMPARE_t)
 
 PERLVARI(Icheckav_save, AV*, Nullav)   /* save CHECK{}s when compiling */
 
+PERLVARI(Iclocktick, long, 0)  /* this many times() ticks in a second */
+
 /* New variables must be added to the very end for binary compatibility.
  * XSUB.h provides wrapper functions via perlapi.h that make this
  * irrelevant, but not all code may be expected to #include XSUB.h. */
diff --git a/perl.c b/perl.c
index 393ad4f..e04670d 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -26,6 +26,18 @@ char *nw_get_sitelib(const char *pl);
 #include <unistd.h>
 #endif
 
+#ifdef __BEOS__
+#  define HZ 1000000
+#endif
+
+#ifndef HZ
+#  ifdef CLK_TCK
+#    define HZ CLK_TCK
+#  else
+#    define HZ 60
+#  endif
+#endif
+
 #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) && !defined(PERL_MICRO)
 char *getenv (char *); /* Usually in <stdlib.h> */
 #endif
@@ -295,6 +307,14 @@ perl_construct(pTHXx)
     PL_origenviron = environ;
 #endif
 
+    /* Use sysconf(_SC_CLK_TCK) if available, if not
+     * available or if the sysconf() fails, use the HZ. */
+#if defined(HAS_SYSCONF) && defined(_SC_CLK_TCK)
+    PL_clocktick = sysconf(_SC_CLK_TCK);
+    if (PL_clocktick <= 0)
+#endif
+        PL_clocktick = HZ;
+
     ENTER;
 }
 
index ddeeab3..7aa9592 100644 (file)
--- a/perlapi.h
+++ b/perlapi.h
@@ -150,6 +150,8 @@ END_EXTERN_C
 #define PL_checkav             (*Perl_Icheckav_ptr(aTHX))
 #undef  PL_checkav_save
 #define PL_checkav_save                (*Perl_Icheckav_save_ptr(aTHX))
+#undef  PL_clocktick
+#define PL_clocktick           (*Perl_Iclocktick_ptr(aTHX))
 #undef  PL_collation_ix
 #define PL_collation_ix                (*Perl_Icollation_ix_ptr(aTHX))
 #undef  PL_collation_name
index 54699c8..9f15b0e 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -4351,26 +4351,6 @@ PP(pp_time)
     RETURN;
 }
 
-/* XXX The POSIX name is CLK_TCK; it is to be preferred
-   to HZ.  Probably.  For now, assume that if the system
-   defines HZ, it does so correctly.  (Will this break
-   on VMS?)
-   Probably we ought to use _sysconf(_SC_CLK_TCK), if
-   it's supported.    --AD  9/96.
-*/
-
-#ifdef __BEOS__
-#  define HZ 1000000
-#endif
-
-#ifndef HZ
-#  ifdef CLK_TCK
-#    define HZ CLK_TCK
-#  else
-#    define HZ 60
-#  endif
-#endif
-
 PP(pp_tms)
 {
 #ifdef HAS_TIMES
@@ -4384,11 +4364,11 @@ PP(pp_tms)
                                                    /* is returned.                   */
 #endif
 
-    PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
+    PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
     if (GIMME == G_ARRAY) {
-       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
-       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
-       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
+       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
+       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
+       PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
     }
     RETURN;
 #else