OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); | 78 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); |
79 return threads; | 79 return threads; |
80 } | 80 } |
81 | 81 |
82 unsigned WorkerThread::workerThreadCount() | 82 unsigned WorkerThread::workerThreadCount() |
83 { | 83 { |
84 MutexLocker lock(threadSetMutex()); | 84 MutexLocker lock(threadSetMutex()); |
85 return workerThreads().size(); | 85 return workerThreads().size(); |
86 } | 86 } |
87 | 87 |
| 88 class WorkerThreadCancelableTask FINAL : public ExecutionContextTask { |
| 89 WTF_MAKE_NONCOPYABLE(WorkerThreadCancelableTask); WTF_MAKE_FAST_ALLOCATED; |
| 90 public: |
| 91 static PassOwnPtr<WorkerThreadCancelableTask> create(const Closure& closure) |
| 92 { |
| 93 return adoptPtr(new WorkerThreadCancelableTask(closure)); |
| 94 } |
| 95 |
| 96 virtual void performTask(ExecutionContext*) OVERRIDE |
| 97 { |
| 98 if (!m_taskCanceled) |
| 99 m_closure(); |
| 100 } |
| 101 |
| 102 void cancelTask() { m_taskCanceled = true; } |
| 103 |
| 104 private: |
| 105 explicit WorkerThreadCancelableTask(const Closure& closure) |
| 106 : m_closure(closure) |
| 107 , m_taskCanceled(false) |
| 108 { } |
| 109 |
| 110 Closure m_closure; |
| 111 bool m_taskCanceled; |
| 112 }; |
| 113 |
88 class WorkerSharedTimer : public SharedTimer { | 114 class WorkerSharedTimer : public SharedTimer { |
89 public: | 115 public: |
90 explicit WorkerSharedTimer(WorkerThread* workerThread) | 116 explicit WorkerSharedTimer(WorkerThread* workerThread) |
91 : m_workerThread(workerThread) | 117 : m_workerThread(workerThread) |
92 , m_nextFireTime(0.0) | 118 , m_nextFireTime(0.0) |
93 , m_running(false) | 119 , m_running(false) |
| 120 , m_lastQueuedTask(0) |
94 { } | 121 { } |
95 | 122 |
96 typedef void (*SharedTimerFunction)(); | 123 typedef void (*SharedTimerFunction)(); |
97 virtual void setFiredFunction(SharedTimerFunction func) | 124 virtual void setFiredFunction(SharedTimerFunction func) |
98 { | 125 { |
99 m_sharedTimerFunction = func; | 126 m_sharedTimerFunction = func; |
100 if (!m_sharedTimerFunction) | 127 if (!m_sharedTimerFunction) |
101 m_nextFireTime = 0.0; | 128 m_nextFireTime = 0.0; |
102 } | 129 } |
103 | 130 |
104 virtual void setFireInterval(double interval) | 131 virtual void setFireInterval(double interval) |
105 { | 132 { |
106 ASSERT(m_sharedTimerFunction); | 133 ASSERT(m_sharedTimerFunction); |
107 | 134 |
108 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of | 135 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of |
109 // why ceil is used in the interval calculation. | 136 // why ceil is used in the interval calculation. |
110 int64 delay = static_cast<int64>(ceil(interval * 1000)); | 137 int64 delay = static_cast<int64>(ceil(interval * 1000)); |
111 | 138 |
112 if (delay < 0) { | 139 if (delay < 0) { |
113 delay = 0; | 140 delay = 0; |
114 m_nextFireTime = 0.0; | 141 m_nextFireTime = 0.0; |
115 } | 142 } |
116 | 143 |
117 m_running = true; | 144 m_running = true; |
118 m_nextFireTime = currentTime() + interval; | 145 m_nextFireTime = currentTime() + interval; |
119 m_workerThread->postDelayedTask(createSameThreadTask(&WorkerSharedTimer:
:OnTimeout, this), delay); | 146 |
| 147 if (m_lastQueuedTask) |
| 148 m_lastQueuedTask->cancelTask(); |
| 149 |
| 150 // Now queue the task as a cancellable one. |
| 151 m_lastQueuedTask = WorkerThreadCancelableTask::create(bind(&WorkerShared
Timer::OnTimeout, this)).leakPtr(); |
| 152 m_workerThread->postDelayedTask(adoptPtr(m_lastQueuedTask), delay); |
120 } | 153 } |
121 | 154 |
122 virtual void stop() | 155 virtual void stop() |
123 { | 156 { |
124 m_running = false; | 157 m_running = false; |
| 158 m_lastQueuedTask = 0; |
125 } | 159 } |
126 | 160 |
127 double nextFireTime() { return m_nextFireTime; } | 161 double nextFireTime() { return m_nextFireTime; } |
128 | 162 |
129 private: | 163 private: |
130 void OnTimeout() | 164 void OnTimeout() |
131 { | 165 { |
132 ASSERT(m_workerThread->workerGlobalScope()); | 166 ASSERT(m_workerThread->workerGlobalScope()); |
| 167 |
| 168 m_lastQueuedTask = 0; |
| 169 |
133 if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalS
cope()->isClosing()) | 170 if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalS
cope()->isClosing()) |
134 m_sharedTimerFunction(); | 171 m_sharedTimerFunction(); |
135 } | 172 } |
136 | 173 |
137 WorkerThread* m_workerThread; | 174 WorkerThread* m_workerThread; |
138 SharedTimerFunction m_sharedTimerFunction; | 175 SharedTimerFunction m_sharedTimerFunction; |
139 double m_nextFireTime; | 176 double m_nextFireTime; |
140 bool m_running; | 177 bool m_running; |
| 178 WorkerThreadCancelableTask* m_lastQueuedTask; |
141 }; | 179 }; |
142 | 180 |
143 class WorkerThreadTask : public blink::WebThread::Task { | 181 class WorkerThreadTask : public blink::WebThread::Task { |
144 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED; | 182 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED; |
145 public: | 183 public: |
146 static PassOwnPtr<WorkerThreadTask> create(const WorkerThread& workerThread,
PassOwnPtr<ExecutionContextTask> task, bool isInstrumented) | 184 static PassOwnPtr<WorkerThreadTask> create(const WorkerThread& workerThread,
PassOwnPtr<ExecutionContextTask> task, bool isInstrumented) |
147 { | 185 { |
148 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented)
); | 186 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented)
); |
149 } | 187 } |
150 | 188 |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 517 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
480 } | 518 } |
481 | 519 |
482 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) | 520 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) |
483 { | 521 { |
484 MutexLocker locker(m_workerInspectorControllerMutex); | 522 MutexLocker locker(m_workerInspectorControllerMutex); |
485 m_workerInspectorController = workerInspectorController; | 523 m_workerInspectorController = workerInspectorController; |
486 } | 524 } |
487 | 525 |
488 } // namespace blink | 526 } // namespace blink |
OLD | NEW |