File: no-suspend-with-lock.rst

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (40 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (11)
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
.. title:: clang-tidy - cppcoreguidelines-no-suspend-with-lock

cppcoreguidelines-no-suspend-with-lock
======================================

Flags coroutines that suspend while a lock guard is in scope at the
suspension point.

When a coroutine suspends, any mutexes held by the coroutine will remain
locked until the coroutine resumes and eventually destructs the lock guard.
This can lead to long periods with a mutex held and runs the risk of deadlock.

Instead, locks should be released before suspending a coroutine.

This check only checks suspending coroutines while a lock_guard is in scope;
it does not consider manual locking or unlocking of mutexes, e.g., through
calls to ``std::mutex::lock()``.

Examples:

.. code-block:: c++

  future bad_coro() {
    std::lock_guard lock{mtx};
    ++some_counter;
    co_await something(); // Suspending while holding a mutex
  }

  future good_coro() {
    {
      std::lock_guard lock{mtx};
      ++some_counter;
    }
    // Destroy the lock_guard to release the mutex before suspending the coroutine
    co_await something(); // Suspending while holding a mutex
  }

This check implements `CP.52
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-locks>`_
from the C++ Core Guidelines.