OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ThreadSafeFunctional_h | 5 #ifndef ThreadSafeFunctional_h |
6 #define ThreadSafeFunctional_h | 6 #define ThreadSafeFunctional_h |
7 | 7 |
8 #include "platform/CrossThreadCopier.h" | 8 #include "platform/CrossThreadCopier.h" |
9 #include "wtf/Functional.h" | 9 #include "wtf/Functional.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 // threadSafeBind() is bind() for cross-thread task posting. | 13 // threadSafeBind() is bind() for cross-thread task posting. |
14 // threadSafeBind() applies CrossThreadCopier to the arguments. | 14 // threadSafeBind() applies CrossThreadCopier to the arguments. |
15 // | 15 // |
16 // Example: | 16 // Example: |
17 // void func1(int, const String&); | 17 // void func1(int, const String&); |
18 // f = threadSafeBind(func1, 42, str); | 18 // f = threadSafeBind(func1, 42, str); |
19 // func1(42, str2) will be called when |f()| is executed, | 19 // func1(42, str2) will be called when |f()| is executed, |
20 // where |str2| is a deep copy of |str| (created by str.isolatedCopy()). | 20 // where |str2| is a deep copy of |str| (created by str.isolatedCopy()). |
21 // | 21 // |
22 // threadSafeBind(str) is similar to bind(str.isolatedCopy()), but the latter | 22 // threadSafeBind(str) is similar to bind(str.isolatedCopy()), but the latter |
23 // is NOT thread-safe due to temporary objects (https://crbug.com/390851). | 23 // is NOT thread-safe due to temporary objects (https://crbug.com/390851). |
24 // | 24 // |
25 // Don't (if you pass the task across threads): | 25 // Don't (if you pass the task across threads): |
26 // bind(func1, 42, str); | 26 // bind(func1, 42, str); |
27 // bind(func1, 42, str.isolatedCopy()); | 27 // bind(func1, 42, str.isolatedCopy()); |
28 | 28 |
29 template<typename... FreeVariableTypes, typename FunctionType, typename... Ps> | 29 template<typename... FreeVariableTypes, typename FunctionType, typename... Ps> |
30 PassOwnPtr<Function<typename WTF::FunctionWrapper<FunctionType>::ResultType(Free
VariableTypes...)>> threadSafeBind( | 30 PassOwnPtr<Function<typename WTF::FunctionWrapper<FunctionType>::ResultType(Free
VariableTypes...), WTF::CrossThreadAffinity>> threadSafeBind( |
31 FunctionType function, | 31 FunctionType function, |
32 const Ps&... parameters) | 32 const Ps&... parameters) |
33 { | 33 { |
34 return bind<FreeVariableTypes...>(function, | 34 return WTF::bindInternal<WTF::CrossThreadAffinity, FreeVariableTypes...>( |
| 35 function, |
35 CrossThreadCopier<Ps>::copy(parameters)...); | 36 CrossThreadCopier<Ps>::copy(parameters)...); |
36 } | 37 } |
37 | 38 |
38 } // namespace blink | 39 } // namespace blink |
39 | 40 |
40 #endif // ThreadSafeFunctional_h | 41 #endif // ThreadSafeFunctional_h |
OLD | NEW |