| 1 | /* @title: Thread Enumerations and Types */ |
| 2 | #pragma once |
| 3 | #include <mem/page.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | struct thread; |
| 8 | struct cpu_context; |
| 9 | |
| 10 | #define THREAD_STACK_SIZE (PAGE_SIZE * 4) |
| 11 | |
| 12 | enum thread_state : uint8_t { |
| 13 | THREAD_STATE_IDLE_THREAD, /* Specifically the idle thread */ |
| 14 | THREAD_STATE_READY, /* Thread is ready to run but not currently running */ |
| 15 | THREAD_STATE_RUNNING, /* Thread is currently executing */ |
| 16 | THREAD_STATE_BLOCKED, /* Waiting on I/O, lock, or condition */ |
| 17 | THREAD_STATE_SLEEPING, /* Temporarily not runnable */ |
| 18 | THREAD_STATE_ZOMBIE, /* Finished executing but hasn't been reaped it yet */ |
| 19 | THREAD_STATE_TERMINATED, /* Fully done, can be cleaned up */ |
| 20 | THREAD_STATE_HALTED, /* Thread manually suspended */ |
| 21 | }; |
| 22 | |
| 23 | enum thread_wait_type : uint8_t { |
| 24 | THREAD_WAIT_NONE, |
| 25 | THREAD_WAIT_UNINTERRUPTIBLE, /* Cannot be interrupted by anything |
| 26 | besides the wake source */ |
| 27 | |
| 28 | THREAD_WAIT_INTERRUPTIBLE, /* Can be interrupted */ |
| 29 | }; |
| 30 | |
| 31 | /* thread_flags: 32 bit bitflags: |
| 32 | * |
| 33 | * ┌───────────────────────────────────────────────────────────┐ |
| 34 | * Bits │ 31..28 27..24 23..20 19..16 15..12 11..8 7..4 3..0 │ |
| 35 | * Use │ AAAA **** **** **** **** **** *RWY DEFP │ |
| 36 | * └───────────────────────────────────────────────────────────┘ |
| 37 | * P - Pinned - Thread is pinned to current CPU |
| 38 | * F - Flexible RT - realtime scheduler related stuff |
| 39 | * E - Executing APC |
| 40 | * D - Dying |
| 41 | * Y - Yielded after a wait (block, sleep) |
| 42 | * W - Wake matched |
| 43 | * r - Realtime fault tolerance |
| 44 | * A - Unused (Available) |
| 45 | * * - Unused (Unavailable) |
| 46 | * |
| 47 | */ |
| 48 | enum thread_flags : uint32_t { |
| 49 | THREAD_FLAG_PINNED = 1, |
| 50 | THREAD_FLAG_FLEXIBLE_RT = 1 << 1, |
| 51 | THREAD_FLAG_EXECUTING_APC = 1 << 2, |
| 52 | THREAD_FLAG_DYING = 1 << 3, |
| 53 | THREAD_FLAG_YIELDED = 1 << 4, |
| 54 | THREAD_FLAG_WAKE_MATCHED = 1 << 5, |
| 55 | THREAD_FLAG_RT_FAULT_TOLERANCE = 1 << 6, |
| 56 | }; |
| 57 | |
| 58 | enum thread_prio_class : uint8_t { |
| 59 | THREAD_PRIO_CLASS_BACKGROUND = 0, /* Background thread */ |
| 60 | THREAD_PRIO_CLASS_TIMESHARE = 1, /* Timesharing thread */ |
| 61 | THREAD_PRIO_CLASS_RT = 2, /* Realtime thread */ |
| 62 | THREAD_PRIO_CLASS_URGENT = 3, /* Urgent thread - ran before RT */ |
| 63 | }; |
| 64 | #define THREAD_PRIO_CLASS_COUNT (4) |
| 65 | |
| 66 | /* Different enums are used for the little |
| 67 | * bit of type safety since different ringbuffers |
| 68 | * are used to keep track of different reasons */ |
| 69 | enum thread_wake_reason : uint8_t { |
| 70 | THREAD_WAKE_REASON_BLOCKING_IO = 1, |
| 71 | THREAD_WAKE_REASON_BLOCKING_MANUAL = 2, |
| 72 | THREAD_WAKE_REASON_SLEEP_TIMEOUT = 3, |
| 73 | THREAD_WAKE_REASON_SLEEP_MANUAL = 4, |
| 74 | }; |
| 75 | |
| 76 | enum thread_block_reason : uint8_t { |
| 77 | THREAD_BLOCK_REASON_IO = 5, |
| 78 | THREAD_BLOCK_REASON_MANUAL = 6, |
| 79 | }; |
| 80 | |
| 81 | enum thread_sleep_reason : uint8_t { |
| 82 | THREAD_SLEEP_REASON_MANUAL = 7, |
| 83 | }; |
| 84 | |
| 85 | /* Used in condvars, totally separate from thread_wake_reason */ |
| 86 | enum wake_reason { |
| 87 | WAKE_REASON_NONE = 0, /* No reason specified */ |
| 88 | WAKE_REASON_SIGNAL = 1, /* Signal from something */ |
| 89 | WAKE_REASON_TIMEOUT = 2, /* Timeout */ |
| 90 | }; |
| 91 | |
| 92 | /* headers that #include this header should be allowed to execute this function |
| 93 | */ |
| 94 | struct thread *thread_create_internal(char *name, void (*entry_point)(void *), |
| 95 | void *arg, size_t stack_size, |
| 96 | va_list args); |
| 97 | |