OLD | NEW |
| (Empty) |
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 #include "base/thread_task_runner_handle.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/lazy_instance.h" | |
10 #include "base/logging.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/threading/sequenced_task_runner_handle.h" | |
13 #include "base/threading/thread_local.h" | |
14 | |
15 namespace base { | |
16 | |
17 namespace { | |
18 | |
19 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle>>::Leaky | |
20 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; | |
21 | |
22 } // namespace | |
23 | |
24 // static | |
25 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { | |
26 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); | |
27 DCHECK(current); | |
28 return current->task_runner_; | |
29 } | |
30 | |
31 // static | |
32 bool ThreadTaskRunnerHandle::IsSet() { | |
33 return !!lazy_tls_ptr.Pointer()->Get(); | |
34 } | |
35 | |
36 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( | |
37 scoped_refptr<SingleThreadTaskRunner> task_runner) | |
38 : task_runner_(std::move(task_runner)) { | |
39 DCHECK(task_runner_->BelongsToCurrentThread()); | |
40 // No SequencedTaskRunnerHandle (which includes ThreadTaskRunnerHandles) | |
41 // should already be set for this thread. | |
42 DCHECK(!SequencedTaskRunnerHandle::IsSet()); | |
43 lazy_tls_ptr.Pointer()->Set(this); | |
44 } | |
45 | |
46 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { | |
47 DCHECK(task_runner_->BelongsToCurrentThread()); | |
48 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); | |
49 lazy_tls_ptr.Pointer()->Set(nullptr); | |
50 } | |
51 | |
52 } // namespace base | |
OLD | NEW |