Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 1 | // Copyright 2018 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_TASK_SEQUENCE_MANAGER_ASSOCIATED_THREAD_ID_H_ |
| 6 | #define BASE_TASK_SEQUENCE_MANAGER_ASSOCIATED_THREAD_ID_H_ |
| 7 | |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 8 | #include <atomic> |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/memory/ref_counted.h" |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 13 | #include "base/optional.h" |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
| 15 | #include "base/threading/platform_thread.h" |
| 16 | #include "base/threading/thread_checker.h" |
| 17 | |
| 18 | namespace base { |
| 19 | namespace sequence_manager { |
| 20 | namespace internal { |
| 21 | |
| 22 | // TODO(eseckler): Make this owned by SequenceManager once the TaskQueue |
| 23 | // refactor has happened (https://crbug.com/865411). |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 24 | // |
| 25 | // This class is thread-safe. But see notes about memory ordering guarantees for |
| 26 | // the various methods. |
| 27 | class BASE_EXPORT AssociatedThreadId |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 28 | : public base::RefCountedThreadSafe<AssociatedThreadId> { |
| 29 | public: |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 30 | AssociatedThreadId(); |
| 31 | |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 32 | // TODO(eseckler): Replace thread_checker with sequence_checker everywhere. |
| 33 | THREAD_CHECKER(thread_checker); |
| 34 | SEQUENCE_CHECKER(sequence_checker); |
| 35 | |
| 36 | static scoped_refptr<AssociatedThreadId> CreateUnbound() { |
| 37 | return MakeRefCounted<AssociatedThreadId>(); |
| 38 | } |
| 39 | |
| 40 | static scoped_refptr<AssociatedThreadId> CreateBound() { |
| 41 | auto associated_thread = MakeRefCounted<AssociatedThreadId>(); |
| 42 | associated_thread->BindToCurrentThread(); |
| 43 | return associated_thread; |
| 44 | } |
| 45 | |
| 46 | // Rebind the associated thread to the current thread. This allows creating |
| 47 | // the SequenceManager and TaskQueues on a different thread/sequence than the |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 48 | // one it will manage. |
| 49 | // |
| 50 | // Can only be called once. |
| 51 | void BindToCurrentThread(); |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 52 | |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 53 | // Returns the id of the thread bound to this object via a previous call to |
| 54 | // BindToCurrentThread(), nullopt if no thread was bound yet. |
| 55 | // |
| 56 | // This method guarantees a happens-before ordering with |
| 57 | // BindToCurrentThread(), that is all memory writes that happened-before the |
| 58 | // call to BindToCurrentThread() will become visible side-effects in the |
| 59 | // current thread. |
| 60 | // |
| 61 | // Attention: The result might be stale by the time this method returns. |
| 62 | Optional<PlatformThreadId> GetBoundThreadId() const { |
| 63 | auto thread_id = thread_id_.load(std::memory_order_acquire); |
| 64 | if (thread_id == kInvalidThreadId) { |
| 65 | return nullopt; |
| 66 | } else { |
| 67 | return thread_id; |
| 68 | } |
| 69 | } |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 70 | |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 71 | // Checks whether this object has already been bound to a thread. |
| 72 | // |
| 73 | // This method guarantees a happens-before ordering with |
| 74 | // BindToCurrentThread(), that is all memory writes that happened-before the |
| 75 | // call to BindToCurrentThread() will become visible side-effects in the |
| 76 | // current thread. |
| 77 | // |
| 78 | // Attention: The result might be stale by the time this method returns. |
| 79 | bool IsBound() const { |
| 80 | return thread_id_.load(std::memory_order_acquire) != kInvalidThreadId; |
| 81 | } |
| 82 | |
| 83 | // Checks whether this object is bound to the current thread. Returns false if |
| 84 | // this object is not bound to any thread. |
| 85 | // |
| 86 | // Note that this method provides no memory ordering guarantees but those are |
| 87 | // not really needed. If this method returns true we are on the same thread |
| 88 | // that called BindToCurrentThread(). If the method returns false this object |
| 89 | // could be unbound, so there is no possible ordering. |
| 90 | // |
| 91 | // Attention:: The result might be stale by the time this method returns. |
| 92 | bool IsBoundToCurrentThread() const { |
| 93 | return thread_id_.load(std::memory_order_relaxed) == |
| 94 | PlatformThread::CurrentId(); |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // TODO(eseckler): Add a method that checks that we are either bound already |
| 98 | // or on the thread which created us and use it in any_thread() accessors. |
| 99 | |
| 100 | private: |
| 101 | friend class base::RefCountedThreadSafe<AssociatedThreadId>; |
Carlos Caballero | 7162ac5 | 2018-11-30 09:46:53 | [diff] [blame] | 102 | ~AssociatedThreadId(); |
| 103 | |
| 104 | // All access to this member can be std::memory_order_relaxed as this class |
| 105 | // provides no ordering guarantees. |
| 106 | std::atomic<PlatformThreadId> thread_id_{kInvalidThreadId}; |
Eric Seckler | 34f0ed04 | 2018-07-26 07:55:16 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace internal |
| 110 | } // namespace sequence_manager |
| 111 | } // namespace base |
| 112 | |
| 113 | #endif // BASE_TASK_SEQUENCE_MANAGER_ASSOCIATED_THREAD_ID_H_ |