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 // This class provides automatic life time management for | |
| 19 // |SingleThreadTaskRunner|. The task runner is stopped when the last reference | |
| 20 // to |AutoThreadTaskRunner| is dropped. | |
| 
 
Wez
2012/08/30 23:56:43
This still isn't true, though; the task runner is
 
alexeypa (please no reviews)
2012/08/31 19:57:36
Done.
 
 | |
| 21 class AutoThreadTaskRunner : public base::SingleThreadTaskRunner { | |
| 
 
Sergey Ulanov
2012/08/31 01:25:18
can you please add some unittests for this class?
 
alexeypa (please no reviews)
2012/08/31 19:57:36
Done.
 
 | |
| 22 public: | |
| 23 // Constructs an instance of |AutoThreadTaskRunner| wrapping |task_runner|. | |
| 24 // |stop_callback| is called (on arbitraty thread) when the last reference to | |
| 25 // the object is dropped. | |
| 26 explicit AutoThreadTaskRunner( | |
| 27 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 28 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 29 const base::Closure& stop_callback); | |
| 30 | |
| 31 // TODO(alexeypa): Remove the |parent| reference once Thread class supports | |
| 32 // stopping the thread's message loop and joining the thread separately. | |
| 
 
Wez
2012/08/30 23:56:43
nit: Add a reference to crbug.com/145856 here?
 
alexeypa (please no reviews)
2012/08/31 19:57:36
Done.
 
 | |
| 33 // | |
| 34 // Background: currently the only legitimate way of stopping a thread is to | |
| 35 // call Thread::Stop() from the same thread that started it. Thread::Stop() | |
| 36 // will stop the thread message loop by posting a private task and join | |
| 37 // the thread. Thread::Stop() verifies that it is called from the right thread | |
| 38 // and that the message loop has been stopped by the private task mentioned | |
| 39 // above. | |
| 
 
Wez
2012/08/30 23:56:43
nit: You only need the first sentence of this comm
 
alexeypa (please no reviews)
2012/08/31 19:57:36
I think it deserves a verbose comment, given the n
 
 | |
| 40 // | |
| 41 // Until Thread::Stop() is split in two separate operations, it should not be | |
| 42 // called while any objects use it. This is achieved by keeping a reference to | |
| 43 // the parent thread's task runner. Since Thread::Stop() is called after | |
| 44 // exiting the parent loop, keeping a reference to it gives enough guaratee. | |
| 
 
Wez
2012/08/30 23:56:43
typo: guarantee
 
alexeypa (please no reviews)
2012/08/31 19:57:36
Done.
 
 | |
| 45 // |parent| should itself be an AutoThreadTaskRunner managed thread, to | |
| 46 // guarantee that it remains alive long enough to perform the Stop(). | |
| 
 
Wez
2012/08/30 23:56:43
Suggest rewording:
"To work around this we require
 
alexeypa (please no reviews)
2012/08/31 19:57:36
This sounds confusing to me. Check out my variant.
 
 | |
| 47 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 48 scoped_refptr<base::SingleThreadTaskRunner> parent); | |
| 
 
Wez
2012/08/30 23:56:43
Do we ever need to pass a non-AutoThreadTaskRunner
 
alexeypa (please no reviews)
2012/08/31 19:57:36
Done.
 
 | |
| 49 AutoThreadTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 50 scoped_refptr<base::SingleThreadTaskRunner> parent, | |
| 51 const base::Closure& stop_callback); | |
| 52 | |
| 53 // SingleThreadTaskRunner implementation | |
| 54 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 55 const base::Closure& task, | |
| 56 base::TimeDelta delay) OVERRIDE; | |
| 57 virtual bool PostNonNestableDelayedTask( | |
| 58 const tracked_objects::Location& from_here, | |
| 59 const base::Closure& task, | |
| 60 base::TimeDelta delay) OVERRIDE; | |
| 61 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | |
| 62 | |
| 63 private: | |
| 64 friend class base::DeleteHelper<AutoThreadTaskRunner>; | |
| 65 | |
| 66 virtual ~AutoThreadTaskRunner(); | |
| 67 | |
| 68 // An reference to the parent message loop to keep it alive while | |
| 69 // |task_runner_| is running. | |
| 70 scoped_refptr<base::SingleThreadTaskRunner> parent_; | |
| 71 | |
| 72 // This callback quits |task_runner_|. It can be run on any thread. | |
| 73 base::Closure stop_callback_; | |
| 74 | |
| 75 // The wrapped task runner. | |
| 76 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(AutoThreadTaskRunner); | |
| 79 }; | |
| 80 | |
| 81 } // namespace remoting | |
| 82 | |
| 83 #endif // REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_ | |
| OLD | NEW |