summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/script_streamer_thread.cc
blob: bcedd5468e21e2950cb96c2fd34bb40d9c5493b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/bindings/core/v8/script_streamer_thread.h"

#include <memory>
#include "base/location.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/script_streamer.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"

namespace blink {

static ScriptStreamerThread* g_shared_thread = nullptr;
// Guards s_sharedThread. s_sharedThread is initialized and deleted in the main
// thread, but also used by the streamer thread. Races can occur during
// shutdown.
static Mutex* g_mutex = nullptr;

void ScriptStreamerThread::Init() {
  // Only enabled when ScheduledScriptStreaming is disabled.
  if (!RuntimeEnabledFeatures::ScheduledScriptStreamingEnabled()) {
    DCHECK(!g_shared_thread);
    DCHECK(IsMainThread());
    // This is called in the main thread before any tasks are created, so no
    // locking is needed.
    g_mutex = new Mutex();
    g_shared_thread = new ScriptStreamerThread();
  }
}

ScriptStreamerThread* ScriptStreamerThread::Shared() {
  DCHECK(!RuntimeEnabledFeatures::ScheduledScriptStreamingEnabled());
  return g_shared_thread;
}

void ScriptStreamerThread::PostTask(CrossThreadClosure task) {
  DCHECK(!RuntimeEnabledFeatures::ScheduledScriptStreamingEnabled());
  DCHECK(IsMainThread());
  MutexLocker locker(mutex_);
  DCHECK(!running_task_);
  running_task_ = true;
  PostCrossThreadTask(*PlatformThread().GetTaskRunner(), FROM_HERE,
                      std::move(task));
}

void ScriptStreamerThread::TaskDone() {
  MutexLocker locker(mutex_);
  DCHECK(running_task_);
  running_task_ = false;
}

Thread& ScriptStreamerThread::PlatformThread() {
  if (!IsRunning()) {
    thread_ = Platform::Current()->CreateThread(
        ThreadCreationParams(WebThreadType::kScriptStreamerThread));
  }
  return *thread_;
}

void ScriptStreamerThread::RunScriptStreamingTask(
    std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask> task,
    ScriptStreamer* streamer) {
  DCHECK(!RuntimeEnabledFeatures::ScheduledScriptStreamingEnabled());
  TRACE_EVENT1(
      "v8,devtools.timeline", "v8.parseOnBackground", "data",
      inspector_parse_script_event::Data(streamer->ScriptResourceIdentifier(),
                                         streamer->ScriptURLString()));
  // Running the task can and will block: SourceStream::GetSomeData will get
  // called and it will block and wait for data from the network.
  task->Run();
  streamer->StreamingCompleteOnBackgroundThread();
  MutexLocker locker(*g_mutex);
  ScriptStreamerThread* thread = Shared();
  if (thread)
    thread->TaskDone();
  // If thread is 0, we're shutting down.
}

}  // namespace blink