[Xapian-tickets] [Xapian] #665: Avoid C library functions not guaranteed to be thread-safe
Xapian
nobody at xapian.org
Tue May 7 05:07:27 BST 2024
#665: Avoid C library functions not guaranteed to be thread-safe
-------------------------+-------------------------------
Reporter: Olly Betts | Owner: Olly Betts
Type: defect | Status: assigned
Priority: highest | Milestone: 1.5.0
Component: Library API | Version: git master
Severity: normal | Resolution:
Keywords: | Blocked By:
Blocking: | Operating System: All
-------------------------+-------------------------------
Comment (by Olly Betts):
The Linux man pages seem to have been rewritten to be much clearer about
the nuances of thread safety since we started on this (or maybe I failed
to fully take in what they were saying before now).
The key point to grasp is that functions that modify the environment are
not safe to call in a multi-threaded program, but that means that in a
multi-threaded program they should not be called so the environment can be
considered as effectively constant and functions which only read it (like
`getenv()`) are therefore safe.
It's not great that in our situation this logic means relying on the code
of the application using Xapian to not modify the environment if it uses
threading, but we have to rely on it not to invoke undefined behaviour in
general.
I think this means we can probably keep using `getenv()` but that we
should only use it in the thread that we're called in. We don't currently
create separate threads of our own, but if/when we start to we should
avoid calling `getenv()` in those threads as the application code may be
single-threaded and thus call `setenv()`.
It's still useful to eliminate calls to `strtod()` as that suffers from
changing behaviour if the locale is changed. I have a WIP branch to do
that.
--
Ticket URL: <https://trac.xapian.org/ticket/665#comment:22>
Xapian <https://xapian.org/>
Xapian
More information about the Xapian-tickets
mailing list