mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-12 22:17:11 -04:00
Finalize code
This commit is contained in:
parent
96e4fc590c
commit
03f68b57a9
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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(){
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user