Finalize code

This commit is contained in:
Baptiste Wicht 2014-01-28 22:19:59 +01:00
parent 96e4fc590c
commit 03f68b57a9
3 changed files with 18 additions and 8 deletions

View File

@ -8,22 +8,23 @@
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include "stl/vector.hpp"
#include "stl/lock_guard.hpp"
#include "spinlock.hpp"
#include "vector.hpp"
#include "scheduler.hpp"
//TODO Implement a queue and use it for more fairness
struct semaphore {
private:
spinlock lock;
std::size_t value;
size_t value;
std::vector<scheduler::pid_t> queue;
public:
void init(std::size_t value) : value(value) {
//Nothing else to init
void init(size_t v){
value = v;
}
void wait(){
@ -43,9 +44,11 @@ public:
if(queue.empty()){
++value;
} else {
auto pid = queue.pop_back();
auto pid = queue.back();
scheduler::unblock_process(pid);
queue.pop_back();
//No need to increment value, the process won't
//decrement it
}

View File

@ -14,7 +14,7 @@ private:
public:
void acquire(){
asm volatile(".retry: ; lock bts [%0], 0; jc .retry" : : "m" (lock));
asm volatile("1: ; lock bts [%0], 0; jc 1" : : "m" (lock));
}
void release(){

View File

@ -8,17 +8,24 @@
#ifndef LOCK_GUARD_HPP
#define LOCK_GUARD_HPP
namespace std {
template<typename Lock>
struct lock_guard {
Lock& lock;
lock_guard(Lock& lock) : lock(lock) {
explicit lock_guard(Lock& l) : lock(l) {
lock.acquire();
}
lock_guard(const lock_guard&) = delete;
lock_guard& operator=(const lock_guard&) = delete;
~lock_guard(){
lock.release();
}
}
};
} //End of namespace std
#endif