Unreviewed, rolling out r235107.
[WebKit-https.git] / Source / JavaScriptCore / runtime / PromiseDeferredTimer.cpp
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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "PromiseDeferredTimer.h"
28
29 #include "JSPromiseDeferred.h"
30 #include "StrongInlines.h"
31 #include "VM.h"
32 #include <wtf/Locker.h>
33 #include <wtf/RunLoop.h>
34
35 namespace JSC {
36
37 namespace PromiseDeferredTimerInternal {
38 static const bool verbose = false;
39 }
40
41 PromiseDeferredTimer::PromiseDeferredTimer(VM& vm)
42     : Base(&vm)
43 {
44 }
45
46 void PromiseDeferredTimer::doWork()
47 {
48     ASSERT(m_vm->currentThreadIsHoldingAPILock());
49     m_taskLock.lock();
50     cancelTimer();
51     if (!m_runTasks) {
52         m_taskLock.unlock();
53         return;
54     }
55
56     while (!m_tasks.isEmpty()) {
57         JSPromiseDeferred* ticket;
58         Task task;
59         std::tie(ticket, task) = m_tasks.takeLast();
60         dataLogLnIf(PromiseDeferredTimerInternal::verbose, "Doing work on promise: ", RawPointer(ticket));
61
62         // We may have already canceled these promises.
63         if (m_pendingPromises.contains(ticket)) {
64             // Allow tasks we run now to schedule work.
65             m_currentlyRunningTask = true;
66             m_taskLock.unlock(); 
67
68             task();
69             m_vm->drainMicrotasks();
70
71             m_taskLock.lock();
72             m_currentlyRunningTask = false;
73         }
74     }
75
76     if (m_pendingPromises.isEmpty() && m_shouldStopRunLoopWhenAllPromisesFinish) {
77 #if USE(CF)
78         CFRunLoopStop(m_runLoop.get());
79 #else
80         RunLoop::current().stop();
81 #endif
82     }
83
84     m_taskLock.unlock();
85 }
86
87 void PromiseDeferredTimer::runRunLoop()
88 {
89     ASSERT(!m_vm->currentThreadIsHoldingAPILock());
90 #if USE(CF)
91     ASSERT(CFRunLoopGetCurrent() == m_runLoop.get());
92 #endif
93     m_shouldStopRunLoopWhenAllPromisesFinish = true;
94     if (m_pendingPromises.size())
95 #if USE(CF)
96         CFRunLoopRun();
97 #else
98         RunLoop::run();
99 #endif
100 }
101
102 void PromiseDeferredTimer::addPendingPromise(JSPromiseDeferred* ticket, Vector<Strong<JSCell>>&& dependencies)
103 {
104     ASSERT(m_vm->currentThreadIsHoldingAPILock());
105     for (unsigned i = 0; i < dependencies.size(); ++i)
106         ASSERT(dependencies[i].get() != ticket);
107
108     auto result = m_pendingPromises.add(ticket, Vector<Strong<JSCell>>());
109     if (result.isNewEntry) {
110         dataLogLnIf(PromiseDeferredTimerInternal::verbose, "Adding new pending promise: ", RawPointer(ticket));
111         dependencies.append(Strong<JSCell>(*m_vm, ticket));
112         result.iterator->value = WTFMove(dependencies);
113     } else {
114         dataLogLnIf(PromiseDeferredTimerInternal::verbose, "Adding new dependencies for promise: ", RawPointer(ticket));
115         result.iterator->value.appendVector(dependencies);
116     }
117
118 #ifndef NDEBUG
119     ticket->promiseAsyncPending();
120 #endif
121 }
122
123 bool PromiseDeferredTimer::hasPendingPromise(JSPromiseDeferred* ticket)
124 {
125     ASSERT(m_vm->currentThreadIsHoldingAPILock());
126     return m_pendingPromises.contains(ticket);
127 }
128
129 bool PromiseDeferredTimer::hasDependancyInPendingPromise(JSPromiseDeferred* ticket, JSCell* dependency)
130 {
131     ASSERT(m_vm->currentThreadIsHoldingAPILock());
132     ASSERT(m_pendingPromises.contains(ticket));
133
134     auto result = m_pendingPromises.get(ticket);
135     return result.contains(dependency);
136 }
137
138 bool PromiseDeferredTimer::cancelPendingPromise(JSPromiseDeferred* ticket)
139 {
140     ASSERT(m_vm->currentThreadIsHoldingAPILock());
141     bool result = m_pendingPromises.remove(ticket);
142
143     if (result)
144         dataLogLnIf(PromiseDeferredTimerInternal::verbose, "Canceling promise: ", RawPointer(ticket));
145
146     return result;
147 }
148
149 void PromiseDeferredTimer::scheduleWorkSoon(JSPromiseDeferred* ticket, Task&& task)
150 {
151     LockHolder locker(m_taskLock);
152     m_tasks.append(std::make_tuple(ticket, WTFMove(task)));
153     if (!isScheduled() && !m_currentlyRunningTask)
154         scheduleTimer(0_s);
155 }
156
157 } // namespace JSC