OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 namespace WTF { | 54 namespace WTF { |
55 | 55 |
56 // Functional.h provides a very simple way to bind a function pointer and argume
nts together into a function object | 56 // Functional.h provides a very simple way to bind a function pointer and argume
nts together into a function object |
57 // that can be stored, copied and invoked, similar to how boost::bind and std::b
ind in C++11. | 57 // that can be stored, copied and invoked, similar to how boost::bind and std::b
ind in C++11. |
58 | 58 |
59 // Thread Safety: | 59 // Thread Safety: |
60 // | 60 // |
61 // WTF::bind() and WTF::Closure should be used for same-thread closures | 61 // WTF::bind() and WTF::Closure should be used for same-thread closures |
62 // only, i.e. the closures must be created, executed and destructed on | 62 // only, i.e. the closures must be created, executed and destructed on |
63 // the same thread. | 63 // the same thread. |
64 // Use crossThreadBind() and CrossThreadClosure if the function/task is called | 64 // |
65 // or destructed on a (potentially) different thread from the current thread. | 65 // Use WTF::crossThreadBind() and WTF::CrossThreadClosure if the function/task |
| 66 // is called or destructed on a (potentially) different thread from the current |
| 67 // thread. |
| 68 // WTF::crossThreadBind() applies CrossThreadCopier to the arguments. |
| 69 // Example: |
| 70 // void func1(int, const String&); |
| 71 // f = crossThreadBind(func1, 42, str); |
| 72 // func1(42, str2) will be called when |f()| is executed, |
| 73 // where |str2| is a deep copy of |str| (created by str.isolatedCopy()). |
| 74 // Don't (if you pass the task across threads): |
| 75 // bind(func1, 42, str); |
| 76 // bind(func1, 42, str.isolatedCopy()); |
66 | 77 |
67 // WTF::bind() and move semantics | 78 // WTF::bind() and move semantics |
68 // ============================== | 79 // ============================== |
69 // | 80 // |
70 // For unbound parameters (arguments supplied later on the bound functor directl
y), there are two ways to pass movable | 81 // For unbound parameters (arguments supplied later on the bound functor directl
y), there are two ways to pass movable |
71 // arguments: | 82 // arguments: |
72 // | 83 // |
73 // 1) Pass by rvalue reference. | 84 // 1) Pass by rvalue reference. |
74 // | 85 // |
75 // void yourFunction(Argument&& argument) { ... } | 86 // void yourFunction(Argument&& argument) { ... } |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 using UnboundRunType = base::MakeUnboundRunType<FunctionType, BoundParameter
s...>; | 273 using UnboundRunType = base::MakeUnboundRunType<FunctionType, BoundParameter
s...>; |
263 return wrapUnique(new Function<UnboundRunType, threadAffinity>(base::Bind(fu
nction, typename ParamStorageTraits<typename std::decay<BoundParameters>::type>:
:StorageType(std::forward<BoundParameters>(boundParameters))...))); | 274 return wrapUnique(new Function<UnboundRunType, threadAffinity>(base::Bind(fu
nction, typename ParamStorageTraits<typename std::decay<BoundParameters>::type>:
:StorageType(std::forward<BoundParameters>(boundParameters))...))); |
264 } | 275 } |
265 | 276 |
266 template <typename FunctionType, typename... BoundParameters> | 277 template <typename FunctionType, typename... BoundParameters> |
267 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, SameThreadAffinity>> bind(FunctionType function, BoundParameters&&... bound
Parameters) | 278 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, SameThreadAffinity>> bind(FunctionType function, BoundParameters&&... bound
Parameters) |
268 { | 279 { |
269 return bindInternal<SameThreadAffinity>(function, std::forward<BoundParamete
rs>(boundParameters)...); | 280 return bindInternal<SameThreadAffinity>(function, std::forward<BoundParamete
rs>(boundParameters)...); |
270 } | 281 } |
271 | 282 |
| 283 template <typename FunctionType, typename... BoundParameters> |
| 284 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, CrossThreadAffinity>> crossThreadBind(FunctionType function, BoundParameter
s&&... boundParameters) |
| 285 { |
| 286 return bindInternal<CrossThreadAffinity>(function, CrossThreadCopier<typenam
e std::decay<BoundParameters>::type>::copy(std::forward<BoundParameters>(boundPa
rameters))...); |
| 287 } |
| 288 |
272 typedef Function<void(), SameThreadAffinity> Closure; | 289 typedef Function<void(), SameThreadAffinity> Closure; |
273 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; | 290 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; |
274 | 291 |
275 } // namespace WTF | 292 } // namespace WTF |
276 | 293 |
| 294 using WTF::crossThreadBind; |
| 295 |
277 using WTF::passed; | 296 using WTF::passed; |
278 using WTF::unretained; | 297 using WTF::unretained; |
279 using WTF::crossThreadUnretained; | 298 using WTF::crossThreadUnretained; |
280 | 299 |
281 using WTF::Function; | 300 using WTF::Function; |
282 using WTF::CrossThreadClosure; | 301 using WTF::CrossThreadClosure; |
283 | 302 |
284 #endif // WTF_Functional_h | 303 #endif // WTF_Functional_h |
OLD | NEW |