diff options
author | Christian Cunningham <cc@localhost> | 2022-03-12 16:39:39 -0800 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-03-12 16:39:39 -0800 |
commit | a29f40e073a0308bd74f0f9803a4660aa233c3ab (patch) | |
tree | e607ee7eed39da0e1dcf65c4eb76dd1426899f85 | |
parent | b62c324bde267a5a940b1d06f44f62a125aef50d (diff) |
"Dynamic" Mutex Allocation
-rw-r--r-- | include/globals.h | 3 | ||||
-rw-r--r-- | include/util/lock.h | 2 | ||||
-rw-r--r-- | include/util/mutex.h | 5 | ||||
-rw-r--r-- | linker.ld | 4 | ||||
-rw-r--r-- | src/globals.c | 3 | ||||
-rw-r--r-- | src/util/lock.c | 1 | ||||
-rw-r--r-- | src/util/mutex.c | 26 |
7 files changed, 42 insertions, 2 deletions
diff --git a/include/globals.h b/include/globals.h index 7bda83c..249ffe7 100644 --- a/include/globals.h +++ b/include/globals.h @@ -1,6 +1,7 @@ #ifndef GLOBALS_H #define GLOBALS_H #include <sys/schedule.h> +#include <util/mutex.h> #ifndef GLOBALS_C extern unsigned long irqlr; @@ -16,6 +17,8 @@ extern struct Thread usrloopthread; extern unsigned int gwidth, gheight, gpitch, gisrgb; extern unsigned char thread_table[MAX_THREADS]; extern struct Thread threads[MAX_THREADS]; +extern unsigned long mutex_table[MAX_MUTEXS]; +extern struct Mutex mutexs[MAX_MUTEXS]; #endif #endif diff --git a/include/util/lock.h b/include/util/lock.h index c5b2f7e..da18f81 100644 --- a/include/util/lock.h +++ b/include/util/lock.h @@ -1,8 +1,6 @@ #ifndef UTIL_LOCK_H #define UTIL_LOCK_H -#include <util/mutex.h> - struct Lock { unsigned long pid; } __attribute__((packed, aligned(4))); diff --git a/include/util/mutex.h b/include/util/mutex.h index ab318b4..c0dd17b 100644 --- a/include/util/mutex.h +++ b/include/util/mutex.h @@ -8,6 +8,8 @@ #define CORE3_PID 4 #define FIRST_AVAIL_PID CORE3_PID+1 +#define MAX_MUTEXS 0x100 + // PID field is first so that it can be treated // as a lock struct Mutex { @@ -15,4 +17,7 @@ struct Mutex { void* addr; } __attribute__((packed, aligned(4))); +struct Mutex* create_mutex(void* addr); +unsigned char delete_mutex(struct Mutex* m); + #endif @@ -38,6 +38,10 @@ SECTIONS . = ALIGN(4096); KEEP(*(.bss.threads")) . = ALIGN(4096); + KEEP(*(.bss.mutexl")) + . = ALIGN(4096); + KEEP(*(.bss.mutexs")) + . = ALIGN(4096); *(.bss) *(.bss.*) KEEP(*(.bss.mmheap)) diff --git a/src/globals.c b/src/globals.c index 29760b6..c704cb4 100644 --- a/src/globals.c +++ b/src/globals.c @@ -1,5 +1,6 @@ #define GLOBALS_C #include <sys/schedule.h> +#include <util/mutex.h> char* os_name = "Jobbed"; #ifndef VERSION char* os_info_v = "?"; @@ -16,6 +17,8 @@ __attribute__((section(".bss"))) unsigned int gwidth; __attribute__((section(".bss"))) unsigned int gheight; __attribute__((section(".bss"))) unsigned int gpitch; __attribute__((section(".bss"))) unsigned int gisrgb; +__attribute__((section(".bss.mutexl"))) unsigned long mutex_table[MAX_MUTEXS]; +__attribute__((section(".bss.mutexs"))) struct Mutex mutexs[MAX_MUTEXS]; // 0 - Free // 1 - Ready // 2 - Waiting for Mutex diff --git a/src/util/lock.c b/src/util/lock.c index 95f93af..eaeb5a8 100644 --- a/src/util/lock.c +++ b/src/util/lock.c @@ -1,5 +1,6 @@ #include <cpu.h> #include <cpu/atomic/swap.h> +#include <util/mutex.h> #include <util/lock.h> // TODO: Improve locking for system diff --git a/src/util/mutex.c b/src/util/mutex.c index e69de29..3171501 100644 --- a/src/util/mutex.c +++ b/src/util/mutex.c @@ -0,0 +1,26 @@ +#include <util/mutex.h> +#include <globals.h> + +struct Mutex* create_mutex(void* addr) +{ + for (unsigned long m = 0; m < MAX_MUTEXS; m++) { + if (mutex_table[m] == 0) { + mutex_table[m] = (unsigned long)addr; + mutexs[m].pid = 0; + mutexs[m].addr = addr; + return &mutexs[m]; + } + } + return 0; +} + +unsigned char delete_mutex(struct Mutex* m) +{ + for (unsigned long i = 0; i < MAX_MUTEXS; i++) { + if (mutex_table[i] == (unsigned long)m->addr) { + mutex_table[i] = 0; + return 0; + } + } + return 1; +} |