Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/core/workers/WorkerThread.cpp

Issue 2025783002: Worker: Introduce an observation mechanism for WorkerThread termination (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@delayed_task
Patch Set: remake and add tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); 140 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex);
141 return mutex; 141 return mutex;
142 } 142 }
143 143
144 static HashSet<WorkerThread*>& workerThreads() 144 static HashSet<WorkerThread*>& workerThreads()
145 { 145 {
146 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); 146 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ());
147 return threads; 147 return threads;
148 } 148 }
149 149
150 WorkerThreadContext::WorkerThreadContext()
151 {
152 DCHECK(isMainThread());
153 }
154
155 WorkerThreadContext::~WorkerThreadContext()
156 {
157 DCHECK(isMainThread());
158 }
159
160 void WorkerThreadContext::notifyContextDestroyed()
161 {
162 DCHECK(isMainThread());
163 DCHECK(!m_wasContextDestroyed);
164 m_wasContextDestroyed = true;
165 LifecycleNotifier::notifyContextDestroyed();
166 }
167
150 WorkerThread::~WorkerThread() 168 WorkerThread::~WorkerThread()
151 { 169 {
152 DCHECK(isMainThread()); 170 DCHECK(isMainThread());
153 MutexLocker lock(threadSetMutex()); 171 MutexLocker lock(threadSetMutex());
154 DCHECK(workerThreads().contains(this)); 172 DCHECK(workerThreads().contains(this));
155 workerThreads().remove(this); 173 workerThreads().remove(this);
156 174
157 // TODO(nhiroki): Record how this thread is terminated (i.e. m_exitCode) 175 // TODO(nhiroki): Record how this thread is terminated (i.e. m_exitCode)
158 // in UMA. 176 // in UMA.
159 DCHECK_NE(ExitCode::NotTerminated, m_exitCode); 177 DCHECK_NE(ExitCode::NotTerminated, m_exitCode);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 : m_forceTerminationDelayInMs(kForceTerminationDelayInMs) 303 : m_forceTerminationDelayInMs(kForceTerminationDelayInMs)
286 , m_inspectorTaskRunner(adoptPtr(new InspectorTaskRunner())) 304 , m_inspectorTaskRunner(adoptPtr(new InspectorTaskRunner()))
287 , m_workerLoaderProxy(workerLoaderProxy) 305 , m_workerLoaderProxy(workerLoaderProxy)
288 , m_workerReportingProxy(workerReportingProxy) 306 , m_workerReportingProxy(workerReportingProxy)
289 , m_terminationEvent(adoptPtr(new WaitableEvent( 307 , m_terminationEvent(adoptPtr(new WaitableEvent(
290 WaitableEvent::ResetPolicy::Manual, 308 WaitableEvent::ResetPolicy::Manual,
291 WaitableEvent::InitialState::NonSignaled))) 309 WaitableEvent::InitialState::NonSignaled)))
292 , m_shutdownEvent(adoptPtr(new WaitableEvent( 310 , m_shutdownEvent(adoptPtr(new WaitableEvent(
293 WaitableEvent::ResetPolicy::Manual, 311 WaitableEvent::ResetPolicy::Manual,
294 WaitableEvent::InitialState::NonSignaled))) 312 WaitableEvent::InitialState::NonSignaled)))
313 , m_workerThreadContext(new WorkerThreadContext)
295 { 314 {
315 DCHECK(isMainThread());
296 MutexLocker lock(threadSetMutex()); 316 MutexLocker lock(threadSetMutex());
297 workerThreads().add(this); 317 workerThreads().add(this);
298 } 318 }
299 319
300 std::unique_ptr<CrossThreadClosure> WorkerThread::createWorkerThreadTask(std::un ique_ptr<ExecutionContextTask> task, bool isInstrumented) 320 std::unique_ptr<CrossThreadClosure> WorkerThread::createWorkerThreadTask(std::un ique_ptr<ExecutionContextTask> task, bool isInstrumented)
301 { 321 {
302 if (isInstrumented) 322 if (isInstrumented)
303 isInstrumented = !task->taskNameForInstrumentation().isEmpty(); 323 isInstrumented = !task->taskNameForInstrumentation().isEmpty();
304 if (isInstrumented) { 324 if (isInstrumented) {
305 DCHECK(isCurrentThread()); 325 DCHECK(isCurrentThread());
(...skipping 26 matching lines...) Expand all
332 m_exitCode = ExitCode::SyncForciblyTerminated; 352 m_exitCode = ExitCode::SyncForciblyTerminated;
333 } 353 }
334 return; 354 return;
335 } 355 }
336 m_terminated = true; 356 m_terminated = true;
337 357
338 // Signal the thread to notify that the thread's stopping. 358 // Signal the thread to notify that the thread's stopping.
339 if (m_terminationEvent) 359 if (m_terminationEvent)
340 m_terminationEvent->signal(); 360 m_terminationEvent->signal();
341 361
362 m_workerThreadContext->notifyContextDestroyed();
363
342 // If the worker thread was never initialized, don't start another 364 // If the worker thread was never initialized, don't start another
343 // shutdown, but still wait for the thread to signal when shutdown has 365 // shutdown, but still wait for the thread to signal when shutdown has
344 // completed on initializeOnWorkerThread(). 366 // completed on initializeOnWorkerThread().
345 if (!m_workerGlobalScope) { 367 if (!m_workerGlobalScope) {
346 DCHECK_EQ(ExitCode::NotTerminated, m_exitCode); 368 DCHECK_EQ(ExitCode::NotTerminated, m_exitCode);
347 m_exitCode = ExitCode::GracefullyTerminated; 369 m_exitCode = ExitCode::GracefullyTerminated;
348 return; 370 return;
349 } 371 }
350 372
351 // Determine if we should synchronously terminate or schedule to terminate 373 // Determine if we should synchronously terminate or schedule to terminate
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 (*task)(); 592 (*task)();
571 } 593 }
572 594
573 WorkerThread::ExitCode WorkerThread::getExitCode() 595 WorkerThread::ExitCode WorkerThread::getExitCode()
574 { 596 {
575 MutexLocker lock(m_threadStateMutex); 597 MutexLocker lock(m_threadStateMutex);
576 return m_exitCode; 598 return m_exitCode;
577 } 599 }
578 600
579 } // namespace blink 601 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698