diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-19 20:28:03 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-19 20:28:03 +0000 |
commit | 5db27db7e09cf3f72c68a8ca668b96177c97101f (patch) | |
tree | 3cfcb4c2c3fdf7543564e94603f33d640167d25e /base/thread_restrictions.cc | |
parent | fe23e9a748e6a1849f22d3d0add4502a3a41604f (diff) | |
download | chromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.zip chromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.tar.gz chromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.tar.bz2 |
base: add a thread-safety assertion checker
ThreadAssertions lets us assert that the current thread is allowed to
make blocking calls. See the header for more background.
This change implements ThreadAssertions but doesn't turn it on for
any of Chrome.
BUG=59575
Review URL: http://codereview.chromium.org/3824006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/thread_restrictions.cc')
-rw-r--r-- | base/thread_restrictions.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/base/thread_restrictions.cc b/base/thread_restrictions.cc new file mode 100644 index 0000000..5be8ad7 --- /dev/null +++ b/base/thread_restrictions.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/thread_restrictions.h" + +// This entire file is compiled out in Release mode. +#ifndef NDEBUG + +#include "base/lazy_instance.h" +#include "base/logging.h" +#include "base/thread_local.h" + +namespace { + +static base::LazyInstance<base::ThreadLocalBoolean> + g_io_disallowed(base::LINKER_INITIALIZED); + +} // anonymous namespace + +namespace base { + +// static +void ThreadRestrictions::SetIOAllowed(bool allowed) { + g_io_disallowed.Get().Set(!allowed); +} + +// static +void ThreadRestrictions::AssertIOAllowed() { + if (g_io_disallowed.Get().Get()) { + LOG(FATAL) << + "Function marked as IO-only was called from a thread that " + "disallows IO! If this thread really should be allowed to " + "make IO calls, adjust the call to " + "base::ThreadRestrictions::SetIOAllowed() in this thread's " + "startup."; + } +} + +} // namespace base + +#endif // NDEBUG |