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#include <wtf/AutodrainedPool.h>
30
31namespace WTF {
32
33CrossThreadTaskHandler::CrossThreadTaskHandler(const char* threadName, AutodrainedPoolForRunLoop useAutodrainedPool)
34 : m_useAutodrainedPool(useAutodrainedPool)
35{
36 ASSERT(isMainThread());
37 Locker<Lock> locker(m_taskThreadCreationLock);
38 Thread::create(threadName, [this] {
39 taskRunLoop();
40 })->detach();
41}
42
43CrossThreadTaskHandler::~CrossThreadTaskHandler()
44{
45 ASSERT(isMainThread());
46}
47
48void CrossThreadTaskHandler::postTask(CrossThreadTask&& task)
49{
50 m_taskQueue.append(WTFMove(task));
51}
52
53void CrossThreadTaskHandler::postTaskReply(CrossThreadTask&& task)
54{
55 m_taskReplyQueue.append(WTFMove(task));
56
57 Locker<Lock> locker(m_mainThreadReplyLock);
58 if (m_mainThreadReplyScheduled)
59 return;
60
61 m_mainThreadReplyScheduled = true;
62 callOnMainThread([this] {
63 handleTaskRepliesOnMainThread();
64 });
65}
66
67void CrossThreadTaskHandler::taskRunLoop()
68{
69 ASSERT(!isMainThread());
70 {
71 Locker<Lock> locker(m_taskThreadCreationLock);
72 }
73
74 while (!m_taskQueue.isKilled()) {
75 {
76 std::unique_ptr<AutodrainedPool> autodrainedPool = (m_useAutodrainedPool == AutodrainedPoolForRunLoop::Use) ? makeUnique<AutodrainedPool>() : nullptr;
77
78 m_taskQueue.waitForMessage().performTask();
79 }
80
81 Locker<Lock> shouldSuspendLocker(m_shouldSuspendLock);
82 while (m_shouldSuspend) {
83 m_suspendedLock.lock();
84 if (!m_suspended) {
85 m_suspended = true;
86 m_suspendedCondition.notifyOne();
87 }
88 m_suspendedLock.unlock();
89 m_shouldSuspendCondition.wait(m_shouldSuspendLock);
90 }
91 }
92}
93
94void CrossThreadTaskHandler::handleTaskRepliesOnMainThread()
95{
96 {
97 Locker<Lock> locker(m_mainThreadReplyLock);
98 m_mainThreadReplyScheduled = false;
99 }
100
101 while (auto task = m_taskReplyQueue.tryGetMessage())
102 task->performTask();
103}
104
105void CrossThreadTaskHandler::suspendAndWait()
106{
107 ASSERT(isMainThread());
108 {
109 Locker<Lock> locker(m_shouldSuspendLock);
110 m_shouldSuspend = true;
111 }
112
113 // Post an empty task to ensure database thread knows m_shouldSuspend and sets m_suspended.
114 postTask(CrossThreadTask([]() { }));
115
116 Locker<Lock> locker(m_suspendedLock);
117 while (!m_suspended)
118 m_suspendedCondition.wait(m_suspendedLock);
119}
120
121void CrossThreadTaskHandler::resume()
122{
123 ASSERT(isMainThread());
124 Locker<Lock> locker(m_shouldSuspendLock);
125 if (m_shouldSuspend) {
126 m_suspendedLock.lock();
127 if (m_suspended)
128 m_suspended = false;
129 m_suspendedLock.unlock();
130 m_shouldSuspend = false;
131 m_shouldSuspendCondition.notifyOne();
132 }
133}
134
135} // namespace WTF
136