OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 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 CCScopedThreadProxy_h | 5 #ifndef CCScopedThreadProxy_h |
6 #define CCScopedThreadProxy_h | 6 #define CCScopedThreadProxy_h |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "cc/cc_export.h" |
10 #include "cc/thread.h" | 11 #include "cc/thread.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 | 14 |
14 namespace cc { | 15 namespace cc { |
15 | 16 |
16 // This class is a proxy used to post tasks to an target thread from any other t
hread. The proxy may be shut down at | 17 // This class is a proxy used to post tasks to an target thread from any other t
hread. The proxy may be shut down at |
17 // any point from the target thread after which no more tasks posted to the prox
y will run. In other words, all | 18 // any point from the target thread after which no more tasks posted to the prox
y will run. In other words, all |
18 // tasks posted via a proxy are scoped to the lifecycle of the proxy. Use this w
hen posting tasks to an object that | 19 // tasks posted via a proxy are scoped to the lifecycle of the proxy. Use this w
hen posting tasks to an object that |
19 // might die with tasks in flight. | 20 // might die with tasks in flight. |
20 // | 21 // |
21 // The proxy must be created and shut down from the target thread, tasks may be
posted from any thread. | 22 // The proxy must be created and shut down from the target thread, tasks may be
posted from any thread. |
22 // | 23 // |
23 // Implementation note: Unlike ScopedRunnableMethodFactory in Chromium, pending
tasks are not cancelled by actually | 24 // Implementation note: Unlike ScopedRunnableMethodFactory in Chromium, pending
tasks are not cancelled by actually |
24 // destroying the proxy. Instead each pending task holds a reference to the prox
y to avoid maintaining an explicit | 25 // destroying the proxy. Instead each pending task holds a reference to the prox
y to avoid maintaining an explicit |
25 // list of outstanding tasks. | 26 // list of outstanding tasks. |
26 class ScopedThreadProxy : public base::RefCountedThreadSafe<ScopedThreadProxy> { | 27 class CC_EXPORT ScopedThreadProxy : public base::RefCountedThreadSafe<ScopedThre
adProxy> { |
27 public: | 28 public: |
28 static scoped_refptr<ScopedThreadProxy> create(cc::Thread* targetThread) | 29 static scoped_refptr<ScopedThreadProxy> create(cc::Thread* targetThread) |
29 { | 30 { |
30 DCHECK(targetThread->belongsToCurrentThread()); | 31 DCHECK(targetThread->belongsToCurrentThread()); |
31 return make_scoped_refptr(new ScopedThreadProxy(targetThread)); | 32 return make_scoped_refptr(new ScopedThreadProxy(targetThread)); |
32 } | 33 } |
33 | 34 |
34 // Can be called from any thread. Posts a task to the target thread that run
s unless | 35 // Can be called from any thread. Posts a task to the target thread that run
s unless |
35 // shutdown() is called before it runs. | 36 // shutdown() is called before it runs. |
36 void postTask(const tracked_objects::Location& location, base::Closure cb); | 37 void postTask(const tracked_objects::Location& location, base::Closure cb); |
37 | 38 |
38 void shutdown(); | 39 void shutdown(); |
39 | 40 |
40 private: | 41 private: |
41 explicit ScopedThreadProxy(cc::Thread* targetThread); | 42 explicit ScopedThreadProxy(cc::Thread* targetThread); |
42 friend class base::RefCountedThreadSafe<ScopedThreadProxy>; | 43 friend class base::RefCountedThreadSafe<ScopedThreadProxy>; |
43 ~ScopedThreadProxy(); | 44 ~ScopedThreadProxy(); |
44 | 45 |
45 void runTaskIfNotShutdown(base::Closure cb); | 46 void runTaskIfNotShutdown(base::Closure cb); |
46 | 47 |
47 cc::Thread* m_targetThread; | 48 cc::Thread* m_targetThread; |
48 bool m_shutdown; // Only accessed on the target thread | 49 bool m_shutdown; // Only accessed on the target thread |
49 }; | 50 }; |
50 | 51 |
51 } // namespace cc | 52 } // namespace cc |
52 | 53 |
53 #endif | 54 #endif |
OLD | NEW |