Home | History | Annotate | Download | only in threading
      1 // Copyright (c) 2012 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_THREAD_CHECKER_H_
      6 #define BASE_THREADING_THREAD_CHECKER_H_
      7 
      8 #include "base/logging.h"
      9 #include "base/threading/thread_checker_impl.h"
     10 
     11 namespace base {
     12 
     13 // Do nothing implementation, for use in release mode.
     14 //
     15 // Note: You should almost always use the ThreadChecker class to get the
     16 // right version for your build configuration.
     17 class ThreadCheckerDoNothing {
     18  public:
     19   bool CalledOnValidThread() const WARN_UNUSED_RESULT {
     20     return true;
     21   }
     22 
     23   void DetachFromThread() {}
     24 };
     25 
     26 // ThreadChecker is a helper class used to help verify that some methods of a
     27 // class are called from the same thread. It provides identical functionality to
     28 // base::NonThreadSafe, but it is meant to be held as a member variable, rather
     29 // than inherited from base::NonThreadSafe.
     30 //
     31 // While inheriting from base::NonThreadSafe may give a clear indication about
     32 // the thread-safety of a class, it may also lead to violations of the style
     33 // guide with regard to multiple inheritance. The choice between having a
     34 // ThreadChecker member and inheriting from base::NonThreadSafe should be based
     35 // on whether:
     36 //  - Derived classes need to know the thread they belong to, as opposed to
     37 //    having that functionality fully encapsulated in the base class.
     38 //  - Derived classes should be able to reassign the base class to another
     39 //    thread, via DetachFromThread.
     40 //
     41 // If neither of these are true, then having a ThreadChecker member and calling
     42 // CalledOnValidThread is the preferable solution.
     43 //
     44 // Example:
     45 // class MyClass {
     46 //  public:
     47 //   void Foo() {
     48 //     DCHECK(thread_checker_.CalledOnValidThread());
     49 //     ... (do stuff) ...
     50 //   }
     51 //
     52 //  private:
     53 //   ThreadChecker thread_checker_;
     54 // }
     55 //
     56 // Note that, when enabled, CalledOnValidThread() returns false when called from
     57 // tasks posted to SingleThreadTaskRunners bound to different sequences, even if
     58 // the tasks happen to run on the same thread (e.g. two independent TaskRunners
     59 // with ExecutionMode::SINGLE_THREADED on the TaskScheduler that happen to share
     60 // a thread).
     61 //
     62 // In Release mode, CalledOnValidThread will always return true.
     63 #if DCHECK_IS_ON()
     64 class ThreadChecker : public ThreadCheckerImpl {
     65 };
     66 #else
     67 class ThreadChecker : public ThreadCheckerDoNothing {
     68 };
     69 #endif  // DCHECK_IS_ON()
     70 
     71 }  // namespace base
     72 
     73 #endif  // BASE_THREADING_THREAD_CHECKER_H_
     74