sergeyu@chromium.org | 675d888 | 2012-05-10 05:16:14 +0900 | [diff] [blame] | 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 "base/lazy_instance.h" |
| 8 | #include "base/single_thread_task_runner.h" |
| 9 | #include "base/threading/thread_local.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | namespace { |
| 14 | |
alokp | e9634be | 2015-10-02 18:23:49 +0900 | [diff] [blame] | 15 | base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle> >::Leaky |
sergeyu@chromium.org | 675d888 | 2012-05-10 05:16:14 +0900 | [diff] [blame] | 16 | lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | // static |
| 21 | scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() { |
| 22 | ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get(); |
| 23 | DCHECK(current); |
| 24 | return current->task_runner_; |
| 25 | } |
| 26 | |
alexeypa@chromium.org | b9baffc | 2013-04-25 05:44:38 +0900 | [diff] [blame] | 27 | // static |
| 28 | bool ThreadTaskRunnerHandle::IsSet() { |
| 29 | return lazy_tls_ptr.Pointer()->Get() != NULL; |
| 30 | } |
| 31 | |
sergeyu@chromium.org | 675d888 | 2012-05-10 05:16:14 +0900 | [diff] [blame] | 32 | ThreadTaskRunnerHandle::ThreadTaskRunnerHandle( |
| 33 | const scoped_refptr<SingleThreadTaskRunner>& task_runner) |
| 34 | : task_runner_(task_runner) { |
| 35 | DCHECK(task_runner_->BelongsToCurrentThread()); |
| 36 | DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 37 | lazy_tls_ptr.Pointer()->Set(this); |
| 38 | } |
| 39 | |
| 40 | ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() { |
| 41 | DCHECK(task_runner_->BelongsToCurrentThread()); |
| 42 | DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this); |
| 43 | lazy_tls_ptr.Pointer()->Set(NULL); |
| 44 | } |
| 45 | |
| 46 | } // namespace base |