Chromium Code Reviews| Index: remoting/base/auto_thread.h |
| diff --git a/remoting/base/auto_thread.h b/remoting/base/auto_thread.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11895781e9b2beabe9996a62df2498d0835d64e6 |
| --- /dev/null |
| +++ b/remoting/base/auto_thread.h |
| @@ -0,0 +1,65 @@ |
| +// 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_H_ |
| +#define REMOTING_BASE_AUTO_THREAD_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/threading/thread.h" |
| + |
| +class MessageLoop; |
| + |
| +namespace remoting { |
| + |
| +// This class provides automatic life time management for a thread running |
| +// a message loop. The thread is stopped when the last reference to |AutoThread| |
| +// is dropped. |AutoThread| is deleted from a a task running on the parent |
| +// message loop. |
| +class AutoThread : public base::SingleThreadTaskRunner { |
| + public: |
| + AutoThread( |
| + const char* name, |
| + scoped_refptr<base::SingleThreadTaskRunner> parent); |
|
Wez
2012/08/24 21:30:49
I don't really see the point of this class; why ca
alexeypa (please no reviews)
2012/08/27 21:19:40
MessageLoop and Thread should be stopped different
|
| + |
| + // A backdoor to the enclosed message loop for the cases when an external |
| + // interface does not take |base::SingleThreadTaskRunner|. |
| + MessageLoop* message_loop() { return thread_.message_loop(); } |
| + |
| + // Wrappers around the corresponding methods of |base::Thread|. |
| + bool Start(); |
| + bool StartWithOptions(const base::Thread::Options& options); |
| + |
| + // 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; |
| + |
| + protected: |
| + // Stops the thread and deletes itself. |
| + virtual void OnDestruct() const OVERRIDE; |
| + |
| + private: |
| + virtual ~AutoThread(); |
| + |
| + friend class base::DeleteHelper<AutoThread>; |
| + |
| + // The parent task runner that will be used to destroy |this|. |
| + scoped_refptr<base::SingleThreadTaskRunner> parent_; |
| + |
| + // The wrapped thread object. |
| + base::Thread thread_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AutoThread); |
| +}; |
| + |
| +} // namespace remoting |
| + |
| +#endif // REMOTING_BASE_AUTO_THREAD_H_ |