OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #if ENABLE(WORKERS) | 33 #if ENABLE(WORKERS) |
34 | 34 |
| 35 #include "ScriptExecutionContext.h" |
| 36 #include "ThreadGlobalData.h" |
| 37 #include "ThreadTimers.h" |
35 #include "WorkerRunLoop.h" | 38 #include "WorkerRunLoop.h" |
36 #include "WorkerContext.h" | 39 #include "WorkerContext.h" |
37 #include "WorkerTask.h" | |
38 #include "WorkerThread.h" | 40 #include "WorkerThread.h" |
39 | 41 |
40 namespace WebCore { | 42 namespace WebCore { |
41 | 43 |
| 44 class WorkerSharedTimer : public SharedTimer { |
| 45 public: |
| 46 WorkerSharedTimer() |
| 47 : m_sharedTimerFunction(0) |
| 48 , m_nextFireTime(0) |
| 49 { |
| 50 } |
| 51 |
| 52 // SharedTimer interface. |
| 53 virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction =
function; } |
| 54 virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; } |
| 55 virtual void stop() { m_nextFireTime = 0; } |
| 56 |
| 57 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; } |
| 58 double fireTime() { return m_nextFireTime; } |
| 59 void fire() { m_sharedTimerFunction(); } |
| 60 |
| 61 private: |
| 62 void (*m_sharedTimerFunction)(); |
| 63 double m_nextFireTime; |
| 64 }; |
| 65 |
| 66 WorkerRunLoop::WorkerRunLoop() |
| 67 : m_sharedTimer(new WorkerSharedTimer) |
| 68 { |
| 69 } |
| 70 |
| 71 WorkerRunLoop::~WorkerRunLoop() |
| 72 { |
| 73 } |
| 74 |
42 void WorkerRunLoop::run(WorkerContext* context) | 75 void WorkerRunLoop::run(WorkerContext* context) |
43 { | 76 { |
44 ASSERT(context); | 77 ASSERT(context); |
45 ASSERT(context->thread()); | 78 ASSERT(context->thread()); |
46 ASSERT(context->thread()->threadID() == currentThread()); | 79 ASSERT(context->thread()->threadID() == currentThread()); |
47 | 80 |
| 81 threadGlobalData().threadTimers().setSharedTimer(m_sharedTimer.get()); |
| 82 |
48 while (true) { | 83 while (true) { |
49 RefPtr<WorkerTask> task; | 84 RefPtr<ScriptExecutionContext::Task> task; |
50 if (!m_messageQueue.waitForMessage(task)) | 85 MessageQueueWaitResult result; |
| 86 |
| 87 if (m_sharedTimer->isActive()) |
| 88 result = m_messageQueue.waitForMessageTimed(task, m_sharedTimer->fir
eTime()); |
| 89 else |
| 90 result = (m_messageQueue.waitForMessage(task) ? MessageQueueMessageR
eceived : MessageQueueTerminated); |
| 91 |
| 92 if (result == MessageQueueTerminated) |
51 break; | 93 break; |
| 94 |
| 95 if (result == MessageQueueMessageReceived) |
| 96 task->performTask(context); |
| 97 else { |
| 98 ASSERT(result == MessageQueueTimeout); |
| 99 m_sharedTimer->fire(); |
| 100 } |
| 101 } |
52 | 102 |
53 task->performTask(context); | 103 threadGlobalData().threadTimers().setSharedTimer(0); |
54 } | |
55 } | 104 } |
56 | 105 |
57 void WorkerRunLoop::terminate() | 106 void WorkerRunLoop::terminate() |
58 { | 107 { |
59 m_messageQueue.kill(); | 108 m_messageQueue.kill(); |
60 } | 109 } |
61 | 110 |
62 void WorkerRunLoop::postTask(PassRefPtr<WorkerTask> task) | 111 void WorkerRunLoop::postTask(PassRefPtr<ScriptExecutionContext::Task> task) |
63 { | 112 { |
64 m_messageQueue.append(task); | 113 m_messageQueue.append(task); |
65 } | 114 } |
66 | 115 |
67 } // namespace WebCore | 116 } // namespace WebCore |
68 | 117 |
69 #endif // ENABLE(WORKERS) | 118 #endif // ENABLE(WORKERS) |
OLD | NEW |