OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google 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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Neither the name of Google Inc. nor the names of its | 10 * * Neither the name of Google Inc. nor the names of its |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 USING_FAST_MALLOC(ExecutionContextTask); | 43 USING_FAST_MALLOC(ExecutionContextTask); |
44 public: | 44 public: |
45 ExecutionContextTask() { } | 45 ExecutionContextTask() { } |
46 virtual ~ExecutionContextTask() { } | 46 virtual ~ExecutionContextTask() { } |
47 virtual void performTask(ExecutionContext*) = 0; | 47 virtual void performTask(ExecutionContext*) = 0; |
48 virtual String taskNameForInstrumentation() const { return String(); } | 48 virtual String taskNameForInstrumentation() const { return String(); } |
49 }; | 49 }; |
50 | 50 |
51 namespace internal { | 51 namespace internal { |
52 | 52 |
| 53 template<WTF::FunctionThreadAffinity threadAffinity> |
| 54 void runCallClosureTask(std::unique_ptr<Function<void(), threadAffinity>> closur
e, ExecutionContext*) |
| 55 { |
| 56 (*closure)(); |
| 57 } |
| 58 |
| 59 template<WTF::FunctionThreadAffinity threadAffinity> |
| 60 void runCallClosureTask(std::unique_ptr<Function<void(ExecutionContext*), thread
Affinity>> closure, ExecutionContext* executionContext) |
| 61 { |
| 62 (*closure)(executionContext); |
| 63 } |
| 64 |
53 template<typename T, WTF::FunctionThreadAffinity threadAffinity> | 65 template<typename T, WTF::FunctionThreadAffinity threadAffinity> |
54 class CallClosureTaskBase : public ExecutionContextTask { | 66 class CallClosureTask final : public ExecutionContextTask { |
55 protected: | 67 public: |
56 explicit CallClosureTaskBase(std::unique_ptr<Function<T, threadAffinity>> cl
osure) | 68 static std::unique_ptr<CallClosureTask> create(std::unique_ptr<Function<T, t
hreadAffinity>> closure) |
| 69 { |
| 70 return wrapUnique(new CallClosureTask(std::move(closure))); |
| 71 } |
| 72 |
| 73 private: |
| 74 explicit CallClosureTask(std::unique_ptr<Function<T, threadAffinity>> closur
e) |
57 : m_closure(std::move(closure)) | 75 : m_closure(std::move(closure)) |
58 { | 76 { |
59 } | 77 } |
60 | 78 |
| 79 void performTask(ExecutionContext* executionContext) override |
| 80 { |
| 81 runCallClosureTask(std::move(m_closure), executionContext); |
| 82 } |
| 83 |
61 std::unique_ptr<Function<T, threadAffinity>> m_closure; | 84 std::unique_ptr<Function<T, threadAffinity>> m_closure; |
62 }; | 85 }; |
63 | 86 |
64 template<WTF::FunctionThreadAffinity threadAffinity> | 87 // Do not use |create| other than in createCrossThreadTask and |
65 class CallClosureTask final : public CallClosureTaskBase<void(), threadAffinity>
{ | 88 // createSameThreadTask. |
66 public: | 89 // See http://crbug.com/390851 |
67 // Do not use |create| other than in createCrossThreadTask and | 90 template<typename T, WTF::FunctionThreadAffinity threadAffinity> |
68 // createSameThreadTask. | 91 std::unique_ptr<CallClosureTask<T, threadAffinity>> createCallClosureTask(std::u
nique_ptr<Function<T, threadAffinity>> closure) |
69 // See http://crbug.com/390851 | 92 { |
70 static std::unique_ptr<CallClosureTask<threadAffinity>> create(std::unique_p
tr<Function<void(), threadAffinity>> closure) | 93 return CallClosureTask<T, threadAffinity>::create(std::move(closure)); |
71 { | 94 } |
72 return wrapUnique(new CallClosureTask<threadAffinity>(std::move(closure)
)); | |
73 } | |
74 | |
75 void performTask(ExecutionContext*) override | |
76 { | |
77 (*this->m_closure)(); | |
78 } | |
79 | |
80 private: | |
81 explicit CallClosureTask(std::unique_ptr<Function<void(), threadAffinity>> c
losure) | |
82 : CallClosureTaskBase<void(), threadAffinity>(std::move(closure)) | |
83 { | |
84 } | |
85 }; | |
86 | |
87 template<WTF::FunctionThreadAffinity threadAffinity> | |
88 class CallClosureWithExecutionContextTask final : public CallClosureTaskBase<voi
d(ExecutionContext*), threadAffinity> { | |
89 public: | |
90 // Do not use |create| other than in createCrossThreadTask and | |
91 // createSameThreadTask. | |
92 // See http://crbug.com/390851 | |
93 static std::unique_ptr<CallClosureWithExecutionContextTask> create(std::uniq
ue_ptr<Function<void(ExecutionContext*), threadAffinity>> closure) | |
94 { | |
95 return wrapUnique(new CallClosureWithExecutionContextTask(std::move(clos
ure))); | |
96 } | |
97 | |
98 void performTask(ExecutionContext* context) override | |
99 { | |
100 (*this->m_closure)(context); | |
101 } | |
102 | |
103 private: | |
104 explicit CallClosureWithExecutionContextTask(std::unique_ptr<Function<void(E
xecutionContext*), threadAffinity>> closure) | |
105 : CallClosureTaskBase<void(ExecutionContext*), threadAffinity>(std::move
(closure)) | |
106 { | |
107 } | |
108 }; | |
109 | 95 |
110 } // namespace internal | 96 } // namespace internal |
111 | 97 |
112 // Create tasks passed within a single thread. | 98 // Create tasks passed within a single thread. |
113 // When posting tasks within a thread, use |createSameThreadTask| instead | 99 // When posting tasks within a thread, use |createSameThreadTask| instead |
114 // of using |bind| directly to state explicitly that there is no need to care | 100 // of using |bind| directly to state explicitly that there is no need to care |
115 // about thread safety when posting the task. | 101 // about thread safety when posting the task. |
116 // When posting tasks across threads, use |createCrossThreadTask|. | 102 // When posting tasks across threads, use |createCrossThreadTask|. |
117 template<typename FunctionType, typename... P> | 103 template<typename FunctionType, typename... P> |
118 std::unique_ptr<ExecutionContextTask> createSameThreadTask( | 104 std::unique_ptr<ExecutionContextTask> createSameThreadTask( |
119 FunctionType function, P&&... parameters) | 105 FunctionType function, P&&... parameters) |
120 { | 106 { |
121 return internal::CallClosureTask<WTF::SameThreadAffinity>::create(bind(funct
ion, std::forward<P>(parameters)...)); | 107 return internal::createCallClosureTask(bind(function, std::forward<P>(parame
ters)...)); |
122 } | 108 } |
123 | 109 |
124 } // namespace blink | 110 } // namespace blink |
125 | 111 |
126 #endif | 112 #endif |
OLD | NEW |