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

View File

@ -14,7 +14,7 @@ private:
public: public:
void acquire(){ 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(){ void release(){

View File

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