aboutsummaryrefslogtreecommitdiff
path: root/emu
diff options
context:
space:
mode:
authorbhgv <bhgv.empire@gmail.com>2020-06-12 21:22:37 +0300
committerbhgv <bhgv.empire@gmail.com>2020-06-12 21:22:37 +0300
commit29cea60e3fae0a5e6b758aab62bfc5d160153576 (patch)
tree2211a784b43e1e81e421d11287f75a26186e65a6 /emu
parente55eda70d7594f90823221af61233792e7a0f4e4 (diff)
sup. FreeRTOS riscV-64 (k210 cpu). 5th step
Diffstat (limited to 'emu')
-rw-r--r--emu/FreeRTOS/cmd.c11
-rw-r--r--emu/FreeRTOS/lock-freertos.c142
-rw-r--r--emu/FreeRTOS/os.c11
3 files changed, 164 insertions, 0 deletions
diff --git a/emu/FreeRTOS/cmd.c b/emu/FreeRTOS/cmd.c
index 2671796..b46dd0a 100644
--- a/emu/FreeRTOS/cmd.c
+++ b/emu/FreeRTOS/cmd.c
@@ -36,6 +36,7 @@ extern int uidnobody;
static int
childproc(Targ *t)
{
+#if 0 // in the FreeRTOS no any "os" cmds
int i, nfd;
if(Debug)
@@ -81,11 +82,14 @@ childproc(Targ *t)
fprint(t->wfd, "exec failed: %s", strerror(errno));
_exit(1);
+#endif
+ return 0;
}
void*
oscmd(char **args, int nice, char *dir, int *fd)
{
+#if 0 // in the FreeRTOS no any "os" cmds
Targ *t;
int r, fd0[2], fd1[2], fd2[2], wfd[2], n, pid;
@@ -169,22 +173,27 @@ Error:
close(wfd[0]);
close(wfd[1]);
error(strerror(r));
+#endif
return nil;
}
int
oscmdkill(void *a)
{
+#if 0 // in the FreeRTOS no any "os" cmds
Targ *t = a;
if(Debug)
print("kill: %d\n", t->pid);
return kill(-t->pid, SIGTERM);
+#endif
+ return 0;
}
int
oscmdwait(void *a, char *buf, int n)
{
+#if 0 // in the FreeRTOS no any "os" cmds
Targ *t = a;
int s;
@@ -204,6 +213,8 @@ oscmdwait(void *a, char *buf, int n)
return snprint(buf, n, "%d 0 0 0 'signal: %d'", t->pid, WTERMSIG(s));
}
return snprint(buf, n, "%d 0 0 0 'odd status: 0x%x'", t->pid, s);
+#endif
+ return 0;
}
void
diff --git a/emu/FreeRTOS/lock-freertos.c b/emu/FreeRTOS/lock-freertos.c
new file mode 100644
index 0000000..7028dbd
--- /dev/null
+++ b/emu/FreeRTOS/lock-freertos.c
@@ -0,0 +1,142 @@
+#include "dat.h"
+#include "fns.h"
+#include "error.h"
+
+void
+lock(Lock *l)
+{
+ int i;
+
+ if(_tas(&l->val) == 0)
+ return;
+ for(i=0; i<100; i++){
+ if(_tas(&l->val) == 0)
+ return;
+ osyield();
+ }
+ for(i=1;; i++){
+ if(_tas(&l->val) == 0)
+ return;
+ osmillisleep(i*10);
+ if(i > 100){
+ osyield();
+ i = 1;
+ }
+ }
+}
+
+int
+canlock(Lock *l)
+{
+ return _tas(&l->val) == 0;
+}
+
+void
+unlock(Lock *l)
+{
+ coherence();
+ l->val = 0;
+}
+
+void
+qlock(QLock *q)
+{
+ Proc *p;
+
+ lock(&q->use);
+ if(!q->locked) {
+ q->locked = 1;
+ unlock(&q->use);
+ return;
+ }
+ p = q->tail;
+ if(p == 0)
+ q->head = up;
+ else
+ p->qnext = up;
+ q->tail = up;
+ up->qnext = 0;
+ unlock(&q->use);
+ osblock();
+}
+
+int
+canqlock(QLock *q)
+{
+ if(!canlock(&q->use))
+ return 0;
+ if(q->locked){
+ unlock(&q->use);
+ return 0;
+ }
+ q->locked = 1;
+ unlock(&q->use);
+ return 1;
+}
+
+void
+qunlock(QLock *q)
+{
+ Proc *p;
+
+ lock(&q->use);
+ p = q->head;
+ if(p) {
+ q->head = p->qnext;
+ if(q->head == 0)
+ q->tail = 0;
+ unlock(&q->use);
+ osready(p);
+ return;
+ }
+ q->locked = 0;
+ unlock(&q->use);
+}
+
+void
+rlock(RWlock *l)
+{
+ qlock(&l->x); /* wait here for writers and exclusion */
+ lock(&l->l);
+ l->readers++;
+ canqlock(&l->k); /* block writers if we are the first reader */
+ unlock(&l->l);
+ qunlock(&l->x);
+}
+
+/* same as rlock but punts if there are any writers waiting */
+int
+canrlock(RWlock *l)
+{
+ if (!canqlock(&l->x))
+ return 0;
+ lock(&l->l);
+ l->readers++;
+ canqlock(&l->k); /* block writers if we are the first reader */
+ unlock(&l->l);
+ qunlock(&l->x);
+ return 1;
+}
+
+void
+runlock(RWlock *l)
+{
+ lock(&l->l);
+ if(--l->readers == 0) /* last reader out allows writers */
+ qunlock(&l->k);
+ unlock(&l->l);
+}
+
+void
+wlock(RWlock *l)
+{
+ qlock(&l->x); /* wait here for writers and exclusion */
+ qlock(&l->k); /* wait here for last reader */
+}
+
+void
+wunlock(RWlock *l)
+{
+ qunlock(&l->k);
+ qunlock(&l->x);
+}
diff --git a/emu/FreeRTOS/os.c b/emu/FreeRTOS/os.c
index 468b232..12056dc 100644
--- a/emu/FreeRTOS/os.c
+++ b/emu/FreeRTOS/os.c
@@ -330,3 +330,14 @@ limbosleep(ulong milsec)
{
return osmillisleep(milsec);
}
+
+
+int
+_tas(int *la)
+{
+ int v = *la;
+
+ *la = 1;
+
+ return v;
+}