1 | /* |
2 | * Copyright (C) 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 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include <wtf/CrossThreadTaskHandler.h> |
28 | |
29 | namespace WTF { |
30 | |
31 | CrossThreadTaskHandler::CrossThreadTaskHandler(const char* threadName) |
32 | { |
33 | ASSERT(isMainThread()); |
34 | Locker<Lock> locker(m_taskThreadCreationLock); |
35 | Thread::create(threadName, [this] { |
36 | taskRunLoop(); |
37 | })->detach(); |
38 | } |
39 | |
40 | CrossThreadTaskHandler::~CrossThreadTaskHandler() |
41 | { |
42 | ASSERT(isMainThread()); |
43 | } |
44 | |
45 | void CrossThreadTaskHandler::postTask(CrossThreadTask&& task) |
46 | { |
47 | m_taskQueue.append(WTFMove(task)); |
48 | } |
49 | |
50 | void CrossThreadTaskHandler::postTaskReply(CrossThreadTask&& task) |
51 | { |
52 | m_taskReplyQueue.append(WTFMove(task)); |
53 | |
54 | Locker<Lock> locker(m_mainThreadReplyLock); |
55 | if (m_mainThreadReplyScheduled) |
56 | return; |
57 | |
58 | m_mainThreadReplyScheduled = true; |
59 | callOnMainThread([this] { |
60 | handleTaskRepliesOnMainThread(); |
61 | }); |
62 | } |
63 | |
64 | void CrossThreadTaskHandler::taskRunLoop() |
65 | { |
66 | ASSERT(!isMainThread()); |
67 | { |
68 | Locker<Lock> locker(m_taskThreadCreationLock); |
69 | } |
70 | |
71 | while (!m_taskQueue.isKilled()) { |
72 | m_taskQueue.waitForMessage().performTask(); |
73 | |
74 | Locker<Lock> shouldSuspendLocker(m_shouldSuspendLock); |
75 | while (m_shouldSuspend) { |
76 | m_suspendedLock.lock(); |
77 | if (!m_suspended) { |
78 | m_suspended = true; |
79 | m_suspendedCondition.notifyOne(); |
80 | } |
81 | m_suspendedLock.unlock(); |
82 | m_shouldSuspendCondition.wait(m_shouldSuspendLock); |
83 | } |
84 | } |
85 | } |
86 | |
87 | void CrossThreadTaskHandler::handleTaskRepliesOnMainThread() |
88 | { |
89 | { |
90 | Locker<Lock> locker(m_mainThreadReplyLock); |
91 | m_mainThreadReplyScheduled = false; |
92 | } |
93 | |
94 | while (auto task = m_taskReplyQueue.tryGetMessage()) |
95 | task->performTask(); |
96 | } |
97 | |
98 | void CrossThreadTaskHandler::suspendAndWait() |
99 | { |
100 | ASSERT(isMainThread()); |
101 | { |
102 | Locker<Lock> locker(m_shouldSuspendLock); |
103 | m_shouldSuspend = true; |
104 | } |
105 | |
106 | // Post an empty task to ensure database thread knows m_shouldSuspend and sets m_suspended. |
107 | postTask(CrossThreadTask([]() { })); |
108 | |
109 | Locker<Lock> locker(m_suspendedLock); |
110 | while (!m_suspended) |
111 | m_suspendedCondition.wait(m_suspendedLock); |
112 | } |
113 | |
114 | void CrossThreadTaskHandler::resume() |
115 | { |
116 | ASSERT(isMainThread()); |
117 | Locker<Lock> locker(m_shouldSuspendLock); |
118 | if (m_shouldSuspend) { |
119 | m_suspendedLock.lock(); |
120 | if (m_suspended) |
121 | m_suspended = false; |
122 | m_suspendedLock.unlock(); |
123 | m_shouldSuspend = false; |
124 | m_shouldSuspendCondition.notifyOne(); |
125 | } |
126 | } |
127 | |
128 | } // namespace WTF |
129 | |