From 1ad64c6cca5810de2a0ec01adcf653a4d28b6990 Mon Sep 17 00:00:00 2001 From: Will Fong Date: Fri, 24 Apr 2015 23:31:37 +0800 Subject: [PATCH 1/3] Add variable 'next_task' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An additional variable 'next_task' is helpful and clear for those who wants to change something according to the next task. If only 'current_task' is used, it must implicitly assume that the next task is the current task before kernel selects the next one. It is a fault when more than 2 tasks are created, for example:  if Task [length of execution] => 1[1T], 2[2T], 3[3T]  Task :       1 -> 2 -> 3 -> 1 -> 2 ->3  Length of execution : 1T->2T->1T->2T->3T->1T (<--- which is wrong) --- 06-Preemptive/os.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/06-Preemptive/os.c b/06-Preemptive/os.c index b5e4cc8..ea3a1af 100644 --- a/06-Preemptive/os.c +++ b/06-Preemptive/os.c @@ -109,6 +109,7 @@ int main(void) unsigned int *usertasks[TASK_LIMIT]; size_t task_count = 0; size_t current_task; + size_t next_task; usart_init(); @@ -127,13 +128,15 @@ int main(void) *SYSTICK_VAL = 0; *SYSTICK_CTRL = 0x07; current_task = 0; + next_task = current_task == (task_count - 1) ? 0 : current_task + 1; while (1) { print_str("OS: Activate next task\n"); usertasks[current_task] = activate(usertasks[current_task]); print_str("OS: Back to OS\n"); - current_task = current_task == (task_count - 1) ? 0 : current_task + 1; + current_task = next_task; + next_task = current_task == (task_count - 1) ? 0 : current_task + 1; } return 0; From 8f09fbd5178e91e93a71f6d1b6ab3eef94816adc Mon Sep 17 00:00:00 2001 From: Will Fong Date: Fri, 24 Apr 2015 23:35:35 +0800 Subject: [PATCH 2/3] Add a simple task_control_block --- 06-Preemptive/os.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/06-Preemptive/os.c b/06-Preemptive/os.c index ea3a1af..3cd5893 100644 --- a/06-Preemptive/os.c +++ b/06-Preemptive/os.c @@ -48,6 +48,11 @@ void delay(int count) while (count--); } +struct task_c_b { + unsigned int stack[STACK_SIZE]; + unsigned int * usertask; +} typedef tcb; + /* Exception return behavior */ #define HANDLER_MSP 0xFFFFFFF1 #define THREAD_MSP 0xFFFFFFF9 @@ -105,8 +110,7 @@ void task2_func(void) int main(void) { - unsigned int user_stacks[TASK_LIMIT][STACK_SIZE]; - unsigned int *usertasks[TASK_LIMIT]; + tcb user_proc[TASK_LIMIT]; size_t task_count = 0; size_t current_task; size_t next_task; @@ -115,10 +119,10 @@ int main(void) print_str("OS: Starting...\n"); print_str("OS: First create task 1\n"); - usertasks[0] = create_task(user_stacks[0], &task1_func); + user_proc[0].usertask = create_task(user_proc[0].stack, &task1_func); task_count += 1; print_str("OS: Back to OS, create task 2\n"); - usertasks[1] = create_task(user_stacks[1], &task2_func); + user_proc[1].usertask = create_task(user_proc[1].stack, &task2_func); task_count += 1; print_str("\nOS: Start round-robin scheduler!\n"); @@ -132,7 +136,7 @@ int main(void) while (1) { print_str("OS: Activate next task\n"); - usertasks[current_task] = activate(usertasks[current_task]); + user_proc[current_task].usertask = activate(user_proc[current_task].usertask); print_str("OS: Back to OS\n"); current_task = next_task; From c1b82cd5fdbadf4525ec50a12dc594e9e9ef6f61 Mon Sep 17 00:00:00 2001 From: Will Fong Date: Sat, 25 Apr 2015 10:28:27 +0800 Subject: [PATCH 3/3] Change the way defining a struct Use typedef struct { ... } tcb; instead of struct task_c_b { ... } typedef tcb; --- 06-Preemptive/os.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/06-Preemptive/os.c b/06-Preemptive/os.c index 3cd5893..63759e4 100644 --- a/06-Preemptive/os.c +++ b/06-Preemptive/os.c @@ -48,10 +48,10 @@ void delay(int count) while (count--); } -struct task_c_b { +typedef struct { unsigned int stack[STACK_SIZE]; unsigned int * usertask; -} typedef tcb; +} tcb; /* Exception return behavior */ #define HANDLER_MSP 0xFFFFFFF1