asyncnal 0.3.1

Fast concurrent & local asyncronous signalling
Documentation
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use core::{pin::Pin, task::Poll};
use std::{cell::{Cell, RefCell}, collections::VecDeque, rc::Rc, sync::{Arc}, task::{Context, Waker}};

use crossbeam_utils::CachePadded;
use lfqueue::UnboundedQueue;

use crate::base::{event::{EventSetter, EventState, EventWaker, impl_async_event}, lot::{AsyncLotSignature, AsyncLotTemplate, impl_async_lot}, signal::{TechnicalCounter, ValidityMarker, ValidityState, WakeQueue}};

use crate::atomic::*;


/// A [CountedEvent] wraps a type that implements the [EventSetter] trait. It can tell
/// you how many waiters there are on that very event. The count will only go up once the
/// waiter actually starts waiting on the event. If there is an immediate return on the first
/// time the wait is polled, the count will not go up.
///
/// The count will go up if the first call to poll wait returns [Poll::Pending], indicating that
/// the waiter is actually waiting on notification. Upon notification, the count will
/// be decremented.
///
/// # Cancel Safety
/// On [Drop] the count is decremented if the [CountedAwaiter] had already
/// incremented the count and is not yet in the complete state.
pub struct CountedEvent<E> {
    inner: E,
    counter: AtomicUsize,
}

#[pin_project::pin_project(PinnedDrop)]
#[allow(private_bounds)]
pub struct CountedAwaiter<'a, E, F>
where
    E: EventSetter<'a>,
    F: Future<Output = ()>,
{
    master: &'a CountedEvent<E>,
    #[pin]
    future: F,
    state: CountedAwaiterState,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CountedAwaiterState {
    /// The waiter has just been created and has
    /// not yet incremented the count.
    Init,
    /// The waiter is in the waiting state, therefore
    /// there has been an increase in the count
    Waiting,
    /// The waiter is actually complete, meaning that the
    /// count has been decremented already.
    Complete,
}

#[pin_project::pinned_drop]
impl<'a, E, F> PinnedDrop for CountedAwaiter<'a, E, F>
where
    E: EventSetter<'a>,
    F: Future<Output = ()>,
{
    fn drop(self: Pin<&mut Self>) {
        match self.state {
            CountedAwaiterState::Init => {
                // If we are initializing then we have not actually
                // modified any of the state, so we are in the clear.
            }
            CountedAwaiterState::Waiting => {
                // If we are in the waiting state then we have incremented
                // the counter, so we need to undo this operation.
                self.master.counter.fetch_sub(1, Ordering::Release);
            }
            CountedAwaiterState::Complete => {
                // If we are complete than there is nothing to
                // do so we do not execute any special drop code.
            }
        }
    }
}

impl<'a, E, F> Future for CountedAwaiter<'a, E, F>
where
    E: EventSetter<'a>,
    F: Future<Output = ()> + Unpin,
{
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        // NOTE ON STATE: State is very important mostly for the reason of
        // properly undoing things when stuff gets cancelled (dropped).
        match this.state {
            CountedAwaiterState::Init => {
                // We are currently initializing. In this case we actually need
                // to increment the waiting variables if there is not an immediate return.
                match this.future.poll(cx) {
                    Poll::Pending => {
                        // In this case we have polled our future, and clearly have not immediately returned,
                        // and thus we must increment the count and transition to the waiting
                        // state.
                        this.master.counter.fetch_add(1, Ordering::Release);
                        *this.state = CountedAwaiterState::Waiting;
                        Poll::Pending
                    }
                    Poll::Ready(_) => {
                        // This is a special case of a fast return. This immediately short circuits
                        // the future, and thus there is no need to increment the count.
                        Poll::Ready(())
                    }
                }
            }
            CountedAwaiterState::Waiting => {
                // We need to check if we are ready to complete.
                match this.future.poll(cx) {
                    // If we are still pending, then there
                    // is nothing to be said here and we just return.
                    Poll::Pending => Poll::Pending,
                    Poll::Ready(_) => {
                        // If we are ready then we decrement the count.
                        this.master.counter.fetch_sub(1, Ordering::Release);
                        *this.state = CountedAwaiterState::Complete;
                        Poll::Ready(())
                    }
                }
            }
            CountedAwaiterState::Complete => {
                // The future should never really be polled here but
                // if we do then we are ready.
                Poll::Ready(())
            }
        }
    }
}

#[allow(private_bounds)]
impl<E> CountedEvent<E>
where
    for<'a> E: EventSetter<'a>,
{
    #[allow(private_interfaces)]
    pub fn new(source: E) -> Self {
        Self {
            inner: source,
            counter: AtomicUsize::new(0),
        }
    }

    #[allow(private_interfaces)]
    pub fn wait(&self) -> <Self as EventSetter>::Waiter {
        <Self as EventSetter>::wait(&self)
    }

    #[allow(private_interfaces)]
    pub fn set_one(&self) {
        <Self as EventSetter>::set_one(&self);
    }

    #[allow(private_interfaces)]
    pub fn set_all(&self) {
        <Self as EventSetter>::set_all(&self, || {});
    }

    pub fn count(&self) -> usize {
        self.counter.load(Acquire)
    }
}

impl<'a, E> EventSetter<'a> for CountedEvent<E>
where
    E: EventSetter<'a> + 'a,
{
    type Waiter = CountedAwaiter<'a, E, E::Waiter>;


    fn new() -> Self {
        Self {
            counter: AtomicUsize::default(),
            inner: E::new()
        }
    }

    fn new_set() -> Self {
        Self {
            counter: AtomicUsize::default(),
            inner: E::new_set()
        }
    }

    fn wait(&'a self) -> CountedAwaiter<'a, E, E::Waiter> {
        // self.counter.fetch_add(1, Release);
        CountedAwaiter {
            master: self,
            future: self.inner.wait(),
            state: CountedAwaiterState::Init,
        }
    }

    fn try_wait(&self) -> bool {
        if self.inner.try_wait() {
            // self.counter.fetch_sub(1, Ordering::Release);
            true
        } else {
            false
        }
    }

    fn set_all<F: FnMut()>(&self, mut functor: F) {
        self.inner.set_all(|| {
            functor();
            self.counter.fetch_sub(1, Ordering::Release);
        });
    }
    fn set_one(&self) -> bool {
        if self.inner.set_one() {
            // self.counter.fetch_sub(1, Ordering::Release);
            true
        } else {
            false
        }
    }
    fn has_waiters(&self) -> bool {
        self.inner.has_waiters()
    }
}



impl ValidityMarker for Arc<AtomicU8> {
    fn create() -> Self {
        Arc::new(AtomicU8::new(ValidityState::Idle as u8))
    }
    fn get(&self) -> ValidityState {
        ValidityState::from_u8(self.load(Acquire)).expect("Invalid validity state discriminant stored.")
    }
    fn set(&self, value: ValidityState) {
        self.store(value as u8, Release);
    }
}

impl TechnicalCounter for CachePadded<AtomicUsize> {
    fn decrement(&self) {
        self.fetch_sub(1, Release);
    }
    fn increment(&self) {
        self.fetch_add(1, Release);
    }
    fn get(&self) -> usize {
        self.load(Acquire)
    }
}

impl<T> WakeQueue<T> for UnboundedQueue<T>
where 
    T: Send + Sync
{
    fn dequeue(&self) -> Option<T> {
        UnboundedQueue::dequeue(&self)
    }
    fn enqueue(&self, item: T) {
        UnboundedQueue::enqueue(&self, item);
    }
}





#[derive(Default)]
struct LocalCounter(Cell<usize>);

impl TechnicalCounter for LocalCounter {
    fn decrement(&self) {
        let current = self.0.get();
        self.0.set(current - 1);
    }
    fn get(&self) -> usize {
        self.0.get()
    }
    fn increment(&self) {
        let current = self.0.get();
        self.0.set(current + 1);
    }
}

struct LocalQueue<T>(RefCell<VecDeque<T>>);


impl<T> Default for LocalQueue<T> {
    fn default() -> Self {
        Self(RefCell::default())
    }
}


impl<T> WakeQueue<T> for LocalQueue<T> {
    fn dequeue(&self) -> Option<T> {
        self.0.borrow_mut().pop_front()
    }
    fn enqueue(&self, item: T) {
        self.0.borrow_mut().push_back(item);
    }
}

impl ValidityMarker for Rc<Cell<ValidityState>> {
    fn create() -> Self {
        Rc::new(Cell::new(ValidityState::Idle))
    }
    fn get(&self) -> ValidityState {
        Cell::get(&self)
    }
    fn set(&self, value: ValidityState) {
        Cell::set(&self, value);
    }
}




impl_async_lot!(
    name = LocalAllocatedAsyncLot,
    waker = EventWaker,
    validity = Rc<Cell<ValidityState>>,
    counter = LocalCounter,
    queue = LocalQueue
);

impl_async_lot!(
    name = AllocatedAsyncLot,
    waker = EventWaker,
    validity = Arc<AtomicU8>,
    counter = CachePadded<AtomicUsize>,
    queue = UnboundedQueue
);


impl EventState for AtomicU8 {
    fn new(state: u8) -> Self {
        AtomicU8::new(state)
    }
    fn cmpxchng_weak(
            &self,
            current: u8,
            new: u8,
            success: Ordering,
            failure: Ordering,
        ) -> Result<u8, u8> {
        AtomicU8::compare_exchange_weak(&self, current, new, success, failure)
    }
    fn store(
            &self,
            value: u8,
            ordering: Ordering
        ) {
        AtomicU8::store(&self, value, ordering);
    }
}


impl_async_event!(
    /// An event for normal usage. It is [Send] and [Sync] and
    /// can be used within multithreaded environments.
    name = Event,
    waitername = EventAwait,
    lot = AllocatedAsyncLot,
    state = AtomicU8
);


unsafe impl Send for Event {}
unsafe impl Sync for Event {}

struct LocalState(Cell<u8>);

impl EventState for LocalState {
    fn cmpxchng_weak(
            &self,
            current: u8,
            new: u8,
            _: Ordering,
            _: Ordering,
        ) -> Result<u8, u8> {
        let value = self.0.get();
        if value == current {
            self.0.set(new);
            Ok(new)
        } else {
            Err(value)
        }
    }
    fn new(state: u8) -> Self {
        Self(Cell::new(state))
    }
    fn store(
            &self,
            value: u8,
            _: Ordering
        ) {
        self.0.set(value);
    }
}

impl_async_event!(
    /// An event for usage within local runtimess. It is `!Send` and
    /// `!Sync`.
    name = LocalEvent,
    waitername = LocalEventAwait,
    lot = LocalAllocatedAsyncLot,
    state = LocalState
);