1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_ 6 #define BASE_THREADING_NON_THREAD_SAFE_H_ 7 #pragma once 8 9 #ifndef NDEBUG 10 #include "base/threading/non_thread_safe_impl.h" 11 #endif 12 13 namespace base { 14 15 // Do nothing implementation of NonThreadSafe, for release mode. 16 // 17 // Note: You should almost always use the NonThreadSafe class to get 18 // the right version of the class for your build configuration. 19 class NonThreadSafeDoNothing { 20 public: CalledOnValidThread()21 bool CalledOnValidThread() const { 22 return true; 23 } 24 25 protected: DetachFromThread()26 void DetachFromThread() {} 27 }; 28 29 // NonThreadSafe is a helper class used to help verify that methods of a 30 // class are called from the same thread. One can inherit from this class 31 // and use CalledOnValidThread() to verify. 32 // 33 // This is intended to be used with classes that appear to be thread safe, but 34 // aren't. For example, a service or a singleton like the preferences system. 35 // 36 // Example: 37 // class MyClass : public base::NonThreadSafe { 38 // public: 39 // void Foo() { 40 // DCHECK(CalledOnValidThread()); 41 // ... (do stuff) ... 42 // } 43 // } 44 // 45 // In Release mode, CalledOnValidThread will always return true. 46 // 47 #ifndef NDEBUG 48 class NonThreadSafe : public NonThreadSafeImpl { 49 }; 50 #else 51 class NonThreadSafe : public NonThreadSafeDoNothing { 52 }; 53 #endif // NDEBUG 54 55 } // namespace base 56 57 #endif // BASE_NON_THREAD_SAFE_H_ 58