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 | |
114 class WorkerSharedTimer : public SharedTimer { | 88 class WorkerSharedTimer : public SharedTimer { |
115 public: | 89 public: |
116 explicit WorkerSharedTimer(WorkerThread* workerThread) | 90 explicit WorkerSharedTimer(WorkerThread* workerThread) |
117 : m_workerThread(workerThread) | 91 : m_workerThread(workerThread) |
118 , m_nextFireTime(0.0) | 92 , m_nextFireTime(0.0) |
119 , m_running(false) | 93 , m_running(false) |
120 , m_lastQueuedTask(0) | |
121 { } | 94 { } |
122 | 95 |
123 typedef void (*SharedTimerFunction)(); | 96 typedef void (*SharedTimerFunction)(); |
124 virtual void setFiredFunction(SharedTimerFunction func) | 97 virtual void setFiredFunction(SharedTimerFunction func) |
125 { | 98 { |
126 m_sharedTimerFunction = func; | 99 m_sharedTimerFunction = func; |
127 if (!m_sharedTimerFunction) | 100 if (!m_sharedTimerFunction) |
128 m_nextFireTime = 0.0; | 101 m_nextFireTime = 0.0; |
129 } | 102 } |
130 | 103 |
131 virtual void setFireInterval(double interval) | 104 virtual void setFireInterval(double interval) |
132 { | 105 { |
133 ASSERT(m_sharedTimerFunction); | 106 ASSERT(m_sharedTimerFunction); |
134 | 107 |
135 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of | 108 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of |
136 // why ceil is used in the interval calculation. | 109 // why ceil is used in the interval calculation. |
137 int64 delay = static_cast<int64>(ceil(interval * 1000)); | 110 int64 delay = static_cast<int64>(ceil(interval * 1000)); |
138 | 111 |
139 if (delay < 0) { | 112 if (delay < 0) { |
140 delay = 0; | 113 delay = 0; |
141 m_nextFireTime = 0.0; | 114 m_nextFireTime = 0.0; |
142 } | 115 } |
143 | 116 |
144 m_running = true; | 117 m_running = true; |
145 m_nextFireTime = currentTime() + interval; | 118 m_nextFireTime = currentTime() + interval; |
146 | 119 m_workerThread->postDelayedTask(createSameThreadTask(&WorkerSharedTimer:
:OnTimeout, this), delay); |
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); | |
153 } | 120 } |
154 | 121 |
155 virtual void stop() | 122 virtual void stop() |
156 { | 123 { |
157 m_running = false; | 124 m_running = false; |
158 if (m_lastQueuedTask) | |
159 m_lastQueuedTask->cancelTask(); | |
160 m_lastQueuedTask = 0; | |
161 } | 125 } |
162 | 126 |
163 double nextFireTime() { return m_nextFireTime; } | 127 double nextFireTime() { return m_nextFireTime; } |
164 | 128 |
165 private: | 129 private: |
166 void OnTimeout() | 130 void OnTimeout() |
167 { | 131 { |
168 ASSERT(m_workerThread->workerGlobalScope()); | 132 ASSERT(m_workerThread->workerGlobalScope()); |
169 | |
170 m_lastQueuedTask = 0; | |
171 | |
172 if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalS
cope()->isClosing()) | 133 if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalS
cope()->isClosing()) |
173 m_sharedTimerFunction(); | 134 m_sharedTimerFunction(); |
174 } | 135 } |
175 | 136 |
176 WorkerThread* m_workerThread; | 137 WorkerThread* m_workerThread; |
177 SharedTimerFunction m_sharedTimerFunction; | 138 SharedTimerFunction m_sharedTimerFunction; |
178 double m_nextFireTime; | 139 double m_nextFireTime; |
179 bool m_running; | 140 bool m_running; |
180 WorkerThreadCancelableTask* m_lastQueuedTask; | |
181 }; | 141 }; |
182 | 142 |
183 class WorkerThreadTask : public blink::WebThread::Task { | 143 class WorkerThreadTask : public blink::WebThread::Task { |
184 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED; | 144 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED; |
185 public: | 145 public: |
186 static PassOwnPtr<WorkerThreadTask> create(const WorkerThread& workerThread,
PassOwnPtr<ExecutionContextTask> task, bool isInstrumented) | 146 static PassOwnPtr<WorkerThreadTask> create(const WorkerThread& workerThread,
PassOwnPtr<ExecutionContextTask> task, bool isInstrumented) |
187 { | 147 { |
188 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented)
); | 148 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented)
); |
189 } | 149 } |
190 | 150 |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 479 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
520 } | 480 } |
521 | 481 |
522 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) | 482 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) |
523 { | 483 { |
524 MutexLocker locker(m_workerInspectorControllerMutex); | 484 MutexLocker locker(m_workerInspectorControllerMutex); |
525 m_workerInspectorController = workerInspectorController; | 485 m_workerInspectorController = workerInspectorController; |
526 } | 486 } |
527 | 487 |
528 } // namespace blink | 488 } // namespace blink |
OLD | NEW |