xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/concurrency/mt-unsafe.rst (revision 6e566bc5523f743bc34a7e26f050f1f2b4d699a8)
1*6e566bc5SRichard.. title:: clang-tidy - concurrency-mt-unsafe
2*6e566bc5SRichard
3*6e566bc5SRichardconcurrency-mt-unsafe
4*6e566bc5SRichard=====================
5*6e566bc5SRichard
6*6e566bc5SRichardChecks for some thread-unsafe functions against a black list of
7*6e566bc5SRichardknown-to-be-unsafe functions. Usually they access static variables without
8*6e566bc5SRichardsynchronization (e.g. gmtime(3)) or utilize signals in a racy way.
9*6e566bc5SRichardThe set of functions to check is specified with the `FunctionSet` option.
10*6e566bc5SRichard
11*6e566bc5SRichardNote that using some thread-unsafe functions may be still valid in
12*6e566bc5SRichardconcurrent programming if only a single thread is used (e.g. setenv(3)),
13*6e566bc5SRichardhowever, some functions may track a state in global variables which
14*6e566bc5SRichardwould be clobbered by subsequent (non-parallel, but concurrent) calls to
15*6e566bc5SRicharda related function. E.g. the following code suffers from unprotected
16*6e566bc5SRichardaccesses to a global state:
17*6e566bc5SRichard
18*6e566bc5SRichard.. code-block:: c++
19*6e566bc5SRichard
20*6e566bc5SRichard    // getnetent(3) maintains global state with DB connection, etc.
21*6e566bc5SRichard    // If a concurrent green thread calls getnetent(3), the global state is corrupted.
22*6e566bc5SRichard    netent = getnetent();
23*6e566bc5SRichard    yield();
24*6e566bc5SRichard    netent = getnetent();
25*6e566bc5SRichard
26*6e566bc5SRichard
27*6e566bc5SRichardExamples:
28*6e566bc5SRichard
29*6e566bc5SRichard.. code-block:: c++
30*6e566bc5SRichard
31*6e566bc5SRichard    tm = gmtime(timep); // uses a global buffer
32*6e566bc5SRichard
33*6e566bc5SRichard    sleep(1); // implementation may use SIGALRM
34*6e566bc5SRichard
35*6e566bc5SRichard.. option:: FunctionSet
36*6e566bc5SRichard
37*6e566bc5SRichard  Specifies which functions in libc should be considered thread-safe,
38*6e566bc5SRichard  possible values are `posix`, `glibc`, or `any`.
39*6e566bc5SRichard
40*6e566bc5SRichard  `posix` means POSIX defined thread-unsafe functions. POSIX.1-2001
41*6e566bc5SRichard  in "2.9.1 Thread-Safety" defines that all functions specified in the
42*6e566bc5SRichard  standard are thread-safe except a predefined list of thread-unsafe
43*6e566bc5SRichard  functions.
44*6e566bc5SRichard
45*6e566bc5SRichard  Glibc defines some of them as thread-safe (e.g. dirname(3)), but adds
46*6e566bc5SRichard  non-POSIX thread-unsafe ones (e.g. getopt_long(3)). Glibc's list is
47*6e566bc5SRichard  compiled from GNU web documentation with a search for MT-Safe tag:
48*6e566bc5SRichard  https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
49*6e566bc5SRichard
50*6e566bc5SRichard  If you want to identify thread-unsafe API for at least one libc or
51*6e566bc5SRichard  unsure which libc will be used, use `any` (default).
52*6e566bc5SRichard
53