File: mt-unsafe.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (52 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
.. title:: clang-tidy - concurrency-mt-unsafe

concurrency-mt-unsafe
=====================

Checks for some thread-unsafe functions against a black list of
known-to-be-unsafe functions. Usually they access static variables without
synchronization (e.g. gmtime(3)) or utilize signals in a racy way.
The set of functions to check is specified with the `FunctionSet` option.

Note that using some thread-unsafe functions may be still valid in
concurrent programming if only a single thread is used (e.g. setenv(3)),
however, some functions may track a state in global variables which
would be clobbered by subsequent (non-parallel, but concurrent) calls to
a related function. E.g. the following code suffers from unprotected
accesses to a global state:

.. code-block:: c++

    // getnetent(3) maintains global state with DB connection, etc.
    // If a concurrent green thread calls getnetent(3), the global state is corrupted.
    netent = getnetent();
    yield();
    netent = getnetent();


Examples:

.. code-block:: c++

    tm = gmtime(timep); // uses a global buffer

    sleep(1); // implementation may use SIGALRM

.. option:: FunctionSet

  Specifies which functions in libc should be considered thread-safe,
  possible values are `posix`, `glibc`, or `any`.

  `posix` means POSIX defined thread-unsafe functions. POSIX.1-2001
  in "2.9.1 Thread-Safety" defines that all functions specified in the
  standard are thread-safe except a predefined list of thread-unsafe
  functions.

  Glibc defines some of them as thread-safe (e.g. dirname(3)), but adds
  non-POSIX thread-unsafe ones (e.g. getopt_long(3)). Glibc's list is
  compiled from GNU web documentation with a search for MT-Safe tag:
  https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html

  If you want to identify thread-unsafe API for at least one libc or
  unsure which libc will be used, use `any` (default).