Emulate 'clock' for MS-Windows

* src/w32.c (sys_clock): New function.  (Bug#44674)
* nt/inc/ms-w32.h (clock): Redirect to sys_clock.
This commit is contained in:
Eli Zaretskii 2022-04-27 21:06:55 +03:00
parent 89519d0932
commit 53e8f00111
2 changed files with 30 additions and 0 deletions

View file

@ -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

View file

@ -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