[joyos] r287 committed - Added realtime priority threads.

1 view
Skip to first unread message

jo...@googlecode.com

unread,
Jan 18, 2011, 12:17:53 AM1/18/11
to joyos...@googlegroups.com
Revision: 287
Author: vaumnou
Date: Mon Jan 17 21:16:52 2011
Log: Added realtime priority threads.
http://code.google.com/p/joyos/source/detail?r=287

Modified:
/trunk/src/drivers/gyro.c
/trunk/src/drivers/rf.c
/trunk/src/inc/kern/thread.h
/trunk/src/kern/main.c
/trunk/src/kern/thread.c
/trunk/tests/panic_nonhalting.c
/trunk/user/demo.c
/trunk/user/mouse/mouse.c
/trunk/user/umain2.c

=======================================
--- /trunk/src/drivers/gyro.c Mon Apr 19 21:40:36 2010
+++ /trunk/src/drivers/gyro.c Mon Jan 17 21:16:52 2011
@@ -64,7 +64,7 @@
_theta = 0;

init_lock (&gyro_lock, "gyro lock");
- create_thread (&gyro_update, STACK_DEFAULT, 0, "gyro");
+ create_thread (&gyro_update, STACK_DEFAULT,
THREAD_PRIORITY_NORMAL, "gyro");
#endif
}

=======================================
--- /trunk/src/drivers/rf.c Mon Apr 19 21:40:36 2010
+++ /trunk/src/drivers/rf.c Mon Jan 17 21:16:52 2011
@@ -456,5 +456,5 @@

#endif

- create_thread (&rf_receive, STACK_DEFAULT, 0, "rf");
-}
+ create_thread (&rf_receive, STACK_DEFAULT,
THREAD_PRIORITY_NORMAL, "rf");
+}
=======================================
--- /trunk/src/inc/kern/thread.h Mon Apr 19 21:40:36 2010
+++ /trunk/src/inc/kern/thread.h Mon Jan 17 21:16:52 2011
@@ -105,6 +105,9 @@
int (*th_func)();
};

+#define THREAD_PRIORITY_NORMAL 0
+#define THREAD_PRIORITY_REALTIME 1
+
// Macros for extracting jmp_buf thingies

#define JMPBUF_FP_OFFSET 16
=======================================
--- /trunk/src/kern/main.c Mon Apr 19 21:40:36 2010
+++ /trunk/src/kern/main.c Mon Jan 17 21:16:52 2011
@@ -87,7 +87,7 @@

#ifndef SIMULATE
init_thread();
- create_thread(&robot_monitor, STACK_DEFAULT, 0, "main");
+ create_thread(&robot_monitor, STACK_DEFAULT,
THREAD_PRIORITY_NORMAL, "main");
rf_init();
schedule();
#else
=======================================
--- /trunk/src/kern/thread.c Mon Apr 19 21:40:36 2010
+++ /trunk/src/kern/thread.c Mon Jan 17 21:16:52 2011
@@ -100,26 +100,49 @@

ATOMIC_END;
}
+
+void resume_if_runnable(int id) {
+ if (threads[id].th_status == THREAD_PAUSED &&
+ threads[id].th_wakeup_time <= global_time)
+ threads[id].th_status = THREAD_RUNNABLE;
+
+ if (threads[id].th_status == THREAD_RUNNABLE)
+ resume(&threads[id]);
+}

// Schedule a new thread to run
// assume interrupts disabled
void schedule(void) {
- int id = current_thread ? current_thread->th_id : MAX_THREADS-1;
+ static int id_lo = 0, id_rt = -1;
+ static uint32_t last_time = 0;
current_thread = NULL;

- int offset;
- for (offset = 1; offset <= MAX_THREADS; offset++) {
- int tid = (id+offset) % MAX_THREADS;
-
- if (threads[tid].th_status == THREAD_PAUSED &&
- threads[tid].th_wakeup_time <= global_time)
- threads[tid].th_status = THREAD_RUNNABLE;
-
- if (threads[tid].th_status == THREAD_RUNNABLE)
- resume(&threads[tid]);
+ if (global_time > last_time) {
+ if (id_rt != -1 ||
+ ((global_time - last_time) > 1 && last_time != 0))
+ panic("Failed to meet realtime commitments");
+ last_time = global_time;
+ id_rt = 0;
+ }
+
+ while (id_rt != -1) {
+ int tid = id_rt;
+ // update id_rt to be the next thread that should be attempted
+ id_rt = (id_rt+2) % (MAX_THREADS+1) - 1;
+ if (threads[tid].th_priority == THREAD_PRIORITY_REALTIME)
+ resume_if_runnable(tid);
}

- // wait for aliens to take us home...
+ int old_id_lo = id_lo;
+ do {
+ int tid = id_lo;
+ // update id_lo to be the next thread that should be attempted
+ id_lo = (id_lo + 1) % MAX_THREADS;
+ if (threads[tid].th_priority == THREAD_PRIORITY_NORMAL)
+ resume_if_runnable(tid);
+ } while (id_lo != old_id_lo);
+
+ // wait for the clock to tick
SREG |= SREG_IF;
for(;;);
}
@@ -419,6 +442,7 @@
threads[i].th_runs = 0;
threads[i].th_stacksize = STACKSIZE;
threads[i].th_func = func;
+ threads[i].th_priority = priority;

// make the jmp_buf something sensible,
// calculate the thread's stack top, and
=======================================
--- /trunk/tests/panic_nonhalting.c Wed Mar 24 17:27:31 2010
+++ /trunk/tests/panic_nonhalting.c Mon Jan 17 21:16:52 2011
@@ -23,7 +23,7 @@
}

int umain(void) {
- create_thread(&panicbreak, 64, 0, "panicbreak");
+ create_thread(&panicbreak, 64, THREAD_PRIORITY_NORMAL, "panicbreak");

while (1) {
yield();
=======================================
--- /trunk/user/demo.c Wed Mar 24 17:27:31 2010
+++ /trunk/user/demo.c Mon Jan 17 21:16:52 2011
@@ -130,7 +130,7 @@
}

int umain(void) {
- create_thread(&display_angle, 64, 0, "display angle");
+ create_thread(&display_angle, 64, THREAD_PRIORITY_NORMAL, "display
angle");

struct pid_controller pid;
init_pid (&pid, 1, 0, 0, &read_angle, &set_turn);
=======================================
--- /trunk/user/mouse/mouse.c Wed Mar 24 17:27:31 2010
+++ /trunk/user/mouse/mouse.c Mon Jan 17 21:16:52 2011
@@ -375,7 +375,7 @@
uint8_t num_low_readings = 0;
activateRun();

- create_thread(&CaughtThread, STACK_DEFAULT, 0, "Caught Thread");
+ create_thread(&CaughtThread, STACK_DEFAULT,
THREAD_PRIORITY_NORMAL, "Caught Thread");

while(1){

=======================================
--- /trunk/user/umain2.c Wed Mar 24 17:27:31 2010
+++ /trunk/user/umain2.c Mon Jan 17 21:16:52 2011
@@ -81,7 +81,7 @@
}

int umain(void) {
- create_thread(&display_angle, 64, 0, "display angle");
+ create_thread(&display_angle, 64, THREAD_PRIORITY_NORMAL, "display
angle");

//struct pid_controller pid;
//init_pid (&pid, 1, 0, 0, &read_angle, &set_turn);

Reply all
Reply to author
Forward
0 new messages