> > * We establish a new macro GNULIB_MBTOWC_SINGLE which means "assume that > > at most one thread invokes mbtowc-like functions". The mbtowc > > replacement uses this instead of using USE_UNLOCKED_IO. Tar can #define > > this new macro. > > I like this idea. The packages can then define or not define each such > macro individually.
Implemented through the following series of patches. 2021-03-07 Bruno Haible <br...@clisp.org> Rename GNULIB_WCHAR_SINGLE to GNULIB_WCHAR_SINGLE_LOCALE. * lib/lc-charset-dispatch.c: Test GNULIB_WCHAR_SINGLE_LOCALE instead of GNULIB_WCHAR_SINGLE. * lib/wcwidth.c: Likewise. * tests/test-wcwidth.c: Likewise. * doc/multithread.texi: Document GNULIB_WCHAR_SINGLE_LOCALE instead of GNULIB_WCHAR_SINGLE. * NEWS: Document the change. 2021-03-06 Bruno Haible <br...@clisp.org> mbrtowc: Allow locking optimization independently of 'unlocked-io'. * lib/mbtowc-lock.h: Test GNULIB_MBRTOWC_SINGLE_THREAD instead of USE_UNLOCKED_IO. * doc/multithread.texi: Document GNULIB_MBRTOWC_SINGLE_THREAD. 2021-03-06 Bruno Haible <br...@clisp.org> regex: Allow locking optimization independently of 'unlocked-io'. * lib/regex_internal.h: Test GNULIB_REGEX_SINGLE_THREAD instead of USE_UNLOCKED_IO. * doc/multithread.texi: Document GNULIB_REGEX_SINGLE_THREAD. 2021-03-06 Bruno Haible <br...@clisp.org> exclude: Allow stdio optimization independently of 'unlocked-io'. * lib/exclude.c: Test GNULIB_EXCLUDE_SINGLE_THREAD instead of USE_UNLOCKED_IO. * modules/exclude (Depends-on): Add unlocked-io-internal. * doc/multithread.texi: Document GNULIB_EXCLUDE_SINGLE_THREAD. 2021-03-06 Bruno Haible <br...@clisp.org> readutmp: Optimize stdio accesses. * lib/readutmp.c: Include unlocked-io.h unconditionally. * modules/readutmp (Depends-on): Add unlocked-io-internal. 2021-03-06 Bruno Haible <br...@clisp.org> mountlist: Optimize stdio accesses. * lib/mountlist.c: Include unlocked-io.h unconditionally. * modules/mountlist (Depends-on): Add unlocked-io-internal. 2021-03-06 Bruno Haible <br...@clisp.org> getusershell: Optimize stdio accesses when possible. * lib/getusershell.c: Test GNULIB_GETUSERSHELL_SINGLE_THREAD instead of USE_UNLOCKED_IO. * modules/getusershell (Depends-on): Add unlocked-io-internal. * doc/multithread.texi: Document GNULIB_GETUSERSHELL_SINGLE_THREAD. 2021-03-06 Bruno Haible <br...@clisp.org> unlocked-io-internal: New module. * m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO): Don't define USE_UNLOCKED_IO here. * modules/unlocked-io-internal: New file, based on modules/unlocked-io. * modules/unlocked-io (Description): Clarify. (Files, Depends-on): Just use the unlocked-io-internal module. (configure.ac): Define GNULIB_STDIO_SINGLE_THREAD and USE_UNLOCKED_IO here. * doc/multithread.texi: Clarify when the 'unlocked-io' module can be used. 2021-03-06 Bruno Haible <br...@clisp.org> posixtm: Remove unused includes. * lib/posixtm.c: Don't include <stdio.h>, <sys/types.h>, unlocked-io.h.
>From 7ea7c3e27964aaf699ca737b11e2edea289f9ed2 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 01:39:16 +0100 Subject: [PATCH 1/9] posixtm: Remove unused includes. * lib/posixtm.c: Don't include <stdio.h>, <sys/types.h>, unlocked-io.h. --- ChangeLog | 5 +++++ lib/posixtm.c | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5a100d7..cfa60e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + posixtm: Remove unused includes. + * lib/posixtm.c: Don't include <stdio.h>, <sys/types.h>, unlocked-io.h. + +2021-03-06 Bruno Haible <br...@clisp.org> + dynarray: Add tests. * tests/test-dynarray.c: New file. * modules/dynarray-tests: New file. diff --git a/lib/posixtm.c b/lib/posixtm.c index dd89330..d142484 100644 --- a/lib/posixtm.c +++ b/lib/posixtm.c @@ -22,15 +22,9 @@ #include "posixtm.h" -#include <stdio.h> #include <stdlib.h> -#include <sys/types.h> #include <string.h> -#if USE_UNLOCKED_IO -# include "unlocked-io.h" -#endif - /* ISDIGIT differs from isdigit, as follows: - Its arg may be any int or unsigned int; it need not be an unsigned char or EOF. -- 2.7.4
>From 212f0b69ab67a4d6230c2ab85e28c2f54b31060c Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 01:58:10 +0100 Subject: [PATCH 2/9] unlocked-io-internal: New module. * m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO): Don't define USE_UNLOCKED_IO here. * modules/unlocked-io-internal: New file, based on modules/unlocked-io. * modules/unlocked-io (Description): Clarify. (Files, Depends-on): Just use the unlocked-io-internal module. (configure.ac): Define GNULIB_STDIO_SINGLE_THREAD and USE_UNLOCKED_IO here. * doc/multithread.texi: Clarify when the 'unlocked-io' module can be used. --- ChangeLog | 13 +++++++++++++ doc/multithread.texi | 16 +++++++++++----- m4/unlocked-io.m4 | 7 +------ modules/unlocked-io | 19 ++++++++++++++----- modules/unlocked-io-internal | 24 ++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 modules/unlocked-io-internal diff --git a/ChangeLog b/ChangeLog index cfa60e0..80d97a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + unlocked-io-internal: New module. + * m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO): Don't define + USE_UNLOCKED_IO here. + * modules/unlocked-io-internal: New file, based on modules/unlocked-io. + * modules/unlocked-io (Description): Clarify. + (Files, Depends-on): Just use the unlocked-io-internal module. + (configure.ac): Define GNULIB_STDIO_SINGLE_THREAD and USE_UNLOCKED_IO + here. + * doc/multithread.texi: Clarify when the 'unlocked-io' module can be + used. + +2021-03-06 Bruno Haible <br...@clisp.org> + posixtm: Remove unused includes. * lib/posixtm.c: Don't include <stdio.h>, <sys/types.h>, unlocked-io.h. diff --git a/doc/multithread.texi b/doc/multithread.texi index 9b1bc2e..7fe19a7 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -258,11 +258,17 @@ if (mt) gl_lock_lock (some_lock); if (mt) gl_lock_unlock (some_lock); @end smallexample @item -The @code{unlocked-io} module is applicable only if all the programs in your -package are single-threaded. It optimizes the operations on @code{FILE} -streams. You need extra code for this: include the @code{"unlocked-io.h"} -header file. Some Gnulib modules that do operations on @code{FILE} streams -have these preparations already included. +You may use the @code{unlocked-io} module if you want the @code{FILE} stream +functions @code{getc}, @code{putc}, etc.@: to use unlocked I/O if available, +throughout the package. Unlocked I/O can improve performance, sometimes +dramatically. But unlocked I/O is safe only in single-threaded programs, +as well as in multithreaded programs for which you can guarantee that +every @code{FILE} stream, including @code{stdin}, @code{stdout}, @code{stderr}, +is used only in a single thread. + +You need extra code for this optimization to be effective: include the +@code{"unlocked-io.h"} header file. Some Gnulib modules that do operations +on @code{FILE} streams have these preparations already included. @item You may define the C macro @code{GNULIB_WCHAR_SINGLE}, if all the programs in your package are single-threaded and won't change the locale after it has diff --git a/m4/unlocked-io.m4 b/m4/unlocked-io.m4 index a2dc8a1..b689020 100644 --- a/m4/unlocked-io.m4 +++ b/m4/unlocked-io.m4 @@ -1,4 +1,4 @@ -# unlocked-io.m4 serial 15 +# unlocked-io.m4 serial 16 # Copyright (C) 1998-2006, 2009-2021 Free Software Foundation, Inc. # @@ -16,11 +16,6 @@ dnl on Solaris 2.6). AC_DEFUN([gl_FUNC_GLIBC_UNLOCKED_IO], [ - AC_DEFINE([USE_UNLOCKED_IO], [1], - [Define to 1 if you want getc etc. to use unlocked I/O if available. - Unlocked I/O can improve performance in unithreaded apps, - but it is not safe for multithreaded apps.]) - dnl Persuade glibc and Solaris <stdio.h> to declare dnl fgets_unlocked(), fputs_unlocked() etc. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) diff --git a/modules/unlocked-io b/modules/unlocked-io index 000fe32..2ed3417 100644 --- a/modules/unlocked-io +++ b/modules/unlocked-io @@ -1,15 +1,24 @@ Description: -Enable faster, non-thread-safe stdio functions if available. +Enable faster, non-thread-safe stdio functions if available, +globally throughout the package. Files: -lib/unlocked-io.h -m4/unlocked-io.m4 Depends-on: -extensions +unlocked-io-internal configure.ac: -gl_FUNC_GLIBC_UNLOCKED_IO +AC_DEFINE([GNULIB_STDIO_SINGLE_THREAD], [1], + [Define to 1 if you want the FILE stream functions getc, putc, etc. + to use unlocked I/O if available, throughout the package. + Unlocked I/O can improve performance, sometimes dramatically. + But unlocked I/O is safe only in single-threaded programs, + as well as in multithreaded programs for which you can guarantee that + every FILE stream, including stdin, stdout, stderr, is used only + in a single thread.]) + +AC_DEFINE([USE_UNLOCKED_IO], [GNULIB_STDIO_SINGLE_THREAD], + [An alias of GNULIB_STDIO_SINGLE_THREAD.]) Makefile.am: diff --git a/modules/unlocked-io-internal b/modules/unlocked-io-internal new file mode 100644 index 0000000..b78944c --- /dev/null +++ b/modules/unlocked-io-internal @@ -0,0 +1,24 @@ +Description: +Allow use of faster, non-thread-safe stdio functions if available, +in specific modules. + +Files: +lib/unlocked-io.h +m4/unlocked-io.m4 + +Depends-on: +extensions + +configure.ac: +gl_FUNC_GLIBC_UNLOCKED_IO + +Makefile.am: + +Include: +"unlocked-io.h" + +License: +GPL + +Maintainer: +Jim Meyering -- 2.7.4
>From 92052c44910568ef6012584a344edcb736949d2c Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:21:49 +0100 Subject: [PATCH 3/9] getusershell: Optimize stdio accesses when possible. * lib/getusershell.c: Test GNULIB_GETUSERSHELL_SINGLE_THREAD instead of USE_UNLOCKED_IO. * modules/getusershell (Depends-on): Add unlocked-io-internal. * doc/multithread.texi: Document GNULIB_GETUSERSHELL_SINGLE_THREAD. --- ChangeLog | 8 ++++++++ doc/multithread.texi | 4 ++++ lib/getusershell.c | 2 +- modules/getusershell | 5 +++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 80d97a0..d6e8d54 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + getusershell: Optimize stdio accesses when possible. + * lib/getusershell.c: Test GNULIB_GETUSERSHELL_SINGLE_THREAD instead of + USE_UNLOCKED_IO. + * modules/getusershell (Depends-on): Add unlocked-io-internal. + * doc/multithread.texi: Document GNULIB_GETUSERSHELL_SINGLE_THREAD. + +2021-03-06 Bruno Haible <br...@clisp.org> + unlocked-io-internal: New module. * m4/unlocked-io.m4 (gl_FUNC_GLIBC_UNLOCKED_IO): Don't define USE_UNLOCKED_IO here. diff --git a/doc/multithread.texi b/doc/multithread.texi index 7fe19a7..a63d3ee 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -274,4 +274,8 @@ You may define the C macro @code{GNULIB_WCHAR_SINGLE}, if all the programs in your package are single-threaded and won't change the locale after it has been initialized. This macro optimizes the functions @code{mbrtowc} and @code{wcwidth}. +@item +You may define the C macro @code{GNULIB_GETUSERSHELL_SINGLE_THREAD}, if all the +programs in your package invoke the functions @code{setusershell}, +@code{getusershell}, @code{endusershell} only from a single thread. @end itemize diff --git a/lib/getusershell.c b/lib/getusershell.c index 6ae9410..be8a068 100644 --- a/lib/getusershell.c +++ b/lib/getusershell.c @@ -39,7 +39,7 @@ #include "stdio--.h" #include "xalloc.h" -#if USE_UNLOCKED_IO +#if GNULIB_GETUSERSHELL_SINGLE_THREAD # include "unlocked-io.h" #endif diff --git a/modules/getusershell b/modules/getusershell index 26f99e5..ed2f30f 100644 --- a/modules/getusershell +++ b/modules/getusershell @@ -8,8 +8,9 @@ m4/getusershell.m4 Depends-on: unistd extensions -fopen-safer [test $HAVE_GETUSERSHELL = 0] -xalloc [test $HAVE_GETUSERSHELL = 0] +fopen-safer [test $HAVE_GETUSERSHELL = 0] +unlocked-io-internal [test $HAVE_GETUSERSHELL = 0] +xalloc [test $HAVE_GETUSERSHELL = 0] configure.ac: gl_FUNC_GETUSERSHELL -- 2.7.4
>From 895c6f0664d19f754fe3905bae1bae83aee46bd0 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:31:45 +0100 Subject: [PATCH 4/9] mountlist: Optimize stdio accesses. * lib/mountlist.c: Include unlocked-io.h unconditionally. * modules/mountlist (Depends-on): Add unlocked-io-internal. --- ChangeLog | 6 ++++++ lib/mountlist.c | 5 ++--- modules/mountlist | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index d6e8d54..1159ee8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + mountlist: Optimize stdio accesses. + * lib/mountlist.c: Include unlocked-io.h unconditionally. + * modules/mountlist (Depends-on): Add unlocked-io-internal. + +2021-03-06 Bruno Haible <br...@clisp.org> + getusershell: Optimize stdio accesses when possible. * lib/getusershell.c: Test GNULIB_GETUSERSHELL_SINGLE_THREAD instead of USE_UNLOCKED_IO. diff --git a/lib/mountlist.c b/lib/mountlist.c index fae5b60..27989d2 100644 --- a/lib/mountlist.c +++ b/lib/mountlist.c @@ -149,9 +149,8 @@ # define MNT_IGNORE(M) 0 #endif -#if USE_UNLOCKED_IO -# include "unlocked-io.h" -#endif +/* Each of the FILE streams in this file is only used in a single thread. */ +#include "unlocked-io.h" /* The results of opendir() in this file are not used with dirfd and fchdir, therefore save some unnecessary work in fchdir.c. */ diff --git a/modules/mountlist b/modules/mountlist index 7542048..8ec4793 100644 --- a/modules/mountlist +++ b/modules/mountlist @@ -11,6 +11,7 @@ Depends-on: fopen-gnu getline open +unlocked-io-internal stdbool stdint strstr-simple -- 2.7.4
>From d26b0a2672f5adec9658ac606661b2f3b24c0336 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:36:31 +0100 Subject: [PATCH 5/9] readutmp: Optimize stdio accesses. * lib/readutmp.c: Include unlocked-io.h unconditionally. * modules/readutmp (Depends-on): Add unlocked-io-internal. --- ChangeLog | 6 ++++++ lib/readutmp.c | 5 ++--- modules/readutmp | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1159ee8..47f6807 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + readutmp: Optimize stdio accesses. + * lib/readutmp.c: Include unlocked-io.h unconditionally. + * modules/readutmp (Depends-on): Add unlocked-io-internal. + +2021-03-06 Bruno Haible <br...@clisp.org> + mountlist: Optimize stdio accesses. * lib/mountlist.c: Include unlocked-io.h unconditionally. * modules/mountlist (Depends-on): Add unlocked-io-internal. diff --git a/lib/readutmp.c b/lib/readutmp.c index 26ad815..73db856 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -34,9 +34,8 @@ #include "xalloc.h" -#if USE_UNLOCKED_IO -# include "unlocked-io.h" -#endif +/* Each of the FILE streams in this file is only used in a single thread. */ +#include "unlocked-io.h" #if 8 <= __GNUC__ # pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess" diff --git a/modules/readutmp b/modules/readutmp index e88897c..18cdad1 100644 --- a/modules/readutmp +++ b/modules/readutmp @@ -12,6 +12,7 @@ xalloc stdbool stdint fopen-gnu +unlocked-io-internal configure.ac: gl_READUTMP -- 2.7.4
>From f4bf3936b988eb65361e04b0ca3898681b787c58 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:42:24 +0100 Subject: [PATCH 6/9] exclude: Allow stdio optimization independently of 'unlocked-io'. * lib/exclude.c: Test GNULIB_EXCLUDE_SINGLE_THREAD instead of USE_UNLOCKED_IO. * modules/exclude (Depends-on): Add unlocked-io-internal. * doc/multithread.texi: Document GNULIB_EXCLUDE_SINGLE_THREAD. --- ChangeLog | 8 ++++++++ doc/multithread.texi | 4 ++++ lib/exclude.c | 2 +- modules/exclude | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 47f6807..d2ffff0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + exclude: Allow stdio optimization independently of 'unlocked-io'. + * lib/exclude.c: Test GNULIB_EXCLUDE_SINGLE_THREAD instead of + USE_UNLOCKED_IO. + * modules/exclude (Depends-on): Add unlocked-io-internal. + * doc/multithread.texi: Document GNULIB_EXCLUDE_SINGLE_THREAD. + +2021-03-06 Bruno Haible <br...@clisp.org> + readutmp: Optimize stdio accesses. * lib/readutmp.c: Include unlocked-io.h unconditionally. * modules/readutmp (Depends-on): Add unlocked-io-internal. diff --git a/doc/multithread.texi b/doc/multithread.texi index a63d3ee..082ccb0 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -278,4 +278,8 @@ been initialized. This macro optimizes the functions @code{mbrtowc} and You may define the C macro @code{GNULIB_GETUSERSHELL_SINGLE_THREAD}, if all the programs in your package invoke the functions @code{setusershell}, @code{getusershell}, @code{endusershell} only from a single thread. +@item +You may define the C macro @code{GNULIB_EXCLUDE_SINGLE_THREAD}, if all the +programs in your package invoke the functions of the @code{exclude} module +only from a single thread. @end itemize diff --git a/lib/exclude.c b/lib/exclude.c index a9c4e68..4ef4e08 100644 --- a/lib/exclude.c +++ b/lib/exclude.c @@ -42,7 +42,7 @@ #include "verify.h" #include "filename.h" -#if USE_UNLOCKED_IO +#if GNULIB_EXCLUDE_SINGLE_THREAD # include "unlocked-io.h" #endif diff --git a/modules/exclude b/modules/exclude index 2529027..13871bd 100644 --- a/modules/exclude +++ b/modules/exclude @@ -14,6 +14,7 @@ mbscasecmp mbuiter regex stdbool +unlocked-io-internal verify xalloc -- 2.7.4
>From ede75ad3eeeafe72d1af9de84897905edf76bd70 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:47:03 +0100 Subject: [PATCH 7/9] regex: Allow locking optimization independently of 'unlocked-io'. * lib/regex_internal.h: Test GNULIB_REGEX_SINGLE_THREAD instead of USE_UNLOCKED_IO. * doc/multithread.texi: Document GNULIB_REGEX_SINGLE_THREAD. --- ChangeLog | 7 +++++++ doc/multithread.texi | 4 ++++ lib/regex_internal.h | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index d2ffff0..f9bdb94 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + regex: Allow locking optimization independently of 'unlocked-io'. + * lib/regex_internal.h: Test GNULIB_REGEX_SINGLE_THREAD instead of + USE_UNLOCKED_IO. + * doc/multithread.texi: Document GNULIB_REGEX_SINGLE_THREAD. + +2021-03-06 Bruno Haible <br...@clisp.org> + exclude: Allow stdio optimization independently of 'unlocked-io'. * lib/exclude.c: Test GNULIB_EXCLUDE_SINGLE_THREAD instead of USE_UNLOCKED_IO. diff --git a/doc/multithread.texi b/doc/multithread.texi index 082ccb0..cb0c620 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -270,6 +270,10 @@ You need extra code for this optimization to be effective: include the @code{"unlocked-io.h"} header file. Some Gnulib modules that do operations on @code{FILE} streams have these preparations already included. @item +You may define the C macro @code{GNULIB_REGEX_SINGLE_THREAD}, if all the +programs in your package invoke the functions of the @code{regex} module +only from a single thread. +@item You may define the C macro @code{GNULIB_WCHAR_SINGLE}, if all the programs in your package are single-threaded and won't change the locale after it has been initialized. This macro optimizes the functions @code{mbrtowc} and diff --git a/lib/regex_internal.h b/lib/regex_internal.h index 4b0a3ef..1245e78 100644 --- a/lib/regex_internal.h +++ b/lib/regex_internal.h @@ -53,14 +53,14 @@ # define lock_fini(lock) ((void) 0) # define lock_lock(lock) __libc_lock_lock (lock) # define lock_unlock(lock) __libc_lock_unlock (lock) -#elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO +#elif defined GNULIB_LOCK && !defined GNULIB_REGEX_SINGLE_THREAD # include "glthread/lock.h" # define lock_define(name) gl_lock_define (, name) # define lock_init(lock) glthread_lock_init (&(lock)) # define lock_fini(lock) glthread_lock_destroy (&(lock)) # define lock_lock(lock) glthread_lock_lock (&(lock)) # define lock_unlock(lock) glthread_lock_unlock (&(lock)) -#elif defined GNULIB_PTHREAD && !defined USE_UNLOCKED_IO +#elif defined GNULIB_PTHREAD && !defined GNULIB_REGEX_SINGLE_THREAD # include <pthread.h> # define lock_define(name) pthread_mutex_t name; # define lock_init(lock) pthread_mutex_init (&(lock), 0) -- 2.7.4
>From cd057fa80c74dcf1d0a74290cb5ad6cdc3136428 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 02:57:46 +0100 Subject: [PATCH 8/9] mbrtowc: Allow locking optimization independently of 'unlocked-io'. * lib/mbtowc-lock.h: Test GNULIB_MBRTOWC_SINGLE_THREAD instead of USE_UNLOCKED_IO. * doc/multithread.texi: Document GNULIB_MBRTOWC_SINGLE_THREAD. --- ChangeLog | 7 +++++++ doc/multithread.texi | 5 +++++ lib/mbtowc-lock.h | 4 +++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f9bdb94..80590b1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2021-03-06 Bruno Haible <br...@clisp.org> + mbrtowc: Allow locking optimization independently of 'unlocked-io'. + * lib/mbtowc-lock.h: Test GNULIB_MBRTOWC_SINGLE_THREAD instead of + USE_UNLOCKED_IO. + * doc/multithread.texi: Document GNULIB_MBRTOWC_SINGLE_THREAD. + +2021-03-06 Bruno Haible <br...@clisp.org> + regex: Allow locking optimization independently of 'unlocked-io'. * lib/regex_internal.h: Test GNULIB_REGEX_SINGLE_THREAD instead of USE_UNLOCKED_IO. diff --git a/doc/multithread.texi b/doc/multithread.texi index cb0c620..b28d1de 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -274,6 +274,11 @@ You may define the C macro @code{GNULIB_REGEX_SINGLE_THREAD}, if all the programs in your package invoke the functions of the @code{regex} module only from a single thread. @item +You may define the C macro @code{GNULIB_MBRTOWC_SINGLE_THREAD}, if all the +programs in your package invoke the functions @code{mbrtowc}, @code{mbrtoc32}, +and the functions of the @code{regex} module only from a single thread. (The +@code{regex} module uses @code{mbrtowc} under the hood.) +@item You may define the C macro @code{GNULIB_WCHAR_SINGLE}, if all the programs in your package are single-threaded and won't change the locale after it has been initialized. This macro optimizes the functions @code{mbrtowc} and diff --git a/lib/mbtowc-lock.h b/lib/mbtowc-lock.h index b7c5ba8..3b6f5f9 100644 --- a/lib/mbtowc-lock.h +++ b/lib/mbtowc-lock.h @@ -32,7 +32,9 @@ mbtowc_unlocked (wchar_t *pwc, const char *p, size_t m) /* Prohibit renaming this symbol. */ #undef gl_get_mbtowc_lock -#ifdef USE_UNLOCKED_IO +#if GNULIB_MBRTOWC_SINGLE_THREAD + +/* All uses of this function are in a single thread. No locking needed. */ static int mbtowc_with_lock (wchar_t *pwc, const char *p, size_t m) -- 2.7.4
>From 2a200f4eb4754823c5674580bb0b68a88fd35077 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 7 Mar 2021 10:45:58 +0100 Subject: [PATCH 9/9] Rename GNULIB_WCHAR_SINGLE to GNULIB_WCHAR_SINGLE_LOCALE. * lib/lc-charset-dispatch.c: Test GNULIB_WCHAR_SINGLE_LOCALE instead of GNULIB_WCHAR_SINGLE. * lib/wcwidth.c: Likewise. * tests/test-wcwidth.c: Likewise. * doc/multithread.texi: Document GNULIB_WCHAR_SINGLE_LOCALE instead of GNULIB_WCHAR_SINGLE. * NEWS: Document the change. --- ChangeLog | 11 +++++++++++ NEWS | 4 ++++ doc/multithread.texi | 13 ++++++++++--- lib/lc-charset-dispatch.c | 6 +++--- lib/wcwidth.c | 2 +- tests/test-wcwidth.c | 2 +- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 80590b1..8b5ed00 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2021-03-07 Bruno Haible <br...@clisp.org> + + Rename GNULIB_WCHAR_SINGLE to GNULIB_WCHAR_SINGLE_LOCALE. + * lib/lc-charset-dispatch.c: Test GNULIB_WCHAR_SINGLE_LOCALE instead of + GNULIB_WCHAR_SINGLE. + * lib/wcwidth.c: Likewise. + * tests/test-wcwidth.c: Likewise. + * doc/multithread.texi: Document GNULIB_WCHAR_SINGLE_LOCALE instead of + GNULIB_WCHAR_SINGLE. + * NEWS: Document the change. + 2021-03-06 Bruno Haible <br...@clisp.org> mbrtowc: Allow locking optimization independently of 'unlocked-io'. diff --git a/NEWS b/NEWS index 318055a..ba89881 100644 --- a/NEWS +++ b/NEWS @@ -60,6 +60,10 @@ User visible incompatible changes Date Modules Changes +2021-03-07 mbrtowc For single-locale optimizations, you now need to + mbrtoc32 define GNULIB_WCHAR_SINGLE_LOCALE instead of + wcwidth GNULIB_WCHAR_SINGLE. + 2021-02-28 parse-datetime The parse_datetime2 function has been moved to the new parse-datetime2 module, so that programs that need just parse_datetime need diff --git a/doc/multithread.texi b/doc/multithread.texi index b28d1de..eef724d 100644 --- a/doc/multithread.texi +++ b/doc/multithread.texi @@ -279,9 +279,16 @@ programs in your package invoke the functions @code{mbrtowc}, @code{mbrtoc32}, and the functions of the @code{regex} module only from a single thread. (The @code{regex} module uses @code{mbrtowc} under the hood.) @item -You may define the C macro @code{GNULIB_WCHAR_SINGLE}, if all the programs in -your package are single-threaded and won't change the locale after it has -been initialized. This macro optimizes the functions @code{mbrtowc} and +You may define the C macro @code{GNULIB_WCHAR_SINGLE_LOCALE}, if all the +programs in your package set the locale early and +@itemize +@item +don't change the locale after it has been initialized, and +@item +don't call locale sensitive functions (@code{mbrtowc}, @code{wcwidth}, etc.@:) +before the locale has been initialized. +@end itemize +This macro optimizes the functions @code{mbrtowc}, @code{mbrtoc32}, and @code{wcwidth}. @item You may define the C macro @code{GNULIB_GETUSERSHELL_SINGLE_THREAD}, if all the diff --git a/lib/lc-charset-dispatch.c b/lib/lc-charset-dispatch.c index 879f71a..5c63c4b 100644 --- a/lib/lc-charset-dispatch.c +++ b/lib/lc-charset-dispatch.c @@ -26,7 +26,7 @@ # include "localcharset.h" # include "streq.h" -# if GNULIB_WCHAR_SINGLE +# if GNULIB_WCHAR_SINGLE_LOCALE /* When we know that the locale does not change, provide a speedup by caching the value of locale_encoding_classification. */ # define locale_encoding_classification_cached locale_encoding_classification @@ -35,7 +35,7 @@ # define locale_encoding_classification_uncached locale_encoding_classification # endif -# if GNULIB_WCHAR_SINGLE +# if GNULIB_WCHAR_SINGLE_LOCALE static inline # endif enc_t @@ -59,7 +59,7 @@ locale_encoding_classification_uncached (void) return enc_other; } -# if GNULIB_WCHAR_SINGLE +# if GNULIB_WCHAR_SINGLE_LOCALE static int cached_locale_enc = -1; diff --git a/lib/wcwidth.c b/lib/wcwidth.c index 9009ebc..8d85082 100644 --- a/lib/wcwidth.c +++ b/lib/wcwidth.c @@ -34,7 +34,7 @@ is_locale_utf8 (void) return STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0); } -#if GNULIB_WCHAR_SINGLE +#if GNULIB_WCHAR_SINGLE_LOCALE /* When we know that the locale does not change, provide a speedup by caching the value of is_locale_utf8. */ static int cached_is_locale_utf8 = -1; diff --git a/tests/test-wcwidth.c b/tests/test-wcwidth.c index 16c6837..e5e3a54 100644 --- a/tests/test-wcwidth.c +++ b/tests/test-wcwidth.c @@ -35,7 +35,7 @@ main () { wchar_t wc; -#if !GNULIB_WCHAR_SINGLE +#if !GNULIB_WCHAR_SINGLE_LOCALE # ifdef C_CTYPE_ASCII /* Test width of ASCII characters. */ for (wc = 0x20; wc < 0x7F; wc++) -- 2.7.4