| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/thread_restrictions.h" | 5 #include "base/threading/thread_restrictions.h" |
| 6 | 6 |
| 7 // This entire file is compiled out in Release mode. | 7 // This entire file is compiled out in Release mode. |
| 8 #ifndef NDEBUG | 8 #ifndef NDEBUG |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/threading/thread_local.h" | 12 #include "base/threading/thread_local.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 LazyInstance<ThreadLocalBoolean>::Leaky | 18 LazyInstance<ThreadLocalBoolean>::Leaky |
| 19 g_io_disallowed = LAZY_INSTANCE_INITIALIZER; | 19 g_io_disallowed = LAZY_INSTANCE_INITIALIZER; |
| 20 | 20 |
| 21 LazyInstance<ThreadLocalBoolean>::Leaky | 21 LazyInstance<ThreadLocalBoolean>::Leaky |
| 22 g_singleton_disallowed = LAZY_INSTANCE_INITIALIZER; | 22 g_singleton_disallowed = LAZY_INSTANCE_INITIALIZER; |
| 23 | 23 |
| 24 LazyInstance<ThreadLocalBoolean>::Leaky |
| 25 g_wait_disallowed = LAZY_INSTANCE_INITIALIZER; |
| 26 |
| 24 } // anonymous namespace | 27 } // anonymous namespace |
| 25 | 28 |
| 26 // static | 29 // static |
| 27 bool ThreadRestrictions::SetIOAllowed(bool allowed) { | 30 bool ThreadRestrictions::SetIOAllowed(bool allowed) { |
| 28 bool previous_disallowed = g_io_disallowed.Get().Get(); | 31 bool previous_disallowed = g_io_disallowed.Get().Get(); |
| 29 g_io_disallowed.Get().Set(!allowed); | 32 g_io_disallowed.Get().Set(!allowed); |
| 30 return !previous_disallowed; | 33 return !previous_disallowed; |
| 31 } | 34 } |
| 32 | 35 |
| 33 // static | 36 // static |
| 34 void ThreadRestrictions::AssertIOAllowed() { | 37 void ThreadRestrictions::AssertIOAllowed() { |
| 35 if (g_io_disallowed.Get().Get()) { | 38 if (g_io_disallowed.Get().Get()) { |
| 36 LOG(FATAL) << | 39 LOG(FATAL) << |
| 37 "Function marked as IO-only was called from a thread that " | 40 "Function marked as IO-only was called from a thread that " |
| 38 "disallows IO! If this thread really should be allowed to " | 41 "disallows IO! If this thread really should be allowed to " |
| 39 "make IO calls, adjust the call to " | 42 "make IO calls, adjust the call to " |
| 40 "base::ThreadRestrictions::SetIOAllowed() in this thread's " | 43 "base::ThreadRestrictions::SetIOAllowed() in this thread's " |
| 41 "startup."; | 44 "startup."; |
| 42 } | 45 } |
| 43 } | 46 } |
| 44 | 47 |
| 48 // static |
| 45 bool ThreadRestrictions::SetSingletonAllowed(bool allowed) { | 49 bool ThreadRestrictions::SetSingletonAllowed(bool allowed) { |
| 46 bool previous_disallowed = g_singleton_disallowed.Get().Get(); | 50 bool previous_disallowed = g_singleton_disallowed.Get().Get(); |
| 47 g_singleton_disallowed.Get().Set(!allowed); | 51 g_singleton_disallowed.Get().Set(!allowed); |
| 48 return !previous_disallowed; | 52 return !previous_disallowed; |
| 49 } | 53 } |
| 50 | 54 |
| 51 // static | 55 // static |
| 52 void ThreadRestrictions::AssertSingletonAllowed() { | 56 void ThreadRestrictions::AssertSingletonAllowed() { |
| 53 if (g_singleton_disallowed.Get().Get()) { | 57 if (g_singleton_disallowed.Get().Get()) { |
| 54 LOG(FATAL) << "LazyInstance/Singleton is not allowed to be used on this " | 58 LOG(FATAL) << "LazyInstance/Singleton is not allowed to be used on this " |
| 55 << "thread. Most likely it's because this thread is not " | 59 << "thread. Most likely it's because this thread is not " |
| 56 << "joinable, so AtExitManager may have deleted the object " | 60 << "joinable, so AtExitManager may have deleted the object " |
| 57 << "on shutdown, leading to a potential shutdown crash."; | 61 << "on shutdown, leading to a potential shutdown crash."; |
| 58 } | 62 } |
| 59 } | 63 } |
| 60 | 64 |
| 65 // static |
| 66 void ThreadRestrictions::DisallowWaiting() { |
| 67 g_wait_disallowed.Get().Set(true); |
| 68 } |
| 69 |
| 70 // static |
| 71 void ThreadRestrictions::AssertWaitAllowed() { |
| 72 if (g_wait_disallowed.Get().Get()) { |
| 73 LOG(FATAL) << "Waiting is not allowed to be used on this thread to prevent" |
| 74 << "jank and deadlock."; |
| 75 } |
| 76 } |
| 77 |
| 78 bool ThreadRestrictions::SetWaitAllowed(bool allowed) { |
| 79 bool previous_disallowed = g_wait_disallowed.Get().Get(); |
| 80 g_wait_disallowed.Get().Set(!allowed); |
| 81 return !previous_disallowed; |
| 82 } |
| 83 |
| 61 } // namespace base | 84 } // namespace base |
| 62 | 85 |
| 63 #endif // NDEBUG | 86 #endif // NDEBUG |
| OLD | NEW |