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
//! Task life cycle: run the body, then tear the task down automatically.
//!
//! Every backend points a new task's initial program counter at
//! [`task_trampoline`], so **the developer never writes scheduling code**: no
//! `yield`, no `exit`, no `scheduler_run()`. Returning from the task body is
//! the exit.
use cratecritical;
use cratering;
use cratescheduler;
use crate;
/// Backend-agnostic task entry point.
///
/// The port arranges for the task's **first** instruction to be here, with the
/// TCB pointer passed in the platform's first argument register (Cortex-M: R0
/// of the initial exception frame; Win32: the thread parameter; POSIX fibers:
/// a callee-saved register restored by the fiber switch).
///
/// # Safety
/// Only the port may call this, exactly once per spawned task, at the bottom
/// of that task's own stack.
pub unsafe extern "C" !
/// Automatic task teardown — the kernel half of "just return from your
/// function".
///
/// Steps, in this exact order (all but the switch inside one critical
/// section, so no tick can observe an inconsistent ring):
///
/// 1. `state = Dead`
/// 2. O(1) unlink: `(*prev).next = next`, `(*next).prev = prev`
/// 3. `active_threads -= 1`, and publish the successor as the ring head
/// 4. queue this TCB for deferred-free (its stack cannot be unmapped while we
/// are still standing on it — [`scheduler::drain_pending_free`] does that
/// from kernel context on the next switch)
/// 5. request an immediate context switch **and restart the slice counter**,
/// so the successor gets a full slice instead of the remainder of this one
///
/// # Safety
/// Callable only from the task's own context, after its body has returned.
pub unsafe !