FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_platform.cc at fix/888923 · ofrobots/node · GitHub
ofrobots
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node_platform.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
531 lines (450 loc) · 16 KB
Breadcrumbs
node
/
src
/
node_platform.cc
Copy path
File metadata and controls
531 lines (450 loc) · 16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#
include
"
node_platform.h
"
#
include
"
node_internals.h
"
#
include
"
env-inl.h
"
#
include
"
debug_utils.h
"
#
include
"
util.h
"
#
include
<
algorithm
>
namespace
node
{
using
v8::HandleScope;
using
v8::Isolate;
using
v8::Local;
using
v8::Object;
using
v8::Platform;
using
v8::Task;
using
node::tracing::TracingController;
struct
PlatformWorkerData
{
TaskQueue<Task>* task_queue;
Mutex* platform_workers_mutex;
ConditionVariable* platform_workers_ready;
int
* pending_platform_workers;
int
id;
};
static
void
BackgroundRunner
(
void
* data) {
std::unique_ptr<PlatformWorkerData>
worker_data
(
static_cast
<PlatformWorkerData*>(data));
TRACE_EVENT_METADATA1
(
"
__metadata
"
,
"
thread_name
"
,
"
name
"
,
"
BackgroundTaskRunner
"
);
//
Notify the main thread that the platform worker is ready.
{
Mutex::ScopedLock
lock
(*worker_data->
platform_workers_mutex
);
(*worker_data->
pending_platform_workers
)--;
worker_data->
platform_workers_ready
->
Signal
(lock);
}
TaskQueue<Task>* background_tasks = worker_data->
task_queue
;
while
(std::unique_ptr<Task> task = background_tasks->
BlockingPop
()) {
task->
Run
();
background_tasks->
NotifyOfCompletion
();
}
}
class
BackgroundTaskRunner
::DelayedTaskScheduler {
public:
explicit
DelayedTaskScheduler
(TaskQueue<Task>* tasks)
: pending_worker_tasks_(tasks) {}
std::unique_ptr<
uv_thread_t
>
Start
() {
auto
start_thread = [](
void
* data) {
static_cast
<DelayedTaskScheduler*>(data)->
Run
();
};
std::unique_ptr<
uv_thread_t
> t {
new
uv_thread_t
() };
uv_sem_init
(&ready_,
0
);
CHECK_EQ
(
0
,
uv_thread_create
(t.
get
(), start_thread,
this
));
uv_sem_wait
(&ready_);
uv_sem_destroy
(&ready_);
return
t;
}
void
PostDelayedTask
(std::unique_ptr<Task> task,
double
delay_in_seconds) {
tasks_.
Push
(std::unique_ptr<Task>(
new
ScheduleTask
(
this
,
std::move
(task),
delay_in_seconds)));
uv_async_send
(&flush_tasks_);
}
void
Stop
() {
tasks_.
Push
(std::unique_ptr<Task>(
new
StopTask
(
this
)));
uv_async_send
(&flush_tasks_);
}
private:
void
Run
() {
TRACE_EVENT_METADATA1
(
"
__metadata
"
,
"
thread_name
"
,
"
name
"
,
"
WorkerThreadsTaskRunner::DelayedTaskScheduler
"
);
loop_.
data
=
this
;
CHECK_EQ
(
0
,
uv_loop_init
(&loop_));
flush_tasks_.
data
=
this
;
CHECK_EQ
(
0
,
uv_async_init
(&loop_, &flush_tasks_, FlushTasks));
uv_sem_post
(&ready_);
uv_run
(&loop_,
UV_RUN_DEFAULT
);
CheckedUvLoopClose
(&loop_);
}
static
void
FlushTasks
(
uv_async_t
* flush_tasks) {
DelayedTaskScheduler* scheduler =
ContainerOf
(&DelayedTaskScheduler::loop_, flush_tasks->
loop
);
while
(std::unique_ptr<Task> task = scheduler->
tasks_
.
Pop
())
task->
Run
();
}
class
StopTask
:
public
Task
{
public:
explicit
StopTask
(DelayedTaskScheduler* scheduler): scheduler_(scheduler) {}
void
Run
()
override
{
std::vector<
uv_timer_t
*> timers;
for
(
uv_timer_t
* timer : scheduler_->
timers_
)
timers.
push_back
(timer);
for
(
uv_timer_t
* timer : timers)
scheduler_->
TakeTimerTask
(timer);
uv_close
(
reinterpret_cast
<
uv_handle_t
*>(&scheduler_->
flush_tasks_
),
[](
uv_handle_t
* handle) {});
}
private:
DelayedTaskScheduler* scheduler_;
};
class
ScheduleTask
:
public
Task
{
public:
ScheduleTask
(DelayedTaskScheduler* scheduler,
std::unique_ptr<Task> task,
double
delay_in_seconds)
: scheduler_(scheduler),
task_
(std::move(task)),
delay_in_seconds_(delay_in_seconds) {}
void
Run
()
override
{
uint64_t
delay_millis =
static_cast
<
uint64_t
>(delay_in_seconds_ +
0.5
) *
1000
;
std::unique_ptr<
uv_timer_t
>
timer
(
new
uv_timer_t
());
CHECK_EQ
(
0
,
uv_timer_init
(&scheduler_->
loop_
, timer.
get
()));
timer->
data
= task_.
release
();
CHECK_EQ
(
0
,
uv_timer_start
(timer.
get
(), RunTask, delay_millis,
0
));
scheduler_->
timers_
.
insert
(timer.
release
());
}
private:
DelayedTaskScheduler* scheduler_;
std::unique_ptr<Task> task_;
double
delay_in_seconds_;
};
static
void
RunTask
(
uv_timer_t
* timer) {
DelayedTaskScheduler* scheduler =
ContainerOf
(&DelayedTaskScheduler::loop_, timer->
loop
);
scheduler->
pending_worker_tasks_
->
Push
(scheduler->
TakeTimerTask
(timer));
}
std::unique_ptr<Task>
TakeTimerTask
(
uv_timer_t
* timer) {
std::unique_ptr<Task>
task
(
static_cast
<Task*>(timer->
data
));
uv_timer_stop
(timer);
uv_close
(
reinterpret_cast
<
uv_handle_t
*>(timer), [](
uv_handle_t
* handle) {
delete
reinterpret_cast
<
uv_timer_t
*>(handle);
});
timers_.
erase
(timer);
return
task;
}
uv_sem_t
ready_;
TaskQueue<v8::Task>* pending_worker_tasks_;
TaskQueue<v8::Task> tasks_;
uv_loop_t
loop_;
uv_async_t
flush_tasks_;
std::unordered_set<
uv_timer_t
*> timers_;
};
BackgroundTaskRunner::BackgroundTaskRunner
(
int
thread_pool_size) {
Mutex::ScopedLock
lock
(platform_workers_mutex_);
pending_platform_workers_ = thread_pool_size;
delayed_task_scheduler_.
reset
(
new
DelayedTaskScheduler
(&background_tasks_));
threads_.
push_back
(delayed_task_scheduler_->
Start
());
for
(
int
i =
0
; i < thread_pool_size; i++) {
PlatformWorkerData* worker_data =
new
PlatformWorkerData{
&background_tasks_, &platform_workers_mutex_,
&platform_workers_ready_, &pending_platform_workers_, i
};
std::unique_ptr<
uv_thread_t
> t {
new
uv_thread_t
() };
if
(
uv_thread_create
(t.
get
(), BackgroundRunner, worker_data) !=
0
)
break
;
threads_.
push_back
(
std::move
(t));
}
//
Wait for platform workers to initialize before continuing with the
//
bootstrap.
while
(pending_platform_workers_ >
0
) {
platform_workers_ready_.
Wait
(lock);
}
}
void
BackgroundTaskRunner::PostTask
(std::unique_ptr<Task> task) {
background_tasks_.
Push
(
std::move
(task));
}
void
BackgroundTaskRunner::PostIdleTask
(std::unique_ptr<v8::IdleTask> task) {
UNREACHABLE
();
}
void
BackgroundTaskRunner::PostDelayedTask
(std::unique_ptr<v8::Task> task,
double
delay_in_seconds) {
delayed_task_scheduler_->
PostDelayedTask
(
std::move
(task), delay_in_seconds);
}
void
BackgroundTaskRunner::BlockingDrain
() {
background_tasks_.
BlockingDrain
();
}
void
BackgroundTaskRunner::Shutdown
() {
background_tasks_.
Stop
();
delayed_task_scheduler_->
Stop
();
for
(
size_t
i =
0
; i < threads_.
size
(); i++) {
CHECK_EQ
(
0
,
uv_thread_join
(threads_[i].
get
()));
}
}
size_t
BackgroundTaskRunner::NumberOfAvailableBackgroundThreads
()
const
{
return
threads_.
size
();
}
PerIsolatePlatformData::PerIsolatePlatformData
(
v8::Isolate* isolate,
uv_loop_t
* loop)
: loop_(loop) {
flush_tasks_ =
new
uv_async_t
();
CHECK_EQ
(
0
,
uv_async_init
(loop, flush_tasks_, FlushTasks));
flush_tasks_->
data
=
static_cast
<
void
*>(
this
);
uv_unref
(
reinterpret_cast
<
uv_handle_t
*>(flush_tasks_));
}
void
PerIsolatePlatformData::FlushTasks
(
uv_async_t
* handle) {
auto
platform_data =
static_cast
<PerIsolatePlatformData*>(handle->
data
);
platform_data->
FlushForegroundTasksInternal
();
}
void
PerIsolatePlatformData::PostIdleTask
(std::unique_ptr<v8::IdleTask> task) {
UNREACHABLE
();
}
void
PerIsolatePlatformData::PostTask
(std::unique_ptr<Task> task) {
CHECK_NE
(flush_tasks_,
nullptr
);
foreground_tasks_.
Push
(
std::move
(task));
uv_async_send
(flush_tasks_);
}
void
PerIsolatePlatformData::PostDelayedTask
(
std::unique_ptr<Task> task,
double
delay_in_seconds) {
CHECK_NE
(flush_tasks_,
nullptr
);
std::unique_ptr<DelayedTask>
delayed
(
new
DelayedTask
());
delayed->
task
=
std::move
(task);
delayed->
platform_data
=
shared_from_this
();
delayed->
timeout
= delay_in_seconds;
foreground_delayed_tasks_.
Push
(
std::move
(delayed));
uv_async_send
(flush_tasks_);
}
PerIsolatePlatformData::~PerIsolatePlatformData
() {
Shutdown
();
}
void
PerIsolatePlatformData::Shutdown
() {
if
(flush_tasks_ ==
nullptr
)
return
;
while
(
FlushForegroundTasksInternal
()) {}
CancelPendingDelayedTasks
();
uv_close
(
reinterpret_cast
<
uv_handle_t
*>(flush_tasks_),
[](
uv_handle_t
* handle) {
delete
reinterpret_cast
<
uv_async_t
*>(handle);
});
flush_tasks_ =
nullptr
;
}
void
PerIsolatePlatformData::ref
() {
ref_count_++;
}
int
PerIsolatePlatformData::unref
() {
return
--ref_count_;
}
NodePlatform::NodePlatform
(
int
thread_pool_size,
TracingController* tracing_controller) {
if
(tracing_controller) {
tracing_controller_ = tracing_controller;
}
else
{
tracing_controller_ =
new
TracingController
();
}
background_task_runner_ =
std::make_shared<BackgroundTaskRunner>(thread_pool_size);
}
void
NodePlatform::RegisterIsolate
(IsolateData* isolate_data,
uv_loop_t
* loop) {
Isolate* isolate = isolate_data->
isolate
();
Mutex::ScopedLock
lock
(per_isolate_mutex_);
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
if
(existing) {
CHECK_EQ
(loop, existing->
event_loop
());
existing->
ref
();
}
else
{
per_isolate_[isolate] =
std::make_shared<PerIsolatePlatformData>(isolate, loop);
}
}
void
NodePlatform::UnregisterIsolate
(IsolateData* isolate_data) {
Isolate* isolate = isolate_data->
isolate
();
Mutex::ScopedLock
lock
(per_isolate_mutex_);
std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
CHECK
(existing);
if
(existing->
unref
() ==
0
) {
existing->
Shutdown
();
per_isolate_.
erase
(isolate);
}
}
void
NodePlatform::Shutdown
() {
background_task_runner_->
Shutdown
();
{
Mutex::ScopedLock
lock
(per_isolate_mutex_);
per_isolate_.
clear
();
}
}
size_t
NodePlatform::NumberOfAvailableBackgroundThreads
() {
return
background_task_runner_->
NumberOfAvailableBackgroundThreads
();
}
void
PerIsolatePlatformData::RunForegroundTask
(std::unique_ptr<Task> task) {
Isolate* isolate =
Isolate::GetCurrent
();
HandleScope
scope
(isolate);
Environment* env =
Environment::GetCurrent
(isolate);
InternalCallbackScope
cb_scope
(env, Local<Object>(), {
0
,
0
},
InternalCallbackScope::
kAllowEmptyResource
);
task->
Run
();
}
void
PerIsolatePlatformData::DeleteFromScheduledTasks
(DelayedTask* task) {
auto
it =
std::find_if
(scheduled_delayed_tasks_.
begin
(),
scheduled_delayed_tasks_.
end
(),
[task](
const
DelayedTaskPointer& delayed) ->
bool
{
return
delayed.
get
() == task;
});
CHECK_NE
(it, scheduled_delayed_tasks_.
end
());
scheduled_delayed_tasks_.
erase
(it);
}
void
PerIsolatePlatformData::RunForegroundTask
(
uv_timer_t
* handle) {
DelayedTask* delayed =
static_cast
<DelayedTask*>(handle->
data
);
RunForegroundTask
(
std::move
(delayed->
task
));
delayed->
platform_data
->
DeleteFromScheduledTasks
(delayed);
}
void
PerIsolatePlatformData::CancelPendingDelayedTasks
() {
scheduled_delayed_tasks_.
clear
();
}
void
NodePlatform::DrainBackgroundTasks
(Isolate* isolate) {
std::shared_ptr<PerIsolatePlatformData> per_isolate =
ForIsolate
(isolate);
do
{
//
Right now, there is no way to drain only background tasks associated
//
with a specific isolate, so this sometimes does more work than
//
necessary. In the long run, that functionality is probably going to
//
be available anyway, though.
background_task_runner_->
BlockingDrain
();
}
while
(per_isolate->
FlushForegroundTasksInternal
());
}
bool
PerIsolatePlatformData::FlushForegroundTasksInternal
() {
bool
did_work =
false
;
while
(std::unique_ptr<DelayedTask> delayed =
foreground_delayed_tasks_.
Pop
()) {
did_work =
true
;
uint64_t
delay_millis =
static_cast
<
uint64_t
>(delayed->
timeout
+
0.5
) *
1000
;
delayed->
timer
.
data
=
static_cast
<
void
*>(delayed.
get
());
uv_timer_init
(loop_, &delayed->
timer
);
//
Timers may not guarantee queue ordering of events with the same delay if
//
the delay is non-zero. This should not be a problem in practice.
uv_timer_start
(&delayed->
timer
, RunForegroundTask, delay_millis,
0
);
uv_unref
(
reinterpret_cast
<
uv_handle_t
*>(&delayed->
timer
));
scheduled_delayed_tasks_.
emplace_back
(delayed.
release
(),
[](DelayedTask* delayed) {
uv_close
(
reinterpret_cast
<
uv_handle_t
*>(&delayed->
timer
),
[](
uv_handle_t
* handle) {
delete
static_cast
<DelayedTask*>(handle->
data
);
});
});
}
//
Move all foreground tasks into a separate queue and flush that queue.
//
This way tasks that are posted while flushing the queue will be run on the
//
next call of FlushForegroundTasksInternal.
std::queue<std::unique_ptr<Task>> tasks = foreground_tasks_.
PopAll
();
while
(!tasks.
empty
()) {
std::unique_ptr<Task> task =
std::move
(tasks.
front
());
tasks.
pop
();
did_work =
true
;
RunForegroundTask
(
std::move
(task));
}
return
did_work;
}
void
NodePlatform::CallOnBackgroundThread
(Task* task,
ExpectedRuntime expected_runtime) {
background_task_runner_->
PostTask
(std::unique_ptr<Task>(task));
}
std::shared_ptr<PerIsolatePlatformData>
NodePlatform::ForIsolate
(Isolate* isolate) {
Mutex::ScopedLock
lock
(per_isolate_mutex_);
std::shared_ptr<PerIsolatePlatformData> data = per_isolate_[isolate];
CHECK
(data);
return
data;
}
void
NodePlatform::CallOnForegroundThread
(Isolate* isolate, Task* task) {
ForIsolate
(isolate)->
PostTask
(std::unique_ptr<Task>(task));
}
void
NodePlatform::CallDelayedOnForegroundThread
(Isolate* isolate,
Task* task,
double
delay_in_seconds) {
ForIsolate
(isolate)->
PostDelayedTask
(
std::unique_ptr<Task>(task), delay_in_seconds);
}
bool
NodePlatform::FlushForegroundTasks
(v8::Isolate* isolate) {
return
ForIsolate
(isolate)->
FlushForegroundTasksInternal
();
}
void
NodePlatform::CancelPendingDelayedTasks
(v8::Isolate* isolate) {
ForIsolate
(isolate)->
CancelPendingDelayedTasks
();
}
bool
NodePlatform::IdleTasksEnabled
(Isolate* isolate) {
return
false
; }
std::shared_ptr<v8::TaskRunner>
NodePlatform::GetBackgroundTaskRunner
(Isolate* isolate) {
return
background_task_runner_;
}
std::shared_ptr<v8::TaskRunner>
NodePlatform::GetForegroundTaskRunner
(Isolate* isolate) {
return
ForIsolate
(isolate);
}
double
NodePlatform::MonotonicallyIncreasingTime
() {
//
Convert nanos to seconds.
return
uv_hrtime
() /
1e9
;
}
double
NodePlatform::CurrentClockTimeMillis
() {
return
SystemClockTimeMillis
();
}
TracingController*
NodePlatform::GetTracingController
() {
return
tracing_controller_;
}
template
<
class
T
>
TaskQueue<T>::TaskQueue()
: lock_(), tasks_available_(), tasks_drained_(),
outstanding_tasks_
(
0
), stopped_(
false
), task_queue_() { }
template
<
class
T
>
void
TaskQueue<T>::Push(std::unique_ptr<T> task) {
Mutex::ScopedLock
scoped_lock
(lock_);
outstanding_tasks_++;
task_queue_.
push
(
std::move
(task));
tasks_available_.
Signal
(scoped_lock);
}
template
<
class
T
>
std::unique_ptr<T> TaskQueue<T>::Pop() {
Mutex::ScopedLock
scoped_lock
(lock_);
if
(task_queue_.
empty
()) {
return
std::unique_ptr<T>(
nullptr
);
}
std::unique_ptr<T> result =
std::move
(task_queue_.
front
());
task_queue_.
pop
();
return
result;
}
template
<
class
T
>
std::unique_ptr<T> TaskQueue<T>::BlockingPop() {
Mutex::ScopedLock
scoped_lock
(lock_);
while
(task_queue_.
empty
() && !stopped_) {
tasks_available_.
Wait
(scoped_lock);
}
if
(stopped_) {
return
std::unique_ptr<T>(
nullptr
);
}
std::unique_ptr<T> result =
std::move
(task_queue_.
front
());
task_queue_.
pop
();
return
result;
}
template
<
class
T
>
void
TaskQueue<T>::NotifyOfCompletion() {
Mutex::ScopedLock
scoped_lock
(lock_);
if
(--outstanding_tasks_ ==
0
) {
tasks_drained_.
Broadcast
(scoped_lock);
}
}
template
<
class
T
>
void
TaskQueue<T>::BlockingDrain() {
Mutex::ScopedLock
scoped_lock
(lock_);
while
(outstanding_tasks_ >
0
) {
tasks_drained_.
Wait
(scoped_lock);
}
}
template
<
class
T
>
void
TaskQueue<T>::Stop() {
Mutex::ScopedLock
scoped_lock
(lock_);
stopped_ =
true
;
tasks_available_.
Broadcast
(scoped_lock);
}
template
<
class
T
>
std::queue<std::unique_ptr<T>> TaskQueue<T>::PopAll() {
Mutex::ScopedLock
scoped_lock
(lock_);
std::queue<std::unique_ptr<T>> result;
result.
swap
(task_queue_);
return
result;
}
}
//
namespace node
Back
|
FazBrowse Home
|
New Git URL