Skip to content

Commit 86174c3

Browse files
cjhopmanfacebook-github-bot
authored andcommitted
Rearrange code + add simple documentation
Summary: The main thing in this module is going to end up being three views of this execution context, moving them to the top of the file. Reviewed By: JakobDegen Differential Revision: D65362045 fbshipit-source-id: 501240ccd55a358ea59b80a90fd169c39752df6b
1 parent 15d8eb8 commit 86174c3

File tree

1 file changed

+78
-73
lines changed

1 file changed

+78
-73
lines changed

app/buck2_futures/src/details/shared_state.rs

+78-73
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,84 @@ use slab::Slab;
2424

2525
use crate::cancellation::CriticalSectionGuard;
2626

27+
/// Shared cancellation related execution context for a cancellable task.
28+
///
29+
/// This is the "outer" context, held by the task's spawned future impl within
30+
/// its poll loop.
31+
pub(crate) struct ExecutionContextOuter {
32+
shared: Arc<Mutex<ExecutionContextData>>,
33+
}
34+
35+
impl ExecutionContextOuter {
36+
pub(crate) fn new() -> (ExecutionContextOuter, ExecutionContextInner) {
37+
let shared = Arc::new(Mutex::new(ExecutionContextData {
38+
cancellation_notification: {
39+
CancellationNotificationData {
40+
inner: Arc::new(CancellationNotificationDataInner {
41+
notified: Default::default(),
42+
wakers: Mutex::new(Some(Default::default())),
43+
}),
44+
}
45+
},
46+
prevent_cancellation: 0,
47+
}));
48+
(
49+
ExecutionContextOuter {
50+
shared: shared.dupe(),
51+
},
52+
ExecutionContextInner { shared },
53+
)
54+
}
55+
56+
pub(crate) fn notify_cancelled(&self) -> bool {
57+
let mut lock = self.shared.lock();
58+
if lock.can_exit() {
59+
true
60+
} else {
61+
lock.notify_cancelled();
62+
false
63+
}
64+
}
65+
66+
pub(crate) fn can_exit(&self) -> bool {
67+
self.shared.lock().can_exit()
68+
}
69+
}
70+
71+
/// Shared cancellation related execution context for a cancellable task.
72+
///
73+
/// This is the "inner" context, used by a CancellationContext to observe cancellation
74+
/// and enter critical sections.
75+
pub(crate) struct ExecutionContextInner {
76+
shared: Arc<Mutex<ExecutionContextData>>,
77+
}
78+
79+
impl ExecutionContextInner {
80+
pub(crate) fn enter_structured_cancellation(&self) -> CriticalSectionGuard {
81+
let mut shared = self.shared.lock();
82+
83+
let notification = shared.enter_structured_cancellation();
84+
85+
CriticalSectionGuard::new_explicit(self, notification)
86+
}
87+
88+
pub(crate) fn try_to_disable_cancellation(&self) -> bool {
89+
let mut shared = self.shared.lock();
90+
if shared.try_to_disable_cancellation() {
91+
true
92+
} else {
93+
// couldn't prevent cancellation, so release our hold onto the counter
94+
shared.exit_prevent_cancellation();
95+
false
96+
}
97+
}
98+
99+
pub(crate) fn exit_prevent_cancellation(&self) -> bool {
100+
let mut shared = self.shared.lock();
101+
shared.exit_prevent_cancellation()
102+
}
103+
}
104+
27105
pub(crate) struct SharedState {
28106
inner: Arc<SharedStateData>,
29107
}
@@ -182,79 +260,6 @@ impl ExecutionContextData {
182260
}
183261
}
184262

185-
/// Context relating to execution of the `poll` of the future. This will contain the information
186-
/// required for the `CancellationContext` that the future holds to enter critical sections and
187-
/// structured cancellations.
188-
pub(crate) struct ExecutionContextOuter {
189-
shared: Arc<Mutex<ExecutionContextData>>,
190-
}
191-
192-
pub(crate) struct ExecutionContextInner {
193-
shared: Arc<Mutex<ExecutionContextData>>,
194-
}
195-
196-
impl ExecutionContextOuter {
197-
pub(crate) fn new() -> (ExecutionContextOuter, ExecutionContextInner) {
198-
let shared = Arc::new(Mutex::new(ExecutionContextData {
199-
cancellation_notification: {
200-
CancellationNotificationData {
201-
inner: Arc::new(CancellationNotificationDataInner {
202-
notified: Default::default(),
203-
wakers: Mutex::new(Some(Default::default())),
204-
}),
205-
}
206-
},
207-
prevent_cancellation: 0,
208-
}));
209-
(
210-
ExecutionContextOuter {
211-
shared: shared.dupe(),
212-
},
213-
ExecutionContextInner { shared },
214-
)
215-
}
216-
217-
pub(crate) fn notify_cancelled(&self) -> bool {
218-
let mut lock = self.shared.lock();
219-
if lock.can_exit() {
220-
true
221-
} else {
222-
lock.notify_cancelled();
223-
false
224-
}
225-
}
226-
227-
pub(crate) fn can_exit(&self) -> bool {
228-
self.shared.lock().can_exit()
229-
}
230-
}
231-
232-
impl ExecutionContextInner {
233-
pub(crate) fn enter_structured_cancellation(&self) -> CriticalSectionGuard {
234-
let mut shared = self.shared.lock();
235-
236-
let notification = shared.enter_structured_cancellation();
237-
238-
CriticalSectionGuard::new_explicit(self, notification)
239-
}
240-
241-
pub(crate) fn try_to_disable_cancellation(&self) -> bool {
242-
let mut shared = self.shared.lock();
243-
if shared.try_to_disable_cancellation() {
244-
true
245-
} else {
246-
// couldn't prevent cancellation, so release our hold onto the counter
247-
shared.exit_prevent_cancellation();
248-
false
249-
}
250-
}
251-
252-
pub(crate) fn exit_prevent_cancellation(&self) -> bool {
253-
let mut shared = self.shared.lock();
254-
shared.exit_prevent_cancellation()
255-
}
256-
}
257-
258263
pub(crate) enum CancellationNotificationStatus {
259264
/// no notifications yet. maps to '0'
260265
Pending,

0 commit comments

Comments
 (0)