| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Neither the name of Google Inc. nor the names of its | |
| 11 * contributors may be used to endorse or promote products derived from | |
| 12 * this software without specific prior written permission. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | |
| 27 #ifndef ExecutionContextTask_h | |
| 28 #define ExecutionContextTask_h | |
| 29 | |
| 30 #include <type_traits> | |
| 31 #include "core/CoreExport.h" | |
| 32 #include "platform/CrossThreadFunctional.h" | |
| 33 #include "platform/wtf/Allocator.h" | |
| 34 #include "platform/wtf/Functional.h" | |
| 35 #include "platform/wtf/Noncopyable.h" | |
| 36 #include "platform/wtf/PtrUtil.h" | |
| 37 #include "platform/wtf/text/WTFString.h" | |
| 38 | |
| 39 namespace blink { | |
| 40 | |
| 41 class ExecutionContext; | |
| 42 | |
| 43 class CORE_EXPORT ExecutionContextTask { | |
| 44 WTF_MAKE_NONCOPYABLE(ExecutionContextTask); | |
| 45 USING_FAST_MALLOC(ExecutionContextTask); | |
| 46 | |
| 47 public: | |
| 48 ExecutionContextTask() {} | |
| 49 virtual ~ExecutionContextTask() {} | |
| 50 virtual void PerformTask(ExecutionContext*) = 0; | |
| 51 | |
| 52 void PerformTaskIfContextIsValid(ExecutionContext* context) { | |
| 53 if (context) | |
| 54 PerformTask(context); | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 namespace internal { | |
| 59 | |
| 60 template <WTF::FunctionThreadAffinity threadAffinity> | |
| 61 void RunCallClosureTask( | |
| 62 std::unique_ptr<Function<void(), threadAffinity>> closure, | |
| 63 ExecutionContext*) { | |
| 64 (*closure)(); | |
| 65 } | |
| 66 | |
| 67 template <WTF::FunctionThreadAffinity threadAffinity> | |
| 68 void RunCallClosureTask( | |
| 69 std::unique_ptr<Function<void(ExecutionContext*), threadAffinity>> closure, | |
| 70 ExecutionContext* execution_context) { | |
| 71 (*closure)(execution_context); | |
| 72 } | |
| 73 | |
| 74 template <typename T, WTF::FunctionThreadAffinity threadAffinity> | |
| 75 class CallClosureTask final : public ExecutionContextTask { | |
| 76 public: | |
| 77 static std::unique_ptr<CallClosureTask> Create( | |
| 78 std::unique_ptr<Function<T, threadAffinity>> closure) { | |
| 79 return WTF::WrapUnique(new CallClosureTask(std::move(closure))); | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 explicit CallClosureTask(std::unique_ptr<Function<T, threadAffinity>> closure) | |
| 84 : closure_(std::move(closure)) {} | |
| 85 | |
| 86 void PerformTask(ExecutionContext* execution_context) override { | |
| 87 RunCallClosureTask(std::move(closure_), execution_context); | |
| 88 } | |
| 89 | |
| 90 std::unique_ptr<Function<T, threadAffinity>> closure_; | |
| 91 }; | |
| 92 | |
| 93 // Do not use |create| other than in createCrossThreadTask and | |
| 94 // createSameThreadTask. | |
| 95 // See http://crbug.com/390851 | |
| 96 template <typename T, WTF::FunctionThreadAffinity threadAffinity> | |
| 97 std::unique_ptr<CallClosureTask<T, threadAffinity>> CreateCallClosureTask( | |
| 98 std::unique_ptr<Function<T, threadAffinity>> closure) { | |
| 99 return CallClosureTask<T, threadAffinity>::Create(std::move(closure)); | |
| 100 } | |
| 101 | |
| 102 } // namespace internal | |
| 103 | |
| 104 // createSameThreadTask() is deprecated and removed. | |
| 105 // Use WTF::bind() and post it to WebTaskRunner obtained by | |
| 106 // TaskRunnerHelper::get() when posting a task within a single thread. | |
| 107 | |
| 108 // createCrossThreadTask() is deprecated and will be removed. | |
| 109 // Use crossThreadBind() and post it to an appropriate task runner | |
| 110 // when posting a task to another thread. | |
| 111 // See https://crbug.com/625927 for details. | |
| 112 // | |
| 113 // createCrossThreadTask(...) is ExecutionContextTask version of | |
| 114 // crossThreadBind(). | |
| 115 // Using WTF::bind() directly is not thread-safe due to temporary objects, see | |
| 116 // https://crbug.com/390851 for details. | |
| 117 // | |
| 118 // Example: | |
| 119 // void func1(int, const String&); | |
| 120 // createCrossThreadTask(func1, 42, str); | |
| 121 // func1(42, str2) will be called, where |str2| is a deep copy of | |
| 122 // |str| (created by str.isolatedCopy()). | |
| 123 // | |
| 124 // Don't (if you pass the task across threads): | |
| 125 // bind(func1, 42, str); | |
| 126 // bind(func1, 42, str.isolatedCopy()); | |
| 127 // | |
| 128 // For functions: | |
| 129 // void functionEC(MP1, ..., MPn, ExecutionContext*); | |
| 130 // void function(MP1, ..., MPn); | |
| 131 // class C { | |
| 132 // void memberEC(MP1, ..., MPn, ExecutionContext*); | |
| 133 // void member(MP1, ..., MPn); | |
| 134 // }; | |
| 135 // We create tasks represented by std::unique_ptr<ExecutionContextTask>: | |
| 136 // createCrossThreadTask(functionEC, const P1& p1, ..., const Pn& pn); | |
| 137 // createCrossThreadTask(memberEC, C* ptr, const P1& p1, ..., const Pn& pn); | |
| 138 // createCrossThreadTask(function, const P1& p1, ..., const Pn& pn); | |
| 139 // createCrossThreadTask(member, C* ptr, const P1& p1, ..., const Pn& pn); | |
| 140 // (|ptr| can also be WeakPtr<C> or other pointer-like types) | |
| 141 // and then the following are called on the target thread: | |
| 142 // functionEC(p1, ..., pn, context); | |
| 143 // ptr->memberEC(p1, ..., pn, context); | |
| 144 // function(p1, ..., pn); | |
| 145 // ptr->member(p1, ..., pn); | |
| 146 // | |
| 147 // ExecutionContext: | |
| 148 // |context| is supplied by the target thread. | |
| 149 // | |
| 150 // Deep copies by crossThreadBind(): | |
| 151 // |ptr|, |p1|, ..., |pn| are processed by crossThreadBind() and thus | |
| 152 // CrossThreadCopier. | |
| 153 // You don't have to call manually e.g. isolatedCopy(). | |
| 154 // To pass things that cannot be copied by CrossThreadCopier | |
| 155 // (e.g. pointers), use crossThreadUnretained() explicitly. | |
| 156 | |
| 157 template <typename FunctionType, typename... P> | |
| 158 std::unique_ptr<ExecutionContextTask> CreateCrossThreadTask( | |
| 159 FunctionType function, | |
| 160 P&&... parameters) { | |
| 161 return internal::CreateCallClosureTask( | |
| 162 CrossThreadBind(function, std::forward<P>(parameters)...)); | |
| 163 } | |
| 164 | |
| 165 } // namespace blink | |
| 166 | |
| 167 #endif | |
| OLD | NEW |