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-22 04:13:51 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
18866
static void PlatformWorkerThread(void* data) {
28
  std::unique_ptr<PlatformWorkerData>
29
37716
      worker_data(static_cast<PlatformWorkerData*>(data));
30
31
18874
  TaskQueue<Task>* pending_worker_tasks = worker_data->task_queue;
32
  TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
33
18865
                        "PlatformWorkerThread");
34
35
4721
  // Notify the main thread that the platform worker is ready.
36
18870
  {
37
37748
    Mutex::ScopedLock lock(*worker_data->platform_workers_mutex);
38
18878
    (*worker_data->pending_platform_workers)--;
39
18878
    worker_data->platform_workers_ready->Signal(lock);
40
  }
41
42
164854
  while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
43
72978
    task->Run();
44
72991
    pending_worker_tasks->NotifyOfCompletion();
45
72992
  }
46
18850
}
47
48
}  // namespace
49
50
4711
class WorkerThreadsTaskRunner::DelayedTaskScheduler {
51
 public:
52
4718
  explicit DelayedTaskScheduler(TaskQueue<Task>* tasks)
53
4718
    : pending_worker_tasks_(tasks) {}
54
55
4718
  std::unique_ptr<uv_thread_t> Start() {
56
14154
    auto start_thread = [](void* data) {
57
4718
      static_cast<DelayedTaskScheduler*>(data)->Run();
58
14147
    };
59
4718
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
60
4718
    uv_sem_init(&ready_, 0);
61
4718
    CHECK_EQ(0, uv_thread_create(t.get(), start_thread, this));
62
4718
    uv_sem_wait(&ready_);
63
4718
    uv_sem_destroy(&ready_);
64
4718
    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
4711
  void Stop() {
74
4711
    tasks_.Push(std::make_unique<StopTask>(this));
75
4711
    uv_async_send(&flush_tasks_);
76
4711
  }
77
78
 private:
79
4718
  void Run() {
80
4718
    TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
81
                          "WorkerThreadsTaskRunner::DelayedTaskScheduler");
82
4718
    loop_.data = this;
83
4718
    CHECK_EQ(0, uv_loop_init(&loop_));
84
9436
    flush_tasks_.data = this;
85
4718
    CHECK_EQ(0, uv_async_init(&loop_, &flush_tasks_, FlushTasks));
86
4718
    uv_sem_post(&ready_);
87
88
4718
    uv_run(&loop_, UV_RUN_DEFAULT);
89
4711
    CheckedUvLoopClose(&loop_);
90
4711
  }
91
92
5243
  static void FlushTasks(uv_async_t* flush_tasks) {
93
    DelayedTaskScheduler* scheduler =
94
5243
        ContainerOf(&DelayedTaskScheduler::loop_, flush_tasks->loop);
95
15729
    while (std::unique_ptr<Task> task = scheduler->tasks_.Pop())
96
10486
      task->Run();
97
5243
  }
98
99
9422
  class StopTask : public Task {
100
   public:
101
4711
    explicit StopTask(DelayedTaskScheduler* scheduler): scheduler_(scheduler) {}
102
103
4711
    void Run() override {
104
9422
      std::vector<uv_timer_t*> timers;
105
5203
      for (uv_timer_t* timer : scheduler_->timers_)
106
492
        timers.push_back(timer);
107
5203
      for (uv_timer_t* timer : timers)
108
492
        scheduler_->TakeTimerTask(timer);
109
9422
      uv_close(reinterpret_cast<uv_handle_t*>(&scheduler_->flush_tasks_),
110
18844
               [](uv_handle_t* handle) {});
111
4711
    }
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
40
  static void RunTask(uv_timer_t* timer) {
142
    DelayedTaskScheduler* scheduler =
143
40
        ContainerOf(&DelayedTaskScheduler::loop_, timer->loop);
144
40
    scheduler->pending_worker_tasks_->Push(scheduler->TakeTimerTask(timer));
145
40
  }
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
4718
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
167
9436
  Mutex platform_workers_mutex;
168
9436
  ConditionVariable platform_workers_ready;
169
170
9436
  Mutex::ScopedLock lock(platform_workers_mutex);
171
4718
  int pending_platform_workers = thread_pool_size;
172
173
9436
  delayed_task_scheduler_ = std::make_unique<DelayedTaskScheduler>(
174
14154
      &pending_worker_tasks_);
175
4718
  threads_.push_back(delayed_task_scheduler_->Start());
176
177
23596
  for (int i = 0; i < thread_pool_size; i++) {
178
    PlatformWorkerData* worker_data = new PlatformWorkerData{
179
18878
      &pending_worker_tasks_, &platform_workers_mutex,
180
      &platform_workers_ready, &pending_platform_workers, i
181
37756
    };
182
37756
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
183
18878
    if (uv_thread_create(t.get(), PlatformWorkerThread,
184
                         worker_data) != 0) {
185
      break;
186
    }
187
18878
    threads_.push_back(std::move(t));
188
  }
189
190
  // Wait for platform workers to initialize before continuing with the
191
  // bootstrap.
192
39234
  while (pending_platform_workers > 0) {
193
17258
    platform_workers_ready.Wait(lock);
194
  }
195
4718
}
196
197
72983
void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
198
72983
  pending_worker_tasks_.Push(std::move(task));
199
72984
}
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
15477
void WorkerThreadsTaskRunner::BlockingDrain() {
207
15477
  pending_worker_tasks_.BlockingDrain();
208
15477
}
209
210
4711
void WorkerThreadsTaskRunner::Shutdown() {
211
4711
  pending_worker_tasks_.Stop();
212
4711
  delayed_task_scheduler_->Stop();
213
28272
  for (size_t i = 0; i < threads_.size(); i++) {
214
23561
    CHECK_EQ(0, uv_thread_join(threads_[i].get()));
215
  }
216
4711
}
217
218
42183
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
219
42183
  return threads_.size();
220
}
221
222
5194
PerIsolatePlatformData::PerIsolatePlatformData(
223
5194
    Isolate* isolate, uv_loop_t* loop)
224
5194
  : isolate_(isolate), loop_(loop) {
225
5194
  flush_tasks_ = new uv_async_t();
226
5194
  CHECK_EQ(0, uv_async_init(loop, flush_tasks_, FlushTasks));
227
5194
  flush_tasks_->data = static_cast<void*>(this);
228
5194
  uv_unref(reinterpret_cast<uv_handle_t*>(flush_tasks_));
229
5194
}
230
231
std::shared_ptr<v8::TaskRunner>
232
36112
PerIsolatePlatformData::GetForegroundTaskRunner() {
233
36112
  return shared_from_this();
234
}
235
236
11382
void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
237
11382
  auto platform_data = static_cast<PerIsolatePlatformData*>(handle->data);
238
11382
  platform_data->FlushForegroundTasksInternal();
239
11382
}
240
241
void PerIsolatePlatformData::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
242
  UNREACHABLE();
243
}
244
245
15489
void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
246
15489
  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
15489
  foreground_tasks_.Push(std::move(task));
252
15489
  uv_async_send(flush_tasks_);
253
}
254
255
4620
void PerIsolatePlatformData::PostDelayedTask(
256
    std::unique_ptr<Task> task, double delay_in_seconds) {
257
4620
  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
9240
  std::unique_ptr<DelayedTask> delayed(new DelayedTask());
263
4620
  delayed->task = std::move(task);
264
4620
  delayed->platform_data = shared_from_this();
265
4620
  delayed->timeout = delay_in_seconds;
266
4620
  foreground_delayed_tasks_.Push(std::move(delayed));
267
4620
  uv_async_send(flush_tasks_);
268
}
269
270
9297
void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr<Task> task) {
271
9297
  PostTask(std::move(task));
272
9297
}
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
976
PerIsolatePlatformData::~PerIsolatePlatformData() {
281
488
  CHECK(!flush_tasks_);
282
488
}
283
284
433
void PerIsolatePlatformData::AddShutdownCallback(void (*callback)(void*),
285
                                                 void* data) {
286
433
  shutdown_callbacks_.emplace_back(ShutdownCallback { callback, data });
287
433
}
288
289
4738
void PerIsolatePlatformData::Shutdown() {
290
4738
  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
4738
  foreground_delayed_tasks_.PopAll();
298
4738
  foreground_tasks_.PopAll();
299
4738
  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
4738
  self_reference_ = shared_from_this();
306
9476
  uv_close(reinterpret_cast<uv_handle_t*>(flush_tasks_),
307
5714
           [](uv_handle_t* handle) {
308
    std::unique_ptr<uv_async_t> flush_tasks {
309
976
        reinterpret_cast<uv_async_t*>(handle) };
310
    PerIsolatePlatformData* platform_data =
311
488
        static_cast<PerIsolatePlatformData*>(flush_tasks->data);
312
488
    platform_data->DecreaseHandleCount();
313
488
    platform_data->self_reference_.reset();
314
10452
  });
315
4738
  flush_tasks_ = nullptr;
316
}
317
318
948
void PerIsolatePlatformData::DecreaseHandleCount() {
319
948
  CHECK_GE(uv_handle_count_, 1);
320
948
  if (--uv_handle_count_ == 0) {
321
921
    for (const auto& callback : shutdown_callbacks_)
322
433
      callback.cb(callback.data);
323
  }
324
948
}
325
326
4718
NodePlatform::NodePlatform(int thread_pool_size,
327
4718
                           v8::TracingController* tracing_controller) {
328
4718
  if (tracing_controller != nullptr) {
329
4711
    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
4718
  SetTracingController(tracing_controller_);
337
  DCHECK_EQ(GetTracingController(), tracing_controller_);
338
  worker_thread_task_runner_ =
339
4718
      std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
340
4718
}
341
342
14133
NodePlatform::~NodePlatform() {
343
4711
  Shutdown();
344
9422
}
345
346
5193
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
347
10386
  Mutex::ScopedLock lock(per_isolate_mutex_);
348
10386
  auto delegate = std::make_shared<PerIsolatePlatformData>(isolate, loop);
349
5193
  IsolatePlatformDelegate* ptr = delegate.get();
350
  auto insertion = per_isolate_.emplace(
351
    isolate,
352
5193
    std::make_pair(ptr, std::move(delegate)));
353
5193
  CHECK(insertion.second);
354
5193
}
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
4738
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
366
9476
  Mutex::ScopedLock lock(per_isolate_mutex_);
367
4738
  auto existing_it = per_isolate_.find(isolate);
368
4738
  CHECK_NE(existing_it, per_isolate_.end());
369
4738
  auto& existing = existing_it->second;
370
4738
  if (existing.second) {
371
4737
    existing.second->Shutdown();
372
  }
373
4738
  per_isolate_.erase(existing_it);
374
4738
}
375
376
433
void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate,
377
                                              void (*cb)(void*), void* data) {
378
866
  Mutex::ScopedLock lock(per_isolate_mutex_);
379
433
  auto it = per_isolate_.find(isolate);
380
433
  if (it == per_isolate_.end()) {
381
    cb(data);
382
    return;
383
  }
384
433
  CHECK(it->second.second);
385
433
  it->second.second->AddShutdownCallback(cb, data);
386
}
387
388
9417
void NodePlatform::Shutdown() {
389
9417
  if (has_shut_down_) return;
390
4711
  has_shut_down_ = true;
391
4711
  worker_thread_task_runner_->Shutdown();
392
393
  {
394
9422
    Mutex::ScopedLock lock(per_isolate_mutex_);
395
4711
    per_isolate_.clear();
396
  }
397
}
398
399
42183
int NodePlatform::NumberOfWorkerThreads() {
400
42183
  return worker_thread_task_runner_->NumberOfWorkerThreads();
401
}
402
403
15235
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
404
15235
  DebugSealHandleScope scope(isolate_);
405
15235
  Environment* env = Environment::GetCurrent(isolate_);
406
15235
  if (env != nullptr) {
407
28095
    v8::HandleScope scope(isolate_);
408
14051
    InternalCallbackScope cb_scope(env, Object::New(isolate_), { 0, 0 },
409
42153
                                   InternalCallbackScope::kNoFlags);
410
14051
    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
1184
    task->Run();
416
  }
417
15228
}
418
419
2
void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) {
420
  auto it = std::find_if(scheduled_delayed_tasks_.begin(),
421
                         scheduled_delayed_tasks_.end(),
422
4
                         [task](const DelayedTaskPointer& delayed) -> bool {
423
4
          return delayed.get() == task;
424
2
      });
425
2
  CHECK_NE(it, scheduled_delayed_tasks_.end());
426
2
  scheduled_delayed_tasks_.erase(it);
427
2
}
428
429
2
void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) {
430
2
  DelayedTask* delayed = ContainerOf(&DelayedTask::timer, handle);
431
2
  delayed->platform_data->RunForegroundTask(std::move(delayed->task));
432
2
  delayed->platform_data->DeleteFromScheduledTasks(delayed);
433
2
}
434
435
9366
void NodePlatform::DrainTasks(Isolate* isolate) {
436
18724
  std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate(isolate);
437
9366
  if (!per_isolate) return;
438
439

15477
  do {
440
    // Worker tasks aren't associated with an Isolate.
441
15477
    worker_thread_task_runner_->BlockingDrain();
442
15477
  } while (per_isolate->FlushForegroundTasksInternal());
443
}
444
445
26863
bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
446
26863
  bool did_work = false;
447
448
  while (std::unique_ptr<DelayedTask> delayed =
449
35779
      foreground_delayed_tasks_.Pop()) {
450
4458
    did_work = true;
451
4458
    uint64_t delay_millis = llround(delayed->timeout * 1000);
452
453
4458
    delayed->timer.data = static_cast<void*>(delayed.get());
454
4458
    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
4458
    uv_timer_start(&delayed->timer, RunForegroundTask, delay_millis, 0);
458
4458
    uv_unref(reinterpret_cast<uv_handle_t*>(&delayed->timer));
459
4458
    uv_handle_count_++;
460
461
8916
    scheduled_delayed_tasks_.emplace_back(delayed.release(),
462
13126
                                          [](DelayedTask* delayed) {
463
8668
      uv_close(reinterpret_cast<uv_handle_t*>(&delayed->timer),
464
5254
               [](uv_handle_t* handle) {
465
        std::unique_ptr<DelayedTask> task {
466
920
            static_cast<DelayedTask*>(handle->data) };
467
460
        task->platform_data->DecreaseHandleCount();
468
9588
      });
469
17584
    });
470
4458
  }
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
53719
  std::queue<std::unique_ptr<Task>> tasks = foreground_tasks_.PopAll();
475
57315
  while (!tasks.empty()) {
476
30459
    std::unique_ptr<Task> task = std::move(tasks.front());
477
15233
    tasks.pop();
478
15233
    did_work = true;
479
15233
    RunForegroundTask(std::move(task));
480
  }
481
53712
  return did_work;
482
}
483
484
72984
void NodePlatform::CallOnWorkerThread(std::unique_ptr<Task> task) {
485
72984
  worker_thread_task_runner_->PostTask(std::move(task));
486
72984
}
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
36112
IsolatePlatformDelegate* NodePlatform::ForIsolate(Isolate* isolate) {
496
72224
  Mutex::ScopedLock lock(per_isolate_mutex_);
497
72224
  auto data = per_isolate_[isolate];
498
36112
  CHECK_NOT_NULL(data.first);
499
72224
  return data.first;
500
}
501
502
std::shared_ptr<PerIsolatePlatformData>
503
9370
NodePlatform::ForNodeIsolate(Isolate* isolate) {
504
18740
  Mutex::ScopedLock lock(per_isolate_mutex_);
505
18740
  auto data = per_isolate_[isolate];
506
9370
  CHECK_NOT_NULL(data.first);
507
18740
  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
18933
std::unique_ptr<v8::JobHandle> NodePlatform::PostJob(v8::TaskPriority priority,
517
                                       std::unique_ptr<v8::JobTask> job_task) {
518
  return v8::platform::NewDefaultJobHandle(
519
18933
      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
36112
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
528
36112
  return ForIsolate(isolate)->GetForegroundTaskRunner();
529
}
530
531
216583
double NodePlatform::MonotonicallyIncreasingTime() {
532
  // Convert nanos to seconds.
533
216583
  return uv_hrtime() / 1e9;
534
}
535
536
15008353
double NodePlatform::CurrentClockTimeMillis() {
537
15008353
  return SystemClockTimeMillis();
538
}
539
540
308980
v8::TracingController* NodePlatform::GetTracingController() {
541
308980
  CHECK_NOT_NULL(tracing_controller_);
542
308980
  return tracing_controller_;
543
}
544
545
4718
Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
546
4718
  return []() {
547
    fprintf(stderr, "\n");
548
    DumpBacktrace(stderr);
549
    fflush(stderr);
550
9436
  };
551
}
552
553
template <class T>
554
19824
TaskQueue<T>::TaskQueue()
555
    : lock_(), tasks_available_(), tasks_drained_(),
556
19824
      outstanding_tasks_(0), stopped_(false), task_queue_() { }
557
558
template <class T>
559
98376
void TaskQueue<T>::Push(std::unique_ptr<T> task) {
560
196752
  Mutex::ScopedLock scoped_lock(lock_);
561
98376
  outstanding_tasks_++;
562
98376
  task_queue_.push(std::move(task));
563
98376
  tasks_available_.Signal(scoped_lock);
564
98376
}
565
566
template <class T>
567
41807
std::unique_ptr<T> TaskQueue<T>::Pop() {
568
83614
  Mutex::ScopedLock scoped_lock(lock_);
569

41807
  if (task_queue_.empty()) {
570
32106
    return std::unique_ptr<T>(nullptr);
571
  }
572
19402
  std::unique_ptr<T> result = std::move(task_queue_.front());
573
9701
  task_queue_.pop();
574
9701
  return result;
575
}
576
577
template <class T>
578
91853
std::unique_ptr<T> TaskQueue<T>::BlockingPop() {
579
183727
  Mutex::ScopedLock scoped_lock(lock_);
580

260540
  while (task_queue_.empty() && !stopped_) {
581
84347
    tasks_available_.Wait(scoped_lock);
582
  }
583
91874
  if (stopped_) {
584
18850
    return std::unique_ptr<T>(nullptr);
585
  }
586
146048
  std::unique_ptr<T> result = std::move(task_queue_.front());
587
73024
  task_queue_.pop();
588
73024
  return result;
589
}
590
591
template <class T>
592
72989
void TaskQueue<T>::NotifyOfCompletion() {
593
146013
  Mutex::ScopedLock scoped_lock(lock_);
594
73024
  if (--outstanding_tasks_ == 0) {
595
33203
    tasks_drained_.Broadcast(scoped_lock);
596
  }
597
73009
}
598
599
template <class T>
600
15477
void TaskQueue<T>::BlockingDrain() {
601
30954
  Mutex::ScopedLock scoped_lock(lock_);
602
17179
  while (outstanding_tasks_ > 0) {
603
851
    tasks_drained_.Wait(scoped_lock);
604
  }
605
15477
}
606
607
template <class T>
608
4711
void TaskQueue<T>::Stop() {
609
9422
  Mutex::ScopedLock scoped_lock(lock_);
610
4711
  stopped_ = true;
611
4711
  tasks_available_.Broadcast(scoped_lock);
612
4711
}
613
614
template <class T>
615
36339
std::queue<std::unique_ptr<T>> TaskQueue<T>::PopAll() {
616
72678
  Mutex::ScopedLock scoped_lock(lock_);
617
36339
  std::queue<std::unique_ptr<T>> result;
618
36339
  result.swap(task_queue_);
619
72678
  return result;
620
}
621
622

14310
}  // namespace node