Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2228)

Unified Diff: base/thread_main_task_runner.cc

Issue 10210008: Make MessageLoopProxy::current() usable on threads that don't have MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/thread_main_task_runner.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/thread_main_task_runner.cc
diff --git a/base/thread_main_task_runner.cc b/base/thread_main_task_runner.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d36cae48ec524bf25ba9609df37f380e63631119
--- /dev/null
+++ b/base/thread_main_task_runner.cc
@@ -0,0 +1,43 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/thread_main_task_runner.h"
+
+#include "base/lazy_instance.h"
+#include "base/threading/thread_local.h"
+
+namespace base {
+
+namespace {
+
+base::LazyInstance<base::ThreadLocalPointer<ThreadMainTaskRunner> >
+ lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+// static
+scoped_refptr<ThreadMainTaskRunner> ThreadMainTaskRunner::current() {
+ ThreadMainTaskRunner* result = lazy_tls_ptr.Pointer()->Get();
+ DCHECK(result);
+ return result;
+}
+
+ThreadMainTaskRunner::ThreadMainTaskRunner()
+ : detached_(false) {
+ DCHECK(!lazy_tls_ptr.Pointer()->Get());
+ lazy_tls_ptr.Pointer()->Set(this);
+}
+
+ThreadMainTaskRunner::~ThreadMainTaskRunner() {
+ DCHECK(detached_);
+}
+
+void ThreadMainTaskRunner::Detach() {
+ DCHECK(BelongsToCurrentThread());
+ DCHECK_EQ(current(), this);
+ lazy_tls_ptr.Pointer()->Set(NULL);
+ detached_ = true;
+}
+
+} // namespace base
« no previous file with comments | « base/thread_main_task_runner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698