mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-09 20:43:34 -04:00
Use the wait_list
This commit is contained in:
parent
6c5bfd491e
commit
4e1efd2eaa
@ -8,10 +8,11 @@
|
|||||||
#ifndef MUTEX_H
|
#ifndef MUTEX_H
|
||||||
#define MUTEX_H
|
#define MUTEX_H
|
||||||
|
|
||||||
#include <circular_buffer.hpp>
|
#include <types.hpp>
|
||||||
#include <lock_guard.hpp>
|
#include <lock_guard.hpp>
|
||||||
|
|
||||||
#include "conc/spinlock.hpp"
|
#include "conc/spinlock.hpp"
|
||||||
|
#include "conc/wait_list.hpp"
|
||||||
|
|
||||||
#include "scheduler.hpp"
|
#include "scheduler.hpp"
|
||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
@ -46,9 +47,9 @@ struct mutex {
|
|||||||
|
|
||||||
value_lock.unlock();
|
value_lock.unlock();
|
||||||
} else {
|
} else {
|
||||||
auto pid = scheduler::get_pid();
|
queue.enqueue();
|
||||||
queue.push(pid);
|
|
||||||
|
|
||||||
|
auto pid = scheduler::get_pid();
|
||||||
scheduler::block_process_light(pid);
|
scheduler::block_process_light(pid);
|
||||||
value_lock.unlock();
|
value_lock.unlock();
|
||||||
scheduler::reschedule();
|
scheduler::reschedule();
|
||||||
@ -83,7 +84,7 @@ struct mutex {
|
|||||||
if (queue.empty()) {
|
if (queue.empty()) {
|
||||||
value = 1;
|
value = 1;
|
||||||
} else {
|
} else {
|
||||||
auto pid = queue.pop();
|
auto pid = queue.dequeue();
|
||||||
scheduler::unblock_process(pid);
|
scheduler::unblock_process(pid);
|
||||||
|
|
||||||
//No need to increment value, the process won't
|
//No need to increment value, the process won't
|
||||||
@ -92,9 +93,9 @@ struct mutex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable spinlock value_lock; ///< The spin protecting the value
|
mutable spinlock value_lock; ///< The spin protecting the value
|
||||||
volatile size_t value = 1; ///< The value of the mutex
|
volatile size_t value = 1; ///< The value of the mutex
|
||||||
circular_buffer<scheduler::pid_t, 16> queue; ///< The sleep queue
|
wait_list queue; ///< The sleep queue
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,10 +8,11 @@
|
|||||||
#ifndef SEMAPHORE_H
|
#ifndef SEMAPHORE_H
|
||||||
#define SEMAPHORE_H
|
#define SEMAPHORE_H
|
||||||
|
|
||||||
#include <circular_buffer.hpp>
|
#include <types.hpp>
|
||||||
#include <lock_guard.hpp>
|
#include <lock_guard.hpp>
|
||||||
|
|
||||||
#include "conc/spinlock.hpp"
|
#include "conc/spinlock.hpp"
|
||||||
|
#include "conc/wait_list.hpp"
|
||||||
|
|
||||||
#include "scheduler.hpp"
|
#include "scheduler.hpp"
|
||||||
|
|
||||||
@ -42,9 +43,9 @@ struct semaphore {
|
|||||||
--value;
|
--value;
|
||||||
value_lock.unlock();
|
value_lock.unlock();
|
||||||
} else {
|
} else {
|
||||||
auto pid = scheduler::get_pid();
|
queue.enqueue();
|
||||||
queue.push(pid);
|
|
||||||
|
|
||||||
|
auto pid = scheduler::get_pid();
|
||||||
scheduler::block_process_light(pid);
|
scheduler::block_process_light(pid);
|
||||||
value_lock.unlock();
|
value_lock.unlock();
|
||||||
scheduler::reschedule();
|
scheduler::reschedule();
|
||||||
@ -83,7 +84,7 @@ struct semaphore {
|
|||||||
++value;
|
++value;
|
||||||
} else {
|
} else {
|
||||||
// Wake up the process
|
// Wake up the process
|
||||||
auto pid = queue.pop();
|
auto pid = queue.dequeue();
|
||||||
scheduler::unblock_process(pid);
|
scheduler::unblock_process(pid);
|
||||||
|
|
||||||
//No need to increment value, the process won't
|
//No need to increment value, the process won't
|
||||||
@ -104,7 +105,7 @@ struct semaphore {
|
|||||||
value += n;
|
value += n;
|
||||||
} else {
|
} else {
|
||||||
while (n && !queue.empty()) {
|
while (n && !queue.empty()) {
|
||||||
auto pid = queue.pop();
|
auto pid = queue.dequeue();
|
||||||
scheduler::unblock_process(pid);
|
scheduler::unblock_process(pid);
|
||||||
--n;
|
--n;
|
||||||
}
|
}
|
||||||
@ -116,9 +117,9 @@ struct semaphore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable spinlock value_lock; ///< The spin lock protecting the counter
|
mutable spinlock value_lock; ///< The spin lock protecting the counter
|
||||||
volatile size_t value; ///< The value of the counter
|
volatile size_t value; ///< The value of the counter
|
||||||
circular_buffer<scheduler::pid_t, 16> queue; ///< The sleep queue
|
wait_list queue; ///< The sleep queue
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user