1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/non_thread_safe.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,52 @@ 1.4 +// Copyright (c) 2006-2008 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 +#ifndef BASE_NON_THREAD_SAFE_H__ 1.9 +#define BASE_NON_THREAD_SAFE_H__ 1.10 + 1.11 +#include "base/platform_thread.h" 1.12 + 1.13 +// A helper class used to help verify that methods of a class are 1.14 +// called from the same thread. One can inherit from this class and use 1.15 +// CalledOnValidThread() to verify. 1.16 +// 1.17 +// This is intended to be used with classes that appear to be thread safe, but 1.18 +// aren't. For example, a service or a singleton like the preferences system. 1.19 +// 1.20 +// Example: 1.21 +// class MyClass : public NonThreadSafe { 1.22 +// public: 1.23 +// void Foo() { 1.24 +// DCHECK(CalledOnValidThread()); 1.25 +// ... (do stuff) ... 1.26 +// } 1.27 +// } 1.28 +// 1.29 +// In Release mode, CalledOnValidThread will always return true. 1.30 +// 1.31 +#ifndef NDEBUG 1.32 +class NonThreadSafe { 1.33 + public: 1.34 + NonThreadSafe(); 1.35 + ~NonThreadSafe(); 1.36 + 1.37 + bool CalledOnValidThread() const; 1.38 + 1.39 + private: 1.40 + PlatformThreadId valid_thread_id_; 1.41 +}; 1.42 +#else 1.43 +// Do nothing in release mode. 1.44 +class NonThreadSafe { 1.45 + public: 1.46 + NonThreadSafe() {} 1.47 + ~NonThreadSafe() {} 1.48 + 1.49 + bool CalledOnValidThread() const { 1.50 + return true; 1.51 + } 1.52 +}; 1.53 +#endif // NDEBUG 1.54 + 1.55 +#endif // BASE_NON_THREAD_SAFE_H__