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-23 04:11:39 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
37719
      worker_data(static_cast<PlatformWorkerData*>(data));
30
31
18877
  TaskQueue<Task>* pending_worker_tasks = worker_data->task_queue;
32
  TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
33
18863
                        "PlatformWorkerThread");
34
35
4734
  // Notify the main thread that the platform worker is ready.
36
18866
  {
37
37748
    Mutex::ScopedLock lock(*worker_data->platform_workers_mutex);
38
18882
    (*worker_data->pending_platform_workers)--;
39
18882
    worker_data->platform_workers_ready->Signal(lock);
40
  }
41
42
165071
  while (std::unique_ptr<Task> task = pending_worker_tasks->BlockingPop()) {
43
73080
    task->Run();
44
73105
    pending_worker_tasks->NotifyOfCompletion();
45
73097
  }
46
18854
}
47
48
}  // namespace
49
50
4712
class WorkerThreadsTaskRunner::DelayedTaskScheduler {
51
 public:
52
4719
  explicit DelayedTaskScheduler(TaskQueue<Task>* tasks)
53
4719
    : pending_worker_tasks_(tasks) {}
54
55
4719
  std::unique_ptr<uv_thread_t> Start() {
56
14157
    auto start_thread = [](void* data) {
57
4719
      static_cast<DelayedTaskScheduler*>(data)->Run();
58
14150
    };
59
4719
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
60
4719
    uv_sem_init(&ready_, 0);
61
4719
    CHECK_EQ(0, uv_thread_create(t.get(), start_thread, this));
62
4719
    uv_sem_wait(&ready_);
63
4719
    uv_sem_destroy(&ready_);
64
4719
    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
4712
  void Stop() {
74
4712
    tasks_.Push(std::make_unique<StopTask>(this));
75
4712
    uv_async_send(&flush_tasks_);
76
4712
  }
77
78
 private:
79
4719
  void Run() {
80
4719
    TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
81
                          "WorkerThreadsTaskRunner::DelayedTaskScheduler");
82
4719
    loop_.data = this;
83
4719
    CHECK_EQ(0, uv_loop_init(&loop_));
84
9438
    flush_tasks_.data = this;
85
4719
    CHECK_EQ(0, uv_async_init(&loop_, &flush_tasks_, FlushTasks));
86
4719
    uv_sem_post(&ready_);
87
88
4719
    uv_run(&loop_, UV_RUN_DEFAULT);
89
4712
    CheckedUvLoopClose(&loop_);
90
4712
  }
91
92
5244
  static void FlushTasks(uv_async_t* flush_tasks) {
93
    DelayedTaskScheduler* scheduler =
94
5244
        ContainerOf(&DelayedTaskScheduler::loop_, flush_tasks->loop);
95
15732
    while (std::unique_ptr<Task> task = scheduler->tasks_.Pop())
96
10488
      task->Run();
97
5244
  }
98
99
9424
  class StopTask : public Task {
100
   public:
101
4712
    explicit StopTask(DelayedTaskScheduler* scheduler): scheduler_(scheduler) {}
102
103
4712
    void Run() override {
104
9424
      std::vector<uv_timer_t*> timers;
105
5208
      for (uv_timer_t* timer : scheduler_->timers_)
106
496
        timers.push_back(timer);
107
5208
      for (uv_timer_t* timer : timers)
108
496
        scheduler_->TakeTimerTask(timer);
109
9424
      uv_close(reinterpret_cast<uv_handle_t*>(&scheduler_->flush_tasks_),
110
18848
               [](uv_handle_t* handle) {});
111
4712
    }
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
4719
WorkerThreadsTaskRunner::WorkerThreadsTaskRunner(int thread_pool_size) {
167
9438
  Mutex platform_workers_mutex;
168
9438
  ConditionVariable platform_workers_ready;
169
170
9438
  Mutex::ScopedLock lock(platform_workers_mutex);
171
4719
  int pending_platform_workers = thread_pool_size;
172
173
9438
  delayed_task_scheduler_ = std::make_unique<DelayedTaskScheduler>(
174
14157
      &pending_worker_tasks_);
175
4719
  threads_.push_back(delayed_task_scheduler_->Start());
176
177
23601
  for (int i = 0; i < thread_pool_size; i++) {
178
    PlatformWorkerData* worker_data = new PlatformWorkerData{
179
18882
      &pending_worker_tasks_, &platform_workers_mutex,
180
      &platform_workers_ready, &pending_platform_workers, i
181
37764
    };
182
37764
    std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
183
18882
    if (uv_thread_create(t.get(), PlatformWorkerThread,
184
                         worker_data) != 0) {
185
      break;
186
    }
187
18882
    threads_.push_back(std::move(t));
188
  }
189
190
  // Wait for platform workers to initialize before continuing with the
191
  // bootstrap.
192
39331
  while (pending_platform_workers > 0) {
193
17306
    platform_workers_ready.Wait(lock);
194
  }
195
4719
}
196
197
73085
void WorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
198
73085
  pending_worker_tasks_.Push(std::move(task));
199
73085
}
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
15524
void WorkerThreadsTaskRunner::BlockingDrain() {
207
15524
  pending_worker_tasks_.BlockingDrain();
208
15524
}
209
210
4712
void WorkerThreadsTaskRunner::Shutdown() {
211
4712
  pending_worker_tasks_.Stop();
212
4712
  delayed_task_scheduler_->Stop();
213
28278
  for (size_t i = 0; i < threads_.size(); i++) {
214
23566
    CHECK_EQ(0, uv_thread_join(threads_[i].get()));
215
  }
216
4712
}
217
218
42426
int WorkerThreadsTaskRunner::NumberOfWorkerThreads() const {
219
42426
  return threads_.size();
220
}
221
222
5198
PerIsolatePlatformData::PerIsolatePlatformData(
223
5198
    Isolate* isolate, uv_loop_t* loop)
224
5198
  : isolate_(isolate), loop_(loop) {
225
5198
  flush_tasks_ = new uv_async_t();
226
5198
  CHECK_EQ(0, uv_async_init(loop, flush_tasks_, FlushTasks));
227
5198
  flush_tasks_->data = static_cast<void*>(this);
228
5198
  uv_unref(reinterpret_cast<uv_handle_t*>(flush_tasks_));
229
5198
}
230
231
std::shared_ptr<v8::TaskRunner>
232
36260
PerIsolatePlatformData::GetForegroundTaskRunner() {
233
36260
  return shared_from_this();
234
}
235
236
11463
void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
237
11463
  auto platform_data = static_cast<PerIsolatePlatformData*>(handle->data);
238
11463
  platform_data->FlushForegroundTasksInternal();
239
11463
}
240
241
void PerIsolatePlatformData::PostIdleTask(std::unique_ptr<v8::IdleTask> task) {
242
  UNREACHABLE();
243
}
244
245
15621
void PerIsolatePlatformData::PostTask(std::unique_ptr<Task> task) {
246
15621
  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
15621
  foreground_tasks_.Push(std::move(task));
252
15621
  uv_async_send(flush_tasks_);
253
}
254
255
4622
void PerIsolatePlatformData::PostDelayedTask(
256
    std::unique_ptr<Task> task, double delay_in_seconds) {
257
4622
  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
9244
  std::unique_ptr<DelayedTask> delayed(new DelayedTask());
263
4622
  delayed->task = std::move(task);
264
4622
  delayed->platform_data = shared_from_this();
265
4622
  delayed->timeout = delay_in_seconds;
266
4622
  foreground_delayed_tasks_.Push(std::move(delayed));
267
4622
  uv_async_send(flush_tasks_);
268
}
269
270
9427
void PerIsolatePlatformData::PostNonNestableTask(std::unique_ptr<Task> task) {
271
9427
  PostTask(std::move(task));
272
9427
}
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
980
PerIsolatePlatformData::~PerIsolatePlatformData() {
281
490
  CHECK(!flush_tasks_);
282
490
}
283
284
436
void PerIsolatePlatformData::AddShutdownCallback(void (*callback)(void*),
285
                                                 void* data) {
286
436
  shutdown_callbacks_.emplace_back(ShutdownCallback { callback, data });
287
436
}
288
289
4742
void PerIsolatePlatformData::Shutdown() {
290
4742
  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
4742
  foreground_delayed_tasks_.PopAll();
298
4742
  foreground_tasks_.PopAll();
299
4742
  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
4742
  self_reference_ = shared_from_this();
306
9484
  uv_close(reinterpret_cast<uv_handle_t*>(flush_tasks_),
307
5724
           [](uv_handle_t* handle) {
308
    std::unique_ptr<uv_async_t> flush_tasks {
309
982
        reinterpret_cast<uv_async_t*>(handle) };
310
    PerIsolatePlatformData* platform_data =
311
491
        static_cast<PerIsolatePlatformData*>(flush_tasks->data);
312
491
    platform_data->DecreaseHandleCount();
313
491
    platform_data->self_reference_.reset();
314
10465
  });
315
4742
  flush_tasks_ = nullptr;
316
}
317
318
951
void PerIsolatePlatformData::DecreaseHandleCount() {
319
951
  CHECK_GE(uv_handle_count_, 1);
320
951
  if (--uv_handle_count_ == 0) {
321
926
    for (const auto& callback : shutdown_callbacks_)
322
436
      callback.cb(callback.data);
323
  }
324
951
}
325
326
4719
NodePlatform::NodePlatform(int thread_pool_size,
327
4719
                           v8::TracingController* tracing_controller) {
328
4719
  if (tracing_controller != nullptr) {
329
4712
    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
4719
  SetTracingController(tracing_controller_);
337
  DCHECK_EQ(GetTracingController(), tracing_controller_);
338
  worker_thread_task_runner_ =
339
4719
      std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
340
4719
}
341
342
14136
NodePlatform::~NodePlatform() {
343
4712
  Shutdown();
344
9424
}
345
346
5197
void NodePlatform::RegisterIsolate(Isolate* isolate, uv_loop_t* loop) {
347
10394
  Mutex::ScopedLock lock(per_isolate_mutex_);
348
10394
  auto delegate = std::make_shared<PerIsolatePlatformData>(isolate, loop);
349
5197
  IsolatePlatformDelegate* ptr = delegate.get();
350
  auto insertion = per_isolate_.emplace(
351
    isolate,
352
5197
    std::make_pair(ptr, std::move(delegate)));
353
5197
  CHECK(insertion.second);
354
5197
}
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
4742
void NodePlatform::UnregisterIsolate(Isolate* isolate) {
366
9484
  Mutex::ScopedLock lock(per_isolate_mutex_);
367
4742
  auto existing_it = per_isolate_.find(isolate);
368
4742
  CHECK_NE(existing_it, per_isolate_.end());
369
4742
  auto& existing = existing_it->second;
370
4742
  if (existing.second) {
371
4741
    existing.second->Shutdown();
372
  }
373
4742
  per_isolate_.erase(existing_it);
374
4742
}
375
376
436
void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate,
377
                                              void (*cb)(void*), void* data) {
378
872
  Mutex::ScopedLock lock(per_isolate_mutex_);
379
436
  auto it = per_isolate_.find(isolate);
380
436
  if (it == per_isolate_.end()) {
381
    cb(data);
382
    return;
383
  }
384
436
  CHECK(it->second.second);
385
436
  it->second.second->AddShutdownCallback(cb, data);
386
}
387
388
9419
void NodePlatform::Shutdown() {
389
9419
  if (has_shut_down_) return;
390
4712
  has_shut_down_ = true;
391
4712
  worker_thread_task_runner_->Shutdown();
392
393
  {
394
9424
    Mutex::ScopedLock lock(per_isolate_mutex_);
395
4712
    per_isolate_.clear();
396
  }
397
}
398
399
42426
int NodePlatform::NumberOfWorkerThreads() {
400
42426
  return worker_thread_task_runner_->NumberOfWorkerThreads();
401
}
402
403
15365
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
404
15365
  DebugSealHandleScope scope(isolate_);
405
15365
  Environment* env = Environment::GetCurrent(isolate_);
406
15365
  if (env != nullptr) {
407
28283
    v8::HandleScope scope(isolate_);
408
14145
    InternalCallbackScope cb_scope(env, Object::New(isolate_), { 0, 0 },
409
42435
                                   InternalCallbackScope::kNoFlags);
410
14145
    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
1220
    task->Run();
416
  }
417
15358
}
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
9370
void NodePlatform::DrainTasks(Isolate* isolate) {
436
18732
  std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate(isolate);
437
9370
  if (!per_isolate) return;
438
439

15524
  do {
440
    // Worker tasks aren't associated with an Isolate.
441
15524
    worker_thread_task_runner_->BlockingDrain();
442
15524
  } while (per_isolate->FlushForegroundTasksInternal());
443
}
444
445
26991
bool PerIsolatePlatformData::FlushForegroundTasksInternal() {
446
26991
  bool did_work = false;
447
448
  while (std::unique_ptr<DelayedTask> delayed =
449
35909
      foreground_delayed_tasks_.Pop()) {
450
4459
    did_work = true;
451
4459
    uint64_t delay_millis = llround(delayed->timeout * 1000);
452
453
4459
    delayed->timer.data = static_cast<void*>(delayed.get());
454
4459
    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
4459
    uv_timer_start(&delayed->timer, RunForegroundTask, delay_millis, 0);
458
4459
    uv_unref(reinterpret_cast<uv_handle_t*>(&delayed->timer));
459
4459
    uv_handle_count_++;
460
461
8918
    scheduled_delayed_tasks_.emplace_back(delayed.release(),
462
13131
                                          [](DelayedTask* delayed) {
463
8672
      uv_close(reinterpret_cast<uv_handle_t*>(&delayed->timer),
464
5256
               [](uv_handle_t* handle) {
465
        std::unique_ptr<DelayedTask> task {
466
920
            static_cast<DelayedTask*>(handle->data) };
467
460
        task->platform_data->DecreaseHandleCount();
468
9592
      });
469
17590
    });
470
4459
  }
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
53974
  std::queue<std::unique_ptr<Task>> tasks = foreground_tasks_.PopAll();
475
57702
  while (!tasks.empty()) {
476
30719
    std::unique_ptr<Task> task = std::move(tasks.front());
477
15363
    tasks.pop();
478
15363
    did_work = true;
479
15363
    RunForegroundTask(std::move(task));
480
  }
481
53967
  return did_work;
482
}
483
484
73085
void NodePlatform::CallOnWorkerThread(std::unique_ptr<Task> task) {
485
73085
  worker_thread_task_runner_->PostTask(std::move(task));
486
73085
}
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
36258
IsolatePlatformDelegate* NodePlatform::ForIsolate(Isolate* isolate) {
496
72518
  Mutex::ScopedLock lock(per_isolate_mutex_);
497
72520
  auto data = per_isolate_[isolate];
498
36260
  CHECK_NOT_NULL(data.first);
499
72520
  return data.first;
500
}
501
502
std::shared_ptr<PerIsolatePlatformData>
503
9374
NodePlatform::ForNodeIsolate(Isolate* isolate) {
504
18748
  Mutex::ScopedLock lock(per_isolate_mutex_);
505
18748
  auto data = per_isolate_[isolate];
506
9374
  CHECK_NOT_NULL(data.first);
507
18748
  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
19040
std::unique_ptr<v8::JobHandle> NodePlatform::PostJob(v8::TaskPriority priority,
517
                                       std::unique_ptr<v8::JobTask> job_task) {
518
  return v8::platform::NewDefaultJobHandle(
519
19040
      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
36258
NodePlatform::GetForegroundTaskRunner(Isolate* isolate) {
528
36258
  return ForIsolate(isolate)->GetForegroundTaskRunner();
529
}
530
531
217414
double NodePlatform::MonotonicallyIncreasingTime() {
532
  // Convert nanos to seconds.
533
217414
  return uv_hrtime() / 1e9;
534
}
535
536
15389444
double NodePlatform::CurrentClockTimeMillis() {
537
15389444
  return SystemClockTimeMillis();
538
}
539
540
309138
v8::TracingController* NodePlatform::GetTracingController() {
541
309138
  CHECK_NOT_NULL(tracing_controller_);
542
309138
  return tracing_controller_;
543
}
544
545
4719
Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
546
4719
  return []() {
547
    fprintf(stderr, "\n");
548
    DumpBacktrace(stderr);
549
    fflush(stderr);
550
9438
  };
551
}
552
553
template <class T>
554
19834
TaskQueue<T>::TaskQueue()
555
    : lock_(), tasks_available_(), tasks_drained_(),
556
19834
      outstanding_tasks_(0), stopped_(false), task_queue_() { }
557
558
template <class T>
559
98608
void TaskQueue<T>::Push(std::unique_ptr<T> task) {
560
197216
  Mutex::ScopedLock scoped_lock(lock_);
561
98608
  outstanding_tasks_++;
562
98608
  task_queue_.push(std::move(task));
563
98608
  tasks_available_.Signal(scoped_lock);
564
98608
}
565
566
template <class T>
567
41938
std::unique_ptr<T> TaskQueue<T>::Pop() {
568
83876
  Mutex::ScopedLock scoped_lock(lock_);
569

41938
  if (task_queue_.empty()) {
570
32235
    return std::unique_ptr<T>(nullptr);
571
  }
572
19406
  std::unique_ptr<T> result = std::move(task_queue_.front());
573
9703
  task_queue_.pop();
574
9703
  return result;
575
}
576
577
template <class T>
578
91969
std::unique_ptr<T> TaskQueue<T>::BlockingPop() {
579
183944
  Mutex::ScopedLock scoped_lock(lock_);
580

260563
  while (task_queue_.empty() && !stopped_) {
581
84308
    tasks_available_.Wait(scoped_lock);
582
  }
583
91975
  if (stopped_) {
584
18854
    return std::unique_ptr<T>(nullptr);
585
  }
586
146242
  std::unique_ptr<T> result = std::move(task_queue_.front());
587
73121
  task_queue_.pop();
588
73121
  return result;
589
}
590
591
template <class T>
592
73101
void TaskQueue<T>::NotifyOfCompletion() {
593
146222
  Mutex::ScopedLock scoped_lock(lock_);
594
73121
  if (--outstanding_tasks_ == 0) {
595
33293
    tasks_drained_.Broadcast(scoped_lock);
596
  }
597
73113
}
598
599
template <class T>
600
15524
void TaskQueue<T>::BlockingDrain() {
601
31048
  Mutex::ScopedLock scoped_lock(lock_);
602
17246
  while (outstanding_tasks_ > 0) {
603
861
    tasks_drained_.Wait(scoped_lock);
604
  }
605
15524
}
606
607
template <class T>
608
4712
void TaskQueue<T>::Stop() {
609
9424
  Mutex::ScopedLock scoped_lock(lock_);
610
4712
  stopped_ = true;
611
4712
  tasks_available_.Broadcast(scoped_lock);
612
4712
}
613
614
template <class T>
615
36475
std::queue<std::unique_ptr<T>> TaskQueue<T>::PopAll() {
616
72950
  Mutex::ScopedLock scoped_lock(lock_);
617
36475
  std::queue<std::unique_ptr<T>> result;
618
36475
  result.swap(task_queue_);
619
72949
  return result;
620
}
621
622

14313
}  // namespace node