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
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277

base / threading / thread_restrictions.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/threading/thread_restrictions.h"

#include "base/check.h"
#include "base/threading/hang_watcher.h"
#include "base/trace_event/base_tracing.h"
#include "build/build_config.h"

namespace base {

BooleanWithOptionalStack::BooleanWithOptionalStack(bool value) : value_(value) {
#if CAPTURE_THREAD_RESTRICTIONS_STACK_TRACES()
  stack_.emplace();
#endif  // CAPTURE_THREAD_RESTRICTIONS_STACK_TRACES()
}

std::ostream& operator<<(std::ostream& out,
                         const BooleanWithOptionalStack& bws) {
  out << bws.value_;
#if CAPTURE_THREAD_RESTRICTIONS_STACK_TRACES()
  if (bws.stack_.has_value()) {
    out << " set by\n" << bws.stack_.value();
  } else {
    out << " (value by default)";
  }
#endif  // CAPTURE_THREAD_RESTRICTIONS_STACK_TRACES()
  return out;
}

// A macro that dumps in official builds (non-fatal) if the condition is false,
// or behaves as DCHECK in DCHECK-enabled builds. Unlike DUMP_WILL_BE_CHECK,
// there is no intent to transform those into CHECKs. Used to report potential
// performance issues.
//
// TODO(crbug.com/363049758): This is temporarily a `DCHECK` to avoid getting a
// lot of crash reports while known issues are being addressed. Change to
// `DUMP_WILL_BE_CHECK` once known issues are addressed.
#define DUMP_OR_DCHECK DCHECK

namespace {

constinit thread_local BooleanWithOptionalStack tls_blocking_disallowed;
constinit thread_local BooleanWithOptionalStack tls_singleton_disallowed;
constinit thread_local BooleanWithOptionalStack
    tls_base_sync_primitives_disallowed;
constinit thread_local BooleanWithOptionalStack
    tls_cpu_intensive_work_disallowed;

}  // namespace

void AssertBlockingAllowed() {
  DUMP_OR_DCHECK(!tls_blocking_disallowed)
      << "Function marked as blocking was called from a scope that disallows "
         "blocking! If this task is running inside the ThreadPool, it needs "
         "to have MayBlock() in its TaskTraits. Otherwise, consider making "
         "this blocking work asynchronous or, as a last resort, you may use "
         "ScopedAllowBlocking (see its documentation for best practices).\n"
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
}

void AssertBlockingDisallowedForTesting() {
  DCHECK(tls_blocking_disallowed)
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
}

void DisallowBlocking() {
  tls_blocking_disallowed = BooleanWithOptionalStack(true);
}

ScopedDisallowBlocking::ScopedDisallowBlocking()
    : resetter_(&tls_blocking_disallowed, BooleanWithOptionalStack(true)) {}

ScopedDisallowBlocking::~ScopedDisallowBlocking() {
  DCHECK(tls_blocking_disallowed)
      << "~ScopedDisallowBlocking() running while surprisingly already no "
         "longer disallowed.\n"
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
}

void DisallowBaseSyncPrimitives() {
  tls_base_sync_primitives_disallowed = BooleanWithOptionalStack(true);
}

ScopedDisallowBaseSyncPrimitives::ScopedDisallowBaseSyncPrimitives()
    : resetter_(&tls_base_sync_primitives_disallowed,
                BooleanWithOptionalStack(true)) {}

ScopedDisallowBaseSyncPrimitives::~ScopedDisallowBaseSyncPrimitives() {
  DCHECK(tls_base_sync_primitives_disallowed)
      << "~ScopedDisallowBaseSyncPrimitives() running while surprisingly "
         "already no longer disallowed.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed;
}

ScopedAllowBaseSyncPrimitives::ScopedAllowBaseSyncPrimitives()
    : resetter_(&tls_base_sync_primitives_disallowed,
                BooleanWithOptionalStack(false)) {
  DCHECK(!tls_blocking_disallowed)
      << "To allow //base sync primitives in a scope where blocking is "
         "disallowed use ScopedAllowBaseSyncPrimitivesOutsideBlockingScope.\n"
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
}

ScopedAllowBaseSyncPrimitives::~ScopedAllowBaseSyncPrimitives() {
  DCHECK(!tls_base_sync_primitives_disallowed)
      << "~ScopedAllowBaseSyncPrimitives() running while surprisingly already "
         "no longer allowed.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed;
}

ScopedAllowBaseSyncPrimitivesForTesting::
    ScopedAllowBaseSyncPrimitivesForTesting()
    : resetter_(&tls_base_sync_primitives_disallowed,
                BooleanWithOptionalStack(false)) {}

ScopedAllowBaseSyncPrimitivesForTesting::
    ~ScopedAllowBaseSyncPrimitivesForTesting() {
  DCHECK(!tls_base_sync_primitives_disallowed)
      << "~ScopedAllowBaseSyncPrimitivesForTesting() running while "  // IN-TEST
         "surprisingly already no longer allowed.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed;
}

ScopedAllowUnresponsiveTasksForTesting::ScopedAllowUnresponsiveTasksForTesting()
    : base_sync_resetter_(&tls_base_sync_primitives_disallowed,
                          BooleanWithOptionalStack(false)),
      blocking_resetter_(&tls_blocking_disallowed,
                         BooleanWithOptionalStack(false)),
      cpu_resetter_(&tls_cpu_intensive_work_disallowed,
                    BooleanWithOptionalStack(false)) {}

ScopedAllowUnresponsiveTasksForTesting::
    ~ScopedAllowUnresponsiveTasksForTesting() {
  DCHECK(!tls_base_sync_primitives_disallowed)
      << "~ScopedAllowUnresponsiveTasksForTesting() running while "  // IN-TEST
         "surprisingly already no longer allowed.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed;
  DCHECK(!tls_blocking_disallowed)
      << "~ScopedAllowUnresponsiveTasksForTesting() running while "  // IN-TEST
         "surprisingly already no longer allowed.\n"
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
  DCHECK(!tls_cpu_intensive_work_disallowed)
      << "~ScopedAllowUnresponsiveTasksForTesting() running while "  // IN-TEST
         "surprisingly already no longer allowed.\n"
      << "tls_cpu_intensive_work_disallowed "
      << tls_cpu_intensive_work_disallowed;
}

namespace internal {

void AssertBaseSyncPrimitivesAllowed() {
  DUMP_OR_DCHECK(!tls_base_sync_primitives_disallowed)
      << "Waiting on a //base sync primitive is not allowed on this thread to "
         "prevent jank and deadlock. If waiting on a //base sync primitive is "
         "unavoidable, do it within the scope of a "
         "ScopedAllowBaseSyncPrimitives. If in a test, use "
         "ScopedAllowBaseSyncPrimitivesForTesting.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed
      << "It can be useful to know that tls_blocking_disallowed is "
      << tls_blocking_disallowed;
}

void ResetThreadRestrictionsForTesting() {
  tls_blocking_disallowed = BooleanWithOptionalStack(false);
  tls_singleton_disallowed = BooleanWithOptionalStack(false);
  tls_base_sync_primitives_disallowed = BooleanWithOptionalStack(false);
  tls_cpu_intensive_work_disallowed = BooleanWithOptionalStack(false);
}

void AssertSingletonAllowed() {
  DUMP_OR_DCHECK(!tls_singleton_disallowed)
      << "LazyInstance/Singleton is not allowed to be used on this thread. "
         "Most likely it's because this thread is not joinable (or the current "
         "task is running with TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN "
         "semantics), so AtExitManager may have deleted the object on "
         "shutdown, leading to a potential shutdown crash. If you need to use "
         "the object from this context, it'll have to be updated to use Leaky "
         "traits.\n"
      << "tls_singleton_disallowed " << tls_singleton_disallowed;
}

}  // namespace internal

void DisallowSingleton() {
  tls_singleton_disallowed = BooleanWithOptionalStack(true);
}

ScopedDisallowSingleton::ScopedDisallowSingleton()
    : resetter_(&tls_singleton_disallowed, BooleanWithOptionalStack(true)) {}

ScopedDisallowSingleton::~ScopedDisallowSingleton() {
  DCHECK(tls_singleton_disallowed)
      << "~ScopedDisallowSingleton() running while surprisingly already no "
         "longer disallowed.\n"
      << "tls_singleton_disallowed " << tls_singleton_disallowed;
}

void AssertLongCPUWorkAllowed() {
  DUMP_OR_DCHECK(!tls_cpu_intensive_work_disallowed)
      << "Function marked as CPU intensive was called from a scope that "
         "disallows this kind of work! Consider making this work "
         "asynchronous.\n"
      << "tls_cpu_intensive_work_disallowed "
      << tls_cpu_intensive_work_disallowed;
}

void DisallowUnresponsiveTasks() {
  DisallowBlocking();
  DisallowBaseSyncPrimitives();
  tls_cpu_intensive_work_disallowed = BooleanWithOptionalStack(true);
}

// static
void PermanentThreadAllowance::AllowBlocking() {
  tls_blocking_disallowed = BooleanWithOptionalStack(false);
}

// static
void PermanentThreadAllowance::AllowBaseSyncPrimitives() {
  tls_base_sync_primitives_disallowed = BooleanWithOptionalStack(false);
}

ScopedAllowBlocking::ScopedAllowBlocking(const Location& from_here)
    : resetter_(&tls_blocking_disallowed, BooleanWithOptionalStack(false)) {
  TRACE_EVENT_BEGIN(
      "base", "ScopedAllowBlocking", [&](perfetto::EventContext ctx) {
        ctx.event()->set_source_location_iid(
            base::trace_event::InternedSourceLocation::Get(&ctx, from_here));
      });
}

ScopedAllowBlocking::~ScopedAllowBlocking() {
  TRACE_EVENT_END0("base", "ScopedAllowBlocking");

  DCHECK(!tls_blocking_disallowed)
      << "~ScopedAllowBlocking() running while surprisingly already no longer "
         "allowed.\n"
      << "tls_blocking_disallowed " << tls_blocking_disallowed;
}

ScopedAllowBaseSyncPrimitivesOutsideBlockingScope::
    ScopedAllowBaseSyncPrimitivesOutsideBlockingScope(const Location& from_here)
    : resetter_(&tls_base_sync_primitives_disallowed,
                BooleanWithOptionalStack(false)) {
  TRACE_EVENT_BEGIN(
      "base", "ScopedAllowBaseSyncPrimitivesOutsideBlockingScope",
      [&](perfetto::EventContext ctx) {
        ctx.event()->set_source_location_iid(
            base::trace_event::InternedSourceLocation::Get(&ctx, from_here));
      });

  // Since this object is used to indicate that sync primitives will be used to
  // wait for an event ignore the current operation for hang watching purposes
  // since the wait time duration is unknown.
  base::HangWatcher::InvalidateActiveExpectations();
}

ScopedAllowBaseSyncPrimitivesOutsideBlockingScope::
    ~ScopedAllowBaseSyncPrimitivesOutsideBlockingScope() {
  TRACE_EVENT_END0("base", "ScopedAllowBaseSyncPrimitivesOutsideBlockingScope");

  DCHECK(!tls_base_sync_primitives_disallowed)
      << "~ScopedAllowBaseSyncPrimitivesOutsideBlockingScope() running while "
         "surprisingly already no longer allowed.\n"
      << "tls_base_sync_primitives_disallowed "
      << tls_base_sync_primitives_disallowed;
}

}  // namespace base