@@ -18,9 +18,8 @@ using v8::TracingController;
18
18
19
19
static void BackgroundRunner (void * data) {
20
20
TaskQueue<Task>* background_tasks = static_cast <TaskQueue<Task>*>(data);
21
- while (Task* task = background_tasks->BlockingPop ()) {
21
+ while (std::unique_ptr< Task> task = background_tasks->BlockingPop ()) {
22
22
task->Run ();
23
- delete task;
24
23
background_tasks->NotifyOfCompletion ();
25
24
}
26
25
}
@@ -39,18 +38,19 @@ void PerIsolatePlatformData::FlushTasks(uv_async_t* handle) {
39
38
platform_data->FlushForegroundTasksInternal ();
40
39
}
41
40
42
- void PerIsolatePlatformData::CallOnForegroundThread (Task* task) {
43
- foreground_tasks_.Push (task);
41
+ void PerIsolatePlatformData::CallOnForegroundThread (
42
+ std::unique_ptr<Task> task) {
43
+ foreground_tasks_.Push (std::move (task));
44
44
uv_async_send (flush_tasks_);
45
45
}
46
46
47
47
void PerIsolatePlatformData::CallDelayedOnForegroundThread (
48
- Task* task, double delay_in_seconds) {
49
- auto delayed = new DelayedTask ();
50
- delayed->task = task;
48
+ std::unique_ptr< Task> task, double delay_in_seconds) {
49
+ std::unique_ptr<DelayedTask> delayed ( new DelayedTask () );
50
+ delayed->task = std::move ( task) ;
51
51
delayed->platform_data = this ;
52
52
delayed->timeout = delay_in_seconds;
53
- foreground_delayed_tasks_.Push (delayed);
53
+ foreground_delayed_tasks_.Push (std::move ( delayed) );
54
54
uv_async_send (flush_tasks_);
55
55
}
56
56
@@ -125,14 +125,13 @@ size_t NodePlatform::NumberOfAvailableBackgroundThreads() {
125
125
return threads_.size ();
126
126
}
127
127
128
- void PerIsolatePlatformData::RunForegroundTask (Task* task) {
128
+ void PerIsolatePlatformData::RunForegroundTask (std::unique_ptr< Task> task) {
129
129
Isolate* isolate = Isolate::GetCurrent ();
130
130
HandleScope scope (isolate);
131
131
Environment* env = Environment::GetCurrent (isolate);
132
132
InternalCallbackScope cb_scope (env, Local<Object>(), { 0 , 0 },
133
133
InternalCallbackScope::kAllowEmptyResource );
134
134
task->Run ();
135
- delete task;
136
135
}
137
136
138
137
void PerIsolatePlatformData::RunForegroundTask (uv_timer_t * handle) {
@@ -141,7 +140,7 @@ void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) {
141
140
auto it = std::find (tasklist.begin (), tasklist.end (), delayed);
142
141
CHECK_NE (it, tasklist.end ());
143
142
tasklist.erase (it);
144
- RunForegroundTask (delayed->task );
143
+ RunForegroundTask (std::move ( delayed->task ) );
145
144
uv_close (reinterpret_cast <uv_handle_t *>(&delayed->timer ),
146
145
[](uv_handle_t * handle) {
147
146
delete static_cast <DelayedTask*>(handle->data );
@@ -162,39 +161,40 @@ void NodePlatform::DrainBackgroundTasks(Isolate* isolate) {
162
161
PerIsolatePlatformData* per_isolate = ForIsolate (isolate);
163
162
164
163
do {
165
- // Right now, there is no way to drain only background tasks associated with
166
- // a specific isolate, so this sometimes does more work than necessary.
167
- // In the long run, that functionality is probably going to be available
168
- // anyway, though.
164
+ // Right now, there is no way to drain only background tasks associated
165
+ // with a specific isolate, so this sometimes does more work than
166
+ // necessary. In the long run, that functionality is probably going to
167
+ // be available anyway, though.
169
168
background_tasks_.BlockingDrain ();
170
169
} while (per_isolate->FlushForegroundTasksInternal ());
171
170
}
172
171
173
172
bool PerIsolatePlatformData::FlushForegroundTasksInternal () {
174
173
bool did_work = false ;
175
174
176
- while (auto delayed = foreground_delayed_tasks_.Pop ()) {
175
+ while (std::unique_ptr<DelayedTask> delayed =
176
+ foreground_delayed_tasks_.Pop ()) {
177
177
did_work = true ;
178
178
uint64_t delay_millis =
179
179
static_cast <uint64_t >(delayed->timeout + 0.5 ) * 1000 ;
180
- delayed->timer .data = static_cast <void *>(delayed);
180
+ delayed->timer .data = static_cast <void *>(delayed. get () );
181
181
uv_timer_init (loop_, &delayed->timer );
182
182
// Timers may not guarantee queue ordering of events with the same delay if
183
183
// the delay is non-zero. This should not be a problem in practice.
184
184
uv_timer_start (&delayed->timer , RunForegroundTask, delay_millis, 0 );
185
185
uv_unref (reinterpret_cast <uv_handle_t *>(&delayed->timer ));
186
- scheduled_delayed_tasks_.push_back (delayed);
186
+ scheduled_delayed_tasks_.push_back (delayed. release () );
187
187
}
188
- while (Task* task = foreground_tasks_.Pop ()) {
188
+ while (std::unique_ptr< Task> task = foreground_tasks_.Pop ()) {
189
189
did_work = true ;
190
- RunForegroundTask (task);
190
+ RunForegroundTask (std::move ( task) );
191
191
}
192
192
return did_work;
193
193
}
194
194
195
195
void NodePlatform::CallOnBackgroundThread (Task* task,
196
196
ExpectedRuntime expected_runtime) {
197
- background_tasks_.Push (task);
197
+ background_tasks_.Push (std::unique_ptr<Task>( task) );
198
198
}
199
199
200
200
PerIsolatePlatformData* NodePlatform::ForIsolate (Isolate* isolate) {
@@ -205,14 +205,14 @@ PerIsolatePlatformData* NodePlatform::ForIsolate(Isolate* isolate) {
205
205
}
206
206
207
207
void NodePlatform::CallOnForegroundThread (Isolate* isolate, Task* task) {
208
- ForIsolate (isolate)->CallOnForegroundThread (task);
208
+ ForIsolate (isolate)->CallOnForegroundThread (std::unique_ptr<Task>( task) );
209
209
}
210
210
211
211
void NodePlatform::CallDelayedOnForegroundThread (Isolate* isolate,
212
212
Task* task,
213
213
double delay_in_seconds) {
214
- ForIsolate (isolate)->CallDelayedOnForegroundThread (task,
215
- delay_in_seconds);
214
+ ForIsolate (isolate)->CallDelayedOnForegroundThread (
215
+ std::unique_ptr<Task>(task), delay_in_seconds);
216
216
}
217
217
218
218
void NodePlatform::FlushForegroundTasks (v8::Isolate* isolate) {
@@ -240,34 +240,34 @@ TaskQueue<T>::TaskQueue()
240
240
outstanding_tasks_ (0 ), stopped_(false ), task_queue_() { }
241
241
242
242
template <class T >
243
- void TaskQueue<T>::Push(T* task) {
243
+ void TaskQueue<T>::Push(std::unique_ptr<T> task) {
244
244
Mutex::ScopedLock scoped_lock (lock_);
245
245
outstanding_tasks_++;
246
- task_queue_.push (task);
246
+ task_queue_.push (std::move ( task) );
247
247
tasks_available_.Signal (scoped_lock);
248
248
}
249
249
250
250
template <class T >
251
- T* TaskQueue<T>::Pop() {
251
+ std::unique_ptr<T> TaskQueue<T>::Pop() {
252
252
Mutex::ScopedLock scoped_lock (lock_);
253
- T* result = nullptr ;
254
- if (!task_queue_.empty ()) {
255
- result = task_queue_.front ();
256
- task_queue_.pop ();
253
+ if (task_queue_.empty ()) {
254
+ return std::unique_ptr<T>(nullptr );
257
255
}
256
+ std::unique_ptr<T> result = std::move (task_queue_.front ());
257
+ task_queue_.pop ();
258
258
return result;
259
259
}
260
260
261
261
template <class T >
262
- T* TaskQueue<T>::BlockingPop() {
262
+ std::unique_ptr<T> TaskQueue<T>::BlockingPop() {
263
263
Mutex::ScopedLock scoped_lock (lock_);
264
264
while (task_queue_.empty () && !stopped_) {
265
265
tasks_available_.Wait (scoped_lock);
266
266
}
267
267
if (stopped_) {
268
- return nullptr ;
268
+ return std::unique_ptr<T>( nullptr ) ;
269
269
}
270
- T* result = task_queue_.front ();
270
+ std::unique_ptr<T> result = std::move ( task_queue_.front () );
271
271
task_queue_.pop ();
272
272
return result;
273
273
}
0 commit comments