1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::state::{Waiter, AcquireStep, Permits};
use std::cell::UnsafeCell;
use std::marker::{PhantomPinned, PhantomData};
use crate::{Semaphore, SemaphoreGuard, SemaphoreGuardArc};
use std::sync::Arc;
use std::panic::{UnwindSafe, RefUnwindSafe};
use std::fmt::{Debug, Formatter};
use std::{fmt};
use std::task::{Context, Poll};
use crate::state::AcquireState::{Available, Queued};
use std::ptr::null;
use std::pin::Pin;
use std::future::Future;
use crate::waker::{WakerResult};
use crate::errors::PoisonError;
pub struct AcquireFuture<'a>(pub(crate) UnsafeCell<Waiter>, pub(crate) PhantomData<&'a Semaphore>, pub(crate) PhantomPinned);
pub struct AcquireFutureArc {
pub(crate) arc: Arc<Semaphore>,
pub(crate) inner: AcquireFuture<'static>,
}
unsafe impl<'a> Sync for AcquireFuture<'a> {}
unsafe impl<'a> Send for AcquireFuture<'a> {}
impl<'a> UnwindSafe for AcquireFuture<'a> {}
impl<'a> RefUnwindSafe for AcquireFuture<'a> {}
impl<'a> Debug for AcquireFuture<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("AcquireFuture").field(&unsafe { self.waiter() }.amount).finish()
}
}
impl Debug for AcquireFutureArc {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("AcquireFutureArc").field(&unsafe { self.inner.waiter() }.amount).finish()
}
}
impl<'a> AcquireFuture<'a> {
unsafe fn waiter(&self) -> &Waiter {
&*self.0.get()
}
unsafe fn poll_enter(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<SemaphoreGuard<'a>, PoisonError>> {
(*self.waiter().semaphore).acquire.transact(|mut acquire| {
let (available, back) = match *acquire {
Queued(back) => (0, back),
Available(available) => {
let available = match available.into_usize() {
None => {
*self.waiter().step.get() = AcquireStep::Done;
return Ok(Poll::Ready(Err(PoisonError)));
}
Some(available) => available,
};
if self.waiter().amount <= available {
*acquire = Available(Permits::new(available - self.waiter().amount));
acquire.commit()?;
*self.waiter().step.get() = AcquireStep::Done;
return Ok(Poll::Ready(Ok(SemaphoreGuard::new(
&*self.waiter().semaphore, self.waiter().amount))));
} else {
(available, null())
}
}
};
assert!(self.waiter().waker.poll(cx).is_pending());
*self.waiter().prev.get() = back;
*acquire = Queued(self.0.get());
acquire.commit()?;
*self.waiter().step.get() = AcquireStep::Waiting;
(*self.waiter().semaphore).release(available);
Ok(Poll::Pending)
})
}
unsafe fn poll_waiting(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<SemaphoreGuard<'a>, PoisonError>> {
match self.waiter().waker.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(poisoned) => {
*self.waiter().step.get() = AcquireStep::Done;
if poisoned {
Poll::Ready(Err(PoisonError))
} else {
Poll::Ready(Ok(SemaphoreGuard::new(&*self.waiter().semaphore, self.waiter().amount)))
}
}
}
}
}
impl<'a> Future for AcquireFuture<'a> {
type Output = Result<SemaphoreGuard<'a>, PoisonError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe {
match *(*self.0.get()).step.get() {
AcquireStep::Entering => {
self.poll_enter(cx)
}
AcquireStep::Waiting => {
self.poll_waiting(cx)
}
AcquireStep::Done => panic!("Polling completed future.")
}
}
}
}
impl Future for AcquireFutureArc {
type Output = Result<SemaphoreGuardArc, PoisonError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe {
let this = self.get_unchecked_mut();
match Pin::new_unchecked(&mut this.inner).poll(cx) {
Poll::Ready(guard) => {
let result =
SemaphoreGuardArc::new(this.arc.clone(), guard?.forget());
Poll::Ready(Ok(result))
}
Poll::Pending => Poll::Pending,
}
}
}
}
impl<'a> Drop for AcquireFuture<'a> {
fn drop(&mut self) {
unsafe {
match *self.waiter().step.get() {
AcquireStep::Waiting => {
match self.waiter().waker.start_cancel() {
WakerResult::Cancelling => {
(*self.waiter().semaphore).next_cancel.transact(|mut next_cancel| {
*self.waiter().next_cancel.get() = *next_cancel;
*next_cancel = self.0.get();
next_cancel.commit()?;
Ok(())
});
(*self.waiter().semaphore).release(0);
self.waiter().waker.wait_cancel();
}
WakerResult::Finished { poisoned } => {
if !poisoned {
(*self.waiter().semaphore).release(self.waiter().amount);
}
}
}
}
AcquireStep::Entering { .. } => {}
AcquireStep::Done => {}
}
}
}
}