/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 | 742k | singleton_allowed(true) { |
50 | 742k | } |
51 | | |
52 | | bool io_allowed; |
53 | | bool wait_allowed; |
54 | | bool singleton_allowed; |
55 | | }; |
56 | | |
57 | 231M | LocalThreadRestrictions* LoadTLS() { |
58 | 231M | BLOCK_STATIC_THREAD_LOCAL(LocalThreadRestrictions, local_thread_restrictions); |
59 | 231M | return local_thread_restrictions; |
60 | 231M | } |
61 | | |
62 | | } // anonymous namespace |
63 | | |
64 | 186k | bool ThreadRestrictions::SetIOAllowed(bool allowed) { |
65 | 186k | bool previous_allowed = LoadTLS()->io_allowed; |
66 | 186k | LoadTLS()->io_allowed = allowed; |
67 | 186k | return previous_allowed; |
68 | 186k | } |
69 | | |
70 | 62.8M | void ThreadRestrictions::AssertIOAllowed() { |
71 | 4.69k | CHECK(LoadTLS()->io_allowed) |
72 | 4.69k | << "Function marked as IO-only was called from a thread that " |
73 | 4.69k | << "disallows IO! If this thread really should be allowed to " |
74 | 4.69k | << "make IO calls, adjust the call to " |
75 | 4.69k | << "yb::ThreadRestrictions::SetIOAllowed() in this thread's " |
76 | 4.69k | << "startup. " |
77 | 4.69k | << (Thread::current_thread() ? Thread::current_thread()->ToString() : "(not a yb::Thread)"); |
78 | 62.8M | } |
79 | | |
80 | 194k | bool ThreadRestrictions::SetWaitAllowed(bool allowed) { |
81 | 194k | bool previous_allowed = LoadTLS()->wait_allowed; |
82 | 194k | LoadTLS()->wait_allowed = allowed; |
83 | 194k | return previous_allowed; |
84 | 194k | } |
85 | | |
86 | 241k | bool ThreadRestrictions::IsWaitAllowed() { |
87 | 241k | return LoadTLS()->wait_allowed; |
88 | 241k | } |
89 | | |
90 | 167M | void ThreadRestrictions::AssertWaitAllowed() { |
91 | 45.6k | CHECK(LoadTLS()->wait_allowed) |
92 | 45.6k | << "Waiting is not allowed to be used on this thread to prevent " |
93 | 45.6k | << "server-wide latency aberrations and deadlocks. " |
94 | 45.6k | << (Thread::current_thread() ? Thread::current_thread()->ToString() : "(not a yb::Thread)"); |
95 | 167M | } |
96 | | |
97 | | } // namespace yb |
98 | | |
99 | | #endif |