1 | /* |
2 | * Copyright (C) 2016 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 "GenericTaskQueue.h" |
28 | |
29 | #include <wtf/Lock.h> |
30 | #include <wtf/MainThread.h> |
31 | #include <wtf/NeverDestroyed.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | TaskDispatcher<Timer>::TaskDispatcher() |
36 | { |
37 | } |
38 | |
39 | void TaskDispatcher<Timer>::postTask(Function<void()>&& function) |
40 | { |
41 | { |
42 | auto locker = holdLock(sharedLock()); |
43 | m_pendingTasks.append(WTFMove(function)); |
44 | pendingDispatchers().append(makeWeakPtr(*this)); |
45 | } |
46 | |
47 | auto startTimer = [] { |
48 | if (!sharedTimer().isActive()) |
49 | sharedTimer().startOneShot(0_s); |
50 | }; |
51 | if (isMainThread()) |
52 | startTimer(); |
53 | else |
54 | callOnMainThread(WTFMove(startTimer)); |
55 | } |
56 | |
57 | Timer& TaskDispatcher<Timer>::sharedTimer() |
58 | { |
59 | ASSERT(isMainThread()); |
60 | static NeverDestroyed<Timer> timer([] { TaskDispatcher<Timer>::sharedTimerFired(); }); |
61 | return timer.get(); |
62 | } |
63 | |
64 | Lock& TaskDispatcher<Timer>::sharedLock() |
65 | { |
66 | static NeverDestroyed<Lock> lock; |
67 | return lock; |
68 | } |
69 | |
70 | void TaskDispatcher<Timer>::sharedTimerFired() |
71 | { |
72 | ASSERT(!sharedTimer().isActive()); |
73 | |
74 | // Copy the pending events first because we don't want to process synchronously the new events |
75 | // queued by the JS events handlers that are executed in the loop below. |
76 | Deque<WeakPtr<TaskDispatcher<Timer>>> queuedDispatchers; |
77 | { |
78 | auto locker = holdLock(sharedLock()); |
79 | queuedDispatchers = WTFMove(pendingDispatchers()); |
80 | } |
81 | while (!queuedDispatchers.isEmpty()) { |
82 | WeakPtr<TaskDispatcher<Timer>> dispatcher = queuedDispatchers.takeFirst(); |
83 | if (!dispatcher) |
84 | continue; |
85 | dispatcher->dispatchOneTask(); |
86 | } |
87 | } |
88 | |
89 | |
90 | Deque<WeakPtr<TaskDispatcher<Timer>>>& TaskDispatcher<Timer>::pendingDispatchers() |
91 | { |
92 | static LazyNeverDestroyed<Deque<WeakPtr<TaskDispatcher<Timer>>>> dispatchers; |
93 | |
94 | static std::once_flag onceFlag; |
95 | std::call_once(onceFlag, [] { |
96 | dispatchers.construct(); |
97 | }); |
98 | |
99 | return dispatchers.get(); |
100 | } |
101 | |
102 | void TaskDispatcher<Timer>::dispatchOneTask() |
103 | { |
104 | WTF::Function<void()> task; |
105 | { |
106 | auto locker = holdLock(sharedLock()); |
107 | ASSERT(!m_pendingTasks.isEmpty()); |
108 | task = m_pendingTasks.takeFirst(); |
109 | } |
110 | task(); |
111 | } |
112 | |
113 | } |
114 | |
115 | |