diff --git a/kernel/include/semaphore.hpp b/kernel/include/semaphore.hpp index 921d0d6e..5081172b 100644 --- a/kernel/include/semaphore.hpp +++ b/kernel/include/semaphore.hpp @@ -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 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 l(lock); if(queue.empty()){ ++value; @@ -49,8 +49,6 @@ public: //No need to increment value, the process won't //decrement it } - - lock.release(); } }; diff --git a/kernel/include/stl/lock_guard.hpp b/kernel/include/stl/lock_guard.hpp new file mode 100644 index 00000000..e4f11502 --- /dev/null +++ b/kernel/include/stl/lock_guard.hpp @@ -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 +struct lock_guard { + Lock& lock; + + lock_guard(Lock& lock) : lock(lock) { + lock.acquire(); + } + + ~lock_guard(){ + lock.release(); + } +} + +#endif