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 <cstddef> | |
8 | |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
8 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
9 #include "base/threading/thread_local.h" | 11 #include "base/threading/thread_local.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> > | 17 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> > |
16 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; | 18 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
17 | 19 |
18 } // namespace | 20 } // namespace |
19 | 21 |
20 // static | 22 // static |
23 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::GetIfExists() { | |
24 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); | |
25 return current ? current->task_runner_ : NULL; | |
26 } | |
27 | |
28 // static | |
21 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { | 29 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { |
22 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); | 30 const scoped_refptr<SingleThreadTaskRunner>& thread_task_runner = |
23 DCHECK(current); | 31 GetIfExists(); |
24 return current->task_runner_; | 32 CHECK(thread_task_runner.get()); |
33 return thread_task_runner; | |
jar (doing other things)
2013/02/16 02:37:27
nit: List methods after constructors and destructo
| |
25 } | 34 } |
26 | 35 |
27 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( | 36 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( |
28 const scoped_refptr<SingleThreadTaskRunner>& task_runner) | 37 const scoped_refptr<SingleThreadTaskRunner>& task_runner) |
29 : task_runner_(task_runner) { | 38 : task_runner_(task_runner) { |
30 DCHECK(task_runner_->BelongsToCurrentThread()); | 39 DCHECK(task_runner_->BelongsToCurrentThread()); |
31 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 40 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
32 lazy_tls_ptr.Pointer()->Set(this); | 41 lazy_tls_ptr.Pointer()->Set(this); |
33 } | 42 } |
34 | 43 |
35 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { | 44 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { |
36 DCHECK(task_runner_->BelongsToCurrentThread()); | 45 DCHECK(task_runner_->BelongsToCurrentThread()); |
37 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); | 46 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); |
38 lazy_tls_ptr.Pointer()->Set(NULL); | 47 lazy_tls_ptr.Pointer()->Set(NULL); |
39 } | 48 } |
40 | 49 |
41 } // namespace base | 50 } // namespace base |
OLD | NEW |