Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 2 * Copyright (C) 2013 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 | 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 21 matching lines...) Expand all Loading... | |
| 32 #include "platform/ThreadSafeFunctional.h" | 32 #include "platform/ThreadSafeFunctional.h" |
| 33 #include "public/platform/Platform.h" | 33 #include "public/platform/Platform.h" |
| 34 #include "wtf/Assertions.h" | 34 #include "wtf/Assertions.h" |
| 35 | 35 |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext* context) | 38 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext* context) |
| 39 : m_context(context) | 39 : m_context(context) |
| 40 , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired) | 40 , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired) |
| 41 , m_suspended(false) | 41 , m_suspended(false) |
| 42 , m_weakFactory(this) | |
| 42 { | 43 { |
| 43 } | 44 } |
| 44 | 45 |
| 45 MainThreadTaskRunner::~MainThreadTaskRunner() | 46 MainThreadTaskRunner::~MainThreadTaskRunner() |
| 46 { | 47 { |
| 47 } | 48 } |
| 48 | 49 |
| 49 DEFINE_TRACE(MainThreadTaskRunner) | |
| 50 { | |
| 51 visitor->trace(m_context); | |
| 52 } | |
| 53 | |
| 54 void MainThreadTaskRunner::postTaskInternal(const WebTraceLocation& location, Pa ssOwnPtr<ExecutionContextTask> task, bool isInspectorTask) | 50 void MainThreadTaskRunner::postTaskInternal(const WebTraceLocation& location, Pa ssOwnPtr<ExecutionContextTask> task, bool isInspectorTask) |
| 55 { | 51 { |
| 56 Platform::current()->mainThread()->getWebTaskRunner()->postTask(location, th readSafeBind( | 52 Platform::current()->mainThread()->getWebTaskRunner()->postTask(location, th readSafeBind( |
| 57 &MainThreadTaskRunner::perform, | 53 &MainThreadTaskRunner::perform, |
| 58 CrossThreadWeakPersistentThisPointer<MainThreadTaskRunner>(this), | 54 AllowCrossThreadAccess(m_weakFactory.createWeakPtr()), |
| 59 passed(std::move(task)), | 55 passed(std::move(task)), |
| 60 isInspectorTask)); | 56 isInspectorTask)); |
| 61 } | 57 } |
| 62 | 58 |
| 63 void MainThreadTaskRunner::postTask(const WebTraceLocation& location, PassOwnPtr <ExecutionContextTask> task) | 59 void MainThreadTaskRunner::postTask(const WebTraceLocation& location, PassOwnPtr <ExecutionContextTask> task) |
| 64 { | 60 { |
| 65 if (!task->taskNameForInstrumentation().isEmpty()) | 61 if (!task->taskNameForInstrumentation().isEmpty()) |
| 66 InspectorInstrumentation::asyncTaskScheduled(m_context, task->taskNameFo rInstrumentation(), task.get()); | 62 InspectorInstrumentation::asyncTaskScheduled(m_context, task->taskNameFo rInstrumentation(), task.get()); |
| 67 postTaskInternal(location, std::move(task), false); | 63 postTaskInternal(location, std::move(task), false); |
| 68 } | 64 } |
| 69 | 65 |
| 70 void MainThreadTaskRunner::postInspectorTask(const WebTraceLocation& location, P assOwnPtr<ExecutionContextTask> task) | 66 void MainThreadTaskRunner::postInspectorTask(const WebTraceLocation& location, P assOwnPtr<ExecutionContextTask> task) |
| 71 { | 67 { |
| 72 postTaskInternal(location, std::move(task), true); | 68 postTaskInternal(location, std::move(task), true); |
| 73 } | 69 } |
| 74 | 70 |
| 75 void MainThreadTaskRunner::perform(PassOwnPtr<ExecutionContextTask> task, bool i sInspectorTask) | 71 void MainThreadTaskRunner::perform(PassOwnPtr<ExecutionContextTask> task, bool i sInspectorTask) |
| 76 { | 72 { |
| 73 // If the owner m_context is about to be swept then it | |
| 74 // is no longer safe to access. | |
| 75 if (ThreadHeap::willObjectBeLazilySwept(m_context.get())) | |
|
haraken
2016/05/04 03:21:33
Why was this check not needed before this CL?
sof
2016/05/04 05:17:39
The closure is constructed using CrossThreadWeakPe
| |
| 76 return; | |
| 77 | |
| 77 if (!isInspectorTask && (m_context->tasksNeedSuspension() || !m_pendingTasks .isEmpty())) { | 78 if (!isInspectorTask && (m_context->tasksNeedSuspension() || !m_pendingTasks .isEmpty())) { |
| 78 m_pendingTasks.append(std::move(task)); | 79 m_pendingTasks.append(std::move(task)); |
| 79 return; | 80 return; |
| 80 } | 81 } |
| 81 | 82 |
| 82 InspectorInstrumentation::AsyncTask asyncTask(m_context, task.get(), !isInsp ectorTask); | 83 InspectorInstrumentation::AsyncTask asyncTask(m_context, task.get(), !isInsp ectorTask); |
| 83 task->performTask(m_context); | 84 task->performTask(m_context); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void MainThreadTaskRunner::suspend() | 87 void MainThreadTaskRunner::suspend() |
| 87 { | 88 { |
| 88 DCHECK(!m_suspended); | 89 DCHECK(!m_suspended); |
| 89 m_pendingTasksTimer.stop(); | 90 m_pendingTasksTimer.stop(); |
| 90 m_suspended = true; | 91 m_suspended = true; |
| 91 } | 92 } |
| 92 | 93 |
| 93 void MainThreadTaskRunner::resume() | 94 void MainThreadTaskRunner::resume() |
| 94 { | 95 { |
| 95 DCHECK(m_suspended); | 96 DCHECK(m_suspended); |
| 96 if (!m_pendingTasks.isEmpty()) | 97 if (!m_pendingTasks.isEmpty()) |
| 97 m_pendingTasksTimer.startOneShot(0, BLINK_FROM_HERE); | 98 m_pendingTasksTimer.startOneShot(0, BLINK_FROM_HERE); |
| 98 | 99 |
| 99 m_suspended = false; | 100 m_suspended = false; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void MainThreadTaskRunner::pendingTasksTimerFired(Timer<MainThreadTaskRunner>*) | 103 void MainThreadTaskRunner::pendingTasksTimerFired(Timer<MainThreadTaskRunner>*) |
| 103 { | 104 { |
| 105 // If the owner m_context is about to be swept then it | |
| 106 // is no longer safe to access. | |
| 107 if (ThreadHeap::willObjectBeLazilySwept(m_context.get())) | |
| 108 return; | |
| 109 | |
| 104 while (!m_pendingTasks.isEmpty()) { | 110 while (!m_pendingTasks.isEmpty()) { |
| 105 OwnPtr<ExecutionContextTask> task = m_pendingTasks[0].release(); | 111 OwnPtr<ExecutionContextTask> task = m_pendingTasks[0].release(); |
| 106 m_pendingTasks.remove(0); | 112 m_pendingTasks.remove(0); |
| 107 const bool instrumenting = !task->taskNameForInstrumentation().isEmpty() ; | 113 const bool instrumenting = !task->taskNameForInstrumentation().isEmpty() ; |
| 108 InspectorInstrumentation::AsyncTask asyncTask(m_context, task.get(), ins trumenting); | 114 InspectorInstrumentation::AsyncTask asyncTask(m_context, task.get(), ins trumenting); |
| 109 task->performTask(m_context); | 115 task->performTask(m_context); |
| 110 } | 116 } |
| 111 } | 117 } |
| 112 | 118 |
| 113 } // namespace blink | 119 } // namespace blink |
| OLD | NEW |