Chromium Code Reviews| Index: Source/bindings/core/v8/V8ScriptStreamerThread.cpp |
| diff --git a/Source/bindings/core/v8/V8ScriptStreamerThread.cpp b/Source/bindings/core/v8/V8ScriptStreamerThread.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..db45bc4918e84c4532182a2b0d995fa69dda7f0d |
| --- /dev/null |
| +++ b/Source/bindings/core/v8/V8ScriptStreamerThread.cpp |
| @@ -0,0 +1,98 @@ |
| +/* |
| + * Copyright (C) 2014 Google Inc. All rights reserved. |
|
haraken
2014/08/17 16:05:27
Nit: Use the 3-line copyright.
marja
2014/08/20 11:45:56
Done.
|
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions |
| + * are met: |
| + * 1. Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * 2. Redistributions in binary form must reproduce the above copyright |
| + * notice, this list of conditions and the following disclaimer in the |
| + * documentation and/or other materials provided with the distribution. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| + * THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "bindings/core/v8/V8ScriptStreamerThread.h" |
| + |
| +#include "bindings/core/v8/V8ScriptStreamer.h" |
| +#include "platform/Task.h" |
| +#include "public/platform/Platform.h" |
| +#include "wtf/MainThread.h" |
| +#include "wtf/PassOwnPtr.h" |
| + |
| +namespace blink { |
| + |
| +static V8ScriptStreamerThread* s_sharedThread = 0; |
| + |
| +void V8ScriptStreamerThread::init() |
| +{ |
| + ASSERT(!s_sharedThread); |
|
haraken
2014/08/17 16:05:27
Add ASSERT(isMainThread()).
marja
2014/08/20 11:45:56
Done.
|
| + s_sharedThread = new V8ScriptStreamerThread(); |
| +} |
| + |
| +void V8ScriptStreamerThread::shutdown() |
| +{ |
| + ASSERT(s_sharedThread); |
|
haraken
2014/08/17 16:05:27
Add ASSERT(isMainThread()).
marja
2014/08/20 11:45:56
That doesn't seem to be true. :/ It's not true in
haraken
2014/08/20 15:00:18
OK :)
|
| + // currentThread will always be non-null in production, but can be null in |
| + // Chromium unit tests. |
| + if (blink::Platform::current()->currentThread() && s_sharedThread->isRunning() |
| + && s_sharedThread->isRunningTask()) { |
|
haraken
2014/08/17 16:05:27
Why is the s_sharedThread->isRunningTask() check n
marja
2014/08/20 11:45:56
Can't it be that Blink is ramping down but the V8
haraken
2014/08/20 15:00:18
I agree that it's possible that the streaming thre
marja
2014/09/09 17:00:26
Yes, I don't think it's safe either, and that's wh
|
| + // The streaming task should exit soon, because the corresponding |
| + // PendingScript should already be destroyed, and it has cancelled the |
| + // streaming. However, the V8 side operations might still be ongoing. |
| + // When V8 asks for more data the next time, the cancelling will take |
| + // effect. |
| + TaskSynchronizer taskSynchronizer; |
| + s_sharedThread->postTask(new Task(WTF::bind(&V8ScriptStreamerThread::markAsCompleted, s_sharedThread, &taskSynchronizer))); |
| + taskSynchronizer.waitForTaskCompletion(); |
| + } |
| + delete s_sharedThread; |
| + s_sharedThread = 0; |
| +} |
| + |
| +V8ScriptStreamerThread* V8ScriptStreamerThread::shared() |
| +{ |
| + return s_sharedThread; |
| +} |
| + |
| +void V8ScriptStreamerThread::postTask(WebThread::Task* task) |
| +{ |
| + m_runningTask = true; |
|
haraken
2014/08/17 16:05:27
Add ASSERT(isMainThread()).
marja
2014/08/20 11:45:56
Done.
|
| + platformThread().postTask(task); |
| +} |
| + |
| +blink::WebThread& V8ScriptStreamerThread::platformThread() |
| +{ |
| + if (!isRunning()) { |
| + m_thread = adoptPtr(blink::Platform::current()->createThread("V8ScriptStreamerThread")); |
| + } |
| + return *m_thread; |
| +} |
| + |
| +ScriptStreamingTask::ScriptStreamingTask(v8::ScriptCompiler::ScriptStreamingTask* task, V8ScriptStreamer* streamer) |
| + : m_v8Task(adoptPtr(task)), m_streamer(streamer) { } |
| + |
| +void ScriptStreamingTask::run() |
| +{ |
| + // Running the task can and will block: SourceStream::GetSomeData will get |
| + // called and it will block and wait for data from the network. |
| + m_v8Task->Run(); |
| + // Post a task to the main thread to signal that V8 has completed the |
| + // streaming. |
| + callOnMainThread(&V8ScriptStreamerThread::taskDone, 0); |
| + callOnMainThread(WTF::bind(&V8ScriptStreamer::streamingComplete, m_streamer)); |
| +} |
| + |
| +} // namespace blink |