YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/thread_restrictions.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <glog/logging.h>
34
35
#include "yb/util/thread.h"
36
#include "yb/util/threadlocal.h"
37
#include "yb/util/thread_restrictions.h"
38
39
#ifdef ENABLE_THREAD_RESTRICTIONS
40
41
namespace yb {
42
43
namespace {
44
45
struct LocalThreadRestrictions {
46
  LocalThreadRestrictions()
47
    : io_allowed(true),
48
      wait_allowed(true),
49
1.63M
      singleton_allowed(true) {
50
1.63M
  }
51
52
  bool io_allowed;
53
  bool wait_allowed;
54
  bool singleton_allowed;
55
};
56
57
629M
LocalThreadRestrictions* LoadTLS() {
58
629M
  BLOCK_STATIC_THREAD_LOCAL(LocalThreadRestrictions, local_thread_restrictions);
59
629M
  return local_thread_restrictions;
60
629M
}
61
62
} // anonymous namespace
63
64
284k
bool ThreadRestrictions::SetIOAllowed(bool allowed) {
65
284k
  bool previous_allowed = LoadTLS()->io_allowed;
66
284k
  LoadTLS()->io_allowed = allowed;
67
284k
  return previous_allowed;
68
284k
}
69
70
106M
void ThreadRestrictions::AssertIOAllowed() {
71
106M
  CHECK(LoadTLS()->io_allowed)
72
6.00k
    << "Function marked as IO-only was called from a thread that "
73
6.00k
    << "disallows IO!  If this thread really should be allowed to "
74
6.00k
    << "make IO calls, adjust the call to "
75
6.00k
    << "yb::ThreadRestrictions::SetIOAllowed() in this thread's "
76
6.00k
    << "startup. "
77
6.00k
    << (Thread::current_thread() ? 
Thread::current_thread()->ToString()0
: "(not a yb::Thread)");
78
106M
}
79
80
302k
bool ThreadRestrictions::SetWaitAllowed(bool allowed) {
81
302k
  bool previous_allowed = LoadTLS()->wait_allowed;
82
302k
  LoadTLS()->wait_allowed = allowed;
83
302k
  return previous_allowed;
84
302k
}
85
86
408k
bool ThreadRestrictions::IsWaitAllowed() {
87
408k
  return LoadTLS()->wait_allowed;
88
408k
}
89
90
521M
void ThreadRestrictions::AssertWaitAllowed() {
91
521M
  CHECK(LoadTLS()->wait_allowed)
92
118k
    << "Waiting is not allowed to be used on this thread to prevent "
93
118k
    << "server-wide latency aberrations and deadlocks. "
94
118k
    << (Thread::current_thread() ? 
Thread::current_thread()->ToString()0
: "(not a yb::Thread)");
95
521M
}
96
97
} // namespace yb
98
99
#endif