Chromium Code Reviews| 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 #ifndef REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_ | |
| 6 #define REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 | |
| 14 class MessageLoop; | |
| 15 | |
| 16 namespace remoting { | |
| 17 | |
| 18 // A wrapper around |SingleThreadTaskRunner| that provides automatic lifetime | |
| 19 // management, by invoking a caller-supplied callback when no more references | |
| 20 // remain, that can stop the wrapped task runner. The caller may also provide a | |
| 21 // reference to a parent |AutoThreadTaskRunner| to express dependencies between | |
| 22 // child and parent thread lifetimes. | |
| 23 class AutoThreadTaskRunner : public base::SingleThreadTaskRunner { | |
| 24 public: | |
| 25 // Constructs an instance of |AutoThreadTaskRunner| wrapping |task_runner|. | |
| 26 // |stop_callback| is called (on arbitraty thread) when the last reference to | |
| 27 // the object is dropped. | |
| 28 explicit AutoThreadTaskRunner( | |
| 29 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 30 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 31 const base::Closure& stop_callback); | |
| 32 | |
| 33 // TODO(alexeypa): Remove the |parent| reference once Thread class supports | |
| 34 // stopping the thread's message loop and joining the thread separately. | |
| 35 // See http://crbug.com/145856. | |
| 36 // | |
| 37 // Background: currently the only legitimate way of stopping a thread is to | |
| 38 // call Thread::Stop() from the same thread that started it. Thread::Stop() | |
| 39 // will stop the thread message loop by posting a private task and join | |
| 40 // the thread. Thread::Stop() verifies that it is called from the right thread | |
| 41 // and that the message loop has been stopped by the private task mentioned | |
| 42 // above. | |
| 43 // | |
| 44 // Due to NPAPI/PPAPI limitations tasks cannot be posted to the main message | |
| 45 // loop when the host plugin is being shut down. This presents a challenge | |
| 46 // 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.
| |
| 47 // 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.
| |
| 48 // (typically the one that started the thread) to keep it alive while | |
| 49 // 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.
| |
| 50 // called when the parent task runner exits. | |
| 51 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 52 scoped_refptr<AutoThreadTaskRunner> parent); | |
| 53 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 54 scoped_refptr<AutoThreadTaskRunner> parent, | |
| 55 const base::Closure& stop_callback); | |
| 56 | |
| 57 // SingleThreadTaskRunner implementation | |
| 58 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 59 const base::Closure& task, | |
| 60 base::TimeDelta delay) OVERRIDE; | |
| 61 virtual bool PostNonNestableDelayedTask( | |
| 62 const tracked_objects::Location& from_here, | |
| 63 const base::Closure& task, | |
| 64 base::TimeDelta delay) OVERRIDE; | |
| 65 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | |
| 66 | |
| 67 private: | |
| 68 friend class base::DeleteHelper<AutoThreadTaskRunner>; | |
| 69 | |
| 70 virtual ~AutoThreadTaskRunner(); | |
| 71 | |
| 72 // An reference to the parent message loop to keep it alive while | |
| 73 // |task_runner_| is running. | |
| 74 scoped_refptr<AutoThreadTaskRunner> parent_; | |
| 75 | |
| 76 // This callback quits |task_runner_|. It can be run on any thread. | |
| 77 base::Closure stop_callback_; | |
| 78 | |
| 79 // The wrapped task runner. | |
| 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(AutoThreadTaskRunner); | |
| 83 }; | |
| 84 | |
| 85 } // namespace remoting | |
| 86 | |
| 87 #endif // REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_ | |
| OLD | NEW |