1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/threading/thread_restrictions.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/threading/thread_restrictions.h" 1.9 + 1.10 +#if ENABLE_THREAD_RESTRICTIONS 1.11 + 1.12 +#include "base/lazy_instance.h" 1.13 +#include "base/logging.h" 1.14 +#include "base/threading/thread_local.h" 1.15 + 1.16 +namespace base { 1.17 + 1.18 +namespace { 1.19 + 1.20 +LazyInstance<ThreadLocalBoolean>::Leaky 1.21 + g_io_disallowed = LAZY_INSTANCE_INITIALIZER; 1.22 + 1.23 +LazyInstance<ThreadLocalBoolean>::Leaky 1.24 + g_singleton_disallowed = LAZY_INSTANCE_INITIALIZER; 1.25 + 1.26 +LazyInstance<ThreadLocalBoolean>::Leaky 1.27 + g_wait_disallowed = LAZY_INSTANCE_INITIALIZER; 1.28 + 1.29 +} // anonymous namespace 1.30 + 1.31 +// static 1.32 +bool ThreadRestrictions::SetIOAllowed(bool allowed) { 1.33 + bool previous_disallowed = g_io_disallowed.Get().Get(); 1.34 + g_io_disallowed.Get().Set(!allowed); 1.35 + return !previous_disallowed; 1.36 +} 1.37 + 1.38 +// static 1.39 +void ThreadRestrictions::AssertIOAllowed() { 1.40 + if (g_io_disallowed.Get().Get()) { 1.41 + LOG(FATAL) << 1.42 + "Function marked as IO-only was called from a thread that " 1.43 + "disallows IO! If this thread really should be allowed to " 1.44 + "make IO calls, adjust the call to " 1.45 + "base::ThreadRestrictions::SetIOAllowed() in this thread's " 1.46 + "startup."; 1.47 + } 1.48 +} 1.49 + 1.50 +// static 1.51 +bool ThreadRestrictions::SetSingletonAllowed(bool allowed) { 1.52 + bool previous_disallowed = g_singleton_disallowed.Get().Get(); 1.53 + g_singleton_disallowed.Get().Set(!allowed); 1.54 + return !previous_disallowed; 1.55 +} 1.56 + 1.57 +// static 1.58 +void ThreadRestrictions::AssertSingletonAllowed() { 1.59 + if (g_singleton_disallowed.Get().Get()) { 1.60 + LOG(FATAL) << "LazyInstance/Singleton is not allowed to be used on this " 1.61 + << "thread. Most likely it's because this thread is not " 1.62 + << "joinable, so AtExitManager may have deleted the object " 1.63 + << "on shutdown, leading to a potential shutdown crash."; 1.64 + } 1.65 +} 1.66 + 1.67 +// static 1.68 +void ThreadRestrictions::DisallowWaiting() { 1.69 + g_wait_disallowed.Get().Set(true); 1.70 +} 1.71 + 1.72 +// static 1.73 +void ThreadRestrictions::AssertWaitAllowed() { 1.74 + if (g_wait_disallowed.Get().Get()) { 1.75 + LOG(FATAL) << "Waiting is not allowed to be used on this thread to prevent" 1.76 + << "jank and deadlock."; 1.77 + } 1.78 +} 1.79 + 1.80 +bool ThreadRestrictions::SetWaitAllowed(bool allowed) { 1.81 + bool previous_disallowed = g_wait_disallowed.Get().Get(); 1.82 + g_wait_disallowed.Get().Set(!allowed); 1.83 + return !previous_disallowed; 1.84 +} 1.85 + 1.86 +} // namespace base 1.87 + 1.88 +#endif // ENABLE_THREAD_RESTRICTIONS