Chromium Code Reviews| 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" | |
| 9 #include "base/callback.h" | |
| 10 #include "cc/thread.h" | |
| 11 #include "base/location.h" | |
| 8 #include "base/logging.h" | 12 #include "base/logging.h" |
| 9 #include "base/threading/platform_thread.h" | |
| 10 #include "cc/thread_task.h" | |
| 11 #include <wtf/OwnPtr.h> | |
| 12 #include <wtf/PassOwnPtr.h> | |
| 13 #include <wtf/ThreadSafeRefCounted.h> | |
| 14 | 13 |
| 15 namespace cc { | 14 namespace cc { |
| 16 | 15 |
| 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 | 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 |
| 18 // any point from the target thread after which no more tasks posted to the prox y will run. In other words, all | 17 // 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 | 18 // 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. | 19 // might die with tasks in flight. |
| 21 // | 20 // |
| 22 // The proxy must be created and shut down from the target thread, tasks may be posted from any thread. | 21 // The proxy must be created and shut down from the target thread, tasks may be posted from any thread. |
| 23 // | 22 // |
| 24 // Implementation note: Unlike ScopedRunnableMethodFactory in Chromium, pending tasks are not cancelled by actually | 23 // 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 | 24 // destroying the proxy. Instead each pending task holds a reference to the prox y to avoid maintaining an explicit |
| 26 // list of outstanding tasks. | 25 // list of outstanding tasks. |
| 27 class ScopedThreadProxy : public ThreadSafeRefCounted<ScopedThreadProxy> { | 26 class ScopedThreadProxy : public base::RefCounted<ScopedThreadProxy> { |
|
enne (OOO)
2012/10/29 17:40:42
Sanity checking: this doesn't need to be thread-sa
| |
| 28 public: | 27 public: |
| 29 static PassRefPtr<ScopedThreadProxy> create(Thread* targetThread) | 28 static scoped_refptr<ScopedThreadProxy> create(cc::Thread* targetThread) |
| 30 { | 29 { |
| 31 DCHECK(base::PlatformThread::CurrentId() == targetThread->threadID()); | 30 DCHECK(targetThread->belongsToCurrentThread()); |
| 32 return adoptRef(new ScopedThreadProxy(targetThread)); | 31 return make_scoped_refptr(new ScopedThreadProxy(targetThread)); |
| 33 } | 32 } |
| 34 | 33 |
| 35 ~ScopedThreadProxy(); | |
| 36 | |
| 37 // Can be called from any thread. Posts a task to the target thread that run s unless | 34 // Can be called from any thread. Posts a task to the target thread that run s unless |
| 38 // shutdown() is called before it runs. | 35 // shutdown() is called before it runs. |
| 39 void postTask(PassOwnPtr<Thread::Task> task) | 36 void postTask(const tracked_objects::Location& location, base::Closure cb); |
| 40 { | |
| 41 ref(); | |
| 42 m_targetThread->postTask(createThreadTask(this, &ScopedThreadProxy::runT askIfNotShutdown, task)); | |
| 43 } | |
| 44 | 37 |
| 45 void shutdown() | 38 void shutdown(); |
| 46 { | |
| 47 DCHECK(base::PlatformThread::CurrentId() == m_targetThread->threadID()); | |
| 48 DCHECK(!m_shutdown); | |
| 49 m_shutdown = true; | |
| 50 } | |
| 51 | 39 |
| 52 private: | 40 private: |
| 53 explicit ScopedThreadProxy(Thread* targetThread); | 41 explicit ScopedThreadProxy(cc::Thread* targetThread); |
| 42 friend class base::RefCounted<ScopedThreadProxy>; | |
| 43 ~ScopedThreadProxy(); | |
| 54 | 44 |
| 55 void runTaskIfNotShutdown(PassOwnPtr<Thread::Task> popTask) | 45 void runTaskIfNotShutdown(base::Closure cb); |
| 56 { | |
| 57 OwnPtr<Thread::Task> task = popTask; | |
| 58 // If our shutdown flag is set, it's possible that m_targetThread has al ready been destroyed so don't | |
| 59 // touch it. | |
| 60 if (m_shutdown) { | |
| 61 deref(); | |
| 62 return; | |
| 63 } | |
| 64 DCHECK(base::PlatformThread::CurrentId() == m_targetThread->threadID()); | |
| 65 task->performTask(); | |
| 66 deref(); | |
| 67 } | |
| 68 | 46 |
| 69 Thread* m_targetThread; | 47 cc::Thread* m_targetThread; |
| 70 bool m_shutdown; // Only accessed on the target thread | 48 bool m_shutdown; // Only accessed on the target thread |
| 71 }; | 49 }; |
| 72 | 50 |
| 73 } | 51 } |
| 74 | 52 |
| 75 #endif | 53 #endif |
| OLD | NEW |