Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: components/scheduler/promises/promise_executor.cc

Issue 1401553002: NOT INTENDED FOR LANDING: A promises demo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Support for rejectatble promises! Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/scheduler/promises/promise_executor.h"
6
7 namespace promise {
8 namespace internal {
9
10 PromiseExecutor::PromiseExecutor() : cursor_(nullptr) {}
11
12 PromiseExecutor::~PromiseExecutor(){};
13
14 void PromiseExecutor::StartResolveInternal(PromiseBase* promise) {
15 if (promise->state() == PromiseBase::State::NEW ||
16 promise->state() == PromiseBase::State::CANCELLED) {
17 promise->set_state(PromiseBase::State::PENDING);
18 }
19
20 promise->SetThisAndPrerequisitesAsShouldRun(this, true);
21 }
22
23 PromiseBase* PromiseExecutor::GetNextReadyPromise() {
24 if (!eager_ready_set_.empty()) {
25 PromiseBase* ready_promise = *eager_ready_set_.begin();
26 eager_ready_set_.erase(ready_promise);
27 return ready_promise;
28 }
29 if (!lazy_ready_set_.empty()) {
30 PromiseBase* ready_promise = *lazy_ready_set_.begin();
31 lazy_ready_set_.erase(ready_promise);
32 return ready_promise;
33 }
34 return nullptr;
35 }
36
37 bool PromiseExecutor::ResolveOnePromise() {
38 PromiseBase* ready_promise = GetNextReadyPromise();
39 if (!ready_promise)
40 return false;
41
42 ResolvePromise(ready_promise);
43 return true;
44 }
45
46 void PromiseExecutor::ResolvePromise(PromiseBase* promise) {
47 DCHECK(promise->state() != PromiseBase::State::RESOLVED);
48 DCHECK(promise->AllPrerequisitesSatisfied());
49
50 if (promise->state() == PromiseBase::State::PARTIALLY_RESOLVED) {
51 promise->MarkAsResolved();
52 } else {
53 promise->ExecutorDrivenResolve();
54 }
55 }
56
57 void PromiseExecutor::RunUntilIdle() {
58 while (ResolveOnePromise()) {
59 }
60 }
61
62 bool PromiseExecutor::OnPromiseResolved(PromiseBase* promise) {
63 bool new_work_to_do = false;
64 for (PromiseBase* dependee : promise->dependants()) {
65 if (dependee->AllPrerequisitesSatisfied()) {
66 if (!dependee->can_run_now())
67 new_work_to_do = true;
68 dependee->SchedulePromiseForResolve();
69 }
70 }
71 return new_work_to_do;
72 }
73
74 bool PromiseExecutor::OnPromiseRejected(PromiseBase* promise) {
75 bool new_work_to_do = false;
76 for (PromiseBase* dependee : promise->dependants()) {
77 dependee->set_state(PromiseBase::State::DEPENDENCY_REJECTED);
78 if (!dependee->can_run_now())
79 new_work_to_do = true;
80 dependee->SchedulePromiseForResolve();
81 }
82 return new_work_to_do;
83 }
84
85 } // namespace internal
86 } // namespace promise
OLDNEW
« no previous file with comments | « components/scheduler/promises/promise_executor.h ('k') | components/scheduler/promises/promise_internal.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698