aboutsummaryrefslogtreecommitdiff
path: root/src/util/mutex.c
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-01-06 15:51:48 -0800
committerChristian Cunningham <cc@localhost>2022-01-06 15:51:48 -0800
commita61201b8047ebe278cfb281723a4bf6c82556472 (patch)
treef3b2d5b4a9e537fa8f370b00d0c4d4b637223303 /src/util/mutex.c
parenta826a645a67c2be3c7acb097c436c810da728ed7 (diff)
Scheduling
Diffstat (limited to 'src/util/mutex.c')
-rw-r--r--src/util/mutex.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/util/mutex.c b/src/util/mutex.c
index ade0be3..de7a515 100644
--- a/src/util/mutex.c
+++ b/src/util/mutex.c
@@ -1,13 +1,23 @@
#include <cpu/atomic/swap.h>
#include <lib/mem.h>
+#include <sys/schedule.h>
#include <util/mutex.h>
unsigned char lock_mutex(struct Mutex* m, unsigned long pid)
{
if (m->pid == NULL_PID) {
- atm_lock(pid, &m->pid);
+ // Use currently running thread's PID if no pid given
+ if (pid == 0) {
+ struct Thread* thread = scheduler.rthread_ll->data;
+ atm_lock(thread->data.pid, &m->pid);
+ } else {
+ atm_lock(pid, &m->pid);
+ }
return 0;
}
+ struct Thread* thread = scheduler.rthread_ll->data;
+ thread->data.status = THREAD_WAITING_FOR_MUTEX;
+ thread->data.mutex_waiting = m;
return 1;
}
@@ -16,7 +26,15 @@ unsigned char lock_mutex(struct Mutex* m, unsigned long pid)
// mutex's pid lock
unsigned char release_mutex(struct Mutex* m, unsigned long pid)
{
- if (m->pid == pid) {
+ // Use current thread's PID if no pid
+ if (pid == 0) {
+ struct Thread* thread = scheduler.rthread_ll->data;
+ if (m->pid == thread->data.pid) {
+ atm_release(&m->pid);
+ return 0;
+ }
+ }
+ else if (m->pid == pid) {
atm_release(&m->pid);
return 0;
}