diff options
| -rw-r--r-- | emu/FreeRTOS/cmd.c | 11 | ||||
| -rw-r--r-- | emu/FreeRTOS/lock-freertos.c | 142 | ||||
| -rw-r--r-- | emu/FreeRTOS/os.c | 11 | ||||
| -rw-r--r-- | libiot/freertos/adds.c | 22 |
4 files changed, 175 insertions, 11 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; +} diff --git a/libiot/freertos/adds.c b/libiot/freertos/adds.c index f2beaa6..4703cdc 100644 --- a/libiot/freertos/adds.c +++ b/libiot/freertos/adds.c @@ -64,11 +64,11 @@ void panic(const char*); #define THREAD_LOCAL_STORAGE_POINTER_ID 0 -// Reference to lua_thread, which is created in app_main -extern pthread_t lua_thread; +//// Reference to lua_thread, which is created in app_main +//extern pthread_t lua_thread; -// Global state -static lua_State *gL = NULL; +//// Global state +//static lua_State *gL = NULL; static int compare(const void *a, const void *b) { if (((task_info_t *)a)->task_type < ((task_info_t *)b)->task_type) { @@ -144,10 +144,10 @@ void uxSetLuaState(lua_State* L) { lua_rtos_tcb->lthread->L = L; } - // If this is the lua thread, store state in global state - if ((uxGetThreadId() == lua_thread) && (gL == NULL)) { - gL = L; - } +// // If this is the lua thread, store state in global state +// if ((uxGetThreadId() == lua_thread) && (gL == NULL)) { +// gL = L; +// } } lua_State* pvGetLuaState() { @@ -163,9 +163,9 @@ lua_State* pvGetLuaState() { } } - if (L == NULL) { - L = gL; - } +// if (L == NULL) { +// L = gL; +// } return L; } |
