OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/thread_task_runner_handle.h" | 5 #include "base/thread_task_runner_handle.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" |
8 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/sequenced_task_runner_handle.h" |
9 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
10 | 14 |
11 namespace base { | 15 namespace base { |
12 | 16 |
13 namespace { | 17 namespace { |
14 | 18 |
15 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> >::Leaky | 19 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle>>::Leaky |
16 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; | 20 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
17 | 21 |
18 } // namespace | 22 } // namespace |
19 | 23 |
20 // static | 24 // static |
21 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { | 25 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { |
22 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); | 26 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); |
23 DCHECK(current); | 27 DCHECK(current); |
24 return current->task_runner_; | 28 return current->task_runner_; |
25 } | 29 } |
26 | 30 |
27 // static | 31 // static |
28 bool ThreadTaskRunnerHandle::IsSet() { | 32 bool ThreadTaskRunnerHandle::IsSet() { |
29 return lazy_tls_ptr.Pointer()->Get() != NULL; | 33 return !!lazy_tls_ptr.Pointer()->Get(); |
30 } | 34 } |
31 | 35 |
32 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( | 36 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( |
33 scoped_refptr<SingleThreadTaskRunner> task_runner) | 37 scoped_refptr<SingleThreadTaskRunner> task_runner) |
34 : task_runner_(std::move(task_runner)) { | 38 : task_runner_(std::move(task_runner)) { |
35 DCHECK(task_runner_->BelongsToCurrentThread()); | 39 DCHECK(task_runner_->BelongsToCurrentThread()); |
36 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 40 // No SequencedTaskRunnerHandle (which includes ThreadTaskRunnerHandles) |
| 41 // should already be set for this thread. |
| 42 DCHECK(!SequencedTaskRunnerHandle::IsSet()); |
37 lazy_tls_ptr.Pointer()->Set(this); | 43 lazy_tls_ptr.Pointer()->Set(this); |
38 } | 44 } |
39 | 45 |
40 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { | 46 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { |
41 DCHECK(task_runner_->BelongsToCurrentThread()); | 47 DCHECK(task_runner_->BelongsToCurrentThread()); |
42 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); | 48 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); |
43 lazy_tls_ptr.Pointer()->Set(NULL); | 49 lazy_tls_ptr.Pointer()->Set(nullptr); |
44 } | 50 } |
45 | 51 |
46 } // namespace base | 52 } // namespace base |
OLD | NEW |