aboutsummaryrefslogtreecommitdiff
path: root/src/task.h
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-12-25 17:53:05 +0200
committeraqua <aqua@iserlohn-fortress.net>2022-12-25 17:53:05 +0200
commita490cf9fece4c4b900d219740123ec8d81c6abb2 (patch)
tree3e4485d75519b79b0e770cacf5ba8b66acce8231 /src/task.h
parentGenerate docs using doxygen instead of sphinx (diff)
downloadkernel-a490cf9fece4c4b900d219740123ec8d81c6abb2.tar.xz
Add sched/roundrobin tests
Diffstat (limited to 'src/task.h')
-rw-r--r--src/task.h78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/task.h b/src/task.h
new file mode 100644
index 0000000..381f628
--- /dev/null
+++ b/src/task.h
@@ -0,0 +1,78 @@
+#pragma once
+
+/**
+ * Representation of a task in the system
+ */
+struct Task {
+ const char *name;
+ int id;
+ int priority;
+ int burst;
+};
+
+#ifdef __cplusplus
+template <typename T> struct Queue {
+ struct Item {
+ Item(T *node) : node(node) {}
+ T *node;
+ Item *next = nullptr;
+
+ [[nodiscard]] bool
+ operator==(const T *other) const
+ {
+ return node == other;
+ }
+ };
+
+ ~Queue() noexcept
+ {
+ for (auto *it = head; it != nullptr;) {
+ auto *current = it;
+ it = it->next;
+ delete current->node;
+ delete current;
+ }
+ }
+
+ /// Insert item at the end of the queue
+ void
+ insert(T *item)
+ {
+ if (head == nullptr) {
+ head = new Item(item);
+ tail = head;
+ }
+ else {
+ tail->next = new Item(item);
+ tail = tail->next;
+ }
+ }
+
+ void
+ remove(T *item)
+ {
+ if (head == nullptr) return;
+ if (item == head->node) {
+ auto *it = head;
+ head = head->next;
+ if (*tail == item) tail = nullptr;
+ delete it;
+ return;
+ }
+
+ Item *prev = nullptr;
+ for (auto *it = head; it != nullptr; it = it->next) {
+ if (it->node == item) {
+ if (prev) { prev->next = it->next; }
+ if (tail == it) { tail = prev; }
+ delete it;
+ return;
+ }
+ prev = it;
+ }
+ }
+
+ Item *head = nullptr;
+ Item *tail = nullptr;
+};
+#endif