emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 53e8f00111: Emulate 'clock' for MS-Windows


From: Eli Zaretskii
Subject: master 53e8f00111: Emulate 'clock' for MS-Windows
Date: Wed, 27 Apr 2022 14:07:58 -0400 (EDT)

branch: master
commit 53e8f00111084b4d98e81a57e025f637835e20a8
Author: Eli Zaretskii <eliz@gnu.org>
Commit: Eli Zaretskii <eliz@gnu.org>

    Emulate 'clock' for MS-Windows
    
    * src/w32.c (sys_clock): New function.  (Bug#44674)
    * nt/inc/ms-w32.h (clock): Redirect to sys_clock.
---
 nt/inc/ms-w32.h |  1 +
 src/w32.c       | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h
index 3f4b2f3489..2dd9a9a476 100644
--- a/nt/inc/ms-w32.h
+++ b/nt/inc/ms-w32.h
@@ -295,6 +295,7 @@ extern int sys_unlink (const char *);
 #undef umask
 #define umask   sys_umask
 extern int sys_umask (int);
+#define clock   sys_clock
 
 /* Subprocess calls that are emulated.  */
 #define spawnve sys_spawnve
diff --git a/src/w32.c b/src/w32.c
index 25f5555af3..1b10b9965f 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -71,6 +71,8 @@ along with GNU Emacs.  If not, see 
<https://www.gnu.org/licenses/>.  */
 
 #undef localtime
 
+#undef clock
+
 char *sys_ctime (const time_t *);
 int sys_chdir (const char *);
 int sys_creat (const char *, int);
@@ -87,6 +89,7 @@ struct tm *sys_localtime (const time_t *);
    compiler to emit a warning about sys_strerror having no
    prototype.  */
 char *sys_strerror (int);
+clock_t sys_clock (void);
 
 #ifdef HAVE_MODULES
 extern void dynlib_reset_last_error (void);
@@ -10155,6 +10158,32 @@ sys_localtime (const time_t *t)
   return localtime (t);
 }
 
+/* The Windows CRT implementation of 'clock' doesn't really return CPU
+   time of the process (it returns the elapsed time since the process
+   started), so we provide a better emulation here, if possible.  */
+clock_t
+sys_clock (void)
+{
+  if (get_process_times_fn)
+    {
+      FILETIME create, exit, kernel, user;
+      HANDLE proc = GetCurrentProcess ();
+      if ((*get_process_times_fn) (proc, &create, &exit, &kernel, &user))
+        {
+          LARGE_INTEGER user_int, kernel_int, total;
+          user_int.LowPart = user.dwLowDateTime;
+          user_int.HighPart = user.dwHighDateTime;
+          kernel_int.LowPart = kernel.dwLowDateTime;
+          kernel_int.HighPart = kernel.dwHighDateTime;
+          total.QuadPart = user_int.QuadPart + kernel_int.QuadPart;
+         /* We could redefine CLOCKS_PER_SEC to provide a finer
+            resolution, but with the basic 15.625 msec resolution of
+            the Windows clock, it doesn't really sound worth the hassle.  */
+         return total.QuadPart / (10000000 / CLOCKS_PER_SEC);
+        }
+    }
+  return clock ();
+}
 
 
 /* Try loading LIBRARY_ID from the file(s) specified in



reply via email to

[Prev in Thread] Current Thread [Next in Thread]