1 | /* |
2 | * Copyright (C) 2013, 2015, 2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/CrossThreadCopier.h> |
29 | #include <wtf/Function.h> |
30 | #include <wtf/RefPtr.h> |
31 | #include <wtf/StdLibExtras.h> |
32 | #include <wtf/ThreadSafeRefCounted.h> |
33 | |
34 | namespace WTF { |
35 | |
36 | class CrossThreadTask { |
37 | WTF_MAKE_FAST_ALLOCATED; |
38 | public: |
39 | CrossThreadTask() = default; |
40 | |
41 | CrossThreadTask(Function<void ()>&& taskFunction) |
42 | : m_taskFunction(WTFMove(taskFunction)) |
43 | { |
44 | ASSERT(m_taskFunction); |
45 | } |
46 | |
47 | void performTask() |
48 | { |
49 | m_taskFunction(); |
50 | } |
51 | |
52 | protected: |
53 | Function<void ()> m_taskFunction; |
54 | }; |
55 | |
56 | template <typename F, typename ArgsTuple, size_t... ArgsIndex> |
57 | void callFunctionForCrossThreadTaskImpl(F function, ArgsTuple&& args, std::index_sequence<ArgsIndex...>) |
58 | { |
59 | function(std::get<ArgsIndex>(std::forward<ArgsTuple>(args))...); |
60 | } |
61 | |
62 | template <typename F, typename ArgsTuple, typename ArgsIndices = std::make_index_sequence<std::tuple_size<ArgsTuple>::value>> |
63 | void callFunctionForCrossThreadTask(F function, ArgsTuple&& args) |
64 | { |
65 | callFunctionForCrossThreadTaskImpl(function, std::forward<ArgsTuple>(args), ArgsIndices()); |
66 | } |
67 | |
68 | template<typename... Parameters, typename... Arguments> |
69 | CrossThreadTask createCrossThreadTask(void (*method)(Parameters...), const Arguments&... arguments) |
70 | { |
71 | return CrossThreadTask([method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable { |
72 | callFunctionForCrossThreadTask(method, WTFMove(arguments)); |
73 | }); |
74 | } |
75 | |
76 | template <typename C, typename MF, typename ArgsTuple, size_t... ArgsIndex> |
77 | void callMemberFunctionForCrossThreadTaskImpl(C* object, MF function, ArgsTuple&& args, std::index_sequence<ArgsIndex...>) |
78 | { |
79 | (object->*function)(std::get<ArgsIndex>(std::forward<ArgsTuple>(args))...); |
80 | } |
81 | |
82 | template <typename C, typename MF, typename ArgsTuple, typename ArgsIndicies = std::make_index_sequence<std::tuple_size<ArgsTuple>::value>> |
83 | void callMemberFunctionForCrossThreadTask(C* object, MF function, ArgsTuple&& args) |
84 | { |
85 | callMemberFunctionForCrossThreadTaskImpl(object, function, std::forward<ArgsTuple>(args), ArgsIndicies()); |
86 | } |
87 | |
88 | template<typename T, typename std::enable_if<std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments> |
89 | CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments) |
90 | { |
91 | return CrossThreadTask([callee = makeRefPtr(&callee), method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable { |
92 | callMemberFunctionForCrossThreadTask(callee.get(), method, WTFMove(arguments)); |
93 | }); |
94 | } |
95 | |
96 | template<typename T, typename std::enable_if<!std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments> |
97 | CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments) |
98 | { |
99 | return CrossThreadTask([callee = &callee, method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable { |
100 | callMemberFunctionForCrossThreadTask(callee, method, WTFMove(arguments)); |
101 | }); |
102 | } |
103 | |
104 | } // namespace WTF |
105 | |
106 | using WTF::CrossThreadTask; |
107 | using WTF::createCrossThreadTask; |
108 | |