summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile2
-rw-r--r--src/common/include/sched.h15
-rw-r--r--src/common/include/stdlib_common.h (renamed from src/common/include/stdlib.h)2
-rw-r--r--src/common/sched.c26
-rw-r--r--src/common/string.c7
5 files changed, 47 insertions, 5 deletions
diff --git a/src/common/Makefile b/src/common/Makefile
index ef0f6b1..8698100 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -1,5 +1,5 @@
Out = _common.o
-Obj = string.o
+Obj = string.o sched.o
ExtObj =
include $(SrcPath)/common.make
diff --git a/src/common/include/sched.h b/src/common/include/sched.h
new file mode 100644
index 0000000..0f8f8f4
--- /dev/null
+++ b/src/common/include/sched.h
@@ -0,0 +1,15 @@
+#ifndef DEF_MUTEX_H
+#define DEF_MUTEX_H
+
+#include <types.h>
+
+#define MUTEX_LOCKED 1
+#define MUTEX_UNLOCKED 0
+
+//A mutex is just an uint32_t
+
+int mutex_lock(uint32_t* mutex); //wait for mutex to be free
+int mutex_lockE(uint32_t* mutex); //lock mutex only if free, returns !0 if locked, 0 if was busy
+void mutex_unlock(uint32_t* mutex);
+
+#endif
diff --git a/src/common/include/stdlib.h b/src/common/include/stdlib_common.h
index 2c5e6dd..80c188f 100644
--- a/src/common/include/stdlib.h
+++ b/src/common/include/stdlib_common.h
@@ -7,7 +7,7 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
void *memcpy(void *dest, const void *src, int count);
-uint8_t *memset(uint8_t *dest, uint8_t val, int count);
+void *memset(void *dest, int val, int count);
uint16_t *memsetw(uint16_t *dest, uint16_t val, int count);
#endif
diff --git a/src/common/sched.c b/src/common/sched.c
new file mode 100644
index 0000000..29ea8a8
--- /dev/null
+++ b/src/common/sched.c
@@ -0,0 +1,26 @@
+#include <sched.h>
+
+/* Internal use only. This function is atomic, meaning it cannot be interrupted by a system task switch. */
+static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) {
+ uint32_t r;
+ asm volatile("xchg (%%ecx), %%eax" : "=a"(r) : "c"(ptr), "a"(newval));
+ return r;
+}
+
+int mutex_lock(uint32_t* mutex) {
+ while (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) {
+ schedule();
+ }
+ return 0;
+}
+
+int mutex_lockE(uint32_t* mutex) {
+ if (atomic_exchange(mutex, MUTEX_LOCKED) == MUTEX_LOCKED) {
+ return 0;
+ }
+ return 1;
+}
+
+void mutex_unlock(uint32_t* mutex) {
+ *mutex = MUTEX_UNLOCKED;
+}
diff --git a/src/common/string.c b/src/common/string.c
index b2ec309..120fd5d 100644
--- a/src/common/string.c
+++ b/src/common/string.c
@@ -1,5 +1,5 @@
#include <string.h>
-#include <stdlib.h>
+#include <stdlib_common.h>
int strlen(const char *str) {
int i = 0;
@@ -63,10 +63,11 @@ void *memcpy(void *vd, const void *vs, int count) {
return vd;
}
-uint8_t *memset(uint8_t *dest, uint8_t val, int count) {
+void *memset(void *dest, int val, int count) {
+ uint8_t *dest_c = (uint8_t*)dest;
int i;
for (i = 0; i < count; i++) {
- dest[i] = val;
+ dest_c[i] = val;
}
return dest;
}