| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/thread_restrictions.h" | 5 #include "base/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/thread_local.h" | 12 #include "base/thread_local.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 LazyInstance<ThreadLocalBoolean, LeakyLazyInstanceTraits<ThreadLocalBoolean> > | 18 LazyInstance<ThreadLocalBoolean, LeakyLazyInstanceTraits<ThreadLocalBoolean> > |
| 19 g_io_disallowed(LINKER_INITIALIZED); | 19 g_io_disallowed(LINKER_INITIALIZED); |
| 20 | 20 |
| 21 LazyInstance<ThreadLocalBoolean, LeakyLazyInstanceTraits<ThreadLocalBoolean> > |
| 22 g_singleton_disallowed(LINKER_INITIALIZED); |
| 23 |
| 21 } // anonymous namespace | 24 } // anonymous namespace |
| 22 | 25 |
| 23 // static | 26 // static |
| 24 bool ThreadRestrictions::SetIOAllowed(bool allowed) { | 27 bool ThreadRestrictions::SetIOAllowed(bool allowed) { |
| 25 bool previous_allowed = g_io_disallowed.Get().Get(); | 28 bool previous_disallowed = g_io_disallowed.Get().Get(); |
| 26 g_io_disallowed.Get().Set(!allowed); | 29 g_io_disallowed.Get().Set(!allowed); |
| 27 return !previous_allowed; | 30 return !previous_disallowed; |
| 28 } | 31 } |
| 29 | 32 |
| 30 // static | 33 // static |
| 31 void ThreadRestrictions::AssertIOAllowed() { | 34 void ThreadRestrictions::AssertIOAllowed() { |
| 32 if (g_io_disallowed.Get().Get()) { | 35 if (g_io_disallowed.Get().Get()) { |
| 33 LOG(FATAL) << | 36 LOG(FATAL) << |
| 34 "Function marked as IO-only was called from a thread that " | 37 "Function marked as IO-only was called from a thread that " |
| 35 "disallows IO! If this thread really should be allowed to " | 38 "disallows IO! If this thread really should be allowed to " |
| 36 "make IO calls, adjust the call to " | 39 "make IO calls, adjust the call to " |
| 37 "base::ThreadRestrictions::SetIOAllowed() in this thread's " | 40 "base::ThreadRestrictions::SetIOAllowed() in this thread's " |
| 38 "startup."; | 41 "startup."; |
| 39 } | 42 } |
| 40 } | 43 } |
| 41 | 44 |
| 45 bool ThreadRestrictions::SetSingletonAllowed(bool allowed) { |
| 46 bool previous_disallowed = g_singleton_disallowed.Get().Get(); |
| 47 g_singleton_disallowed.Get().Set(!allowed); |
| 48 return !previous_disallowed; |
| 49 } |
| 50 |
| 51 // static |
| 52 void ThreadRestrictions::AssertSingletonAllowed() { |
| 53 if (g_singleton_disallowed.Get().Get()) { |
| 54 LOG(FATAL) << "LazyInstance/Singleton is not allowed to be used on this " |
| 55 << "thread. Most likely it's because this thread is not " |
| 56 << "joinable, so AtExitManager may have deleted the object " |
| 57 << "on shutdown, leading to a potential shutdown crash."; |
| 58 } |
| 59 } |
| 60 |
| 42 } // namespace base | 61 } // namespace base |
| 43 | 62 |
| 44 #endif // NDEBUG | 63 #endif // NDEBUG |
| OLD | NEW |