1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "bindings/core/v8/ScriptStreamerThread.h"
8 #include "bindings/core/v8/ScriptStreamer.h"
9 #include "platform/Task.h"
10 #include "platform/TraceEvent.h"
11 #include "public/platform/Platform.h"
12 #include "wtf/MainThread.h"
13 #include "wtf/PassOwnPtr.h"
17 static ScriptStreamerThread* s_sharedThread = 0;
19 void ScriptStreamerThread::init()
21 ASSERT(!s_sharedThread);
22 ASSERT(isMainThread());
23 s_sharedThread = new ScriptStreamerThread();
26 void ScriptStreamerThread::shutdown()
28 ASSERT(s_sharedThread);
29 delete s_sharedThread;
33 ScriptStreamerThread* ScriptStreamerThread::shared()
35 return s_sharedThread;
38 void ScriptStreamerThread::postTask(WebThread::Task* task)
40 ASSERT(isMainThread());
41 MutexLocker locker(m_mutex);
42 ASSERT(!m_runningTask);
44 platformThread().postTask(task);
47 void ScriptStreamerThread::taskDone()
49 MutexLocker locker(m_mutex);
50 ASSERT(m_runningTask);
51 m_runningTask = false;
54 blink::WebThread& ScriptStreamerThread::platformThread()
57 m_thread = adoptPtr(blink::Platform::current()->createThread("ScriptStreamerThread"));
61 ScriptStreamingTask::ScriptStreamingTask(v8::ScriptCompiler::ScriptStreamingTask* task, ScriptStreamer* streamer)
62 : m_v8Task(adoptPtr(task)), m_streamer(streamer) { }
64 void ScriptStreamingTask::run()
66 TRACE_EVENT0("v8", "v8.parseOnBackground");
67 // Running the task can and will block: SourceStream::GetSomeData will get
68 // called and it will block and wait for data from the network.
70 // Post a task to the main thread to signal that V8 has completed the
72 callOnMainThread(WTF::bind(&ScriptStreamer::streamingComplete, m_streamer));
73 ScriptStreamerThread::shared()->taskDone();