michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_NON_THREAD_SAFE_H__ michael@0: #define BASE_NON_THREAD_SAFE_H__ michael@0: michael@0: #include "base/platform_thread.h" michael@0: michael@0: // A helper class used to help verify that methods of a class are michael@0: // called from the same thread. One can inherit from this class and use michael@0: // CalledOnValidThread() to verify. michael@0: // michael@0: // This is intended to be used with classes that appear to be thread safe, but michael@0: // aren't. For example, a service or a singleton like the preferences system. michael@0: // michael@0: // Example: michael@0: // class MyClass : public NonThreadSafe { michael@0: // public: michael@0: // void Foo() { michael@0: // DCHECK(CalledOnValidThread()); michael@0: // ... (do stuff) ... michael@0: // } michael@0: // } michael@0: // michael@0: // In Release mode, CalledOnValidThread will always return true. michael@0: // michael@0: #ifndef NDEBUG michael@0: class NonThreadSafe { michael@0: public: michael@0: NonThreadSafe(); michael@0: ~NonThreadSafe(); michael@0: michael@0: bool CalledOnValidThread() const; michael@0: michael@0: private: michael@0: PlatformThreadId valid_thread_id_; michael@0: }; michael@0: #else michael@0: // Do nothing in release mode. michael@0: class NonThreadSafe { michael@0: public: michael@0: NonThreadSafe() {} michael@0: ~NonThreadSafe() {} michael@0: michael@0: bool CalledOnValidThread() const { michael@0: return true; michael@0: } michael@0: }; michael@0: #endif // NDEBUG michael@0: michael@0: #endif // BASE_NON_THREAD_SAFE_H__