| 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 #include "config.h" | |
| 6 #include "ccthread_impl.h" | |
| 7 | |
| 8 #include "cc/completion_event.h" | |
| 9 #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h" | |
| 10 #include "third_party/WebKit/Source/Platform/chromium/public/WebThread.h" | |
| 11 | |
| 12 using cc::Thread; | |
| 13 using cc::CompletionEvent; | |
| 14 | |
| 15 namespace WebKit { | |
| 16 | |
| 17 // Task that, when runs, places the current thread ID into the provided | |
| 18 // pointer and signals a completion event. | |
| 19 // | |
| 20 // Does not provide a PassOwnPtr<GetThreadIDTask>::create method because | |
| 21 // the resulting object is always handed into a WebThread, which does not unders
tand | |
| 22 // PassOwnPtrs. | |
| 23 class GetThreadIDTask : public WebThread::Task { | |
| 24 public: | |
| 25 GetThreadIDTask(base::PlatformThreadId* result, CompletionEvent* completion) | |
| 26 : m_completion(completion) | |
| 27 , m_result(result) { } | |
| 28 | |
| 29 virtual ~GetThreadIDTask() { } | |
| 30 | |
| 31 virtual void run() | |
| 32 { | |
| 33 *m_result = base::PlatformThread::CurrentId(); | |
| 34 m_completion->signal(); | |
| 35 } | |
| 36 | |
| 37 private: | |
| 38 CompletionEvent* m_completion; | |
| 39 base::PlatformThreadId* m_result; | |
| 40 }; | |
| 41 | |
| 42 // General adapter from a Thread::Task to a WebThread::Task. | |
| 43 class ThreadTaskAdapter : public WebThread::Task { | |
| 44 public: | |
| 45 explicit ThreadTaskAdapter(PassOwnPtr<Thread::Task> task) : m_task(task) { } | |
| 46 | |
| 47 virtual ~ThreadTaskAdapter() { } | |
| 48 | |
| 49 virtual void run() | |
| 50 { | |
| 51 m_task->performTask(); | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 OwnPtr<Thread::Task> m_task; | |
| 56 }; | |
| 57 | |
| 58 scoped_ptr<Thread> CCThreadImpl::createForCurrentThread() | |
| 59 { | |
| 60 return scoped_ptr<Thread>(new CCThreadImpl(Platform::current()->currentThrea
d(), true)).Pass(); | |
| 61 } | |
| 62 | |
| 63 scoped_ptr<Thread> CCThreadImpl::createForDifferentThread(WebThread* thread) | |
| 64 { | |
| 65 return scoped_ptr<Thread>(new CCThreadImpl(thread, false)).Pass(); | |
| 66 } | |
| 67 | |
| 68 CCThreadImpl::~CCThreadImpl() | |
| 69 { | |
| 70 } | |
| 71 | |
| 72 void CCThreadImpl::postTask(PassOwnPtr<Thread::Task> task) | |
| 73 { | |
| 74 m_thread->postTask(new ThreadTaskAdapter(task)); | |
| 75 } | |
| 76 | |
| 77 void CCThreadImpl::postDelayedTask(PassOwnPtr<Thread::Task> task, long long dela
yMs) | |
| 78 { | |
| 79 m_thread->postDelayedTask(new ThreadTaskAdapter(task), delayMs); | |
| 80 } | |
| 81 | |
| 82 base::PlatformThreadId CCThreadImpl::threadID() const | |
| 83 { | |
| 84 return m_threadID; | |
| 85 } | |
| 86 | |
| 87 CCThreadImpl::CCThreadImpl(WebThread* thread, bool currentThread) | |
| 88 : m_thread(thread) | |
| 89 { | |
| 90 if (currentThread) { | |
| 91 m_threadID = base::PlatformThread::CurrentId(); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 // Get the threadId for the newly-created thread by running a task | |
| 96 // on that thread, blocking on the result. | |
| 97 CompletionEvent completion; | |
| 98 m_thread->postTask(new GetThreadIDTask(&m_threadID, &completion)); | |
| 99 completion.wait(); | |
| 100 } | |
| 101 | |
| 102 } // namespace WebKit | |
| OLD | NEW |