1

merged cleanup

This commit is contained in:
2022-07-24 21:12:31 +02:00
parent 5ff3d72bfd
commit 6481bae5f6
92 changed files with 663 additions and 755 deletions

View File

@ -3,39 +3,39 @@
void Semaphore::p() {
// Lock to allow deterministic operations on counter/queue
this->lock.acquire();
lock.acquire();
if (this->counter > 0) {
if (counter > 0) {
// Semaphore can be acquired
this->counter = this->counter - 1;
this->lock.release();
counter = counter - 1;
lock.release();
} else {
// Block and manage thread in semaphore queue until it's woken up by v() again
if (!wait_queue.initialized()) { // TODO: I will replace this suboptimal datastructure in the future
wait_queue.reserve();
}
this->wait_queue.push_back(scheduler.get_active());
wait_queue.push_back(scheduler.get_active());
CPU::disable_int(); // Make sure the block() comes through after releasing the lock
this->lock.release();
lock.release();
scheduler.block(); // Moves to next thread, enables int
}
}
void Semaphore::v() {
this->lock.acquire();
lock.acquire();
if (!this->wait_queue.empty()) {
if (!wait_queue.empty()) {
// Semaphore stays busy and unblocks next thread to work in critical section
unsigned int tid = this->wait_queue.front();
this->wait_queue.erase(wait_queue.begin());
unsigned int tid = wait_queue.front();
wait_queue.erase(wait_queue.begin());
CPU::disable_int(); // Make sure the deblock() comes through after releasing the lock
this->lock.release();
lock.release();
scheduler.deblock(tid); // Enables int
} else {
// No more threads want to work so free semaphore
this->counter = this->counter + 1;
this->lock.release();
counter = counter + 1;
lock.release();
}
}