From: Eli Zaretskii Date: Wed, 27 Apr 2022 18:06:55 +0000 (+0300) Subject: Emulate 'clock' for MS-Windows X-Git-Tag: emacs-29.0.90~1931^2~261 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=53e8f00111084b4d98e81a57e025f637835e20a8;p=emacs.git Emulate 'clock' for MS-Windows * src/w32.c (sys_clock): New function. (Bug#44674) * nt/inc/ms-w32.h (clock): Redirect to sys_clock. --- diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h index 3f4b2f34892..2dd9a9a476c 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 25f5555af30..1b10b9965fb 100644 --- a/src/w32.c +++ b/src/w32.c @@ -71,6 +71,8 @@ along with GNU Emacs. If not, see . */ #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