1

fix semaphore spinlock bug

This commit is contained in:
2022-07-11 15:37:26 +02:00
parent 52ad3fc752
commit 66b3b914d0
2 changed files with 9 additions and 6 deletions

View File

@ -182,6 +182,7 @@ void Scheduler::block() {
cpu.disable_int(); cpu.disable_int();
Thread& next = *(Thread*)this->readyQueue.dequeue(); Thread& next = *(Thread*)this->readyQueue.dequeue();
// Current thread is not added to readyQueue, gets managed by semaphore
this->dispatch(next); this->dispatch(next);
} }

View File

@ -8,13 +8,13 @@ void Semaphore::p() {
if (this->counter > 0) { if (this->counter > 0) {
// Semaphore can be acquired // Semaphore can be acquired
this->counter = this->counter - 1; this->counter = this->counter - 1;
this->lock.release();
} else { } else {
// Block and manage thread in semaphore queue until it's woken up by v() // Block and manage thread in semaphore queue until it's woken up by v() again
this->waitQueue.enqueue(scheduler.get_active()); this->waitQueue.enqueue(scheduler.get_active());
scheduler.block(); this->lock.release();
scheduler.block(); // Moves to next thread
} }
this->lock.release();
} }
void Semaphore::v() { void Semaphore::v() {
@ -22,11 +22,13 @@ void Semaphore::v() {
if (!this->waitQueue.isEmpty()) { if (!this->waitQueue.isEmpty()) {
// Semaphore stays busy and unblocks next thread to work in critical section // Semaphore stays busy and unblocks next thread to work in critical section
scheduler.deblock((Thread*)this->waitQueue.dequeue()); Thread* next = (Thread*)this->waitQueue.dequeue();
this->lock.release();
scheduler.deblock(next);
} else { } else {
// No more threads want to work so free semaphore // No more threads want to work so free semaphore
this->counter = this->counter + 1; this->counter = this->counter + 1;
this->lock.release();
} }
this->lock.release();
} }