Safer version of sleep queue

This commit is contained in:
Baptiste Wicht 2014-03-31 23:02:26 +02:00
parent 210ba3933d
commit 2c7521ed7e

View File

@ -9,61 +9,65 @@
#define SLEEP_QUEUE_H
#include <queue.hpp>
#include <lock_guard.hpp>
#include "spinlock.hpp"
#include "arch.hpp"
#include "scheduler.hpp"
#include "console.hpp"
struct sleep_queue {
private:
mutable spinlock lock;
std::queue<scheduler::pid_t> queue;
scheduler::sleep_queue_ptr* head = nullptr;
scheduler::sleep_queue_ptr* tail = nullptr;
public:
bool empty() const {
std::lock_guard<spinlock> l(lock);
return queue.empty();
}
scheduler::pid_t top_process() const {
std::lock_guard<spinlock> l(lock);
return queue.top();
return head == nullptr;
}
scheduler::pid_t wake_up(){
std::lock_guard<spinlock> l(lock);
size_t rflags;
arch::disable_hwint(rflags);
//Get the first process
auto pid = queue.top();
auto pid = head->pid;
//Remove the process from the queue
queue.pop();
head = head->next;
//Indicate to the scheduler that this process will be able
//to run
scheduler::unblock_process(pid);
arch::enable_hwint(rflags);
return pid;
}
void sleep(){
lock.acquire();
size_t rflags;
arch::disable_hwint(rflags);
//Get the current process information
auto pid = scheduler::get_pid();
auto queue_ptr = scheduler::queue_ptr(pid);
queue_ptr->pid = pid;
queue_ptr->next = nullptr;
queue_ptr->prev = nullptr;
//Enqueue the process in the sleep queue
queue.push(pid);
lock.release();
if(!head){
head = queue_ptr;
} else {
tail->next = queue_ptr;
tail = queue_ptr;
}
//This process will sleep
scheduler::block_process(pid);
scheduler::block_process();
arch::enable_hwint(rflags);
}
};