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
//! Support for values with the `Core.Task` type.
//!
//! The documentation for this module has been slightly adapted from the comments for this struct
//! in [`julia.h`]
//!
//! [`julia.h`]: https://github.com/JuliaLang/julia/blob/96786e22ccabfdafd073122abb1fb69cea921e17/src/julia.h#L1727
use super::symbol::Symbol;
use super::Value;
use crate::error::{JlrsError, JlrsResult};
use crate::traits::Cast;
use crate::{impl_julia_type, impl_julia_typecheck, impl_valid_layout};
use jl_sys::{jl_task_t, jl_task_type};
use std::marker::PhantomData;

/// A Julia `Task` (coroutine).
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
#[repr(transparent)]
pub struct Task<'frame>(*mut jl_task_t, PhantomData<&'frame ()>);

impl<'frame> Task<'frame> {
    pub(crate) unsafe fn wrap(task: *mut jl_task_t) -> Self {
        Task(task, PhantomData)
    }

    #[doc(hidden)]
    pub unsafe fn ptr(self) -> *mut jl_task_t {
        self.0
    }

    /// Invasive linked list for scheduler
    pub fn next(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let next = (&*self.ptr()).next;
            if next.is_null() {
                None
            } else {
                Some(Value::wrap(next))
            }
        }
    }

    /// Invasive linked list for scheduler
    pub fn queue(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let queue = (&*self.ptr()).queue;
            if queue.is_null() {
                None
            } else {
                Some(Value::wrap(queue))
            }
        }
    }

    /// The `tls` field.
    pub fn tls(self) -> Value<'frame, 'static> {
        unsafe {
            let tls = (&*self.ptr()).tls;
            Value::wrap(tls)
        }
    }

    /// The `state` field.
    pub fn state(self) -> Symbol<'frame> {
        unsafe {
            let state = (&*self.ptr()).state;
            Symbol::wrap(state)
        }
    }

    /// The `donenotify` field.
    pub fn donenotify(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let donenotify = (&*self.ptr()).donenotify;
            if donenotify.is_null() {
                None
            } else {
                Some(Value::wrap(donenotify))
            }
        }
    }

    /// The `result` field.
    pub fn result(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let result = (&*self.ptr()).result;
            if result.is_null() {
                None
            } else {
                Some(Value::wrap(result))
            }
        }
    }

    /// The `exception` field.
    pub fn exception(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let exception = (&*self.ptr()).exception;
            if exception.is_null() {
                None
            } else {
                Some(Value::wrap(exception))
            }
        }
    }

    /// The `backtrace` field.
    pub fn backtrace(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let backtrace = (&*self.ptr()).backtrace;
            if backtrace.is_null() {
                None
            } else {
                Some(Value::wrap(backtrace))
            }
        }
    }

    pub fn logstate(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let logstate = (&*self.ptr()).logstate;
            if logstate.is_null() {
                None
            } else {
                Some(Value::wrap(logstate))
            }
        }
    }

    pub fn start(self) -> Option<Value<'frame, 'static>> {
        unsafe {
            let start = (&*self.ptr()).start;
            if start.is_null() {
                None
            } else {
                Some(Value::wrap(start))
            }
        }
    }

    /// Record whether this Task can be migrated to a new thread
    pub fn sticky(self) -> u8 {
        unsafe { (&*self.ptr()).sticky }
    }

    /// Convert `self` to a `Value`.
    pub fn as_value(self) -> Value<'frame, 'static> {
        self.into()
    }
}

impl<'frame> Into<Value<'frame, 'static>> for Task<'frame> {
    fn into(self) -> Value<'frame, 'static> {
        unsafe { Value::wrap(self.ptr().cast()) }
    }
}

unsafe impl<'frame, 'data> Cast<'frame, 'data> for Task<'frame> {
    type Output = Self;
    fn cast(value: Value<'frame, 'data>) -> JlrsResult<Self::Output> {
        if value.is::<Self::Output>() {
            return unsafe { Ok(Self::cast_unchecked(value)) };
        }

        Err(JlrsError::NotATask)?
    }

    unsafe fn cast_unchecked(value: Value<'frame, 'data>) -> Self::Output {
        Self::wrap(value.ptr().cast())
    }
}

impl_julia_typecheck!(Task<'frame>, jl_task_type, 'frame);
impl_julia_type!(Task<'frame>, jl_task_type, 'frame);
impl_valid_layout!(Task<'frame>, 'frame);