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
3 // found in the LICENSE file.
5 #ifndef ThreadSafeFunctional_h
6 #define ThreadSafeFunctional_h
8 #include "platform/CrossThreadCopier.h"
9 #include "wtf/Functional.h"
13 // threadSafeBind() is bind() for cross-thread task posting.
14 // threadSafeBind() applies CrossThreadCopier to the arguments.
17 // void func1(int, const String&);
18 // f = threadSafeBind(func1, 42, str);
19 // func1(42, str2) will be called when |f()| is executed,
20 // where |str2| is a deep copy of |str| (created by str.isolatedCopy()).
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).
25 // Don't (if you pass the task across threads):
26 // bind(func1, 42, str);
27 // bind(func1, 42, str.isolatedCopy());
29 template<typename
... FreeVariableTypes
, typename FunctionType
>
30 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
31 FunctionType function
)
33 return bind
<FreeVariableTypes
...>(function
);
36 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
>
37 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
38 FunctionType function
,
41 return bind
<FreeVariableTypes
...>(function
,
42 CrossThreadCopier
<P1
>::copy(parameter1
));
45 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
, typename P2
>
46 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
47 FunctionType function
,
48 const P1
& parameter1
, const P2
& parameter2
)
50 return bind
<FreeVariableTypes
...>(function
,
51 CrossThreadCopier
<P1
>::copy(parameter1
),
52 CrossThreadCopier
<P2
>::copy(parameter2
));
55 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
, typename P2
, typename P3
>
56 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
57 FunctionType function
,
58 const P1
& parameter1
, const P2
& parameter2
, const P3
& parameter3
)
60 return bind
<FreeVariableTypes
...>(function
,
61 CrossThreadCopier
<P1
>::copy(parameter1
),
62 CrossThreadCopier
<P2
>::copy(parameter2
),
63 CrossThreadCopier
<P3
>::copy(parameter3
));
66 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
, typename P2
, typename P3
, typename P4
>
67 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
68 FunctionType function
,
69 const P1
& parameter1
, const P2
& parameter2
, const P3
& parameter3
, const P4
& parameter4
)
71 return bind
<FreeVariableTypes
...>(function
,
72 CrossThreadCopier
<P1
>::copy(parameter1
),
73 CrossThreadCopier
<P2
>::copy(parameter2
),
74 CrossThreadCopier
<P3
>::copy(parameter3
),
75 CrossThreadCopier
<P4
>::copy(parameter4
));
78 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
, typename P2
, typename P3
, typename P4
, typename P5
>
79 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
80 FunctionType function
,
81 const P1
& parameter1
, const P2
& parameter2
, const P3
& parameter3
, const P4
& parameter4
, const P5
& parameter5
)
83 return bind
<FreeVariableTypes
...>(function
,
84 CrossThreadCopier
<P1
>::copy(parameter1
),
85 CrossThreadCopier
<P2
>::copy(parameter2
),
86 CrossThreadCopier
<P3
>::copy(parameter3
),
87 CrossThreadCopier
<P4
>::copy(parameter4
),
88 CrossThreadCopier
<P5
>::copy(parameter5
));
91 template<typename
... FreeVariableTypes
, typename FunctionType
, typename P1
, typename P2
, typename P3
, typename P4
, typename P5
, typename P6
>
92 PassOwnPtr
<Function
<typename
WTF::FunctionWrapper
<FunctionType
>::ResultType(FreeVariableTypes
...)>> threadSafeBind(
93 FunctionType function
,
94 const P1
& parameter1
, const P2
& parameter2
, const P3
& parameter3
, const P4
& parameter4
, const P5
& parameter5
, const P6
& parameter6
)
96 return bind
<FreeVariableTypes
...>(function
,
97 CrossThreadCopier
<P1
>::copy(parameter1
),
98 CrossThreadCopier
<P2
>::copy(parameter2
),
99 CrossThreadCopier
<P3
>::copy(parameter3
),
100 CrossThreadCopier
<P4
>::copy(parameter4
),
101 CrossThreadCopier
<P5
>::copy(parameter5
),
102 CrossThreadCopier
<P6
>::copy(parameter6
));
107 #endif // ThreadSafeFunctional_h