Home | History | Annotate | Download | only in threading
      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_THREAD_CHECKER_IMPL_H_
      6 #define BASE_THREADING_THREAD_CHECKER_IMPL_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/sequence_token.h"
     11 #include "base/synchronization/lock.h"
     12 #include "base/threading/platform_thread.h"
     13 
     14 namespace base {
     15 
     16 // Real implementation of ThreadChecker, for use in debug mode, or for temporary
     17 // use in release mode (e.g. to CHECK on a threading issue seen only in the
     18 // wild).
     19 //
     20 // Note: You should almost always use the ThreadChecker class to get the right
     21 // version for your build configuration.
     22 class BASE_EXPORT ThreadCheckerImpl {
     23  public:
     24   ThreadCheckerImpl();
     25   ~ThreadCheckerImpl();
     26 
     27   bool CalledOnValidThread() const WARN_UNUSED_RESULT;
     28 
     29   // Changes the thread that is checked for in CalledOnValidThread.  This may
     30   // be useful when an object may be created on one thread and then used
     31   // exclusively on another thread.
     32   void DetachFromThread();
     33 
     34  private:
     35   void EnsureAssigned() const;
     36 
     37   // Members are mutable so that CalledOnValidThread() can set them.
     38 
     39   // Synchronizes access to all members.
     40   mutable base::Lock lock_;
     41 
     42   // Thread on which CalledOnValidThread() may return true.
     43   mutable PlatformThreadRef thread_id_;
     44 
     45   // TaskToken for which CalledOnValidThread() always returns true. This allows
     46   // CalledOnValidThread() to return true when called multiple times from the
     47   // same task, even if it's not running in a single-threaded context itself
     48   // (allowing usage of ThreadChecker/NonThreadSafe objects on the stack in the
     49   // scope of one-off tasks). Note: CalledOnValidThread() may return true even
     50   // if the current TaskToken is not equal to this.
     51   mutable TaskToken task_token_;
     52 
     53   // SequenceToken for which CalledOnValidThread() may return true. Used to
     54   // ensure that CalledOnValidThread() doesn't return true for TaskScheduler
     55   // tasks that happen to run on the same thread but weren't posted to the same
     56   // SingleThreadTaskRunner.
     57   mutable SequenceToken sequence_token_;
     58 };
     59 
     60 }  // namespace base
     61 
     62 #endif  // BASE_THREADING_THREAD_CHECKER_IMPL_H_
     63