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
//! A context for a task
//!
//! [TypedContext](struct.TypedContext.html) corresponds to a continuation. You can wake the task
//! via `.waker()` and perform a special effect called [Continue](../struct.Continue.html).
//! `Continue` effect makes the handler wait for the source computation to complete.

use super::{Continue, Effect, Effectful, Poll};

use std::cell::Cell;
use std::fmt;
use std::pin::Pin;
use std::sync::Arc;

use crossbeam_channel::{bounded, Receiver, Sender};

thread_local! {
    static TLS_CX: Cell<Option<&'static Context>> = Cell::new(None);
}

/// The untyped context of the task
///
/// `Context` coordinates how the task gets notified of the next poll
#[derive(Clone)]
pub struct Context {
    notify: Arc<dyn Notify>,
}

impl Context {
    /// Construct a context out of [Notify](trait.Notify.html)
    pub fn from_notify(notify: Arc<dyn Notify>) -> Self {
        Context { notify }
    }

    pub fn notify(&self) -> &Arc<dyn Notify> {
        &self.notify
    }
}

/// A notification object
pub trait Notify: Send + Sync {
    /// Notify the runtime of the wakeup of the task
    fn wake(&self);
}

/// The waker of the task
pub struct Waker<E: Effect> {
    sender: Sender<E::Output>,
    notify: Arc<dyn Notify>,
}

impl<E: Effect> fmt::Debug for Waker<E>
where
    E::Output: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Waker")
            .field("sender", &self.sender)
            .field("notify", &"<...>")
            .finish()
    }
}

impl<E: Effect> Clone for Waker<E> {
    fn clone(&self) -> Self {
        Self {
            sender: self.sender.clone(),
            notify: Arc::clone(&self.notify),
        }
    }
}

impl<E: Effect> Waker<E> {
    fn new(sender: Sender<E::Output>, notify: Arc<dyn Notify>) -> Self {
        Self { sender, notify }
    }

    /// Wake up the task
    pub fn wake(&self, v: E::Output) {
        // set handler result.
        // do not notify the task if send fails because the result will not be used.
        if let Ok(()) = self.sender.send(v) {
            // wake up the task
            self.notify.wake();
        }
    }

    /// Wake up the task if no other wakers have already woken it up
    ///
    /// # Errors
    /// Returns an error if another waker has already woken up the task
    pub fn try_wake(&self, v: E::Output) -> Result<(), E::Output> {
        // set handler result.
        // do not notify the task if send fails because the result will not be used.
        match self.sender.try_send(v) {
            Ok(()) => {
                // wake up the task
                self.notify.wake();
                Ok(())
            }
            Err(e) => Err(e.into_inner()),
        }
    }
}

/// Create a channel for communicating the result of an effect under the context
pub fn channel<E: Effect>(cx: &Context) -> (TypedContext<E>, Receiver<E::Output>) {
    let (sender, receiver) = bounded(1);
    let waker = Waker::new(sender, Arc::clone(&cx.notify));
    (TypedContext(waker), receiver)
}

/// The typed context of the task
pub struct TypedContext<E: Effect>(Waker<E>);

impl<E: Effect> fmt::Debug for TypedContext<E>
where
    E::Output: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("TypedContext").field(&self.0).finish()
    }
}

impl<E: Effect> TypedContext<E> {
    /// Retrieve a waker of the task
    pub fn waker(&self) -> Waker<E> {
        self.0.clone()
    }

    /// Create a [Continue](../struct.Continue.html) effect for the completion of the source computation
    pub fn continuation<R>(self) -> Continue<R> {
        Continue::new()
    }

    /// Wake up the task and create a [Continue](../struct.Continue.html) effect
    ///
    /// This method can be used to immediately resume the source computation
    pub fn resume<R>(self, v: E::Output) -> Continue<R> {
        self.0.wake(v);
        self.continuation()
    }
}

struct SetContextOnDrop(Option<&'static Context>);

impl Drop for SetContextOnDrop {
    fn drop(&mut self) {
        TLS_CX.with(|tls_cx| {
            tls_cx.replace(self.0.take());
        })
    }
}

/// Set the thread-local task context used by the generator-backed effectful computation
pub fn set_task_context<F, R>(cx: &Context, f: F) -> R
where
    F: FnOnce() -> R,
{
    let old_cx = TLS_CX.with(|tls_cx| {
        tls_cx.replace(Some(unsafe {
            &*(cx as *const Context) as &'static Context
        }))
    });
    let _reset_cx = SetContextOnDrop(old_cx);

    f()
}

/// Get the thread-local task context used by the generator-backed effectful computation
///
/// # Panics
/// Panics if the thread-local task is not set
pub fn get_task_context<'a>() -> &'a Context {
    TLS_CX
        .with(|tls_cx| tls_cx.get().take())
        .expect("thread local context must be set")
}

/// Poll the given computation with the thread-local task context
///
/// # Panics
/// Panics if the thread-local task is not set
pub fn poll_with_task_context<C: Effectful>(comp: Pin<&mut C>) -> Poll<C::Output, C::Effect> {
    comp.poll(get_task_context())
}