From 3fdb19cf7a5c3c4ada691fdb9d8e01701c747298 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 15 Jul 2019 02:36:10 +0200 Subject: [PATCH] sched_yield: New module. * lib/sched.in.h: Add _GL_FUNCDECL_RPL, _GL_WARN_ON_USE placeholders. (sched_yield): New declaration. * lib/sched_yield.c: New file. * m4/sched_yield.m4: New file. * m4/sched_h.m4 (gl_SCHED_H): Require gl_SCHED_H_DEFAULTS. Arrange to provide a replacement sched.h always. Test whether sched_yield is declared. (gl_SCHED_MODULE_INDICATOR, gl_SCHED_H_DEFAULTS): New macros. * modules/sched (Depends-on): Add snippet/c++defs, snippet/warn-on-use. (Makefile.am): Provide a replacement sched.h always. Substitute GNULIB_SCHED_YIELD, HAVE_SCHED_YIELD, REPLACE_SCHED_YIELD, _GL_FUNCDECL_RPL, _GL_WARN_ON_USE. * modules/sched_yield: New file. * doc/posix-functions/sched_yield.texi: Mention the new module. --- ChangeLog | 18 ++++ doc/posix-functions/sched_yield.texi | 10 +- lib/sched.in.h | 27 ++++++ lib/sched_yield.c | 46 +++++++++ m4/sched_h.m4 | 134 +++++++++++++++------------ m4/sched_yield.m4 | 23 +++++ modules/sched | 16 ++-- modules/sched_yield | 32 +++++++ 8 files changed, 236 insertions(+), 70 deletions(-) create mode 100644 lib/sched_yield.c create mode 100644 m4/sched_yield.m4 create mode 100644 modules/sched_yield diff --git a/ChangeLog b/ChangeLog index 6616163abb..db2da4483d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2019-07-14 Bruno Haible + + sched_yield: New module. + * lib/sched.in.h: Add _GL_FUNCDECL_RPL, _GL_WARN_ON_USE placeholders. + (sched_yield): New declaration. + * lib/sched_yield.c: New file. + * m4/sched_yield.m4: New file. + * m4/sched_h.m4 (gl_SCHED_H): Require gl_SCHED_H_DEFAULTS. Arrange to + provide a replacement sched.h always. Test whether sched_yield is + declared. + (gl_SCHED_MODULE_INDICATOR, gl_SCHED_H_DEFAULTS): New macros. + * modules/sched (Depends-on): Add snippet/c++defs, snippet/warn-on-use. + (Makefile.am): Provide a replacement sched.h always. Substitute + GNULIB_SCHED_YIELD, HAVE_SCHED_YIELD, REPLACE_SCHED_YIELD, + _GL_FUNCDECL_RPL, _GL_WARN_ON_USE. + * modules/sched_yield: New file. + * doc/posix-functions/sched_yield.texi: Mention the new module. + 2019-07-14 Bruno Haible windows-spin: New module. diff --git a/doc/posix-functions/sched_yield.texi b/doc/posix-functions/sched_yield.texi index 7be9062a5d..35357a2e89 100644 --- a/doc/posix-functions/sched_yield.texi +++ b/doc/posix-functions/sched_yield.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/sched_yield.html} -Gnulib module: --- +Gnulib module: sched_yield Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +Minix 3.1.8, mingw, MSVC 14. +But the provided replacement is just a dummy on some of these platforms: +Minix 3.1.8. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -Minix 3.1.8, mingw, MSVC 14. @end itemize diff --git a/lib/sched.in.h b/lib/sched.in.h index 1fd64b9dbb..af4b55a677 100644 --- a/lib/sched.in.h +++ b/lib/sched.in.h @@ -48,6 +48,10 @@ # include #endif +/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ + +/* The definition of _GL_WARN_ON_USE is copied here. */ + #if !@HAVE_STRUCT_SCHED_PARAM@ # if !GNULIB_defined_struct_sched_param @@ -66,5 +70,28 @@ struct sched_param # define SCHED_OTHER 0 #endif +#if @GNULIB_SCHED_YIELD@ +# if @REPLACE_SCHED_YIELD@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef sched_yield +# define sched_yield rpl_sched_yield +# endif +_GL_FUNCDECL_RPL (sched_yield, int, (void)); +_GL_CXXALIAS_RPL (sched_yield, int, (void)); +# else +# if !@HAVE_SCHED_YIELD@ +_GL_FUNCDECL_SYS (sched_yield, int, (void)); +# endif +_GL_CXXALIAS_SYS (sched_yield, int, (void)); +# endif +_GL_CXXALIASWARN (sched_yield); +#elif defined GNULIB_POSIXCHECK +# undef sched_yield +# if HAVE_RAW_DECL_SCHED_YIELD +_GL_WARN_ON_USE (sched_yield, "sched_yield is not portable - " + "use gnulib module sched_yield for portability"); +# endif +#endif + #endif /* _@GUARD_PREFIX@_SCHED_H */ #endif /* _@GUARD_PREFIX@_SCHED_H */ diff --git a/lib/sched_yield.c b/lib/sched_yield.c new file mode 100644 index 0000000000..57490d141c --- /dev/null +++ b/lib/sched_yield.c @@ -0,0 +1,46 @@ +/* Schedule other threads to run. + Copyright (C) 2019 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +/* Written by Bruno Haible , 2019. */ + +#include + +/* Specification. */ +#include + +#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS +/* Use Windows threads. */ + +# define WIN32_LEAN_AND_MEAN /* avoid including junk */ +# include + +int +sched_yield (void) +{ + Sleep (0); + return 0; +} + +#else +/* Provide a dummy implementation for single-threaded applications. */ + +int +sched_yield (void) +{ + return 0; +} + +#endif diff --git a/m4/sched_h.m4 b/m4/sched_h.m4 index c7a1d974e4..2cc814e2e7 100644 --- a/m4/sched_h.m4 +++ b/m4/sched_h.m4 @@ -1,4 +1,4 @@ -# sched_h.m4 serial 11 +# sched_h.m4 serial 12 dnl Copyright (C) 2008-2019 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -8,68 +8,84 @@ dnl Written by Bruno Haible. AC_DEFUN([gl_SCHED_H], [ + dnl Use AC_REQUIRE here, so that the default behavior below is expanded + dnl once only, before all statements that occur in other macros. + AC_REQUIRE([gl_SCHED_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + AC_CHECK_HEADERS_ONCE([sys/cdefs.h]) - AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM([[ - #include - struct sched_param a; - int b[] = { SCHED_FIFO, SCHED_RR, SCHED_OTHER }; - pid_t t1; - ]])], - [SCHED_H=''], - [SCHED_H='sched.h' - AC_CHECK_HEADERS([sched.h], [], [], - [[#if HAVE_SYS_CDEFS_H - #include - #endif - ]]) - gl_NEXT_HEADERS([sched.h]) + AC_CHECK_HEADERS([sched.h], [], [], + [[#if HAVE_SYS_CDEFS_H + #include + #endif + ]]) + gl_NEXT_HEADERS([sched.h]) + + if test "$ac_cv_header_sched_h" = yes; then + HAVE_SCHED_H=1 + else + HAVE_SCHED_H=0 + fi + AC_SUBST([HAVE_SCHED_H]) - if test "$ac_cv_header_sched_h" = yes; then - HAVE_SCHED_H=1 - else - HAVE_SCHED_H=0 - fi - AC_SUBST([HAVE_SCHED_H]) + if test "$HAVE_SCHED_H" = 1; then + AC_CHECK_TYPE([struct sched_param], + [HAVE_STRUCT_SCHED_PARAM=1], [HAVE_STRUCT_SCHED_PARAM=0], + [[#if HAVE_SYS_CDEFS_H + #include + #endif + #include + ]]) + else + HAVE_STRUCT_SCHED_PARAM=0 + case "$host_os" in + os2*) + dnl On OS/2 kLIBC, struct sched_param is in spawn.h. + AC_CHECK_TYPE([struct sched_param], + [HAVE_STRUCT_SCHED_PARAM=1], [], + [#include ]) + ;; + vms) + dnl On OpenVMS 7.2 or newer, struct sched_param is in pthread.h. + AC_CHECK_TYPE([struct sched_param], + [HAVE_STRUCT_SCHED_PARAM=1], [], + [#include ]) + ;; + esac + fi + AC_SUBST([HAVE_STRUCT_SCHED_PARAM]) - if test "$HAVE_SCHED_H" = 1; then - AC_CHECK_TYPE([struct sched_param], - [HAVE_STRUCT_SCHED_PARAM=1], [HAVE_STRUCT_SCHED_PARAM=0], - [[#if HAVE_SYS_CDEFS_H - #include - #endif - #include - ]]) - else - HAVE_STRUCT_SCHED_PARAM=0 - case "$host_os" in - os2*) - dnl On OS/2 kLIBC, struct sched_param is in spawn.h. - AC_CHECK_TYPE([struct sched_param], - [HAVE_STRUCT_SCHED_PARAM=1], [], - [#include ]) - ;; - vms) - dnl On OpenVMS 7.2 or newer, struct sched_param is in pthread.h. - AC_CHECK_TYPE([struct sched_param], - [HAVE_STRUCT_SCHED_PARAM=1], [], - [#include ]) - ;; - esac - fi - AC_SUBST([HAVE_STRUCT_SCHED_PARAM]) + if test "$ac_cv_header_sys_cdefs_h" = yes; then + HAVE_SYS_CDEFS_H=1 + else + HAVE_SYS_CDEFS_H=0 + fi + AC_SUBST([HAVE_SYS_CDEFS_H]) - if test "$ac_cv_header_sys_cdefs_h" = yes; then - HAVE_SYS_CDEFS_H=1 - else - HAVE_SYS_CDEFS_H=0 - fi - AC_SUBST([HAVE_SYS_CDEFS_H]) + dnl Ensure the type pid_t gets defined. + AC_REQUIRE([AC_TYPE_PID_T]) + + dnl Check for declarations of anything we want to poison if the + dnl corresponding gnulib module is not in use, if it is not common + dnl enough to be declared everywhere. + gl_WARN_ON_USE_PREPARE([[#include + ]], [sched_yield]) +]) - dnl Ensure the type pid_t gets defined. - AC_REQUIRE([AC_TYPE_PID_T]) - ]) - AC_SUBST([SCHED_H]) - AM_CONDITIONAL([GL_GENERATE_SCHED_H], [test -n "$SCHED_H"]) +AC_DEFUN([gl_SCHED_MODULE_INDICATOR], +[ + dnl Use AC_REQUIRE here, so that the default settings are expanded once only. + AC_REQUIRE([gl_SCHED_H_DEFAULTS]) + gl_MODULE_INDICATOR_SET_VARIABLE([$1]) + dnl Define it also as a C macro, for the benefit of the unit tests. + gl_MODULE_INDICATOR_FOR_TESTS([$1]) +]) + +AC_DEFUN([gl_SCHED_H_DEFAULTS], +[ + GNULIB_SCHED_YIELD=0; AC_SUBST([GNULIB_SCHED_YIELD]) + dnl Assume proper GNU behavior unless another module says otherwise. + HAVE_SCHED_YIELD=1; AC_SUBST([HAVE_SCHED_YIELD]) + REPLACE_SCHED_YIELD=0; AC_SUBST([REPLACE_SCHED_YIELD]) ]) diff --git a/m4/sched_yield.m4 b/m4/sched_yield.m4 new file mode 100644 index 0000000000..a65b7e8795 --- /dev/null +++ b/m4/sched_yield.m4 @@ -0,0 +1,23 @@ +# sched_yield.m4 serial 1 +dnl Copyright (C) 2019 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_SCHED_YIELD], +[ + AC_REQUIRE([gl_SCHED_H_DEFAULTS]) + AC_REQUIRE([gl_THREADLIB]) + AC_REQUIRE([AC_CANONICAL_HOST]) + AC_REQUIRE([gl_YIELD]) + + if { case "$host_os" in mingw*) true;; *) false;; esac; } \ + && test $gl_threads_api = windows; then + dnl Choose function names that don't conflict with the mingw-w64 winpthreads + dnl library. + REPLACE_SCHED_YIELD=1 + else + AC_CHECK_DECL([sched_yield], , [HAVE_SCHED_YIELD=0], + [[#include ]]) + fi +]) diff --git a/modules/sched b/modules/sched index b6f285234b..0b6bcae569 100644 --- a/modules/sched +++ b/modules/sched @@ -7,17 +7,18 @@ m4/sched_h.m4 Depends-on: include_next +snippet/c++defs +snippet/warn-on-use configure.ac: gl_SCHED_H Makefile.am: -BUILT_SOURCES += $(SCHED_H) +BUILT_SOURCES += sched.h # We need the following in order to create a replacement for when # the system doesn't have one. -if GL_GENERATE_SCHED_H -sched.h: sched.in.h $(top_builddir)/config.status +sched.h: sched.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(AM_V_GEN)rm -f $@-t $@ && \ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \ sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \ @@ -28,13 +29,14 @@ sched.h: sched.in.h $(top_builddir)/config.status -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \ -e 's|@''NEXT_SCHED_H''@|$(NEXT_SCHED_H)|g' \ -e 's|@''HAVE_STRUCT_SCHED_PARAM''@|$(HAVE_STRUCT_SCHED_PARAM)|g' \ + -e 's/@''GNULIB_SCHED_YIELD''@/$(GNULIB_SCHED_YIELD)/g' \ + -e 's|@''HAVE_SCHED_YIELD''@|$(HAVE_SCHED_YIELD)|g' \ + -e 's|@''REPLACE_SCHED_YIELD''@|$(REPLACE_SCHED_YIELD)|g' \ + -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ + -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ < $(srcdir)/sched.in.h; \ } > $@-t && \ mv $@-t $@ -else -sched.h: $(top_builddir)/config.status - rm -f $@ -endif MOSTLYCLEANFILES += sched.h sched.h-t Include: diff --git a/modules/sched_yield b/modules/sched_yield new file mode 100644 index 0000000000..3a2ae01504 --- /dev/null +++ b/modules/sched_yield @@ -0,0 +1,32 @@ +Description: +Schedule other threads to run. + +Files: +lib/sched_yield.c +m4/sched_yield.m4 +m4/yield.m4 + +Depends-on: +sched +threadlib + +configure.ac: +gl_FUNC_SCHED_YIELD +if test $HAVE_SCHED_YIELD = 0 || test $REPLACE_SCHED_YIELD = 1; then + AC_LIBOBJ([sched_yield]) +fi +gl_SCHED_MODULE_INDICATOR([sched_yield]) + +Makefile.am: + +Include: + + +Link: +$(YIELD_LIB) + +License: +LGPLv2+ + +Maintainer: +all -- 2.39.5