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

Unified Diff: remoting/base/auto_thread_task_runner.h

Issue 10829467: [Chromoting] Introducing refcount-based life time management of the message loops in the service (d… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR feedback and a unit test for AutoThreadTaskRunner. Created 8 years, 4 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
Index: remoting/base/auto_thread_task_runner.h
diff --git a/remoting/base/auto_thread_task_runner.h b/remoting/base/auto_thread_task_runner.h
new file mode 100644
index 0000000000000000000000000000000000000000..5ea7ca1fc5dd9033bb7c200940c8ea23f4338f22
--- /dev/null
+++ b/remoting/base/auto_thread_task_runner.h
@@ -0,0 +1,87 @@
+// 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.
+
+#ifndef REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_
+#define REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_
+
+#include "base/basictypes.h"
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/single_thread_task_runner.h"
+
+class MessageLoop;
+
+namespace remoting {
+
+// A wrapper around |SingleThreadTaskRunner| that provides automatic lifetime
+// management, by invoking a caller-supplied callback when no more references
+// remain, that can stop the wrapped task runner. The caller may also provide a
+// reference to a parent |AutoThreadTaskRunner| to express dependencies between
+// child and parent thread lifetimes.
+class AutoThreadTaskRunner : public base::SingleThreadTaskRunner {
+ public:
+ // Constructs an instance of |AutoThreadTaskRunner| wrapping |task_runner|.
+ // |stop_callback| is called (on arbitraty thread) when the last reference to
+ // the object is dropped.
+ explicit AutoThreadTaskRunner(
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner);
+ AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ const base::Closure& stop_callback);
+
+ // TODO(alexeypa): Remove the |parent| reference once Thread class supports
+ // stopping the thread's message loop and joining the thread separately.
+ // See http://crbug.com/145856.
+ //
+ // Background: currently the only legitimate way of stopping a thread is to
+ // call Thread::Stop() from the same thread that started it. Thread::Stop()
+ // will stop the thread message loop by posting a private task and join
+ // the thread. Thread::Stop() verifies that it is called from the right thread
+ // and that the message loop has been stopped by the private task mentioned
+ // above.
+ //
+ // Due to NPAPI/PPAPI limitations tasks cannot be posted to the main message
+ // loop when the host plugin is being shut down. This presents a challenge
+ // since Thread::Stop() cannot be called from an arbirtary thread. To work
Wez 2012/09/04 18:09:38 typo: arbitrary
alexeypa (please no reviews) 2012/09/04 18:42:53 Done.
+ // this around we keep a reference to the parent task runner
Wez 2012/09/04 18:09:38 typo: around this
alexeypa (please no reviews) 2012/09/04 18:42:53 Done.
+ // (typically the one that started the thread) to keep it alive while
+ // the worker thread is in use. Thread::Stop() to stop the worker thread is
Wez 2012/09/04 18:09:38 nit: Reword "Thread::Stop() is called to stop the
alexeypa (please no reviews) 2012/09/04 18:42:53 Done.
+ // called when the parent task runner exits.
+ AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ scoped_refptr<AutoThreadTaskRunner> parent);
+ AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ scoped_refptr<AutoThreadTaskRunner> parent,
+ const base::Closure& stop_callback);
+
+ // SingleThreadTaskRunner implementation
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ const base::Closure& task,
+ base::TimeDelta delay) OVERRIDE;
+ virtual bool PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ const base::Closure& task,
+ base::TimeDelta delay) OVERRIDE;
+ virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
+
+ private:
+ friend class base::DeleteHelper<AutoThreadTaskRunner>;
+
+ virtual ~AutoThreadTaskRunner();
+
+ // An reference to the parent message loop to keep it alive while
+ // |task_runner_| is running.
+ scoped_refptr<AutoThreadTaskRunner> parent_;
+
+ // This callback quits |task_runner_|. It can be run on any thread.
+ base::Closure stop_callback_;
+
+ // The wrapped task runner.
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(AutoThreadTaskRunner);
+};
+
+} // namespace remoting
+
+#endif // REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_
« no previous file with comments | « no previous file | remoting/base/auto_thread_task_runner.cc » ('j') | remoting/base/auto_thread_task_runner_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698