Skip to content

Commit

Permalink
Add DeleteSoon(..., std::unique_ptr<T>)
Browse files Browse the repository at this point in the history
There are cases where we want to delete an object held by a unique_ptr in
another thread. SequencedTaskRunner::DeleteSoon takes a raw pointer, which
means we need to call release(), like

task_runner->DeleteSoon(FROM_HERE, pointer.release());

This CL makes it possible to use std::move, like

task_runner->DeleteSoon(FROM_HERE, std::move(pointer));

Bug: None
Change-Id: I269d82547103ebe8c78c2498d269ba28ee5ca88f
Reviewed-on: https://chromium-review.googlesource.com/515264
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: Nico Weber <thakis@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#474879}
  • Loading branch information
yutakahirano authored and Commit Bot committed May 26, 2017
1 parent d5e5040 commit cfcf2ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions base/sequenced_task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef BASE_SEQUENCED_TASK_RUNNER_H_
#define BASE_SEQUENCED_TASK_RUNNER_H_

#include <memory>

#include "base/base_export.h"
#include "base/callback.h"
#include "base/sequenced_task_runner_helpers.h"
Expand Down Expand Up @@ -127,6 +129,12 @@ class BASE_EXPORT SequencedTaskRunner : public TaskRunner {
object);
}

template <class T>
bool DeleteSoon(const tracked_objects::Location& from_here,
std::unique_ptr<T> object) {
return DeleteSoon(from_here, object.release());
}

// Submits a non-nestable task to release the given object. Returns
// true if the object may be released at some point in the future,
// and false if the object definitely will not be released.
Expand Down
8 changes: 8 additions & 0 deletions content/public/browser/browser_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_
#define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_

#include <memory>
#include <string>
#include <utility>

Expand Down Expand Up @@ -173,6 +174,13 @@ class CONTENT_EXPORT BrowserThread {
return GetTaskRunnerForThread(identifier)->DeleteSoon(from_here, object);
}

template <class T>
static bool DeleteSoon(ID identifier,
const tracked_objects::Location& from_here,
std::unique_ptr<T> object) {
return DeleteSoon(identifier, from_here, object.release());
}

template <class T>
static bool ReleaseSoon(ID identifier,
const tracked_objects::Location& from_here,
Expand Down

0 comments on commit cfcf2ce

Please sign in to comment.