| 1 | /* |
| 2 | * Copyright (C) 2007-2019 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/RunLoop.h> |
| 39 | #include <wtf/StdLibExtras.h> |
| 40 | #include <wtf/ThreadSpecific.h> |
| 41 | #include <wtf/Threading.h> |
| 42 | |
| 43 | namespace WTF { |
| 44 | |
| 45 | static bool callbacksPaused; // This global variable is only accessed from main thread. |
| 46 | static Lock mainThreadFunctionQueueMutex; |
| 47 | |
| 48 | static Deque<Function<void ()>>& functionQueue() |
| 49 | { |
| 50 | static NeverDestroyed<Deque<Function<void ()>>> functionQueue; |
| 51 | return functionQueue; |
| 52 | } |
| 53 | |
| 54 | // Share this initializeKey with initializeMainThread and initializeMainThreadToProcessMainThread. |
| 55 | static std::once_flag initializeKey; |
| 56 | void initializeMainThread() |
| 57 | { |
| 58 | std::call_once(initializeKey, [] { |
| 59 | initializeThreading(); |
| 60 | initializeMainThreadPlatform(); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | #if !USE(WEB_THREAD) |
| 65 | bool canCurrentThreadAccessThreadLocalData(Thread& thread) |
| 66 | { |
| 67 | return &thread == &Thread::current(); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | // 0.1 sec delays in UI is approximate threshold when they become noticeable. Have a limit that's half of that. |
| 72 | static constexpr auto maxRunLoopSuspensionTime = 50_ms; |
| 73 | |
| 74 | void dispatchFunctionsFromMainThread() |
| 75 | { |
| 76 | ASSERT(isMainThread()); |
| 77 | |
| 78 | if (callbacksPaused) |
| 79 | return; |
| 80 | |
| 81 | auto startTime = MonotonicTime::now(); |
| 82 | |
| 83 | Function<void ()> function; |
| 84 | |
| 85 | while (true) { |
| 86 | { |
| 87 | std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex); |
| 88 | if (!functionQueue().size()) |
| 89 | break; |
| 90 | |
| 91 | function = functionQueue().takeFirst(); |
| 92 | } |
| 93 | |
| 94 | function(); |
| 95 | |
| 96 | // Clearing the function can have side effects, so do so outside of the lock above. |
| 97 | function = nullptr; |
| 98 | |
| 99 | // If we are running accumulated functions for too long so UI may become unresponsive, we need to |
| 100 | // yield so the user input can be processed. Otherwise user may not be able to even close the window. |
| 101 | // This code has effect only in case the scheduleDispatchFunctionsOnMainThread() is implemented in a way that |
| 102 | // allows input events to be processed before we are back here. |
| 103 | if (MonotonicTime::now() - startTime > maxRunLoopSuspensionTime) { |
| 104 | scheduleDispatchFunctionsOnMainThread(); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool isMainRunLoop() |
| 111 | { |
| 112 | return RunLoop::isMain(); |
| 113 | } |
| 114 | |
| 115 | void callOnMainRunLoop(Function<void()>&& function) |
| 116 | { |
| 117 | RunLoop::main().dispatch(WTFMove(function)); |
| 118 | } |
| 119 | |
| 120 | void callOnMainThread(Function<void()>&& function) |
| 121 | { |
| 122 | ASSERT(function); |
| 123 | |
| 124 | bool needToSchedule = false; |
| 125 | |
| 126 | { |
| 127 | std::lock_guard<Lock> lock(mainThreadFunctionQueueMutex); |
| 128 | needToSchedule = functionQueue().size() == 0; |
| 129 | functionQueue().append(WTFMove(function)); |
| 130 | } |
| 131 | |
| 132 | if (needToSchedule) |
| 133 | scheduleDispatchFunctionsOnMainThread(); |
| 134 | } |
| 135 | |
| 136 | void setMainThreadCallbacksPaused(bool paused) |
| 137 | { |
| 138 | ASSERT(isMainThread()); |
| 139 | |
| 140 | if (callbacksPaused == paused) |
| 141 | return; |
| 142 | |
| 143 | callbacksPaused = paused; |
| 144 | |
| 145 | if (!callbacksPaused) |
| 146 | scheduleDispatchFunctionsOnMainThread(); |
| 147 | } |
| 148 | |
| 149 | bool isMainThreadOrGCThread() |
| 150 | { |
| 151 | if (Thread::mayBeGCThread()) |
| 152 | return true; |
| 153 | |
| 154 | return isMainThread(); |
| 155 | } |
| 156 | |
| 157 | enum class MainStyle : bool { |
| 158 | Thread, |
| 159 | RunLoop |
| 160 | }; |
| 161 | |
| 162 | static void callOnMainAndWait(WTF::Function<void()>&& function, MainStyle mainStyle) |
| 163 | { |
| 164 | |
| 165 | if (mainStyle == MainStyle::Thread ? isMainThread() : isMainRunLoop()) { |
| 166 | function(); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | Lock mutex; |
| 171 | Condition conditionVariable; |
| 172 | |
| 173 | bool isFinished = false; |
| 174 | |
| 175 | auto functionImpl = [&, function = WTFMove(function)] { |
| 176 | function(); |
| 177 | |
| 178 | std::lock_guard<Lock> lock(mutex); |
| 179 | isFinished = true; |
| 180 | conditionVariable.notifyOne(); |
| 181 | }; |
| 182 | |
| 183 | switch (mainStyle) { |
| 184 | case MainStyle::Thread: |
| 185 | callOnMainThread(WTFMove(functionImpl)); |
| 186 | break; |
| 187 | case MainStyle::RunLoop: |
| 188 | callOnMainRunLoop(WTFMove(functionImpl)); |
| 189 | }; |
| 190 | |
| 191 | std::unique_lock<Lock> lock(mutex); |
| 192 | conditionVariable.wait(lock, [&] { |
| 193 | return isFinished; |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | void callOnMainRunLoopAndWait(WTF::Function<void()>&& function) |
| 198 | { |
| 199 | callOnMainAndWait(WTFMove(function), MainStyle::RunLoop); |
| 200 | } |
| 201 | |
| 202 | void callOnMainThreadAndWait(WTF::Function<void()>&& function) |
| 203 | { |
| 204 | callOnMainAndWait(WTFMove(function), MainStyle::Thread); |
| 205 | } |
| 206 | |
| 207 | } // namespace WTF |
| 208 | |