Warning C26138

Suspending a coroutine while holding lock 'lock'.

Remarks

Warning C26138 warns when a coroutine is suspended while holding a lock. In general, we can't know how long will a coroutine remain in the suspended state so this pattern may result in longer critical sections than expected.

Code analysis name: SUSPENDED_WITH_LOCK

Examples

The following code will generate C26138.

#include <experimental/generator>
#include <future>
#include <mutex>

using namespace std::experimental;

std::mutex global_m;
_Guarded_by_(global_m) int var = 0;

generator<int> mutex_acquiring_generator() {
  global_m.lock();
  ++var;
  co_yield 1;                           // @expected(26138), global_m is hold while yielding.
  global_m.unlock();
}

generator<int> mutex_acquiring_generator_report_once() {
  global_m.lock();
  ++var;
  co_yield 1;                           // @expected(26138), global_m is hold while yielding.
  co_yield 1;                           // @expected(26138), global_m is hold while yielding.
  global_m.unlock();
}

The following code will correct these warnings.

#include <experimental/generator>
#include <future>
#include <mutex>

using namespace std::experimental;

std::mutex global_m;
_Guarded_by_(global_m) int var = 0;

generator<int> mutex_acquiring_generator2() {
  {
    global_m.lock();
    ++var;
    global_m.unlock();
  }
  co_yield 1;                           // no 26138, global_m is already released above.
}