mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-13 14:36:37 -04:00
Clean the code using a lock guard
This commit is contained in:
parent
e0ee4266b8
commit
96e4fc590c
@ -8,6 +8,8 @@
|
|||||||
#ifndef SEMAPHORE_H
|
#ifndef SEMAPHORE_H
|
||||||
#define SEMAPHORE_H
|
#define SEMAPHORE_H
|
||||||
|
|
||||||
|
#include "stl/lock_guard.hpp"
|
||||||
|
|
||||||
#include "spinlock.hpp"
|
#include "spinlock.hpp"
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
|
|
||||||
@ -25,7 +27,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wait(){
|
void wait(){
|
||||||
lock.acquire();
|
std::lock_guard<spinlock> l(lock);
|
||||||
|
|
||||||
if(value > 0){
|
if(value > 0){
|
||||||
--value;
|
--value;
|
||||||
@ -33,12 +35,10 @@ public:
|
|||||||
queue.push_back(scheduler::get_pid());
|
queue.push_back(scheduler::get_pid());
|
||||||
scheduler::block_process(scheduler::get_pid());
|
scheduler::block_process(scheduler::get_pid());
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal(){
|
void signal(){
|
||||||
lock.acquire();
|
std::lock_guard<spinlock> l(lock);
|
||||||
|
|
||||||
if(queue.empty()){
|
if(queue.empty()){
|
||||||
++value;
|
++value;
|
||||||
@ -49,8 +49,6 @@ public:
|
|||||||
//No need to increment value, the process won't
|
//No need to increment value, the process won't
|
||||||
//decrement it
|
//decrement it
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.release();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
24
kernel/include/stl/lock_guard.hpp
Normal file
24
kernel/include/stl/lock_guard.hpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user