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<typename T> | 53 template<typename T, WTF::FunctionThreadAffinity threadAffinity> |
54 class CallClosureTaskBase : public ExecutionContextTask { | 54 class CallClosureTaskBase : public ExecutionContextTask { |
55 protected: | 55 protected: |
56 CallClosureTaskBase(PassOwnPtr<Function<T>> closure, bool isSameThread) | 56 explicit CallClosureTaskBase(PassOwnPtr<Function<T, threadAffinity>> closure
) |
57 : m_closure(std::move(closure)) | 57 : m_closure(std::move(closure)) |
58 #if ENABLE(ASSERT) | |
59 , m_isSameThread(isSameThread) | |
60 , m_createdThread(currentThread()) | |
61 #endif | |
62 { | 58 { |
63 } | 59 } |
64 | 60 |
65 void checkThread() | 61 OwnPtr<Function<T, threadAffinity>> m_closure; |
66 { | |
67 #if ENABLE(ASSERT) | |
68 if (m_isSameThread) { | |
69 RELEASE_ASSERT(m_createdThread == currentThread()); | |
70 } | |
71 #endif | |
72 } | |
73 | |
74 OwnPtr<Function<T>> m_closure; | |
75 | |
76 private: | |
77 #if ENABLE(ASSERT) | |
78 bool m_isSameThread; | |
79 ThreadIdentifier m_createdThread; | |
80 #endif | |
81 }; | 62 }; |
82 | 63 |
83 class CallClosureTask final : public CallClosureTaskBase<void()> { | 64 template<WTF::FunctionThreadAffinity threadAffinity> |
| 65 class CallClosureTask final : public CallClosureTaskBase<void(), threadAffinity>
{ |
84 public: | 66 public: |
85 // Do not use |create| other than in createCrossThreadTask and | 67 // Do not use |create| other than in createCrossThreadTask and |
86 // createSameThreadTask. | 68 // createSameThreadTask. |
87 // See http://crbug.com/390851 | 69 // See http://crbug.com/390851 |
88 static PassOwnPtr<CallClosureTask> create(PassOwnPtr<Closure> closure, bool
isSameThread = false) | 70 static PassOwnPtr<CallClosureTask<threadAffinity>> create(PassOwnPtr<Functio
n<void(), threadAffinity>> closure) |
89 { | 71 { |
90 return adoptPtr(new CallClosureTask(std::move(closure), isSameThread)); | 72 return adoptPtr(new CallClosureTask<threadAffinity>(std::move(closure)))
; |
91 } | 73 } |
92 | 74 |
93 void performTask(ExecutionContext*) override | 75 void performTask(ExecutionContext*) override |
94 { | 76 { |
95 checkThread(); | 77 (*this->m_closure)(); |
96 (*m_closure)(); | |
97 } | 78 } |
98 | 79 |
99 private: | 80 private: |
100 CallClosureTask(PassOwnPtr<Closure> closure, bool isSameThread) | 81 explicit CallClosureTask(PassOwnPtr<Function<void(), threadAffinity>> closur
e) |
101 : CallClosureTaskBase<void()>(std::move(closure), isSameThread) | 82 : CallClosureTaskBase<void(), threadAffinity>(std::move(closure)) |
102 { | 83 { |
103 } | 84 } |
104 }; | 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 PassOwnPtr<CallClosureWithExecutionContextTask> create(PassOwnPtr<Fun
ction<void(ExecutionContext*), threadAffinity>> closure) |
| 94 { |
| 95 return adoptPtr(new CallClosureWithExecutionContextTask(std::move(closur
e))); |
| 96 } |
| 97 |
| 98 void performTask(ExecutionContext* context) override |
| 99 { |
| 100 (*this->m_closure)(context); |
| 101 } |
| 102 |
| 103 private: |
| 104 explicit CallClosureWithExecutionContextTask(PassOwnPtr<Function<void(Execut
ionContext*), threadAffinity>> closure) |
| 105 : CallClosureTaskBase<void(ExecutionContext*), threadAffinity>(std::move
(closure)) |
| 106 { |
| 107 } |
| 108 }; |
105 | 109 |
106 } // namespace internal | 110 } // namespace internal |
107 | 111 |
108 // Create tasks passed within a single thread. | 112 // Create tasks passed within a single thread. |
109 // When posting tasks within a thread, use |createSameThreadTask| instead | 113 // When posting tasks within a thread, use |createSameThreadTask| instead |
110 // of using |bind| directly to state explicitly that there is no need to care | 114 // of using |bind| directly to state explicitly that there is no need to care |
111 // about thread safety when posting the task. | 115 // about thread safety when posting the task. |
112 // When posting tasks across threads, use |createCrossThreadTask|. | 116 // When posting tasks across threads, use |createCrossThreadTask|. |
113 template<typename FunctionType, typename... P> | 117 template<typename FunctionType, typename... P> |
114 PassOwnPtr<ExecutionContextTask> createSameThreadTask( | 118 PassOwnPtr<ExecutionContextTask> createSameThreadTask( |
115 FunctionType function, const P&... parameters) | 119 FunctionType function, const P&... parameters) |
116 { | 120 { |
117 return internal::CallClosureTask::create(bind(function, parameters...), true
); | 121 return internal::CallClosureTask<WTF::SameThreadAffinity>::create(bind(funct
ion, parameters...)); |
118 } | 122 } |
119 | 123 |
120 } // namespace blink | 124 } // namespace blink |
121 | 125 |
122 #endif | 126 #endif |
OLD | NEW |