diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2022-03-08 18:46:47 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2022-03-08 18:51:18 -0800 |
commit | 6e66d1f90a078099911c52b5ed00b7385d095957 (patch) | |
tree | 6e403964af2878229b4f3c2fd4951c9c98e2b625 /test/src/emacs-module-resources | |
parent | ec35e2621a945797cc4978ebf2e0fe0b0f436705 (diff) | |
download | emacs-6e66d1f90a078099911c52b5ed00b7385d095957.tar.gz emacs-6e66d1f90a078099911c52b5ed00b7385d095957.tar.bz2 emacs-6e66d1f90a078099911c52b5ed00b7385d095957.zip |
Decouple mod-test.c from Gnulib
mod-test.c should not use Gnulib code, as that creates
unnecessary coupling between Emacs internals and this test module.
Also Gnulib code is compiled without -fPIC whereas mod-test.c
needs -fPIC and recompiling Gnulib with -fPIC would be too painful.
* src/Makefile.in (LIB_NANOSLEEP): New macro.
(LIBES): Use it.
* test/Makefile.in (REPLACE_FREE, FREE_SOURCE_0, FREE_SOURCE_0):
Remove. All uses removed.
(LIB_CLOCK_GETTIME, LIB_NANOSLEEP): New macros.
(MODULE_CFLAGS): Do not -I from lib as that would include Gnulib
modifications to standard .h files (e.g., "#define nanosleep
rpl_nanosleep") and we don’t want the Gnulib replacements.
Instead, for gmp.h (on platforms lacking <gmp.h>) simply '-I.'
with a suitable gmp.h.
(gmp.h): New rule to create a suitable gmp.h.
($(test_module)): Depend on config.h since the code uses config.h.
Depend on gmp.h if we need to create a suitable one.
If compiling mini-gmp.h, compile the original one and not
the Emacs-specific one, to lessen coupling with Emacs internals.
Link with LIB_CLOCK_GETTIME and LIB_NANOSLEEP.
(clean): Remove gmp.h.
* test/src/emacs-module-resources/mod-test.c: Don’t include timespec.h.
All uses of timespec.h APIs changed to use system-supplied APIs.
Change _Static_assert to plain assert, so that we needn’t rely
on Gnulib’s _Static_assert.
(timespec_le) [CLOCK_REALTIME]: New function.
Change use of timespec_cmp changed to use this instead.
(Fmod_test_sleep_until, Fmod_test_nanoseconds):
Define these functions and their Lisp names mod-test-sleep-until
and mod-test-nanoseconds only if CLOCK_REALTIME,
since they now won’t work on platforms lacking CLOCK_REALTIME.
(Fmod_test_nanoseconds): Just use _Static_assert since it
should work on all platforms.
* test/src/emacs-module-tests.el (mod-test-sleep-until)
(mod-test-nanoseconds, mod-test-double):
Skip test if the corresponding functione is not defined.
Diffstat (limited to 'test/src/emacs-module-resources')
-rw-r--r-- | test/src/emacs-module-resources/mod-test.c | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/test/src/emacs-module-resources/mod-test.c b/test/src/emacs-module-resources/mod-test.c index 015c1efd978..187af821c22 100644 --- a/test/src/emacs-module-resources/mod-test.c +++ b/test/src/emacs-module-resources/mod-test.c @@ -47,8 +47,6 @@ uintptr_t _beginthread (void (__cdecl *)(void *), unsigned, void *); #include <gmp.h> #include <emacs-module.h> -#include "timespec.h" - int plugin_is_GPL_compatible; #if INTPTR_MAX <= 0 @@ -74,9 +72,6 @@ int plugin_is_GPL_compatible; # error "INTPTR_MAX too large" #endif -/* Smoke test to verify that EMACS_LIMB_MAX is defined. */ -_Static_assert (0 < EMACS_LIMB_MAX, "EMACS_LIMB_MAX missing or incorrect"); - /* Always return symbol 't'. */ static emacs_value Fmod_test_return_t (emacs_env *env, ptrdiff_t nargs, emacs_value args[], @@ -422,6 +417,16 @@ signal_errno (emacs_env *env, const char *function) signal_system_error (env, errno, function); } +#ifdef CLOCK_REALTIME + +/* Whether A <= B. */ +static bool +timespec_le (struct timespec a, struct timespec b) +{ + return (a.tv_sec < b.tv_sec + || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec)); +} + /* A long-running operation that occasionally calls `should_quit' or `process_input'. */ @@ -434,11 +439,13 @@ Fmod_test_sleep_until (emacs_env *env, ptrdiff_t nargs, emacs_value *args, if (env->non_local_exit_check (env)) return NULL; const bool process_input = env->is_not_nil (env, args[1]); - const struct timespec amount = make_timespec(0, 10000000); + const struct timespec amount = { .tv_nsec = 10000000 }; while (true) { - const struct timespec now = current_timespec (); - if (timespec_cmp (now, until) >= 0) + struct timespec now; + if (clock_gettime (CLOCK_REALTIME, &now) != 0) + return NULL; + if (timespec_le (until, now)) break; if (nanosleep (&amount, NULL) && errno != EINTR) { @@ -452,6 +459,7 @@ Fmod_test_sleep_until (emacs_env *env, ptrdiff_t nargs, emacs_value *args, } return env->intern (env, "finished"); } +#endif static emacs_value Fmod_test_add_nanosecond (emacs_env *env, ptrdiff_t nargs, emacs_value *args, @@ -553,6 +561,7 @@ make_big_integer (emacs_env *env, const mpz_t value) return result; } +#ifdef CLOCK_REALTIME static emacs_value Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data) { assert (nargs == 1); @@ -560,11 +569,6 @@ Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void mpz_t nanoseconds; assert (LONG_MIN <= time.tv_sec && time.tv_sec <= LONG_MAX); mpz_init_set_si (nanoseconds, time.tv_sec); -#ifdef __MINGW32__ - _Static_assert (1000000000 <= ULONG_MAX, "unsupported architecture"); -#else - static_assert (1000000000 <= ULONG_MAX, "unsupported architecture"); -#endif mpz_mul_ui (nanoseconds, nanoseconds, 1000000000); assert (0 <= time.tv_nsec && time.tv_nsec <= ULONG_MAX); mpz_add_ui (nanoseconds, nanoseconds, time.tv_nsec); @@ -572,6 +576,7 @@ Fmod_test_nanoseconds (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void mpz_clear (nanoseconds); return result; } +#endif static emacs_value Fmod_test_double (emacs_env *env, ptrdiff_t nargs, emacs_value *args, @@ -631,7 +636,7 @@ sleep_for_half_second (void) #ifdef WINDOWSNT Sleep (500); #else - const struct timespec sleep = {0, 500000000}; + const struct timespec sleep = { .tv_nsec = 500000000 }; if (nanosleep (&sleep, NULL) != 0) perror ("nanosleep"); #endif @@ -763,6 +768,11 @@ bind_function (emacs_env *env, const char *name, emacs_value Sfun) int emacs_module_init (struct emacs_runtime *ert) { + /* These smoke tests don't use _Static_assert because too many + compilers lack support for _Static_assert. */ + assert (0 < EMACS_LIMB_MAX); + assert (1000000000 <= ULONG_MAX); + /* Check that EMACS_MAJOR_VERSION is defined and an integral constant. */ char dummy[EMACS_MAJOR_VERSION]; @@ -815,9 +825,13 @@ emacs_module_init (struct emacs_runtime *ert) DEFUN ("mod-test-invalid-load", Fmod_test_invalid_load, 0, 0, NULL, NULL); DEFUN ("mod-test-invalid-finalizer", Fmod_test_invalid_finalizer, 0, 0, NULL, NULL); +#ifdef CLOCK_REALTIME DEFUN ("mod-test-sleep-until", Fmod_test_sleep_until, 2, 2, NULL, NULL); +#endif DEFUN ("mod-test-add-nanosecond", Fmod_test_add_nanosecond, 1, 1, NULL, NULL); +#ifdef CLOCK_REALTIME DEFUN ("mod-test-nanoseconds", Fmod_test_nanoseconds, 1, 1, NULL, NULL); +#endif DEFUN ("mod-test-double", Fmod_test_double, 1, 1, NULL, NULL); DEFUN ("mod-test-make-function-with-finalizer", Fmod_test_make_function_with_finalizer, 0, 0, NULL, NULL); |