From a490cf9fece4c4b900d219740123ec8d81c6abb2 Mon Sep 17 00:00:00 2001 From: aqua Date: Sun, 25 Dec 2022 17:53:05 +0200 Subject: Add sched/roundrobin tests --- src/task.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/task.h (limited to 'src/task.h') 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 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 -- cgit v1.2.1