[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] use unlocked stdio functions
From: |
Chet Ramey |
Subject: |
Re: [PATCH] use unlocked stdio functions |
Date: |
Wed, 14 Feb 2024 11:45:44 -0500 |
User-agent: |
Mozilla Thunderbird |
On 2/14/24 11:18 AM, Vito Caputo wrote:
On Wed, Feb 14, 2024 at 10:59:57AM -0500, Chet Ramey wrote:
On 2/5/24 10:47 PM, Grisha Levit wrote:
Bash makes many calls to stdio functions that may have unlocked_stdio(3)
equivalents. Since the locking functionality provided by the regular
versions is only useful in multi-threaded applications, it probably makes
sense for Bash to use the *_unlocked versions where available.
Thanks for the patch; this looks like a great idea.
I thought this was only necessary for C programs built with pthreads
linked in / -D_REENTRANT. Is that no longer the case? Or has bash
started making use of pthreads?
It's more like the opposite: the stdio functions provided by glibc and
other versions of the standard C library are thread-safe by default, so
multi-threaded programs don't have to do anything. They're ok for non-
threaded programs, too, except that those programs have to absorb the
locking overhead. If you are sure that your program doesn't need the
thread-safe functions, which basically work by locking FILE * objects so
only one thread can use and modify them at a time (see flockfile(3)), you
can use the unlocked versions.
Most of the time, the lock overhead is small. But for some usage patterns,
it can be significant.
These functions are defined by POSIX as part of the base system:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc_unlocked.html#tag_16_188
They didn't used to be part of the base standard.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/