security/sandbox/chromium/base/threading/thread_restrictions.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial