Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | 5 #ifndef REMOTING_BASE_SCOPED_THREAD_PROXY_H_ |
| 6 #define REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | 6 #define REMOTING_BASE_SCOPED_THREAD_PROXY_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | |
| 8 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
| 9 #include "base/message_loop_proxy.h" | 10 #include "base/memory/ref_counted.h" |
| 11 | |
| 12 namespace base { | |
| 13 class SingleThreadTaskRunner; | |
| 14 } // namespace base | |
| 15 | |
| 16 namespace tracked_objects { | |
| 17 class Location; | |
| 18 } // namespace tracked_objects | |
| 10 | 19 |
| 11 namespace remoting { | 20 namespace remoting { |
| 12 | 21 |
| 13 // ScopedThreadProxy is proxy for message loops that cancels all | 22 // ScopedThreadProxy is a task runner proxy that cancels all pending |
|
Wez
2012/05/26 00:18:40
nit: We should consider renaming this ScopedTaskRu
Sergey Ulanov
2012/05/30 01:07:07
Ok, I will hold this CL until you land yours.
Wez
2012/05/30 20:56:45
CL 10454018 has landed; let me know when you're re
| |
| 14 // pending tasks when it is destroyed. It can be used to post tasks | 23 // tasks when it is destroyed. It can be used to post tasks for a |
| 15 // for a non-refcounted object. Must be deleted on the thread it | 24 // non-refcounted object. Must be deleted on the thread it belongs to. |
| 16 // belongs to. | |
| 17 // | |
| 18 // | 25 // |
| 19 // The main difference from WeakPtr<> is that this class can be used safely to | 26 // The main difference from WeakPtr<> is that this class can be used safely to |
| 20 // post tasks from different threads. | 27 // post tasks from different threads. |
| 21 // It is similar to WeakHandle<> used in sync: the main difference is | 28 // It is similar to WeakHandle<> used in sync: the main difference is |
| 22 // that WeakHandle<> creates closures itself, while ScopedThreadProxy | 29 // that WeakHandle<> creates closures itself, while ScopedThreadProxy |
| 23 // accepts base::Closure instances which caller needs to create using | 30 // accepts base::Closure instances which caller needs to create using |
| 24 // base::Bind(). | 31 // base::Bind(). |
| 25 // | 32 // |
| 26 // TODO(sergeyu): Potentially we could use WeakHandle<> instead of | 33 // TODO(sergeyu): Potentially we could use WeakHandle<> instead of |
| 27 // this class. Consider migrating to WeakHandle<> when it is moved to | 34 // this class. Consider migrating to WeakHandle<> when it is moved to |
| 28 // src/base and support for delayed tasks is implemented. | 35 // src/base and support for delayed tasks is implemented. |
| 29 // | 36 // |
| 30 // Usage: | 37 // Usage: |
| 31 // class MyClass { | 38 // class MyClass { |
| 32 // public: | 39 // public: |
| 33 // MyClass() | 40 // MyClass() |
| 34 // : thread_proxy_(base::MessageLoopProxy::current()) {} | 41 // : thread_proxy_(base::ThreadTaskRunnerHandle::Get()) {} |
|
Wez
2012/05/26 00:18:40
Where are you getting ThreadTaskRunnerHandle from?
Sergey Ulanov
2012/05/30 01:07:07
This is a comment, so I don't need an include for
| |
| 35 // | 42 // |
| 36 // // Always called on the thread on which this object was created. | 43 // // Always called on the thread on which this object was created. |
| 37 // void NonThreadSafeMethod() {} | 44 // void NonThreadSafeMethod() {} |
| 38 // | 45 // |
| 39 // // Can be called on any thread. | 46 // // Can be called on any thread. |
| 40 // void ThreadSafeMethod() { | 47 // void ThreadSafeMethod() { |
| 41 // thread_proxy_.PostTask(FROM_HERE, base::Bind( | 48 // thread_proxy_.PostTask(FROM_HERE, base::Bind( |
| 42 // &MyClass::NonThreadSafeMethod, base::Unretained(this))); | 49 // &MyClass::NonThreadSafeMethod, base::Unretained(this))); |
| 43 // } | 50 // } |
| 44 // | 51 // |
| 45 // private: | 52 // private: |
| 46 // ScopedThreadProxy thread_proxy_; | 53 // ScopedThreadProxy thread_proxy_; |
| 47 // }; | 54 // }; |
| 48 class ScopedThreadProxy { | 55 class ScopedThreadProxy { |
| 49 public: | 56 public: |
| 50 ScopedThreadProxy(base::MessageLoopProxy* message_loop); | 57 ScopedThreadProxy(base::SingleThreadTaskRunner* task_runner); |
| 51 ~ScopedThreadProxy(); | 58 ~ScopedThreadProxy(); |
| 52 | 59 |
| 53 void PostTask(const tracked_objects::Location& from_here, | 60 void PostTask(const tracked_objects::Location& from_here, |
| 54 const base::Closure& closure); | 61 const base::Closure& closure); |
| 55 void PostDelayedTask(const tracked_objects::Location& from_here, | 62 void PostDelayedTask(const tracked_objects::Location& from_here, |
| 56 const base::Closure& closure, | 63 const base::Closure& closure, |
| 57 int64 delay_ms); | 64 int64 delay_ms); |
| 58 | 65 |
| 59 // Cancels all tasks posted via this proxy. Must be called on the | 66 // Cancels all tasks posted via this proxy. Must be called on the |
| 60 // thread this object belongs to. | 67 // thread this object belongs to. |
| 61 void Detach(); | 68 void Detach(); |
| 62 | 69 |
| 63 private: | 70 private: |
| 64 class Core; | 71 class Core; |
| 65 | 72 |
| 66 scoped_refptr<Core> core_; | 73 scoped_refptr<Core> core_; |
| 67 | 74 |
| 68 DISALLOW_COPY_AND_ASSIGN(ScopedThreadProxy); | 75 DISALLOW_COPY_AND_ASSIGN(ScopedThreadProxy); |
| 69 }; | 76 }; |
| 70 | 77 |
| 71 } // namespace remoting | 78 } // namespace remoting |
| 72 | 79 |
| 73 #endif // REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | 80 #endif // REMOTING_BASE_SCOPED_THREAD_PROXY_H_ |
| OLD | NEW |