1

fix semaphore bug (preempt before block/deblock)

This commit is contained in:
2022-07-22 18:13:30 +02:00
parent 43477834fc
commit cde3967961

View File

@ -2,7 +2,7 @@
#include "kernel/Globals.h"
void Semaphore::p() {
// Lock to allow deterministic operations on counter
// Lock to allow deterministic operations on counter/queue
this->lock.acquire();
if (this->counter > 0) {
@ -12,8 +12,10 @@ void Semaphore::p() {
} else {
// Block and manage thread in semaphore queue until it's woken up by v() again
this->wait_queue.push_back(scheduler.get_active());
cpu.disable_int(); // Make sure the block() comes through after releasing the lock
this->lock.release();
scheduler.block(); // Moves to next thread
scheduler.block(); // Moves to next thread, enables int
}
}
@ -22,14 +24,12 @@ void Semaphore::v() {
if (!this->wait_queue.empty()) {
// Semaphore stays busy and unblocks next thread to work in critical section
if (this->wait_queue.empty()) {
this->lock.release();
return;
}
unsigned int tid = this->wait_queue.front();
this->wait_queue.erase(wait_queue.begin());
cpu.disable_int(); // Make sure the deblock() comes through after releasing the lock
this->lock.release();
scheduler.deblock(tid);
scheduler.deblock(tid); // Enables int
} else {
// No more threads want to work so free semaphore
this->counter = this->counter + 1;