GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_platform.cc Lines: 356 371 96.0 %
Date: 2021-04-28 04:12:15 Branches: 93 120 77.5 %

Line Branch Exec Source
1
#include "node_platform.h"
2
#include "node_internals.h"
3
4
#include "env-inl.h"
5
#include "debug_utils-inl.h"
6
#include <algorithm>  // find_if(), find(), move()
7
#include <cmath>  // llround()
8
#include <memory>  // unique_ptr(), shared_ptr(), make_shared()
9
10
namespace node {
11
12
using v8::Isolate;
13
using v8::Object;
14
using v8::Platform;
15
using v8::Task;
16
17
namespace {
18
19
struct PlatformWorkerData {
20
  TaskQueue<Task>* task_queue;
21
  Mutex* platform_workers_mutex;
22
  ConditionVariable* platform_workers_ready;
23
  int* pending_platform_workers;
24
  int id;
25
};
26
27
19086
static void PlatformWorkerThread(void* data) {
28
  std::unique_ptr<PlatformWorkerData>
29
38164
      worker_data(static_cast<PlatformWorkerData*>(data));
30
31
19103
  TaskQueue<Task>* pending_worker_tasks = worker_data->task_queue;
32
  TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
33
19088
                        "PlatformWorkerThread");
34
35
4786
  // Notify the main thread that the platform worker is ready.
36
19096
  {
37
38202
    Mutex::ScopedLock lock(*worker_data->platform_workers_mutex);
38
19106
    (*worker_data->pending_platform_workers)--;
39
19106
    worker_data->platform_workers_ready->Signal(lock);
40
  }
41
42
162064
  while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
43
71466
    task->Run();
44
71487
    pending_worker_tasks->NotifyOfCompletion();
45
71482
  }
46
19077
}
47
48
}  // namespace
49
50
4768
class WorkerThreadsTaskRunner::DelayedTaskScheduler {
51
 public:
52
4775
  explicit DelayedTaskScheduler(TaskQueue<Task>* tasks)
53
4775
    : pending_worker_tasks_(tasks) {}
54
55
4775
  std::unique_ptr<uv_thread_t> Start() {
56
14325
    auto start_thread = [](void* data) {
57
4775
      static_cast<DelayedTaskScheduler*>(data)->Run();
58
14318
    };
59
4775
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
60
4775
    uv_sem_init(&ready_, 0);
61
4775
    CHECK_EQ(0, uv_thread_create(t.get(), start_thread, this));
62
4775
    uv_sem_wait(&ready_);
63
4775
    uv_sem_destroy(&ready_);
64
4775
    return t;
65
  }
66
67
532
  void PostDelayedTask(std::unique_ptr<Task> task, double delay_in_seconds) {
68
1064
    tasks_.Push(std::make_unique<ScheduleTask>(this, std::move(task),
69
532
                                               delay_in_seconds));
70
532
    uv_async_send(&flush_tasks_);
71
532
  }
72
73
4768
  void Stop() {
74
4768
    tasks_.Push(std::make_unique<StopTask>(this));
75
4768
    uv_async_send(&flush_tasks_);
76
4768
  }
77
78
 private:
79
4775
  void Run() {
80
4775
    TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
81
                          "WorkerThreadsTaskRunner::DelayedTaskScheduler");
82
4775
    loop_.data = this;
83
4775
    CHECK_EQ(0, uv_loop_init(&loop_));
84
9550
    flush_tasks_.data = this;
85
4775
    CHECK_EQ(0, uv_async_init(&loop_, &flush_tasks_, FlushTasks));
86
4775
    uv_sem_post(&ready_);
87
88
4775
    uv_run(&loop_, UV_RUN_DEFAULT);
89
4768
    CheckedUvLoopClose(&loop_);
90
4768
  }
91
92
5300
  static void FlushTasks(uv_async_t* flush_tasks) {
93
    DelayedTaskScheduler* scheduler =
94
5300
        ContainerOf(&DelayedTaskScheduler::loop_, flush_tasks->loop);
95
15900
    while (std::unique_ptr<Task> task = scheduler->tasks_.Pop())
96
10600
      task->Run();
97
5300
  }
98
99
9536
  class StopTask : public Task {
100
   public:
101
4768
    explicit StopTask(DelayedTaskScheduler* scheduler): scheduler_(scheduler) {}
102
103
4768
    void Run() override {
104
9536
      std::vector<uv_timer_t*> timers;
105
5264
      for (uv_timer_t* timer : scheduler_->timers_)
106
496
        timers.push_back(timer);
107
5264
      for (uv_timer_t* timer : timers)
108
496
        scheduler_->TakeTimerTask(timer);
109
9536
      uv_close(reinterpret_cast<uv_handle_t*>(&scheduler_->flush_tasks_),
110
19072
               [](uv_handle_t* handle) {});
111
4768
    }
112
113
   private:
114
     DelayedTaskScheduler* scheduler_;
115
  };
116
117
1064
  class ScheduleTask : public Task {
118
   public:
119
532
    ScheduleTask(DelayedTaskScheduler* scheduler,
120
                 std::unique_ptr<Task> task,
121
                 double delay_in_seconds)
122
532
      : scheduler_(scheduler),
123
532
        task_(std::move(task)),
124
1064
        delay_in_seconds_(delay_in_seconds) {}
125
126
532
    void Run() override {
127
532
      uint64_t delay_millis = llround(delay_in_seconds_ * 1000);
128
1064
      std::unique_ptr<uv_timer_t> timer(new uv_timer_t());
129
532
      CHECK_EQ(0, uv_timer_init(&scheduler_->loop_, timer.get()));
130
532
      timer->data = task_.release();
131
532
      CHECK_EQ(0, uv_timer_start(timer.get(), RunTask, delay_millis, 0));
132
532
      scheduler_->timers_.insert(timer.release());
133
532
    }
134
135
   private:
136
    DelayedTaskScheduler* scheduler_;
137
    std::unique_ptr<Task> task_;
138
    double delay_in_seconds_;
139
  };
140
141
36
  static void RunTask(uv_timer_t* timer) {
142
    DelayedTaskScheduler* scheduler =
143
36
        ContainerOf(&DelayedTaskScheduler::loop_, timer->loop);
144
36
    scheduler->pending_worker_tasks_->Push(scheduler->TakeTimerTask(timer));
145
36
  }
146
147
532
  std::unique_ptr<Task> TakeTimerTask(uv_timer_t* timer) {
148
532
    std::unique_ptr<Task> task(static_cast<Task*>(timer->data));
149
532
    uv_timer_stop(timer);
150
2660
    uv_close(reinterpret_cast<uv_handle_t*>(timer), [](uv_handle_t* handle) {
151
532
      delete reinterpret_cast<uv_timer_t*>(handle);
152
2128
    });
153
532
    timers_.erase(timer);
154
532
    return task;
155
  }
156
157
  uv_sem_t ready_;
158
  TaskQueue<Task>* pending_worker_tasks_;
159
160
  TaskQueue<Task> tasks_;
161
  uv_loop_t loop_;
162
  uv_async_t flush_tasks_;
163
  std::unordered_set<uv_timer_t*> timers_;
164
};
165
166
4775
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
167
9550
  Mutex platform_workers_mutex;
168
9550
  ConditionVariable platform_workers_ready;
169
170
9550
  Mutex::ScopedLock lock(platform_workers_mutex);
171
4775
  int pending_platform_workers = thread_pool_size;
172
173
9550
  delayed_task_scheduler_ = std::make_unique<DelayedTaskScheduler>(
174
14325
      &pending_worker_tasks_);
175
4775
  threads_.push_back(delayed_task_scheduler_->Start());
176
177
23881
  for (int i = 0; i < thread_pool_size; i++) {
178
    PlatformWorkerData* worker_data = new PlatformWorkerData{
179
19106
      &pending_worker_tasks_, &platform_workers_mutex,
180
      &platform_workers_ready, &pending_platform_workers, i
181
38212
    };
182
38212
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
183
19106
    if (uv_thread_create(t.get(), PlatformWorkerThread,
184
                         worker_data) != 0) {
185
      break;
186
    }
187
19106
    threads_.push_back(std::move(t));
188
  }
189
190
  // Wait for platform workers to initialize before continuing with the
191
  // bootstrap.
192
39703
  while (pending_platform_workers > 0) {
193
17464
    platform_workers_ready.Wait(lock);
194
  }
195
4775
}
196
197
71467
void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
198
71467
  pending_worker_tasks_.Push(std::move(task));
199
71467
}
200
201
532
void WorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
202
                                              double delay_in_seconds) {
203
532
  delayed_task_scheduler_->PostDelayedTask(std::move(task), delay_in_seconds);
204
532
}
205
206
16384
void WorkerThreadsTaskRunner::BlockingDrain() {
207
16384
  pending_worker_tasks_.BlockingDrain();
208
16384
}
209
210
4768
void WorkerThreadsTaskRunner::Shutdown() {
211
4768
  pending_worker_tasks_.Stop();
212
4768
  delayed_task_scheduler_->Stop();
213
28614
  for (size_t i = 0; i < threads_.size(); i++) {
214
23846
    CHECK_EQ(0, uv_thread_join(threads_[i].get()));
215
  }
216
4768
}
217
218
42121
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
219
42121
  return threads_.size();
220
}
221
222
5246
PerIsolatePlatformData::PerIsolatePlatformData(
223
5246
    Isolate* isolate, uv_loop_t* loop)
224
5246
  : isolate_(isolate), loop_(loop) {
225
5246
  flush_tasks_ = new uv_async_t();
226
5246
  CHECK_EQ(0, uv_async_init(loop, flush_tasks_, FlushTasks));
227
5246
  flush_tasks_->data = static_cast<void*>(this);
228
5246
  uv_unref(reinterpret_cast<uv_handle_t*>(flush_tasks_));
229
5246
}
230
231
std::shared_ptr<v8::TaskRunner>
232
36350
PerIsolatePlatformData::GetForegroundTaskRunner() {
233
36350
  return shared_from_this();
234
}
235
236
10277
void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
237
10277
  auto platform_data = static_cast<PerIsolatePlatformData*>(handle->data);
238
10277
  platform_data->FlushForegroundTasksInternal();
239
10277
}
240
241
void PerIsolatePlatformData::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
242
  UNREACHABLE();
243
}
244
245
15518
void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
246
15518
  if (flush_tasks_ == nullptr) {
247
    // V8 may post tasks during Isolate disposal. In that case, the only
248
    // sensible path forward is to discard the task.
249
    return;
250
  }
251
15518
  foreground_tasks_.Push(std::move(task));
252
15519
  uv_async_send(flush_tasks_);
253
}
254
255
4667
void PerIsolatePlatformData::PostDelayedTask(
256
    std::unique_ptr<Task> task, double delay_in_seconds) {
257
4667
  if (flush_tasks_ == nullptr) {
258
    // V8 may post tasks during Isolate disposal. In that case, the only
259
    // sensible path forward is to discard the task.
260
    return;
261
  }
262
9334
  std::unique_ptr<DelayedTask> delayed(new DelayedTask());
263
4667
  delayed->task = std::move(task);
264
4667
  delayed->platform_data = shared_from_this();
265
4667
  delayed->timeout = delay_in_seconds;
266
4667
  foreground_delayed_tasks_.Push(std::move(delayed));
267
4667
  uv_async_send(flush_tasks_);
268
}
269
270
9359
void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr<Task> task) {
271
9359
  PostTask(std::move(task));
272
9359
}
273
274
void PerIsolatePlatformData::PostNonNestableDelayedTask(
275
    std::unique_ptr<Task> task,
276
    double delay_in_seconds) {
277
  PostDelayedTask(std::move(task), delay_in_seconds);
278
}
279
280
964
PerIsolatePlatformData::~PerIsolatePlatformData() {
281
482
  CHECK(!flush_tasks_);
282
482
}
283
284
428
void PerIsolatePlatformData::AddShutdownCallback(void (*callback)(void*),
285
                                                 void* data) {
286
428
  shutdown_callbacks_.emplace_back(ShutdownCallback { callback, data });
287
428
}
288
289
4675
void PerIsolatePlatformData::Shutdown() {
290
4675
  if (flush_tasks_ == nullptr)
291
    return;
292
293
  // While there should be no V8 tasks in the queues at this point, it is
294
  // possible that Node.js-internal tasks from e.g. the inspector are still
295
  // lying around. We clear these queues and ignore the return value,
296
  // effectively deleting the tasks instead of running them.
297
4675
  foreground_delayed_tasks_.PopAll();
298
4675
  foreground_tasks_.PopAll();
299
4675
  scheduled_delayed_tasks_.clear();
300
301
  // Both destroying the scheduled_delayed_tasks_ lists and closing
302
  // flush_tasks_ handle add tasks to the event loop. We keep a count of all
303
  // non-closed handles, and when that reaches zero, we inform any shutdown
304
  // callbacks that the platform is done as far as this Isolate is concerned.
305
4675
  self_reference_ = shared_from_this();
306
9350
  uv_close(reinterpret_cast<uv_handle_t*>(flush_tasks_),
307
5641
           [](uv_handle_t* handle) {
308
    std::unique_ptr<uv_async_t> flush_tasks {
309
966
        reinterpret_cast<uv_async_t*>(handle) };
310
    PerIsolatePlatformData* platform_data =
311
483
        static_cast<PerIsolatePlatformData*>(flush_tasks->data);
312
483
    platform_data->DecreaseHandleCount();
313
483
    platform_data->self_reference_.reset();
314
10316
  });
315
4675
  flush_tasks_ = nullptr;
316
}
317
318
938
void PerIsolatePlatformData::DecreaseHandleCount() {
319
938
  CHECK_GE(uv_handle_count_, 1);
320
938
  if (--uv_handle_count_ == 0) {
321
911
    for (const auto& callback : shutdown_callbacks_)
322
428
      callback.cb(callback.data);
323
  }
324
938
}
325
326
4775
NodePlatform::NodePlatform(int thread_pool_size,
327
4775
                           v8::TracingController* tracing_controller) {
328
4775
  if (tracing_controller != nullptr) {
329
4768
    tracing_controller_ = tracing_controller;
330
  } else {
331
7
    tracing_controller_ = new v8::TracingController();
332
  }
333
  // TODO(addaleax): It's a bit icky that we use global state here, but we can't
334
  // really do anything about it unless V8 starts exposing a way to access the
335
  // current v8::Platform instance.
336
4775
  SetTracingController(tracing_controller_);
337
  DCHECK_EQ(GetTracingController(), tracing_controller_);
338
  worker_thread_task_runner_ =
339
4775
      std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
340
4775
}
341
342
14304
NodePlatform::~NodePlatform() {
343
4768
  Shutdown();
344
9536
}
345
346
5245
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
347
10490
  Mutex::ScopedLock lock(per_isolate_mutex_);
348
10490
  auto delegate = std::make_shared<PerIsolatePlatformData>(isolate, loop);
349
5245
  IsolatePlatformDelegate* ptr = delegate.get();
350
  auto insertion = per_isolate_.emplace(
351
    isolate,
352
5245
    std::make_pair(ptr, std::move(delegate)));
353
5245
  CHECK(insertion.second);
354
5245
}
355
356
1
void NodePlatform::RegisterIsolate(Isolate* isolate,
357
                                   IsolatePlatformDelegate* delegate) {
358
2
  Mutex::ScopedLock lock(per_isolate_mutex_);
359
  auto insertion = per_isolate_.emplace(
360
    isolate,
361
1
    std::make_pair(delegate, std::shared_ptr<PerIsolatePlatformData>{}));
362
1
  CHECK(insertion.second);
363
1
}
364
365
4675
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
366
9350
  Mutex::ScopedLock lock(per_isolate_mutex_);
367
4675
  auto existing_it = per_isolate_.find(isolate);
368
4675
  CHECK_NE(existing_it, per_isolate_.end());
369
4675
  auto& existing = existing_it->second;
370
4675
  if (existing.second) {
371
4674
    existing.second->Shutdown();
372
  }
373
4675
  per_isolate_.erase(existing_it);
374
4675
}
375
376
428
void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate,
377
                                              void (*cb)(void*), void* data) {
378
856
  Mutex::ScopedLock lock(per_isolate_mutex_);
379
428
  auto it = per_isolate_.find(isolate);
380
428
  if (it == per_isolate_.end()) {
381
    cb(data);
382
    return;
383
  }
384
428
  CHECK(it->second.second);
385
428
  it->second.second->AddShutdownCallback(cb, data);
386
}
387
388
9531
void NodePlatform::Shutdown() {
389
9531
  if (has_shut_down_) return;
390
4768
  has_shut_down_ = true;
391
4768
  worker_thread_task_runner_->Shutdown();
392
393
  {
394
9536
    Mutex::ScopedLock lock(per_isolate_mutex_);
395
4768
    per_isolate_.clear();
396
  }
397
}
398
399
42121
int NodePlatform::NumberOfWorkerThreads() {
400
42121
  return worker_thread_task_runner_->NumberOfWorkerThreads();
401
}
402
403
15193
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
404
15193
  DebugSealHandleScope scope(isolate_);
405
15193
  Environment* env = Environment::GetCurrent(isolate_);
406
15193
  if (env != nullptr) {
407
28077
    v8::HandleScope scope(isolate_);
408
14042
    InternalCallbackScope cb_scope(env, Object::New(isolate_), { 0, 0 },
409
42126
                                   InternalCallbackScope::kNoFlags);
410
14042
    task->Run();
411
  } else {
412
    // The task is moved out of InternalCallbackScope if env is not available.
413
    // This is a required else block, and should not be removed.
414
    // See comment: https://github.com/nodejs/node/pull/34688#pullrequestreview-463867489
415
1151
    task->Run();
416
  }
417
15186
}
418
419
5
void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) {
420
  auto it = std::find_if(scheduled_delayed_tasks_.begin(),
421
                         scheduled_delayed_tasks_.end(),
422
10
                         [task](const DelayedTaskPointer& delayed) -> bool {
423
10
          return delayed.get() == task;
424
5
      });
425
5
  CHECK_NE(it, scheduled_delayed_tasks_.end());
426
5
  scheduled_delayed_tasks_.erase(it);
427
5
}
428
429
5
void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) {
430
5
  DelayedTask* delayed = ContainerOf(&DelayedTask::timer, handle);
431
5
  delayed->platform_data->RunForegroundTask(std::move(delayed->task));
432
5
  delayed->platform_data->DeleteFromScheduledTasks(delayed);
433
5
}
434
435
9236
void NodePlatform::DrainTasks(Isolate* isolate) {
436
18464
  std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate(isolate);
437
9236
  if (!per_isolate) return;
438
439

16384
  do {
440
    // Worker tasks aren't associated with an Isolate.
441
16384
    worker_thread_task_runner_->BlockingDrain();
442
16384
  } while (per_isolate->FlushForegroundTasksInternal());
443
}
444
445
26665
bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
446
26665
  bool did_work = false;
447
448
  while (std::unique_ptr<DelayedTask> delayed =
449
35673
      foreground_delayed_tasks_.Pop()) {
450
4504
    did_work = true;
451
4504
    uint64_t delay_millis = llround(delayed->timeout * 1000);
452
453
4504
    delayed->timer.data = static_cast<void*>(delayed.get());
454
4504
    uv_timer_init(loop_, &delayed->timer);
455
    // Timers may not guarantee queue ordering of events with the same delay if
456
    // the delay is non-zero. This should not be a problem in practice.
457
4504
    uv_timer_start(&delayed->timer, RunForegroundTask, delay_millis, 0);
458
4504
    uv_unref(reinterpret_cast<uv_handle_t*>(&delayed->timer));
459
4504
    uv_handle_count_++;
460
461
9008
    scheduled_delayed_tasks_.emplace_back(delayed.release(),
462
13038
                                          [](DelayedTask* delayed) {
463
8534
      uv_close(reinterpret_cast<uv_handle_t*>(&delayed->timer),
464
5176
               [](uv_handle_t* handle) {
465
        std::unique_ptr<DelayedTask> task {
466
910
            static_cast<DelayedTask*>(handle->data) };
467
455
        task->platform_data->DecreaseHandleCount();
468
9443
      });
469
17542
    });
470
4504
  }
471
  // Move all foreground tasks into a separate queue and flush that queue.
472
  // This way tasks that are posted while flushing the queue will be run on the
473
  // next call of FlushForegroundTasksInternal.
474
53322
  std::queue<std::unique_ptr<Task>> tasks = foreground_tasks_.PopAll();
475
57027
  while (!tasks.empty()) {
476
30369
    std::unique_ptr<Task> task = std::move(tasks.front());
477
15188
    tasks.pop();
478
15188
    did_work = true;
479
15188
    RunForegroundTask(std::move(task));
480
  }
481
53315
  return did_work;
482
}
483
484
71467
void NodePlatform::CallOnWorkerThread(std::unique_ptr<Task> task) {
485
71467
  worker_thread_task_runner_->PostTask(std::move(task));
486
71467
}
487
488
532
void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
489
                                             double delay_in_seconds) {
490
1064
  worker_thread_task_runner_->PostDelayedTask(std::move(task),
491
532
                                              delay_in_seconds);
492
532
}
493
494
495
36350
IsolatePlatformDelegate* NodePlatform::ForIsolate(Isolate* isolate) {
496
72700
  Mutex::ScopedLock lock(per_isolate_mutex_);
497
72700
  auto data = per_isolate_[isolate];
498
36350
  CHECK_NOT_NULL(data.first);
499
72700
  return data.first;
500
}
501
502
std::shared_ptr<PerIsolatePlatformData>
503
9240
NodePlatform::ForNodeIsolate(Isolate* isolate) {
504
18480
  Mutex::ScopedLock lock(per_isolate_mutex_);
505
18480
  auto data = per_isolate_[isolate];
506
9240
  CHECK_NOT_NULL(data.first);
507
18480
  return data.second;
508
}
509
510
4
bool NodePlatform::FlushForegroundTasks(Isolate* isolate) {
511
8
  std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate(isolate);
512
4
  if (!per_isolate) return false;
513
4
  return per_isolate->FlushForegroundTasksInternal();
514
}
515
516
18873
std::unique_ptr<v8::JobHandle> NodePlatform::PostJob(v8::TaskPriority priority,
517
                                       std::unique_ptr<v8::JobTask> job_task) {
518
  return v8::platform::NewDefaultJobHandle(
519
18873
      this, priority, std::move(job_task), NumberOfWorkerThreads());
520
}
521
522
bool NodePlatform::IdleTasksEnabled(Isolate* isolate) {
523
  return ForIsolate(isolate)->IdleTasksEnabled();
524
}
525
526
std::shared_ptr<v8::TaskRunner>
527
36350
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
528
36350
  return ForIsolate(isolate)->GetForegroundTaskRunner();
529
}
530
531
210501
double NodePlatform::MonotonicallyIncreasingTime() {
532
  // Convert nanos to seconds.
533
210501
  return uv_hrtime() / 1e9;
534
}
535
536
15739264
double NodePlatform::CurrentClockTimeMillis() {
537
15739264
  return SystemClockTimeMillis();
538
}
539
540
312332
v8::TracingController* NodePlatform::GetTracingController() {
541
312332
  CHECK_NOT_NULL(tracing_controller_);
542
312332
  return tracing_controller_;
543
}
544
545
4775
Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
546
4775
  return []() {
547
    fprintf(stderr, "\n");
548
    DumpBacktrace(stderr);
549
    fflush(stderr);
550
9550
  };
551
}
552
553
template <class T>
554
20042
TaskQueue<T>::TaskQueue()
555
    : lock_(), tasks_available_(), tasks_drained_(),
556
20042
      outstanding_tasks_(0), stopped_(false), task_queue_() { }
557
558
template <class T>
559
96989
void TaskQueue<T>::Push(std::unique_ptr<T> task) {
560
193978
  Mutex::ScopedLock scoped_lock(lock_);
561
96989
  outstanding_tasks_++;
562
96989
  task_queue_.push(std::move(task));
563
96989
  tasks_available_.Signal(scoped_lock);
564
96989
}
565
566
template <class T>
567
41769
std::unique_ptr<T> TaskQueue<T>::Pop() {
568
83538
  Mutex::ScopedLock scoped_lock(lock_);
569

41769
  if (task_queue_.empty()) {
570
31965
    return std::unique_ptr<T>(nullptr);
571
  }
572
19608
  std::unique_ptr<T> result = std::move(task_queue_.front());
573
9804
  task_queue_.pop();
574
9804
  return result;
575
}
576
577
template <class T>
578
90573
std::unique_ptr<T> TaskQueue<T>::BlockingPop() {
579
181152
  Mutex::ScopedLock scoped_lock(lock_);
580

257393
  while (task_queue_.empty() && !stopped_) {
581
83421
    tasks_available_.Wait(scoped_lock);
582
  }
583
90579
  if (stopped_) {
584
19078
    return std::unique_ptr<T>(nullptr);
585
  }
586
143002
  std::unique_ptr<T> result = std::move(task_queue_.front());
587
71501
  task_queue_.pop();
588
71501
  return result;
589
}
590
591
template <class T>
592
71487
void TaskQueue<T>::NotifyOfCompletion() {
593
142988
  Mutex::ScopedLock scoped_lock(lock_);
594
71501
  if (--outstanding_tasks_ == 0) {
595
33197
    tasks_drained_.Broadcast(scoped_lock);
596
  }
597
71488
}
598
599
template <class T>
600
16384
void TaskQueue<T>::BlockingDrain() {
601
32768
  Mutex::ScopedLock scoped_lock(lock_);
602
18070
  while (outstanding_tasks_ > 0) {
603
843
    tasks_drained_.Wait(scoped_lock);
604
  }
605
16384
}
606
607
template <class T>
608
4768
void TaskQueue<T>::Stop() {
609
9536
  Mutex::ScopedLock scoped_lock(lock_);
610
4768
  stopped_ = true;
611
4768
  tasks_available_.Broadcast(scoped_lock);
612
4768
}
613
614
template <class T>
615
36015
std::queue<std::unique_ptr<T>> TaskQueue<T>::PopAll() {
616
72030
  Mutex::ScopedLock scoped_lock(lock_);
617
36015
  std::queue<std::unique_ptr<T>> result;
618
36015
  result.swap(task_queue_);
619
72030
  return result;
620
}
621
622

14478
}  // namespace node