dynamic task allocation
authorMart Lubbers <mart@martlubbers.net>
Wed, 5 Apr 2017 09:47:31 +0000 (11:47 +0200)
committerMart Lubbers <mart@martlubbers.net>
Wed, 5 Apr 2017 09:47:31 +0000 (11:47 +0200)
client/client.c
client/interpret.c
client/task.c
client/task.h

index 59c611f..ce1d1f2 100644 (file)
@@ -45,7 +45,12 @@ void read_message(void)
                        break;
                case MSG_DEL_TASK:
                        debug("Receiving a delete task request");
-                       task_delete();
+                       c = read16();
+                       task_delete(c);
+                       //Write acknowledgement
+                       write_byte('d');
+                       write16(c);
+                       write_byte('\n');
                        break;
                case MSG_GET_TASK:
                        debug("Receiving a task");
@@ -79,13 +84,10 @@ void loop(void)
 #endif
        debug("Loop");
 
-       struct task *t;
-       long cyclestart;
-
        read_message();
 
        //Run tasks
-       cyclestart = getmillis();
+       unsigned long cyclestart = getmillis();
        for(struct task *t = task_head(); t != NULL; t = task_next(t)){
                //interrupt task
                if(is_interrupt_task(t) && had_interrupt(t)){
@@ -97,9 +99,9 @@ void loop(void)
                        run_task(t);
 
                        //Oneshot task, thus disable
-//                     if(t->interval == 0){
-//                             curtask->used = false;
-//                     }
+                       if(t->interval == 0){
+                               task_delete(t->taskid);
+                       }
                        t->lastrun = cyclestart;
                }
        }
index 9862e7b..6f2f20d 100644 (file)
@@ -19,7 +19,7 @@
 void run_task(struct task *t)
 {
        uint8_t *program = t->bc;
-       int plen = t->tlen;
+       int plen = t->tasklength;
        int pc = 0;
        int sp = 0;
        char stack[STACKSIZE] = {0};
index 12789ce..b420c19 100644 (file)
 uint8_t taskspace[TASKSPACE] = {0};
 uint8_t *spacepointer = (uint8_t *)&taskspace;
 uint8_t taskid = 0;
-struct task *head = NULL;
 
 void task_init(void)
 {
        memset(&taskspace, 0, TASKSPACE);
-       head = NULL;
 }
 
 void task_register(void)
@@ -27,9 +25,6 @@ void task_register(void)
 
        //We haven't had a task before, therefore we initialize head
        struct task *t = (struct task *)spacepointer;
-       if(head == NULL) {
-               head = t;
-       }
 
        //Read interval
        t->interval = read16();
@@ -48,12 +43,15 @@ void task_register(void)
                die("Out of memory... Not enough to allocate bytes");
        }
 
+       spacepointer += sizeof(struct task);
+       t->bc = spacepointer;
        spacepointer += t->tasklength;
 
        //Read task bytecode
        for(i = 0; i<t->tasklength; i++){
                t->bc[i] = read_byte();
        }
+
        //Return the task number for later removal
        debug("Received a task of length %d", t->tasklength);
        t->lastrun = 0L;
@@ -78,32 +76,31 @@ bool had_interrupt(struct task* t)
 
 struct task *task_head(void)
 {
-       return head;
+       return spacepointer == (uint8_t *)&taskspace ? NULL
+               : (struct task *)&taskspace;
 }
 
 struct task *task_next(struct task *t)
 {
-       if(t == NULL){
-               return NULL;
-       }
-
        uint8_t *next = (uint8_t *)t;
        next += sizeof(struct task);
        next += t->tasklength;
 
-       return next > spacepointer ? NULL : (struct task *)next;
+       return next >= spacepointer ? NULL : (struct task *)next;
 }
 
-void task_delete(void)
+void task_delete(uint8_t c)
 {
-       uint8_t c = read16();
-       struct task *t = NULL;
+       debug("Going to delete task: %i", c);
+       debug("spacepointer: %p", spacepointer);
+       struct task *t = task_head();
        while(t != NULL){
                if(t->taskid == c){
                        break;
                }
                t = task_next(t);
        }
+
        if(t != NULL){
                //We found the task, now we move everything from the end of the task up
                //to the spacepointer right
@@ -116,14 +113,6 @@ void task_delete(void)
 
                //Decrement the spacepointer
                spacepointer -= end-start;
-
-               if(t == head){
-                       head = task_next(t);
-               }
-
-               //Write acknowledgement
-               write_byte('d');
-               write16(c);
-               write_byte('\n');
        }
+       debug("spacepointer: %p", spacepointer);
 }
index 375aafc..5dba8d2 100644 (file)
@@ -23,7 +23,7 @@ struct task *task_head(void);
 struct task *task_next(struct task *t);
 void task_init(void);
 void task_register(void);
-void task_delete(void);
+void task_delete(uint8_t num);
 
 #ifdef __cplusplus
 }