1

adapt to vector

This commit is contained in:
2022-07-21 02:44:01 +02:00
parent fa7d9d4d19
commit ae7294601d
6 changed files with 91 additions and 66 deletions

View File

@ -11,7 +11,7 @@ void Semaphore::p() {
this->lock.release();
} else {
// Block and manage thread in semaphore queue until it's woken up by v() again
this->waitQueue.insert_last(scheduler.get_active());
this->waitQueue.push_back(scheduler.get_active());
this->lock.release();
scheduler.block(); // Moves to next thread
}
@ -22,13 +22,12 @@ void Semaphore::v() {
if (!this->waitQueue.empty()) {
// Semaphore stays busy and unblocks next thread to work in critical section
Thread* next = this->waitQueue.remove_first().value_or(nullptr);
if (next == nullptr) {
// TODO: Log this once the logger is static
if (this->waitQueue.empty()) {
this->lock.release();
return;
}
Thread* next = this->waitQueue[0];
this->waitQueue.erase(waitQueue.begin());
this->lock.release();
scheduler.deblock(next);
} else {

View File

@ -13,15 +13,14 @@
#include "kernel/threads/Thread.h"
#include "lib/SpinLock.h"
#include "user/lib/ArrayList.h"
#include "user/lib/Vector.h"
class Semaphore {
private:
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
// Queue fuer wartende Threads.
ArrayList<Thread*> waitQueue;
bse::Vector<Thread*> waitQueue;
SpinLock lock;
int counter;

View File

@ -1,15 +1,14 @@
#include "user/MainMenu.h"
#include "user/demo/ArrayDemo.h"
#include "user/demo/ArrayListDemo.h"
#include "user/demo/BlueScreenDemo.h"
#include "user/demo/HeapDemo.h"
#include "user/demo/KeyboardDemo.h"
#include "user/demo/LinkedListDemo.h"
#include "user/demo/PCSPKdemo.h"
#include "user/demo/PreemptiveThreadDemo.h"
#include "user/demo/SmartPointerDemo.h"
#include "user/demo/TextDemo.h"
#include "user/demo/VBEdemo.h"
#include "user/demo/VectorDemo.h"
void print_demo_menu() {
kout.lock();
@ -59,17 +58,12 @@ void MainMenu::run() {
break;
case 'q':
choosen_demo = new ArrayListDemo();
choosen_demo = new VectorDemo();
break;
case 'w':
// NOTE: The LinkedListDemo will take considerably longer for the threadswitching part
// because for every insertion/deletion memory will be allocated for the wrappers
choosen_demo = new LinkedListDemo();
break;
case 'e':
choosen_demo = new ArrayDemo();
break;
case 'r':
case 'e':
choosen_demo = new SmartPointerDemo();
break;
}

View File

@ -1,23 +1,12 @@
#include "user/demo/SmartPointerDemo.h"
#include "kernel/threads/IdleThread.h"
#include "user/lib/Array.h"
#include "user/lib/mem/UniquePointer.h"
// #include <memory>
void SmartPointerDemo::run() {
kout.clear();
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
// <memory> legit smartpointer
// {
// log << INFO << "Allocating new unique_ptr<int>..." << endl;
// std::unique_ptr<int> ptr1 = std::make_unique<int>(1);
// std::unique_ptr<int> ptr2 = std::make_unique<int>(2);
// log << INFO << "Moving..." << endl;
// ptr2 = std::move(ptr1);
// log << INFO << "Leaving scope..." << endl;
// }
// log << INFO << "Should be deleted by now..." << endl;
{
log << INFO << "Allocating new unique_ptr<int>..." << endl;
bse::unique_ptr<int> ptr = bse::make_unique<int>(1);
@ -85,5 +74,48 @@ void SmartPointerDemo::run() {
}
log << INFO << "Should be deleted by now..." << endl;
// NOTE: This wasn't working because of a missing operator[] delete in the allocator
log << INFO << "Allocating unique_ptr<int>*..." << endl;
bse::unique_ptr<int>* ptrptr = new bse::unique_ptr<int>[10];
delete[] ptrptr;
log << INFO << "Should be deleted by now..." << endl;
// =====================================================================
{
log << INFO << "Stackallocating Array<bse::unique_ptr<int>, 10>..." << endl;
bse::Array<bse::unique_ptr<int>, 10> arr;
log << INFO << "Populating slot 0..." << endl;
arr[0] = bse::make_unique<int>(1);
log << INFO << "Moving slot 0 to slot 1..." << endl;
arr[1] = std::move(arr[0]);
log << INFO << "Leaving scope" << endl;
}
log << INFO << "Should be deleted by now..." << endl;
{
log << INFO << "Heapallocating Array<bse::unique_ptr<int>, 10>..." << endl;
bse::Array<bse::unique_ptr<int>, 10>* arr = new bse::Array<bse::unique_ptr<int>, 10>;
log << INFO << "Populating slot 0..." << endl;
(*arr)[0] = bse::make_unique<int>(1);
log << INFO << "Moving slot 0 to slot 1..." << endl;
(*arr)[1] = std::move((*arr)[0]);
log << INFO << "Deleting" << endl;
delete arr;
log << INFO << "Leaving scope" << endl;
}
log << INFO << "Should be deleted by now..." << endl;
{
log << INFO << "ArrayList<bse::unique_ptr<int>>..." << endl;
bse::Vector<bse::unique_ptr<int>> vec;
log << INFO << "2x insertion" << endl;
vec.push_back(bse::make_unique<int>(1));
vec.push_back(bse::make_unique<int>(2));
log << INFO << "Leaving scope" << endl;
}
log << INFO << "Should be deleted by now..." << endl;
scheduler.exit();
}

View File

@ -2,8 +2,8 @@
#define __KeyEventManager_Include_H_
#include "user/event/KeyEventListener.h"
#include "user/lib/ArrayList.h"
#include "user/lib/Logger.h"
#include "user/lib/Vector.h"
// NOTE: Could do this more generally but we only have key events
@ -13,7 +13,7 @@ private:
Logger log;
ArrayList<KeyEventListener*> listeners;
bse::Vector<KeyEventListener*> listeners;
public:
KeyEventManager() : log("KEvMan") {}

View File

@ -5,47 +5,48 @@
#include <cstddef>
#include <utility>
template<typename T, const std::size_t N, typename I = Iterator<T>>
class Array {
public:
using Type = T;
using Iterator = I;
namespace bse {
private:
Type buf[N];
template<typename T, const std::size_t N>
class Array {
public:
using Iterator = ContinuousIterator<T>;
public:
Iterator begin() { return Iterator(&buf[0]); }
Iterator end() { return Iterator(&buf[N]); }
constexpr Iterator begin() const { return this->begin(); }
constexpr Iterator end() const { return this->end(); }
private:
T buf[N];
T& operator[](std::size_t i) {
return this->buf[i];
}
public:
Iterator begin() { return Iterator(&buf[0]); }
Iterator end() { return Iterator(&buf[N]); }
constexpr const T& operator[](std::size_t i) const {
return this->buf[i];
}
void swap(Array<Type, N>& other) {
for (std::size_t i = 0; i < N; ++i) {
std::swap(this->buf[i], other[i]);
T& operator[](std::size_t i) {
return this->buf[i];
}
}
// Array& other has to have size n:
// arr1.swap_n<5>(arr2) => arr2 has size 5, arr1 has size >= 5
template<std::size_t n>
void swap_n(Array<Type, n>& other) {
for (std::size_t i = 0; i < n; ++i) {
std::swap(this->buf[i], other[i]);
constexpr const T& operator[](std::size_t i) const {
return this->buf[i];
}
}
constexpr std::size_t size() const {
return N;
}
};
void swap(Array<T, N>& other) {
for (std::size_t i = 0; i < N; ++i) {
std::swap(this->buf[i], other[i]);
}
}
// Array& other has to have size n:
// arr1.swap_n<5>(arr2) => arr2 has size 5, arr1 has size >= 5
template<std::size_t n>
void swap_n(Array<T, n>& other) {
for (std::size_t i = 0; i < n; ++i) {
std::swap(this->buf[i], other[i]);
}
}
constexpr std::size_t size() const {
return N;
}
};
} // namespace bse
#endif