Commit 5fb77ec1 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Add some clarifying comments to `CrossThreadFunction`.

Change-Id: Id9c015b40c46b2b6d12bfd6b7d5d6cb869e91137
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2266376Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Auto-Submit: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782815}
parent 233cf5cc
...@@ -12,26 +12,39 @@ ...@@ -12,26 +12,39 @@
namespace WTF { namespace WTF {
// CrossThreadBindOnce is Bind() for cross-thread task posting. // `CrossThreadBindOnce()` and `CrossThreadBindRepeating()` are the Blink
// Analogously, CrossThreadBindRepeating() is a repeating version // equivalents of `base::BindOnce()` and `base::BindRepeating()` for creating
// of CrossThreadBindOnce(). // a callback that is run or destroyed on a different thread.
// Both apply CrossThreadCopier to the arguments.
// //
// TODO(crbug.com/963574): Deprecate CrossThreadBindRepeating(). // Unlike `base::RepeatingCallback`, a repeatable cross-thread function is *not*
// copyable. This is intentional: a number of objects in Blink (notably
// `String`) are thread-hostile: allowing a cross-thread function to be copied
// means it would be easy to end up in situations where multiple threads might
// unsafely reference the same `String` object.
//
// TODO(crbug.com/963574): Deprecate `CrossThreadBindRepeating()`.
// //
// Example: // Example:
// void Func1(int, const String&); // // Given the prototype:
// f = CrossThreadBindOnce(&Func1, 42, str); // // void MyFunction(const String&, int);
// Func1(42, str2) will be called when |std::move(f).Run()| is executed, // String str = "Hello world!";
// where |str2| is a deep copy of |str| (created by str.IsolatedCopy()). // CrossThreadFunction<void(int)> f =
// CrossThreadBindOnce(&MyFunction, str);
// std::move(f).Run(42); // Calls MyFunction(<deep copy of `str`>, 42);
//
// Arguments bound to a `CrossThreadFunction` are copied with
// `CrossThreadCopier`. In the case of `String`, the argument is a deep copy of
// `str` that is created by `String::IsolatedCopy()`.
// //
// CrossThreadBindOnce(str) is similar to // Important!
// Bind(str.IsolatedCopy()), but the latter is NOT thread-safe due to // `CrossThreadBindOnce(str)` is similar to `BindOnce(str.IsolatedCopy())`, but
// temporary objects (https://crbug.com/390851). // historically, the latter was unsafe since it was possible to end up with
// situations where a thread-hostile `String` would be referenced on multiple
// threads, leading to crashes. See https://crbug.com/390851 for more details.
// //
// Don't (if you pass the task across threads): // In contrast, `CrossThreadBindOnce()` and `CrossThreadBindRepeating()` are
// Bind(&Func1, 42, str); // implemented in a way that only the destination thread can refer to any bound
// Bind(&Func1, 42, str.IsolatedCopy()); // arguments.
namespace internal { namespace internal {
......
...@@ -62,9 +62,10 @@ namespace WTF { ...@@ -62,9 +62,10 @@ namespace WTF {
// WTF::Bind(), WTF::BindRepeating and base::{Once,Repeating}Callback should // WTF::Bind(), WTF::BindRepeating and base::{Once,Repeating}Callback should
// be used for same-thread closures only, i.e. the closures must be created, // be used for same-thread closures only, i.e. the closures must be created,
// executed and destructed on the same thread. // executed and destructed on the same thread.
//
// Use CrossThreadBindOnce() and CrossThreadBindRepeating() if the function/task // Use CrossThreadBindOnce() and CrossThreadBindRepeating() if the function/task
// is called or destructed on a (potentially) different thread from the current // is called or destructed on a (potentially) different thread from the current
// thread. // thread. See cross_thread_functional.h for more details.
// WTF::Bind() / WTF::BindRepeating() and move semantics // WTF::Bind() / WTF::BindRepeating() and move semantics
// ===================================================== // =====================================================
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment