adapt to vector
This commit is contained in:
@ -11,7 +11,7 @@ void Semaphore::p() {
|
|||||||
this->lock.release();
|
this->lock.release();
|
||||||
} else {
|
} else {
|
||||||
// Block and manage thread in semaphore queue until it's woken up by v() again
|
// 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();
|
this->lock.release();
|
||||||
scheduler.block(); // Moves to next thread
|
scheduler.block(); // Moves to next thread
|
||||||
}
|
}
|
||||||
@ -22,13 +22,12 @@ void Semaphore::v() {
|
|||||||
|
|
||||||
if (!this->waitQueue.empty()) {
|
if (!this->waitQueue.empty()) {
|
||||||
// Semaphore stays busy and unblocks next thread to work in critical section
|
// Semaphore stays busy and unblocks next thread to work in critical section
|
||||||
Thread* next = this->waitQueue.remove_first().value_or(nullptr);
|
if (this->waitQueue.empty()) {
|
||||||
if (next == nullptr) {
|
|
||||||
// TODO: Log this once the logger is static
|
|
||||||
this->lock.release();
|
this->lock.release();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Thread* next = this->waitQueue[0];
|
||||||
|
this->waitQueue.erase(waitQueue.begin());
|
||||||
this->lock.release();
|
this->lock.release();
|
||||||
scheduler.deblock(next);
|
scheduler.deblock(next);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -13,15 +13,14 @@
|
|||||||
|
|
||||||
#include "kernel/threads/Thread.h"
|
#include "kernel/threads/Thread.h"
|
||||||
#include "lib/SpinLock.h"
|
#include "lib/SpinLock.h"
|
||||||
#include "user/lib/ArrayList.h"
|
#include "user/lib/Vector.h"
|
||||||
|
|
||||||
class Semaphore {
|
class Semaphore {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
|
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
|
||||||
|
|
||||||
// Queue fuer wartende Threads.
|
// Queue fuer wartende Threads.
|
||||||
ArrayList<Thread*> waitQueue;
|
bse::Vector<Thread*> waitQueue;
|
||||||
SpinLock lock;
|
SpinLock lock;
|
||||||
|
|
||||||
int counter;
|
int counter;
|
||||||
|
|||||||
@ -1,15 +1,14 @@
|
|||||||
#include "user/MainMenu.h"
|
#include "user/MainMenu.h"
|
||||||
#include "user/demo/ArrayDemo.h"
|
#include "user/demo/ArrayDemo.h"
|
||||||
#include "user/demo/ArrayListDemo.h"
|
|
||||||
#include "user/demo/BlueScreenDemo.h"
|
#include "user/demo/BlueScreenDemo.h"
|
||||||
#include "user/demo/HeapDemo.h"
|
#include "user/demo/HeapDemo.h"
|
||||||
#include "user/demo/KeyboardDemo.h"
|
#include "user/demo/KeyboardDemo.h"
|
||||||
#include "user/demo/LinkedListDemo.h"
|
|
||||||
#include "user/demo/PCSPKdemo.h"
|
#include "user/demo/PCSPKdemo.h"
|
||||||
#include "user/demo/PreemptiveThreadDemo.h"
|
#include "user/demo/PreemptiveThreadDemo.h"
|
||||||
#include "user/demo/SmartPointerDemo.h"
|
#include "user/demo/SmartPointerDemo.h"
|
||||||
#include "user/demo/TextDemo.h"
|
#include "user/demo/TextDemo.h"
|
||||||
#include "user/demo/VBEdemo.h"
|
#include "user/demo/VBEdemo.h"
|
||||||
|
#include "user/demo/VectorDemo.h"
|
||||||
|
|
||||||
void print_demo_menu() {
|
void print_demo_menu() {
|
||||||
kout.lock();
|
kout.lock();
|
||||||
@ -59,17 +58,12 @@ void MainMenu::run() {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'q':
|
case 'q':
|
||||||
choosen_demo = new ArrayListDemo();
|
choosen_demo = new VectorDemo();
|
||||||
break;
|
break;
|
||||||
case 'w':
|
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();
|
choosen_demo = new ArrayDemo();
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'e':
|
||||||
choosen_demo = new SmartPointerDemo();
|
choosen_demo = new SmartPointerDemo();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +1,12 @@
|
|||||||
#include "user/demo/SmartPointerDemo.h"
|
#include "user/demo/SmartPointerDemo.h"
|
||||||
|
#include "kernel/threads/IdleThread.h"
|
||||||
|
#include "user/lib/Array.h"
|
||||||
#include "user/lib/mem/UniquePointer.h"
|
#include "user/lib/mem/UniquePointer.h"
|
||||||
// #include <memory>
|
|
||||||
|
|
||||||
void SmartPointerDemo::run() {
|
void SmartPointerDemo::run() {
|
||||||
kout.clear();
|
kout.clear();
|
||||||
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
|
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;
|
log << INFO << "Allocating new unique_ptr<int>..." << endl;
|
||||||
bse::unique_ptr<int> ptr = bse::make_unique<int>(1);
|
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;
|
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();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
#define __KeyEventManager_Include_H_
|
#define __KeyEventManager_Include_H_
|
||||||
|
|
||||||
#include "user/event/KeyEventListener.h"
|
#include "user/event/KeyEventListener.h"
|
||||||
#include "user/lib/ArrayList.h"
|
|
||||||
#include "user/lib/Logger.h"
|
#include "user/lib/Logger.h"
|
||||||
|
#include "user/lib/Vector.h"
|
||||||
|
|
||||||
// NOTE: Could do this more generally but we only have key events
|
// NOTE: Could do this more generally but we only have key events
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ private:
|
|||||||
|
|
||||||
Logger log;
|
Logger log;
|
||||||
|
|
||||||
ArrayList<KeyEventListener*> listeners;
|
bse::Vector<KeyEventListener*> listeners;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
KeyEventManager() : log("KEvMan") {}
|
KeyEventManager() : log("KEvMan") {}
|
||||||
|
|||||||
@ -5,20 +5,19 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
template<typename T, const std::size_t N, typename I = Iterator<T>>
|
namespace bse {
|
||||||
|
|
||||||
|
template<typename T, const std::size_t N>
|
||||||
class Array {
|
class Array {
|
||||||
public:
|
public:
|
||||||
using Type = T;
|
using Iterator = ContinuousIterator<T>;
|
||||||
using Iterator = I;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type buf[N];
|
T buf[N];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Iterator begin() { return Iterator(&buf[0]); }
|
Iterator begin() { return Iterator(&buf[0]); }
|
||||||
Iterator end() { return Iterator(&buf[N]); }
|
Iterator end() { return Iterator(&buf[N]); }
|
||||||
constexpr Iterator begin() const { return this->begin(); }
|
|
||||||
constexpr Iterator end() const { return this->end(); }
|
|
||||||
|
|
||||||
T& operator[](std::size_t i) {
|
T& operator[](std::size_t i) {
|
||||||
return this->buf[i];
|
return this->buf[i];
|
||||||
@ -28,7 +27,7 @@ public:
|
|||||||
return this->buf[i];
|
return this->buf[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(Array<Type, N>& other) {
|
void swap(Array<T, N>& other) {
|
||||||
for (std::size_t i = 0; i < N; ++i) {
|
for (std::size_t i = 0; i < N; ++i) {
|
||||||
std::swap(this->buf[i], other[i]);
|
std::swap(this->buf[i], other[i]);
|
||||||
}
|
}
|
||||||
@ -37,7 +36,7 @@ public:
|
|||||||
// Array& other has to have size n:
|
// Array& other has to have size n:
|
||||||
// arr1.swap_n<5>(arr2) => arr2 has size 5, arr1 has size >= 5
|
// arr1.swap_n<5>(arr2) => arr2 has size 5, arr1 has size >= 5
|
||||||
template<std::size_t n>
|
template<std::size_t n>
|
||||||
void swap_n(Array<Type, n>& other) {
|
void swap_n(Array<T, n>& other) {
|
||||||
for (std::size_t i = 0; i < n; ++i) {
|
for (std::size_t i = 0; i < n; ++i) {
|
||||||
std::swap(this->buf[i], other[i]);
|
std::swap(this->buf[i], other[i]);
|
||||||
}
|
}
|
||||||
@ -48,4 +47,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace bse
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user