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_H_ |
| 6 #define REMOTING_BASE_AUTO_THREAD_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread.h" |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 // This class along with |scoped_refptr| provides automatic life time management |
| 16 // for a thread running a message loop. The thread is stopped on the parent's |
| 17 // message loop when the last |scoped_refptr| reference to |AutoThread| is |
| 18 // deleted. |
| 19 class AutoThread : public base::SingleThreadTaskRunner { |
| 20 public: |
| 21 AutoThread( |
| 22 const char* name, |
| 23 scoped_refptr<base::SingleThreadTaskRunner> parent); |
| 24 virtual ~AutoThread(); |
| 25 |
| 26 // Wrappers around the corresponding methods of |base::Thread|. |
| 27 bool Start(); |
| 28 bool StartWithOptions(const base::Thread::Options& options); |
| 29 |
| 30 // SingleThreadTaskRunner implementation |
| 31 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 32 const base::Closure& task, |
| 33 base::TimeDelta delay) OVERRIDE; |
| 34 virtual bool PostNonNestableDelayedTask( |
| 35 const tracked_objects::Location& from_here, |
| 36 const base::Closure& task, |
| 37 base::TimeDelta delay) OVERRIDE; |
| 38 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 39 |
| 40 protected: |
| 41 // Stops the thread and deletes itself. |
| 42 virtual void OnDestruct() const OVERRIDE; |
| 43 |
| 44 private: |
| 45 // Parent task runner that will be used to destroy |this|. |
| 46 scoped_refptr<base::SingleThreadTaskRunner> parent_; |
| 47 |
| 48 // Wrapper thread object. |
| 49 base::Thread thread_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(AutoThread); |
| 52 }; |
| 53 |
| 54 } // namespace remoting |
| 55 |
| 56 #endif // REMOTING_BASE_AUTO_THREAD_H_ |
OLD | NEW |