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 20 matching lines...) Expand all Loading... | |
31 #include "bindings/core/v8/ScriptSourceCode.h" | 31 #include "bindings/core/v8/ScriptSourceCode.h" |
32 #include "bindings/core/v8/V8GCController.h" | 32 #include "bindings/core/v8/V8GCController.h" |
33 #include "bindings/core/v8/V8Initializer.h" | 33 #include "bindings/core/v8/V8Initializer.h" |
34 #include "core/dom/Microtask.h" | 34 #include "core/dom/Microtask.h" |
35 #include "core/inspector/InspectorInstrumentation.h" | 35 #include "core/inspector/InspectorInstrumentation.h" |
36 #include "core/inspector/WorkerInspectorController.h" | 36 #include "core/inspector/WorkerInspectorController.h" |
37 #include "core/workers/DedicatedWorkerGlobalScope.h" | 37 #include "core/workers/DedicatedWorkerGlobalScope.h" |
38 #include "core/workers/WorkerClients.h" | 38 #include "core/workers/WorkerClients.h" |
39 #include "core/workers/WorkerReportingProxy.h" | 39 #include "core/workers/WorkerReportingProxy.h" |
40 #include "core/workers/WorkerThreadStartupData.h" | 40 #include "core/workers/WorkerThreadStartupData.h" |
41 #include "platform/PlatformThreadData.h" | |
42 #include "platform/Task.h" | 41 #include "platform/Task.h" |
43 #include "platform/ThreadTimers.h" | |
44 #include "platform/heap/SafePoint.h" | 42 #include "platform/heap/SafePoint.h" |
45 #include "platform/heap/ThreadState.h" | 43 #include "platform/heap/ThreadState.h" |
46 #include "platform/weborigin/KURL.h" | 44 #include "platform/weborigin/KURL.h" |
47 #include "public/platform/Platform.h" | 45 #include "public/platform/Platform.h" |
48 #include "public/platform/WebThread.h" | 46 #include "public/platform/WebThread.h" |
49 #include "public/platform/WebWaitableEvent.h" | 47 #include "public/platform/WebWaitableEvent.h" |
50 #include "wtf/Noncopyable.h" | 48 #include "wtf/Noncopyable.h" |
51 #include "wtf/WeakPtr.h" | 49 #include "wtf/WeakPtr.h" |
52 #include "wtf/text/WTFString.h" | 50 #include "wtf/text/WTFString.h" |
53 | 51 |
54 namespace blink { | 52 namespace blink { |
55 | 53 |
56 namespace { | 54 namespace { |
57 const int64_t kShortIdleHandlerDelayMs = 1000; | |
58 const int64_t kLongIdleHandlerDelayMs = 10*1000; | |
59 | 55 |
60 class MicrotaskRunner : public WebThread::TaskObserver { | 56 class MicrotaskRunner : public WebThread::TaskObserver { |
61 public: | 57 public: |
62 explicit MicrotaskRunner(WorkerThread* workerThread) | 58 explicit MicrotaskRunner(WorkerThread* workerThread) |
63 : m_workerThread(workerThread) | 59 : m_workerThread(workerThread) |
64 { | 60 { |
65 } | 61 } |
66 | 62 |
67 virtual void willProcessTask() override { } | 63 virtual void willProcessTask() override { } |
68 virtual void didProcessTask() override | 64 virtual void didProcessTask() override |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 : m_closure(closure) | 120 : m_closure(closure) |
125 , m_weakFactory(this) | 121 , m_weakFactory(this) |
126 , m_taskCanceled(false) | 122 , m_taskCanceled(false) |
127 { } | 123 { } |
128 | 124 |
129 OwnPtr<Closure> m_closure; | 125 OwnPtr<Closure> m_closure; |
130 WeakPtrFactory<WorkerThreadCancelableTask> m_weakFactory; | 126 WeakPtrFactory<WorkerThreadCancelableTask> m_weakFactory; |
131 bool m_taskCanceled; | 127 bool m_taskCanceled; |
132 }; | 128 }; |
133 | 129 |
134 class WorkerSharedTimer : public SharedTimer { | |
135 public: | |
136 explicit WorkerSharedTimer(WorkerThread* workerThread) | |
137 : m_workerThread(workerThread) | |
138 , m_running(false) | |
139 { } | |
140 | |
141 typedef void (*SharedTimerFunction)(); | |
142 virtual void setFiredFunction(SharedTimerFunction func) | |
143 { | |
144 m_sharedTimerFunction = func; | |
145 } | |
146 | |
147 virtual void setFireInterval(double interval) | |
148 { | |
149 ASSERT(m_sharedTimerFunction); | |
150 | |
151 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of | |
152 // why ceil is used in the interval calculation. | |
153 int64_t delay = static_cast<int64_t>(ceil(interval * 1000)); | |
154 | |
155 if (delay < 0) { | |
156 delay = 0; | |
157 } | |
158 | |
159 m_running = true; | |
160 | |
161 if (m_lastQueuedTask.get()) | |
162 m_lastQueuedTask->cancelTask(); | |
163 | |
164 // Now queue the task as a cancellable one. | |
165 OwnPtr<WorkerThreadCancelableTask> task = WorkerThreadCancelableTask::cr eate(bind(&WorkerSharedTimer::OnTimeout, this)); | |
166 m_lastQueuedTask = task->createWeakPtr(); | |
167 m_workerThread->postDelayedTask(FROM_HERE, task.release(), delay); | |
168 } | |
169 | |
170 virtual void stop() | |
171 { | |
172 m_running = false; | |
173 m_lastQueuedTask = nullptr; | |
174 } | |
175 | |
176 private: | |
177 void OnTimeout() | |
178 { | |
179 ASSERT(m_workerThread->workerGlobalScope()); | |
180 | |
181 m_lastQueuedTask = nullptr; | |
182 | |
183 if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalS cope()->isClosing()) | |
184 m_sharedTimerFunction(); | |
185 } | |
186 | |
187 WorkerThread* m_workerThread; | |
188 SharedTimerFunction m_sharedTimerFunction; | |
189 bool m_running; | |
190 | |
191 // The task to run OnTimeout, if any. While OnTimeout resets | |
192 // m_lastQueuedTask, this must be a weak pointer because the | |
193 // worker runloop may delete the task as it is shutting down. | |
194 WeakPtr<WorkerThreadCancelableTask> m_lastQueuedTask; | |
195 }; | |
196 | |
197 class WorkerThreadTask : public blink::WebThread::Task { | 130 class WorkerThreadTask : public blink::WebThread::Task { |
198 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED(WorkerThread Task); | 131 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED(WorkerThread Task); |
199 public: | 132 public: |
200 static PassOwnPtr<WorkerThreadTask> create(WorkerThread& workerThread, PassO wnPtr<ExecutionContextTask> task, bool isInstrumented) | 133 static PassOwnPtr<WorkerThreadTask> create(WorkerThread& workerThread, PassO wnPtr<ExecutionContextTask> task, bool isInstrumented) |
201 { | 134 { |
202 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); | 135 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); |
203 } | 136 } |
204 | 137 |
205 virtual ~WorkerThreadTask() { } | 138 virtual ~WorkerThreadTask() { } |
206 | 139 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 m_workerInspectorController->interruptAndDispatchInspectorCommands(); | 224 m_workerInspectorController->interruptAndDispatchInspectorCommands(); |
292 } | 225 } |
293 | 226 |
294 PlatformThreadId WorkerThread::platformThreadId() const | 227 PlatformThreadId WorkerThread::platformThreadId() const |
295 { | 228 { |
296 if (!m_thread) | 229 if (!m_thread) |
297 return 0; | 230 return 0; |
298 return m_thread->platformThread().threadId(); | 231 return m_thread->platformThread().threadId(); |
299 } | 232 } |
300 | 233 |
234 class WorkerThreadIdleTask : public WebThread::IdleTask { | |
235 public: | |
236 explicit WorkerThreadIdleTask(WorkerThread* thread) | |
237 : m_thread(thread) { } | |
238 | |
239 ~WorkerThreadIdleTask() override { } | |
240 | |
241 void run(double deadlineSeconds) override | |
242 { | |
243 m_thread->idleTask(deadlineSeconds); | |
244 } | |
245 | |
246 private: | |
247 RawPtr<WorkerThread> m_thread; | |
248 }; | |
249 | |
301 void WorkerThread::initialize() | 250 void WorkerThread::initialize() |
302 { | 251 { |
303 KURL scriptURL = m_startupData->m_scriptURL; | 252 KURL scriptURL = m_startupData->m_scriptURL; |
304 String sourceCode = m_startupData->m_sourceCode; | 253 String sourceCode = m_startupData->m_sourceCode; |
305 WorkerThreadStartMode startMode = m_startupData->m_startMode; | 254 WorkerThreadStartMode startMode = m_startupData->m_startMode; |
306 OwnPtr<Vector<char>> cachedMetaData = m_startupData->m_cachedMetaData.releas e(); | 255 OwnPtr<Vector<char>> cachedMetaData = m_startupData->m_cachedMetaData.releas e(); |
307 V8CacheOptions v8CacheOptions = m_startupData->m_v8CacheOptions; | 256 V8CacheOptions v8CacheOptions = m_startupData->m_v8CacheOptions; |
308 | 257 |
309 { | 258 { |
310 MutexLocker lock(m_threadCreationMutex); | 259 MutexLocker lock(m_threadCreationMutex); |
311 | 260 |
312 // The worker was terminated before the thread had a chance to run. | 261 // The worker was terminated before the thread had a chance to run. |
313 if (m_terminated) { | 262 if (m_terminated) { |
314 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 263 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
315 // This can free this thread object, hence it must not be touched af terwards. | 264 // This can free this thread object, hence it must not be touched af terwards. |
316 m_workerReportingProxy.workerThreadTerminated(); | 265 m_workerReportingProxy.workerThreadTerminated(); |
317 return; | 266 return; |
318 } | 267 } |
319 | 268 |
320 m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); | 269 m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); |
321 m_thread->addTaskObserver(m_microtaskRunner.get()); | 270 m_thread->addTaskObserver(m_microtaskRunner.get()); |
322 m_thread->attachGC(); | 271 m_thread->attachGC(); |
323 | 272 |
324 m_isolate = initializeIsolate(); | 273 m_isolate = initializeIsolate(); |
325 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 274 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
326 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); | 275 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); |
327 | |
328 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); | |
329 } | 276 } |
330 | 277 |
331 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). | 278 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). |
332 didStartRunLoop(); | 279 didStartRunLoop(); |
333 | 280 |
334 // Notify proxy that a new WorkerGlobalScope has been created and started. | 281 // Notify proxy that a new WorkerGlobalScope has been created and started. |
335 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); | 282 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
336 | 283 |
337 WorkerScriptController* script = m_workerGlobalScope->script(); | 284 WorkerScriptController* script = m_workerGlobalScope->script(); |
338 if (!script->isExecutionForbidden()) | 285 if (!script->isExecutionForbidden()) |
339 script->initializeContextIfNeeded(); | 286 script->initializeContextIfNeeded(); |
340 if (startMode == PauseWorkerGlobalScopeOnStart) | 287 if (startMode == PauseWorkerGlobalScopeOnStart) |
341 m_workerGlobalScope->workerInspectorController()->pauseOnStart(); | 288 m_workerGlobalScope->workerInspectorController()->pauseOnStart(); |
342 | 289 |
343 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); | 290 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); |
344 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); | 291 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); |
345 m_workerGlobalScope->didEvaluateWorkerScript(); | 292 m_workerGlobalScope->didEvaluateWorkerScript(); |
346 m_workerReportingProxy.didEvaluateWorkerScript(success); | 293 m_workerReportingProxy.didEvaluateWorkerScript(success); |
347 | 294 |
348 postInitialize(); | 295 postInitialize(); |
349 | 296 |
350 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); | 297 m_thread->postIdleTaskAfterWakeup(FROM_HERE, new WorkerThreadIdleTask(this)) ; |
351 } | 298 } |
352 | 299 |
353 PassOwnPtr<WebThreadSupportingGC> WorkerThread::createWebThreadSupportingGC() | 300 PassOwnPtr<WebThreadSupportingGC> WorkerThread::createWebThreadSupportingGC() |
354 { | 301 { |
355 return WebThreadSupportingGC::create("WebCore: Worker"); | 302 return WebThreadSupportingGC::create("WebCore: Worker"); |
356 } | 303 } |
357 | 304 |
358 void WorkerThread::cleanup() | 305 void WorkerThread::cleanup() |
359 { | 306 { |
360 // This should be called before we start the shutdown procedure. | 307 // This should be called before we start the shutdown procedure. |
(...skipping 12 matching lines...) Expand all Loading... | |
373 destroyIsolate(); | 320 destroyIsolate(); |
374 | 321 |
375 m_thread->removeTaskObserver(m_microtaskRunner.get()); | 322 m_thread->removeTaskObserver(m_microtaskRunner.get()); |
376 m_microtaskRunner = nullptr; | 323 m_microtaskRunner = nullptr; |
377 | 324 |
378 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 325 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
379 // This can free this thread object, hence it must not be touched afterwards . | 326 // This can free this thread object, hence it must not be touched afterwards . |
380 workerReportingProxy().workerThreadTerminated(); | 327 workerReportingProxy().workerThreadTerminated(); |
381 | 328 |
382 m_terminationEvent->signal(); | 329 m_terminationEvent->signal(); |
383 | |
384 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | |
385 PlatformThreadData::current().destroy(); | |
386 } | 330 } |
387 | 331 |
388 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { | 332 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { |
389 public: | 333 public: |
390 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() | 334 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() |
391 { | 335 { |
392 return adoptPtr(new WorkerThreadShutdownFinishTask()); | 336 return adoptPtr(new WorkerThreadShutdownFinishTask()); |
393 } | 337 } |
394 | 338 |
395 virtual void performTask(ExecutionContext *context) | 339 virtual void performTask(ExecutionContext *context) |
(...skipping 14 matching lines...) Expand all Loading... | |
410 public: | 354 public: |
411 static PassOwnPtr<WorkerThreadShutdownStartTask> create() | 355 static PassOwnPtr<WorkerThreadShutdownStartTask> create() |
412 { | 356 { |
413 return adoptPtr(new WorkerThreadShutdownStartTask()); | 357 return adoptPtr(new WorkerThreadShutdownStartTask()); |
414 } | 358 } |
415 | 359 |
416 virtual void performTask(ExecutionContext *context) | 360 virtual void performTask(ExecutionContext *context) |
417 { | 361 { |
418 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 362 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
419 workerGlobalScope->stopActiveDOMObjects(); | 363 workerGlobalScope->stopActiveDOMObjects(); |
420 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
421 | 364 |
422 // Event listeners would keep DOMWrapperWorld objects alive for too long . Also, they have references to JS objects, | 365 // Event listeners would keep DOMWrapperWorld objects alive for too long . Also, they have references to JS objects, |
423 // which become dangling once Heap is destroyed. | 366 // which become dangling once Heap is destroyed. |
424 workerGlobalScope->removeAllEventListeners(); | 367 workerGlobalScope->removeAllEventListeners(); |
425 | 368 |
426 // Stick a shutdown command at the end of the queue, so that we deal | 369 // Stick a shutdown command at the end of the queue, so that we deal |
427 // with all the cleanup tasks the databases post first. | 370 // with all the cleanup tasks the databases post first. |
428 workerGlobalScope->postTask(FROM_HERE, WorkerThreadShutdownFinishTask::c reate()); | 371 workerGlobalScope->postTask(FROM_HERE, WorkerThreadShutdownFinishTask::c reate()); |
429 } | 372 } |
430 | 373 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
502 | 445 |
503 for (WorkerThread* thread : threads) | 446 for (WorkerThread* thread : threads) |
504 thread->terminationEvent()->wait(); | 447 thread->terminationEvent()->wait(); |
505 } | 448 } |
506 | 449 |
507 bool WorkerThread::isCurrentThread() const | 450 bool WorkerThread::isCurrentThread() const |
508 { | 451 { |
509 return m_thread && m_thread->isCurrentThread(); | 452 return m_thread && m_thread->isCurrentThread(); |
510 } | 453 } |
511 | 454 |
512 void WorkerThread::idleHandler() | 455 void WorkerThread::idleTask(double deadlineSeconds) |
513 { | 456 { |
514 ASSERT(m_workerGlobalScope.get()); | 457 if (doIdleGc(deadlineSeconds)) |
515 int64_t delay = kLongIdleHandlerDelayMs; | 458 m_thread->postIdleTaskAfterWakeup(FROM_HERE, new WorkerThreadIdleTask(th is)); |
rmcilroy
2015/04/13 13:55:07
I've just realized that this was previously sendin
alex clarke (OOO till 29th)
2015/04/14 13:04:35
OK I can do that, but I need to wire CanExceedIdle
| |
459 else | |
460 m_thread->postIdleTask(FROM_HERE, new WorkerThreadIdleTask(this)); | |
461 } | |
516 | 462 |
517 // Do a script engine idle notification if the next event is distant enough. | 463 bool WorkerThread::doIdleGc(double deadlineSeconds) |
518 const double kMinIdleTimespan = 0.3; | 464 { |
519 const double nextFireTime = PlatformThreadData::current().threadTimers().nex tFireTime(); | 465 if (deadlineSeconds > Platform::current()->monotonicallyIncreasingTime()) |
520 if (nextFireTime == 0.0 || nextFireTime > currentTime() + kMinIdleTimespan) { | 466 return isolate()->IdleNotificationDeadline(deadlineSeconds); |
521 bool hasMoreWork = !isolate()->IdleNotificationDeadline(Platform::curren t()->monotonicallyIncreasingTime() + 1.0); | |
522 if (hasMoreWork) | |
523 delay = kShortIdleHandlerDelayMs; | |
524 } | |
525 | 467 |
526 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), delay); | 468 return false; |
527 } | 469 } |
528 | 470 |
529 void WorkerThread::postTask(const WebTraceLocation& location, PassOwnPtr<Executi onContextTask> task) | 471 void WorkerThread::postTask(const WebTraceLocation& location, PassOwnPtr<Executi onContextTask> task) |
530 { | 472 { |
531 m_thread->postTask(location, WorkerThreadTask::create(*this, task, true).lea kPtr()); | 473 m_thread->postTask(location, WorkerThreadTask::create(*this, task, true).lea kPtr()); |
532 } | 474 } |
533 | 475 |
534 void WorkerThread::postDelayedTask(const WebTraceLocation& location, PassOwnPtr< ExecutionContextTask> task, long long delayMs) | 476 void WorkerThread::postDelayedTask(const WebTraceLocation& location, PassOwnPtr< ExecutionContextTask> task, long long delayMs) |
535 { | 477 { |
536 m_thread->postDelayedTask(location, WorkerThreadTask::create(*this, task, tr ue).leakPtr(), delayMs); | 478 m_thread->postDelayedTask(location, WorkerThreadTask::create(*this, task, tr ue).leakPtr(), delayMs); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 552 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
611 } | 553 } |
612 | 554 |
613 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) | 555 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) |
614 { | 556 { |
615 MutexLocker locker(m_workerInspectorControllerMutex); | 557 MutexLocker locker(m_workerInspectorControllerMutex); |
616 m_workerInspectorController = workerInspectorController; | 558 m_workerInspectorController = workerInspectorController; |
617 } | 559 } |
618 | 560 |
619 } // namespace blink | 561 } // namespace blink |
OLD | NEW |