Xyris  0.5
tasks.hpp
Go to the documentation of this file.
1 /**
2  * @file tasks.hpp
3  * @author Micah Switzer ([email protected])
4  * @brief
5  * @version 0.3
6  * @date 2020-08-29
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2020
9  *
10  */
11 #pragma once
12 
13 #include <stdint.h>
14 #include <Arch/Arch.hpp>
15 #include <Memory/paging.hpp>
16 
17 #define TIME_SLICE_SIZE (1 * 1000 * 1000ULL)
18 
20 {
28 };
29 
31 
32 struct task
33 {
34  uintptr_t stack_top;
35  uintptr_t page_dir;
36  struct task *next;
38  uint64_t time_used;
39  uint64_t wakeup_time;
40  const char *name;
42 };
43 
44 extern struct task *current_task;
45 
46 #define TASK_ONLY if (current_task != NULL)
47 
48 struct tasklist
49 {
50  struct task *head;
51  struct task *tail;
52 };
53 
54 #define MAX_TASKS_QUEUED 8
55 struct task_sync
56 {
57  struct task* possessor;
58  const char *dbg_name;
59  struct tasklist waiting;
60 };
61 
62 static inline void tasks_sync_init(struct task_sync *ts) {
63  *ts = {
64  .possessor = NULL,
65  .dbg_name = NULL,
66  .waiting = { },
67  };
68 }
69 
70 /**
71  * @brief Initializes the kernel task manager.
72  *
73  */
74 void tasks_init();
75 /**
76  * @brief Switches to a provided task.
77  *
78  * @param task Pointer to the task struct
79  */
80 extern "C" void tasks_switch_to(struct task *task);
81 /**
82  * @brief Creates a new kernel task with a provided entry point, register storage struct,
83  * and task state struct. If the storage parameter is provided, the struct task struct
84  * provided will be written to. If NULL is passed as the storage parameter, a pointer
85  * to a allocated task will be returned.
86  *
87  * @param entry Task function entry point
88  * @param storage Task stack structure (if NULL, a pointer to the task is returned)
89  * @param state Task state structure
90  * @param name Task name (for debugging / printing)
91  * @return struct task* Pointer to the created kernel task
92  */
93 struct task *tasks_new(void (*entry)(void), struct task *storage, task_state state, const char *name);
94 /**
95  * @brief Tell the kernel task scheduler to schedule all of the added tasks.
96  *
97  */
98 void tasks_schedule();
99 /**
100  * @brief Returns the lifetime of the current task (in nanoseconds).
101  *
102  * @return uint64_t Task lifetime (in nanoseconds)
103  */
104 uint64_t tasks_get_self_time();
105 /**
106  * @brief Blocks the current task.
107  *
108  * @param reason
109  */
110 void tasks_block_current(task_state reason);
111 /**
112  * @brief Unblocks the current task.
113  *
114  * @param task
115  */
116 void tasks_unblock(struct task *task);
117 /**
118  * @brief Sleeps until the provided absolute time (in nanoseconds).
119  *
120  * @param time Absolute time to sleep until (in nanoseconds since boot)
121  */
122 void tasks_nano_sleep_until(uint64_t time);
123 /**
124  * @brief Sleep for a given period of time (in nanoseconds).
125  *
126  * @param time Nanoseconds to sleep
127  */
128 void tasks_nano_sleep(uint64_t time);
129 /**
130  * @brief Exits the current task.
131  *
132  */
133 void tasks_exit(void);
134 
135 void tasks_sync_block(struct task_sync *tsc);
136 
137 void tasks_sync_unblock(struct task_sync *tsc);
tasks_new
struct task * tasks_new(void(*entry)(void), struct task *storage, task_state state, const char *name)
Creates a new kernel task with a provided entry point, register storage struct, and task state struct...
Definition: tasks.cpp:289
ALLOC_STATIC
@ ALLOC_STATIC
Definition: tasks.hpp:30
task
Definition: tasks.hpp:32
tasklist
Definition: tasks.hpp:48
tasks_unblock
void tasks_unblock(struct task *task)
Unblocks the current task.
Definition: tasks.cpp:431
task_alloc
task_alloc
Definition: tasks.hpp:30
TASK_READY
@ TASK_READY
Definition: tasks.hpp:22
current_task
struct task * current_task
Definition: tasks.cpp:40
tasks_nano_sleep
void tasks_nano_sleep(uint64_t time)
Sleep for a given period of time (in nanoseconds).
Definition: tasks.cpp:506
TASK_SLEEPING
@ TASK_SLEEPING
Definition: tasks.hpp:23
task::time_used
uint64_t time_used
Definition: tasks.hpp:38
task::alloc
task_alloc alloc
Definition: tasks.hpp:41
ALLOC_DYNAMIC
@ ALLOC_DYNAMIC
Definition: tasks.hpp:30
TASK_STATE_COUNT
@ TASK_STATE_COUNT
Definition: tasks.hpp:27
task_sync::possessor
struct task * possessor
Definition: tasks.hpp:57
task_sync::dbg_name
const char * dbg_name
Definition: tasks.hpp:58
task_sync
Definition: tasks.hpp:55
TASK_RUNNING
@ TASK_RUNNING
Definition: tasks.hpp:21
TASK_STOPPED
@ TASK_STOPPED
Definition: tasks.hpp:25
task::stack_top
uintptr_t stack_top
Definition: tasks.hpp:34
task_sync::waiting
struct tasklist waiting
Definition: tasks.hpp:59
Arch.hpp
Architecture control and initialization.
tasks_init
void tasks_init()
Initializes the kernel task manager.
Definition: tasks.cpp:151
tasks_nano_sleep_until
void tasks_nano_sleep_until(uint64_t time)
Sleeps until the provided absolute time (in nanoseconds).
Definition: tasks.cpp:494
TASK_PAUSED
@ TASK_PAUSED
Definition: tasks.hpp:26
tasks_sync_unblock
void tasks_sync_unblock(struct task_sync *tsc)
Definition: tasks.cpp:575
tasks_get_self_time
uint64_t tasks_get_self_time()
Returns the lifetime of the current task (in nanoseconds).
Definition: tasks.cpp:416
tasklist::head
struct task * head
Definition: tasks.hpp:50
tasks_block_current
void tasks_block_current(task_state reason)
Blocks the current task.
Definition: tasks.cpp:422
task_state
task_state
Definition: tasks.hpp:19
task::state
task_state state
Definition: tasks.hpp:37
tasks_schedule
void tasks_schedule()
Tell the kernel task scheduler to schedule all of the added tasks.
Definition: tasks.cpp:406
paging.hpp
task::wakeup_time
uint64_t wakeup_time
Definition: tasks.hpp:39
task::name
const char * name
Definition: tasks.hpp:40
tasks_exit
void tasks_exit(void)
Exits the current task.
Definition: tasks.cpp:511
task::page_dir
uintptr_t page_dir
Definition: tasks.hpp:35
tasks_switch_to
void tasks_switch_to(struct task *task)
Switches to a provided task.
task::next
struct task * next
Definition: tasks.hpp:36
TASK_BLOCKED
@ TASK_BLOCKED
Definition: tasks.hpp:24
tasks_sync_block
void tasks_sync_block(struct task_sync *tsc)
Definition: tasks.cpp:560
tasklist::tail
struct task * tail
Definition: tasks.hpp:51