12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- #include < unordered_map >
15
+ #include < map >
16
16
#include < utility>
17
17
#include < vector>
18
18
@@ -47,13 +47,14 @@ std::pair<FutureID, bool> EventManager::TrackEvent(WGPUCallbackMode mode,
47
47
void EventManager::ShutDown () {
48
48
// Call any outstanding callbacks before destruction.
49
49
while (true ) {
50
- std::unordered_map <FutureID, TrackedEvent> movedEvents;
50
+ std::map <FutureID, TrackedEvent> movedEvents;
51
51
mTrackedEvents .Use ([&](auto trackedEvents) { movedEvents = std::move (*trackedEvents); });
52
52
53
53
if (movedEvents.empty ()) {
54
54
break ;
55
55
}
56
56
57
+ // Ordering guaranteed because we are using a sorted map.
57
58
for (auto & [futureID, trackedEvent] : movedEvents) {
58
59
// Event should be already marked Ready since events are actually driven by
59
60
// RequestTrackers (at the time of this writing), which all shut down before this.
@@ -74,6 +75,7 @@ void EventManager::SetFutureReady(FutureID futureID) {
74
75
}
75
76
76
77
void EventManager::ProcessPollEvents () {
78
+ // Since events are already stored in an ordered map, this list must already be ordered.
77
79
std::vector<TrackedEvent> eventsToCompleteNow;
78
80
79
81
// TODO(crbug.com/dawn/2060): EventManager shouldn't bother to track ProcessEvents-type events
@@ -120,7 +122,9 @@ WGPUWaitStatus EventManager::WaitAny(size_t count, WGPUFutureWaitInfo* infos, ui
120
122
return WGPUWaitStatus_Success;
121
123
}
122
124
123
- std::vector<TrackedEvent> eventsToCompleteNow;
125
+ // Since the user can specify the FutureIDs in any order, we need to use another ordered map
126
+ // here to ensure that the result is ordered for JS event ordering.
127
+ std::map<FutureID, TrackedEvent> eventsToCompleteNow;
124
128
bool anyCompleted = false ;
125
129
const FutureID firstInvalidFutureID = mNextFutureID ;
126
130
mTrackedEvents .Use ([&](auto trackedEvents) {
@@ -141,15 +145,15 @@ WGPUWaitStatus EventManager::WaitAny(size_t count, WGPUFutureWaitInfo* infos, ui
141
145
if (event.mReady ) {
142
146
anyCompleted = true ;
143
147
if (event.mCallback ) {
144
- eventsToCompleteNow.emplace_back ( std::move (event));
148
+ eventsToCompleteNow.emplace (it-> first , std::move (event));
145
149
}
146
150
trackedEvents->erase (it);
147
151
}
148
152
}
149
153
});
150
154
151
155
// TODO(crbug.com/dawn/2066): Guarantee the event ordering from the JS spec.
152
- for (TrackedEvent& event : eventsToCompleteNow) {
156
+ for (auto & [_, event] : eventsToCompleteNow) {
153
157
DAWN_ASSERT (event.mReady && event.mCallback );
154
158
// .completed has already been set to true (before the callback, per API contract).
155
159
event.mCallback (EventCompletionType::Ready);
0 commit comments