1 | /* |
2 | * Copyright (C) 2007-2017 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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | #include <wtf/MainThread.h> |
31 | |
32 | #include <mutex> |
33 | #include <wtf/Condition.h> |
34 | #include <wtf/Deque.h> |
35 | #include <wtf/Lock.h> |
36 | #include <wtf/MonotonicTime.h> |
37 | #include <wtf/NeverDestroyed.h> |
38 | #include <wtf/StdLibExtras.h> |
39 | #include <wtf/ThreadSpecific.h> |
40 | #include <wtf/Threading.h> |
41 | |
42 | namespace WTF { |
43 | |
44 | static bool callbacksPaused; // This global variable is only accessed from main thread. |
45 | static Lock mainThreadFunctionQueueMutex; |
46 | |
47 | static Deque<Function<void ()>>& functionQueue() |
48 | { |
49 | static NeverDestroyed<Deque<Function<void ()>>> functionQueue; |
50 | return functionQueue; |
51 | } |
52 | |
53 | // Share this initializeKey with initializeMainThread and initializeMainThreadToProcessMainThread. |
54 | static std::once_flag initializeKey; |
55 | void initializeMainThread() |
56 | { |
57 | std::call_once(initializeKey, [] { |
58 | initializeThreading(); |
59 | initializeMainThreadPlatform(); |
60 | }); |
61 | } |
62 | |
63 | #if PLATFORM(COCOA) |
64 | #if !USE(WEB_THREAD) |
65 | void initializeMainThreadToProcessMainThread() |
66 | { |
67 | std::call_once(initializeKey, [] { |
68 | initializeThreading(); |
69 | initializeMainThreadToProcessMainThreadPlatform(); |
70 | }); |
71 | } |
72 | #else |
73 | void initializeWebThread() |
74 | { |
75 | static std::once_flag initializeKey; |
76 | std::call_once(initializeKey, [] { |
77 | initializeWebThreadPlatform(); |
78 | }); |
79 | } |
80 | #endif // !USE(WEB_THREAD) |
81 | #endif // PLATFORM(COCOA) |
82 | |
83 | #if !USE(WEB_THREAD) |
84 | bool canAccessThreadLocalDataForThread(Thread& thread) |
85 | { |
86 | return &thread == &Thread::current(); |
87 | } |
88 | #endif |
89 | |
90 | // 0.1 sec delays in UI is approximate threshold when they become noticeable. Have a limit that's half of that. |
91 | static const auto maxRunLoopSuspensionTime = 50_ms; |
92 | |
93 | void dispatchFunctionsFromMainThread() |
94 | { |
95 | ASSERT(isMainThread()); |
96 | |
97 | if (callbacksPaused) |
98 | return; |
99 | |
100 | auto startTime = MonotonicTime::now(); |
101 | |
102 | Function<void ()> function; |
103 | |
104 | while (true) { |
105 | { |
106 | std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex); |
107 | if (!functionQueue().size()) |
108 | break; |
109 | |
110 | function = functionQueue().takeFirst(); |
111 | } |
112 | |
113 | function(); |
114 | |
115 | // Clearing the function can have side effects, so do so outside of the lock above. |
116 | function = nullptr; |
117 | |
118 | // If we are running accumulated functions for too long so UI may become unresponsive, we need to |
119 | // yield so the user input can be processed. Otherwise user may not be able to even close the window. |
120 | // This code has effect only in case the scheduleDispatchFunctionsOnMainThread() is implemented in a way that |
121 | // allows input events to be processed before we are back here. |
122 | if (MonotonicTime::now() - startTime > maxRunLoopSuspensionTime) { |
123 | scheduleDispatchFunctionsOnMainThread(); |
124 | break; |
125 | } |
126 | } |
127 | } |
128 | |
129 | void callOnMainThread(Function<void()>&& function) |
130 | { |
131 | ASSERT(function); |
132 | |
133 | bool needToSchedule = false; |
134 | |
135 | { |
136 | std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex); |
137 | needToSchedule = functionQueue().size() == 0; |
138 | functionQueue().append(WTFMove(function)); |
139 | } |
140 | |
141 | if (needToSchedule) |
142 | scheduleDispatchFunctionsOnMainThread(); |
143 | } |
144 | |
145 | void setMainThreadCallbacksPaused(bool paused) |
146 | { |
147 | ASSERT(isMainThread()); |
148 | |
149 | if (callbacksPaused == paused) |
150 | return; |
151 | |
152 | callbacksPaused = paused; |
153 | |
154 | if (!callbacksPaused) |
155 | scheduleDispatchFunctionsOnMainThread(); |
156 | } |
157 | |
158 | bool isMainThreadOrGCThread() |
159 | { |
160 | if (Thread::mayBeGCThread()) |
161 | return true; |
162 | |
163 | return isMainThread(); |
164 | } |
165 | |
166 | void callOnMainThreadAndWait(WTF::Function<void()>&& function) |
167 | { |
168 | if (isMainThread()) { |
169 | function(); |
170 | return; |
171 | } |
172 | |
173 | Lock mutex; |
174 | Condition conditionVariable; |
175 | |
176 | bool isFinished = false; |
177 | |
178 | callOnMainThread([&, function = WTFMove(function)] { |
179 | function(); |
180 | |
181 | std::lock_guard<Lock> lock(mutex); |
182 | isFinished = true; |
183 | conditionVariable.notifyOne(); |
184 | }); |
185 | |
186 | std::unique_lock<Lock> lock(mutex); |
187 | conditionVariable.wait(lock, [&] { |
188 | return isFinished; |
189 | }); |
190 | } |
191 | |
192 | } // namespace WTF |
193 | |