aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-02-13 17:31:22 -0700
committerChristian Cunningham <cc@localhost>2022-02-13 17:31:22 -0700
commite814983a452c40734c5ecc63581df88d3925835e (patch)
tree39ea355b1e47fa0baf3acb49e2229616827f4af5
parenta0d6941b2ef689302dce37d1a864fd4213a0a559 (diff)
Mode-aware locking
-rw-r--r--include/util/lock.h2
-rw-r--r--src/tests/test.c12
-rw-r--r--src/util/lock.c24
3 files changed, 31 insertions, 7 deletions
diff --git a/include/util/lock.h b/include/util/lock.h
index 78add88..f6f8f53 100644
--- a/include/util/lock.h
+++ b/include/util/lock.h
@@ -7,6 +7,8 @@ struct Lock {
unsigned long pid;
} __attribute__((packed, aligned(4)));
+void lock(struct Lock* l);
+void unlock(struct Lock* l);
struct Lock* create_lock(void);
#endif
diff --git a/src/tests/test.c b/src/tests/test.c
index 40e2679..071323e 100644
--- a/src/tests/test.c
+++ b/src/tests/test.c
@@ -33,28 +33,28 @@ void test_entry(void)
add_thread(btest, 0, 4);
}
-static struct Mutex testm = {.addr = 0, .pid = 0};
-//static struct Lock testm = {.pid = 0};
+//static struct Mutex testm = {.addr = 0, .pid = 0};
+static struct Lock testm = {.pid = 0};
void ctest1(void)
{
uart_string("1 Started\n");
- sys1(SYS_LOCK, &testm);
+ lock(&testm);
uart_string("1 Finished\n");
}
void ctest2(void)
{
uart_string("2 Started\n");
- sys1(SYS_LOCK, &testm);
+ lock(&testm);
uart_string("2 Finished\n");
- sys1(SYS_UNLOCK, &testm);
+ unlock(&testm);
}
void ctest3(void)
{
uart_string("3 Started\n");
- sys1(SYS_UNLOCK, &testm);
+ unlock(&testm);
uart_string("3 Finished\n");
}
diff --git a/src/util/lock.c b/src/util/lock.c
index 2a7d35e..6b048f4 100644
--- a/src/util/lock.c
+++ b/src/util/lock.c
@@ -1,5 +1,27 @@
-#include <util/lock.h>
+#include <cpu.h>
+#include <cpu/atomic/swap.h>
#include <lib/kmem.h>
+#include <util/lock.h>
+
+void lock(struct Lock* l)
+{
+ unsigned long mode = getmode() & 0x1F;
+ if (mode == 0x10) {
+ sys1(SYS_LOCK, l);
+ } else {
+ atm_lock(CORE0_PID, (unsigned long*)l);
+ }
+}
+
+void unlock(struct Lock* l)
+{
+ unsigned long mode = getmode() & 0x1F;
+ if (mode == 0x10) {
+ sys1(SYS_UNLOCK, l);
+ } else {
+ atm_release((unsigned long*)l);
+ }
+}
struct Lock* create_lock(void)
{