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_main_task_runner.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/threading/thread_local.h" |
| 9 |
| 10 namespace base { |
| 11 |
| 12 namespace { |
| 13 |
| 14 base::LazyInstance<base::ThreadLocalPointer<ThreadMainTaskRunner> > |
| 15 lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
| 16 |
| 17 } // namespace |
| 18 |
| 19 // static |
| 20 scoped_refptr<ThreadMainTaskRunner> ThreadMainTaskRunner::current() { |
| 21 ThreadMainTaskRunner* result = lazy_tls_ptr.Pointer()->Get(); |
| 22 DCHECK(result); |
| 23 return result; |
| 24 } |
| 25 |
| 26 ThreadMainTaskRunner::ThreadMainTaskRunner() |
| 27 : detached_(false) { |
| 28 DCHECK(!lazy_tls_ptr.Pointer()->Get()); |
| 29 lazy_tls_ptr.Pointer()->Set(this); |
| 30 } |
| 31 |
| 32 ThreadMainTaskRunner::~ThreadMainTaskRunner() { |
| 33 DCHECK(detached_); |
| 34 } |
| 35 |
| 36 void ThreadMainTaskRunner::Detach() { |
| 37 DCHECK(BelongsToCurrentThread()); |
| 38 DCHECK_EQ(current(), this); |
| 39 lazy_tls_ptr.Pointer()->Set(NULL); |
| 40 detached_ = true; |
| 41 } |
| 42 |
| 43 } // namespace base |
OLD | NEW |