aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-02-13 17:13:17 -0700
committerChristian Cunningham <cc@localhost>2022-02-13 17:13:17 -0700
commita0d6941b2ef689302dce37d1a864fd4213a0a559 (patch)
tree10376ed4d35fb6d89a4fb0bb2cb1c4228ddb6d98
parent22b2957d4610f201bd2d365d42de285478093c01 (diff)
Implement generic lock
-rw-r--r--include/cpu.h2
-rw-r--r--include/util/lock.h12
-rw-r--r--include/util/mutex.h6
-rw-r--r--src/exceptions/svc.S14
-rw-r--r--src/tests/test.c10
-rw-r--r--src/util/lock.c9
6 files changed, 38 insertions, 15 deletions
diff --git a/include/cpu.h b/include/cpu.h
index c200016..6dbaa74 100644
--- a/include/cpu.h
+++ b/include/cpu.h
@@ -91,5 +91,7 @@ static inline void* getirqstack(void)
#define SYS_TIME 1
#define SYS_SCHED 2
#define SYS_FREE_STACK 3
+#define SYS_LOCK 4
+#define SYS_UNLOCK 5
#endif
diff --git a/include/util/lock.h b/include/util/lock.h
new file mode 100644
index 0000000..78add88
--- /dev/null
+++ b/include/util/lock.h
@@ -0,0 +1,12 @@
+#ifndef UTIL_LOCK_H
+#define UTIL_LOCK_H
+
+#include <util/mutex.h>
+
+struct Lock {
+ unsigned long pid;
+} __attribute__((packed, aligned(4)));
+
+struct Lock* create_lock(void);
+
+#endif
diff --git a/include/util/mutex.h b/include/util/mutex.h
index 907fe5b..61237b8 100644
--- a/include/util/mutex.h
+++ b/include/util/mutex.h
@@ -8,10 +8,12 @@
#define CORE3_PID 4
#define FIRST_AVAIL_PID CORE3_PID+1
+// PID field is first so that it can be treated
+// as a lock
struct Mutex {
- void* addr;
unsigned long pid;
-} __attribute__((packed, aligned(4)));;
+ void* addr;
+} __attribute__((packed, aligned(4)));
struct Mutex* create_mutex(void* addr);
diff --git a/src/exceptions/svc.S b/src/exceptions/svc.S
index dc1a351..cffa09c 100644
--- a/src/exceptions/svc.S
+++ b/src/exceptions/svc.S
@@ -50,12 +50,11 @@ svc_000003: // Clean task stack
sub r2, #1
str r2, [r3]
b svc_exit
-svc_000004: // Lock Mutex (usr_r0 = struct Mutex*)
+svc_000004: // Lock Lock (usr_r0 = struct Lock*)
ldr r3, =scheduler
ldr r2, [r3, #0] // struct Thread* rthread
ldr r1, [r2, #0x10] // unsigned long pid
- ldr r0, [sp, #0] // struct Mutex* m
- add r0, #4 // Point to pid
+ ldr r0, [sp, #0] // struct Lock* m
1: clrex
ldrex r2, [r0, #0]
cmp r2, #0
@@ -66,20 +65,17 @@ svc_000004: // Lock Mutex (usr_r0 = struct Mutex*)
dmb
b svc_exit
svc_000004_delay_mutex:
- // r0 = struct Mutex* m
- sub r0, #4
+ // r0 = struct Lock* m
bl sched_mutex_yield
ldmfd sp!, {r0-r12,lr}
b schedule
-svc_000005: // Release Mutex
- ldr r0, [sp, #0] // struct Mutex* m
- add r0, #4
+svc_000005: // Release Lock
+ ldr r0, [sp, #0] // struct Lock* m
mov r1, #0
dmb
str r1, [r0, #0]
dsb
sev
- sub r0, #4
bl sched_mutex_resurrect
ldmfd sp!, {r0-r12,lr}
b schedule
diff --git a/src/tests/test.c b/src/tests/test.c
index 8b01706..40e2679 100644
--- a/src/tests/test.c
+++ b/src/tests/test.c
@@ -5,6 +5,7 @@
#include <sys/core.h>
#include <sys/schedule.h>
#include <util/mutex.h>
+#include <util/lock.h>
extern void atest(void);
void btest(void);
@@ -33,26 +34,27 @@ void test_entry(void)
}
static struct Mutex testm = {.addr = 0, .pid = 0};
+//static struct Lock testm = {.pid = 0};
void ctest1(void)
{
uart_string("1 Started\n");
- sys1(4, &testm);
+ sys1(SYS_LOCK, &testm);
uart_string("1 Finished\n");
}
void ctest2(void)
{
uart_string("2 Started\n");
- sys1(4, &testm);
+ sys1(SYS_LOCK, &testm);
uart_string("2 Finished\n");
- sys1(5, &testm);
+ sys1(SYS_UNLOCK, &testm);
}
void ctest3(void)
{
uart_string("3 Started\n");
- sys1(5, &testm);
+ sys1(SYS_UNLOCK, &testm);
uart_string("3 Finished\n");
}
diff --git a/src/util/lock.c b/src/util/lock.c
new file mode 100644
index 0000000..2a7d35e
--- /dev/null
+++ b/src/util/lock.c
@@ -0,0 +1,9 @@
+#include <util/lock.h>
+#include <lib/kmem.h>
+
+struct Lock* create_lock(void)
+{
+ struct Lock* l = (struct Lock*)kmalloc(sizeof(struct Lock));
+ l->pid = 0;
+ return l;
+}