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
34namespace WTF {
35
36class CrossThreadTask {
37public:
38 CrossThreadTask() = default;
39
40 CrossThreadTask(Function<void ()>&& taskFunction)
41 : m_taskFunction(WTFMove(taskFunction))
42 {
43 ASSERT(m_taskFunction);
44 }
45
46 void performTask()
47 {
48 m_taskFunction();
49 }
50
51protected:
52 Function<void ()> m_taskFunction;
53};
54
55template <typename F, typename ArgsTuple, size_t... ArgsIndex>
56void callFunctionForCrossThreadTaskImpl(F function, ArgsTuple&& args, std::index_sequence<ArgsIndex...>)
57{
58 function(std::get<ArgsIndex>(std::forward<ArgsTuple>(args))...);
59}
60
61template <typename F, typename ArgsTuple, typename ArgsIndices = std::make_index_sequence<std::tuple_size<ArgsTuple>::value>>
62void callFunctionForCrossThreadTask(F function, ArgsTuple&& args)
63{
64 callFunctionForCrossThreadTaskImpl(function, std::forward<ArgsTuple>(args), ArgsIndices());
65}
66
67template<typename... Parameters, typename... Arguments>
68CrossThreadTask createCrossThreadTask(void (*method)(Parameters...), const Arguments&... arguments)
69{
70 return CrossThreadTask([method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
71 callFunctionForCrossThreadTask(method, WTFMove(arguments));
72 });
73}
74
75template <typename C, typename MF, typename ArgsTuple, size_t... ArgsIndex>
76void callMemberFunctionForCrossThreadTaskImpl(C* object, MF function, ArgsTuple&& args, std::index_sequence<ArgsIndex...>)
77{
78 (object->*function)(std::get<ArgsIndex>(std::forward<ArgsTuple>(args))...);
79}
80
81template <typename C, typename MF, typename ArgsTuple, typename ArgsIndicies = std::make_index_sequence<std::tuple_size<ArgsTuple>::value>>
82void callMemberFunctionForCrossThreadTask(C* object, MF function, ArgsTuple&& args)
83{
84 callMemberFunctionForCrossThreadTaskImpl(object, function, std::forward<ArgsTuple>(args), ArgsIndicies());
85}
86
87template<typename T, typename std::enable_if<std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments>
88CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments)
89{
90 return CrossThreadTask([callee = makeRefPtr(&callee), method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
91 callMemberFunctionForCrossThreadTask(callee.get(), method, WTFMove(arguments));
92 });
93}
94
95template<typename T, typename std::enable_if<!std::is_base_of<ThreadSafeRefCounted<T>, T>::value, int>::type = 0, typename... Parameters, typename... Arguments>
96CrossThreadTask createCrossThreadTask(T& callee, void (T::*method)(Parameters...), const Arguments&... arguments)
97{
98 return CrossThreadTask([callee = &callee, method, arguments = std::make_tuple(crossThreadCopy(arguments)...)]() mutable {
99 callMemberFunctionForCrossThreadTask(callee, method, WTFMove(arguments));
100 });
101}
102
103} // namespace WTF
104
105using WTF::CrossThreadTask;
106using WTF::createCrossThreadTask;
107