| 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 "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> > | 15 base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> > |
| 16 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; | 16 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { | 21 scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { |
| 22 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); | 22 ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); |
| 23 DCHECK(current); | 23 return current ? current->task_runner_ : nullptr; |
| 24 return current->task_runner_; | |
| 25 } | 24 } |
| 26 | 25 |
| 27 // static | 26 // static |
| 28 bool ThreadTaskRunnerHandle::IsSet() { | 27 bool ThreadTaskRunnerHandle::IsSet() { |
| 29 return lazy_tls_ptr.Pointer()->Get() != NULL; | 28 return lazy_tls_ptr.Pointer()->Get() != NULL; |
| 30 } | 29 } |
| 31 | 30 |
| 32 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( | 31 ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( |
| 33 const scoped_refptr<SingleThreadTaskRunner>& task_runner) | 32 const scoped_refptr<SingleThreadTaskRunner>& task_runner) |
| 34 : task_runner_(task_runner) { | 33 : task_runner_(task_runner) { |
| 35 DCHECK(task_runner_->BelongsToCurrentThread()); | 34 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 36 DCHECK(!lazy_tls_ptr.Pointer()->Get()); | 35 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 37 lazy_tls_ptr.Pointer()->Set(this); | 36 lazy_tls_ptr.Pointer()->Set(this); |
| 38 } | 37 } |
| 39 | 38 |
| 40 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { | 39 ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { |
| 41 DCHECK(task_runner_->BelongsToCurrentThread()); | 40 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 42 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); | 41 DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); |
| 43 lazy_tls_ptr.Pointer()->Set(NULL); | 42 lazy_tls_ptr.Pointer()->Set(NULL); |
| 44 } | 43 } |
| 45 | 44 |
| 46 } // namespace base | 45 } // namespace base |
| OLD | NEW |