Clean the code using a lock guard

This commit is contained in:
Baptiste Wicht 2014-01-28 22:10:18 +01:00
parent e0ee4266b8
commit 96e4fc590c
2 changed files with 28 additions and 6 deletions

View File

@ -8,6 +8,8 @@
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include "stl/lock_guard.hpp"
#include "spinlock.hpp"
#include "vector.hpp"
@ -25,7 +27,7 @@ public:
}
void wait(){
lock.acquire();
std::lock_guard<spinlock> l(lock);
if(value > 0){
--value;
@ -33,12 +35,10 @@ public:
queue.push_back(scheduler::get_pid());
scheduler::block_process(scheduler::get_pid());
}
lock.release();
}
void signal(){
lock.acquire();
std::lock_guard<spinlock> l(lock);
if(queue.empty()){
++value;
@ -49,8 +49,6 @@ public:
//No need to increment value, the process won't
//decrement it
}
lock.release();
}
};

View File

@ -0,0 +1,24 @@
//=======================================================================
// Copyright Baptiste Wicht 2013-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#ifndef LOCK_GUARD_HPP
#define LOCK_GUARD_HPP
template<typename Lock>
struct lock_guard {
Lock& lock;
lock_guard(Lock& lock) : lock(lock) {
lock.acquire();
}
~lock_guard(){
lock.release();
}
}
#endif