bug-gettext
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multithreading advice on the manual


From: Bruno Haible
Subject: Re: Multithreading advice on the manual
Date: Fri, 09 Jun 2023 17:23:24 +0200

Hi Miguel,

> I've been reading the gettext manual and I've found an unfortunate
> passage here[1].

In this section I wrote: "The more general solution for initialization
functions, POSIX pthread_once, is not needed in this case." What is
wrong about it?

> The multithreaded example contains a race condition
> (volatile doesn't mean atomic)

This is not a problem. It means that the initialization function may
be called more than once. But since all this function does is to call
bindtextdomain() with literal arguments, and bindtextdomain() is MT-safe
and idempotent, it is not a problem if two different threads call this
function at nearly the same time.

> and a TOCTOU error even after changing to an atomic variable.

What do you mean by that, exactly? (I have a suspicion, but I prefer
if you formulate it.)

> The implementation of this pattern is what
> (pthread_)call_once does, so instead of discouraging its use I propose
> promoting it.

> The attached patch documents the modern usage of once_flag/call_once
> with a link to GNU libc manual and modifies the paragraph below the
> example to suggest either pthread_once or any other equivalent primitive
> provided by the system.
> 
> What do you think?

Unfortunately, this approach has two problems:

  * call_once has a lot of portability problems [2]. pthread_once has
    fewer portability problems [3], but still. The effect is that only
    packages that rely on Gnulib could use the code snippet. However,
    the gettext manual targets package of any license (since glibc and
    libintl are under LGPL).

  * Using call_once or pthread_once has link requirements, see [4][5].
    So, it has a globally visible effect on the library.

I'm OK with including your changed wording, as comments, with a
"TODO: revisit in 2030" marker. In the hope that, by then, the
portability problems will have become unimportant.

Bruno

[1] https://www.gnu.org/software/gettext/manual/html_node/Libraries.html
[2] https://www.gnu.org/software/gnulib/manual/html_node/call_005fonce.html
[3] https://www.gnu.org/software/gnulib/manual/html_node/pthread_005fonce.html
[4] https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=modules/call_once
[5] 
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=modules/pthread-once






reply via email to

[Prev in Thread] Current Thread [Next in Thread]