From 572e1c2dd2496ce939465b2363d0a7144f2396fa Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 27 Oct 2020 19:17:45 +0100 Subject: [PATCH] made the topic list, the base class of the scheduler --- Makefile | 2 +- include/gp/vfs/runqueue.hpp | 82 +++++++++++++++++++++++++++++++++++++ include/gp/vfs/system.hpp | 7 ++-- tests/cbor_test.cpp | 4 +- 4 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 include/gp/vfs/runqueue.hpp diff --git a/Makefile b/Makefile index f1e75f8..b2a410a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CXX= clang++-10 -CXXFLAGS= --std=c++20 -O0 -g -pthread -DGP_TESTS -DFUZZ_STRENGTH=500 -DNO_BENCH=1 -pedantic \ +CXXFLAGS= --std=c++20 -O2 -g -pthread -DGP_TESTS -DFUZZ_STRENGTH=100000 -DNO_BENCH=0 -pedantic \ -fprofile-instr-generate -fcoverage-mapping -Wno-unknown-attributes -fno-omit-frame-pointer \ # -fsanitize=address -fsanitize-blacklist=blacklist.txt diff --git a/include/gp/vfs/runqueue.hpp b/include/gp/vfs/runqueue.hpp new file mode 100644 index 0000000..5dbabf8 --- /dev/null +++ b/include/gp/vfs/runqueue.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include "gp/vfs/process_data.hpp" + +#include + +class runqueue{ + + struct node{ + std::atomic_bool is_locked; + gp::process_data* value; + std::atomic next; + + bool try_acquire() noexcept { + bool expected = false; + return !(is_locked.compare_exchange_strong(expected, true)); + } + + void release() noexcept { + is_locked.store(false); + } + }; + + using node_ptr = struct node*; + using node_ptr_rep = std::atomic; + + struct topic_list{ + node_ptr_rep start; + node_ptr_rep end; + + // NODES ARE ACQUIRED ON POP + node_ptr try_pop() { + auto ptr = start.load(); + if(!ptr) return nullptr; + + if(ptr->try_acquire()) { + auto replace = ptr->next.load(); + auto expected = ptr; + if(end.load() == ptr) { + end.store(nullptr); + } + if(start.compare_exchange_strong(expected, replace)) { + return ptr; + } else { + return nullptr; + } + } else { + return nullptr; + } + } + + // ONLY PUSH ACQUIRED NODES + bool try_push(node_ptr node) { + auto ed = end.load(); + if(ed) { + if(ed->try_acquire()) { + node->next.store(ed); + if(end.compare_exchange_strong(ed, node)) { + ed->release(); + node->release(); + return true; + } else { + ed->release(); + node->release(); + return false; + } + } else return false; + } else { + if(end.compare_exchange_strong(ed, node)) { + start.store(node); + node->release(); + return true; + } else { + return false; + } + } + } + }; + + topic_list running; + topic_list waiting; +}; \ No newline at end of file diff --git a/include/gp/vfs/system.hpp b/include/gp/vfs/system.hpp index 8baa0ea..bf85cd1 100644 --- a/include/gp/vfs/system.hpp +++ b/include/gp/vfs/system.hpp @@ -48,9 +48,8 @@ public: scheduler::scheduler(class system& v) : sys(v){} -no_inline_decl( - void scheduler::yield_to(size_t target_pid) -) { +void scheduler::yield_to(size_t target_pid) +{ auto& cur = current ? sys.processes[current-1]->specifics : root; auto& target = target_pid ? sys.processes[target_pid-1]->specifics : root; current = target_pid; @@ -67,7 +66,7 @@ no_inline_decl( } }(static_cast(target.push(this))); } else { - volatile scheduler* new_p = static_cast(target.push(this)); + [[maybe_unused]] volatile scheduler* new_p = static_cast(target.push(this)); } } diff --git a/tests/cbor_test.cpp b/tests/cbor_test.cpp index 899fe3e..e3ed2c4 100644 --- a/tests/cbor_test.cpp +++ b/tests/cbor_test.cpp @@ -487,8 +487,8 @@ struct cbor_test : public test_scaffold { data = meta; - gp::array serialized; - gp::array serialized_manual{ + gp::array serialized; + gp::array serialized_manual{ std::byte(0b10100010), std::byte(0b01000101), std::byte('h'),