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
//! Async executor and some basic futures.

use crate::sync::Spinlock;
use crate::waker;
use crate::{
    executor::{ExecuteWeight, Executor, LocalExecutor, DEFAULT_EXECUTOR},
    sleep_queue::SleepQueue,
};
use alloc::{boxed::Box, sync::Arc};
use core::{
    alloc::AllocError,
    future::Future,
    pin::Pin,
    sync::atomic::{AtomicU8, AtomicUsize, Ordering},
    task::{Context, Poll},
};
use intrusive_collections::{intrusive_adapter, LinkedListLink};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};

pub(crate) struct Task {
    sleep_kind: AtomicU8,
    /// Quantum(= weight < MAX_QUANTUM) for time-sharing tasks or
    /// priority(= weight - MAX_QUANTUM) for real-time tasks.
    weight: AtomicUsize,
    /// Quantum in this round.
    quantum: AtomicUsize,
    /// Timestamp of last tick, used to measure how long this task has been executed.
    timestamp: AtomicUsize,
    /// Index of its local executor, only used for time-sharing tasks.
    local_executor_id: AtomicUsize,
    ctx: &'static Executor,
    future: Spinlock<Pin<Box<dyn Future<Output = ()> + Send + 'static>>>,
    /// SAFETY: Guarded by the lock of task queue.
    link: LinkedListLink,
}

intrusive_adapter!(pub(crate) TaskAdapter = Arc<Task>: Task { link: LinkedListLink });

#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive)]
pub(crate) enum SleepKind {
    Any,
    Mutex,
    Reader,
    Writer,
    UpgradableReader,
}

impl Drop for Task {
    fn drop(&mut self) {
        self.ctx.ntasks.fetch_sub(1, Ordering::SeqCst);
    }
}

impl Task {
    pub fn new(
        ctx: &'static Executor,
        local_executor_id: usize,
        weight: ExecuteWeight,
        future: impl Future<Output = ()> + 'static + Send,
    ) -> Result<Arc<Self>, AllocError> {
        let boxed_future = Box::try_new(future)?;
        let raw_weight = weight.into_raw_weight();
        let task = Arc::try_new(Task {
            sleep_kind: AtomicU8::new(0),
            future: Spinlock::new(Box::into_pin(boxed_future)),
            link: LinkedListLink::new(),
            weight: AtomicUsize::new(raw_weight),
            quantum: AtomicUsize::new(raw_weight),
            local_executor_id: AtomicUsize::new(local_executor_id),
            ctx,
            timestamp: AtomicUsize::new(0),
        })?;
        ctx.ntasks.fetch_add(1, Ordering::SeqCst);
        Ok(task)
    }

    pub fn get_sleep_kind(&self) -> SleepKind {
        let raw_sleep_kind = self.sleep_kind.load(Ordering::Relaxed);
        SleepKind::from_u8(raw_sleep_kind).unwrap()
    }

    pub fn set_sleep_kind(&self, sleep_kind: SleepKind) {
        self.sleep_kind
            .store(sleep_kind.to_u8().unwrap(), Ordering::Relaxed);
    }

    pub fn set_weight(&self, weight: ExecuteWeight) {
        self.weight
            .store(weight.into_raw_weight(), Ordering::Relaxed);
    }

    pub fn get_weight(&self) -> ExecuteWeight {
        ExecuteWeight::from_raw_weight(self.weight.load(Ordering::Relaxed))
    }

    pub fn get_local_executor(&self) -> &'static LocalExecutor {
        let idx = self.local_executor_id.load(Ordering::Relaxed);
        &self.ctx.local_executors[idx]
    }

    pub fn switch_executor(&self, executor_id: usize, from_round: usize, to_round: usize) {
        let diff_round = to_round - from_round;
        let weight = self.weight.load(Ordering::Relaxed) as usize;
        let quantum = self.quantum.load(Ordering::Relaxed);
        self.quantum
            .store(quantum + diff_round * weight, Ordering::Relaxed);
        self.local_executor_id.store(executor_id, Ordering::Relaxed);
    }

    pub fn reload_quantum(&self) {
        let quantum = self.weight.load(Ordering::Relaxed) as usize;
        self.quantum.store(quantum, Ordering::Relaxed);
    }

    /// TODO: use hardware timestamp.
    fn get_current_timestamp(&self) -> usize {
        self.timestamp.load(Ordering::Relaxed) + 1
    }

    pub fn tick_begin(&self) {
        self.timestamp
            .store(self.get_current_timestamp(), Ordering::Relaxed);
    }

    /// Return if the task still have some quantum.
    pub fn tick(&self) -> Option<usize> {
        let timestamp = self.timestamp.load(Ordering::Relaxed);
        let diff = self.get_current_timestamp() - timestamp;
        let mut quantum = self.quantum.load(Ordering::Relaxed);
        quantum = quantum.checked_sub(diff).unwrap_or(0);
        self.quantum.store(quantum, Ordering::Relaxed);
        self.timestamp.store(timestamp + diff, Ordering::Relaxed);
        if quantum == 0 {
            None
        } else {
            Some(quantum)
        }
    }

    pub fn poll(self: &Arc<Self>) -> Poll<()> {
        let waker = waker::waker(self.clone());
        let context = &mut Context::from_waker(&waker);
        let mut future = self.future.lock();
        self.tick_begin();
        future.as_mut().poll(context)
    }
}

impl Task {
    fn from_ctx(ctx: &mut Context<'_>) -> Arc<Task> {
        waker::get_task(ctx.waker())
    }

    pub fn sleep_back(queue: &mut SleepQueue, sleep_kind: SleepKind, ctx: &mut Context<'_>) {
        let task = Task::from_ctx(ctx);
        task.set_sleep_kind(sleep_kind);
        queue.push_back(task);
    }

    pub fn sleep_front(queue: &mut SleepQueue, sleep_kind: SleepKind, ctx: &mut Context<'_>) {
        let task = Task::from_ctx(ctx);
        task.set_sleep_kind(sleep_kind);
        queue.push_front(task);
    }

    pub fn wakeup_front(queue: &mut SleepQueue) -> Option<SleepKind> {
        let task = queue.pop_front()?;
        let sleep_kind = task.get_sleep_kind();
        let executor = task.get_local_executor();
        executor.reschedule(task, false);
        Some(sleep_kind)
    }

    pub fn wakeup_all(queue: &mut SleepQueue) {
        while let Some(_) = Task::wakeup_front(queue) {}
    }
}

/// SAFETY: See [`Task`].
unsafe impl Sync for Task {}

/// Spawn a task to the default executor.
///
/// Note that it doesn't mean to execute the task, just spawn it.
///
/// # Examples
///
/// ```
/// use ksched::task;
///
/// task::spawn(async {
///    println!("hello, world");
/// }).expect("oom");
/// ```
pub fn spawn<T>(future: T) -> Result<(), AllocError>
where
    T: Future<Output = ()> + Send + 'static,
{
    DEFAULT_EXECUTOR.local_executors[0].spawn(future)
}

/// Run until all tasks in default executor are done.
///
/// # Examples
///
/// ```
/// use ksched::task;
///
/// task::spawn(async move {
///     println!("hello, world");
/// }).unwrap();
/// task::run();
/// ```
pub fn run() {
    DEFAULT_EXECUTOR.local_executors[0].run();
}

/// Cooperatively gives up a timeslice to the task scheduler.
///
/// Calling this function will move the currently executing future to the back
/// of the execution queue, making room for other futures to execute. This is
/// especially useful after running CPU-intensive operations inside a future.
///
/// # Examples
///
/// ```
/// # ksched::task::spawn(async {
/// #
/// ksched::task::yield_now().await;
/// #
/// # });
/// # ksched::task::run();
/// ```
pub async fn yield_now() {
    struct YieldNow(bool);

    impl Future for YieldNow {
        type Output = ();
        fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            if !self.0 {
                self.0 = true;
                let executor = task.get_local_executor();
                executor.reschedule(task, false);
                Poll::Pending
            } else {
                Poll::Ready(())
            }
        }
    }

    YieldNow(false).await
}

/// Make a task into a time-sharing task with specific quantum per round.
pub async fn set_quantum(quantum: usize) {
    struct SetQuantum(ExecuteWeight);

    impl Future for SetQuantum {
        type Output = ();
        fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            task.set_weight(self.0);
            Poll::Ready(())
        }
    }

    SetQuantum(ExecuteWeight::from_quantum(quantum)).await
}

/// Retrieve the quantum of a time-sharing task.
///
/// Return [None] if this is a real-time task.
pub async fn get_quantum() -> Option<usize> {
    struct GetQuantum;

    impl Future for GetQuantum {
        type Output = Option<usize>;
        fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            let result = if let ExecuteWeight::TimeSharing(quantum) = task.get_weight() {
                Some(quantum)
            } else {
                None
            };
            Poll::Ready(result)
        }
    }

    GetQuantum.await
}

/// Make a task into a real-time one with specific priority.
pub async fn set_priority(priority: usize) {
    struct SetPriority(ExecuteWeight);

    impl Future for SetPriority {
        type Output = ();
        fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            task.set_weight(self.0);
            Poll::Ready(())
        }
    }

    SetPriority(ExecuteWeight::from_priority(priority)).await
}

/// Retrieve the priority of a real-time task.
///
/// Return [None] if this is a time-sharing task.
pub async fn get_priority() -> Option<usize> {
    struct GetPriority;

    impl Future for GetPriority {
        type Output = Option<usize>;
        fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            let result = if let ExecuteWeight::RealTime(priority) = task.get_weight() {
                Some(priority)
            } else {
                None
            };
            Poll::Ready(result)
        }
    }

    GetPriority.await
}

/// Inject a preemptable point for fair scheduling and real-time scheduling.
///
/// If you have NCPU local executors and preemptable points are carefully injected into your code
/// so that the time consumed between any two sequential preemptable points is O(1), then for each
/// executor, the real-time tasks with highest priority is guaranteed to be waken up in O(NCPU).
pub async fn preempt_point() {
    struct PreemptPoint;

    impl Future for PreemptPoint {
        type Output = ();
        fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            let task = Task::from_ctx(ctx);
            let executor = task.get_local_executor();
            if executor.try_preempt(task).is_ok() {
                Poll::Pending
            } else {
                Poll::Ready(())
            }
        }
    }

    PreemptPoint.await
}

pub async fn sleep(_key: usize) {
    todo!()
}