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