diff options
author | Jakob Odersky <jodersky@gmail.com> | 2014-03-17 16:50:15 +0100 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2014-03-17 16:53:46 +0100 |
commit | 2500120f64db83fc682f38a83f7f9e03ed8a5123 (patch) | |
tree | 4b9c2086c9d5dde0a89b4968db05e3b52aaa1e65 | |
parent | 01c10b2eadbcd09b6ac34ab80cac1b65c302152e (diff) | |
download | mux-2500120f64db83fc682f38a83f7f9e03ed8a5123.tar.gz mux-2500120f64db83fc682f38a83f7f9e03ed8a5123.tar.bz2 mux-2500120f64db83fc682f38a83f7f9e03ed8a5123.zip |
add spin-locks
-rw-r--r-- | kernel/task/include/task/lock.h | 11 | ||||
-rw-r--r-- | kernel/task/sched.c | 10 | ||||
-rw-r--r-- | kernel/time/mcu/atmega2560/clock.c | 2 | ||||
-rw-r--r-- | main.c | 38 |
4 files changed, 34 insertions, 27 deletions
diff --git a/kernel/task/include/task/lock.h b/kernel/task/include/task/lock.h index eaa661c..47246b2 100644 --- a/kernel/task/include/task/lock.h +++ b/kernel/task/include/task/lock.h @@ -1,20 +1,21 @@ #ifndef LOCK_H #define LOCK_H -#include "task/shed.h" +#include "task/sched.h" -typedef char spin_lock_t; +typedef volatile char spin_lock_t; #define SPIN_LOCK_UNLOCKED 0 +#define SPIN_LOCK_LOCKED 0 -static inline void spin_lock(struct spin_lock_t* lock) { +static inline void spin_lock(spin_lock_t* lock) { while(*lock != SPIN_LOCK_UNLOCKED) {yield();}; cli(); - *lock = 1; + *lock = SPIN_LOCK_LOCKED; sei(); } -static inline void spin_unlock(struct spin_lock_t* lock) { +static inline void spin_unlock(spin_lock_t* lock) { cli(); *lock = SPIN_LOCK_UNLOCKED; sei(); diff --git a/kernel/task/sched.c b/kernel/task/sched.c index 652d562..ceaf9bb 100644 --- a/kernel/task/sched.c +++ b/kernel/task/sched.c @@ -35,7 +35,17 @@ void sched_init() { ret(); } + +#include <avr/io.h> +void toggle_led() { + DDRB |= (1 << 7); + PORTB ^= (1 << 7); +} + + + void schedule() { + toggle_led(); if(!list_empty(&ready)) { current = list_entry(ready.next, struct tcb_t, queue); list_move_tail(ready.next, &ready); diff --git a/kernel/time/mcu/atmega2560/clock.c b/kernel/time/mcu/atmega2560/clock.c index 6821456..95622dc 100644 --- a/kernel/time/mcu/atmega2560/clock.c +++ b/kernel/time/mcu/atmega2560/clock.c @@ -16,7 +16,7 @@ void clock_init(int ms, void (*tick)()) { unsigned long int fcpu = (unsigned long int) F_CPU; unsigned long int hz = 1000l / ((unsigned long int) ms); - unsigned long int hz_counter = fcpu / (1024 * (hz)) - 1; + unsigned long int hz_counter = fcpu / (1024l * (hz)) - 1; OCR3A = hz_counter; on_tick = tick; } @@ -6,54 +6,50 @@ #include "task/sched.h" #include "bug/panic.h" #include "bug/debug.h" +#include "task/lock.h" +#include "time/clock.h" #include "mcu/task/context.h" #include "tshield/tshield.h" #define WAIT_CYCLES(cycles) for (volatile unsigned long i = 0; i < cycles; ++i) {} -struct list_head frozen = LIST_HEAD_INIT(frozen); +spin_lock_t on_lock = SPIN_LOCK_UNLOCKED; +volatile char on = 0; -void freeze() __attribute__ ( ( naked ) ); -void freeze() { - context_save(); - sleep_queue(&frozen); - schedule(); - context_restore(); +void read(char id) { + while(1) { + spin_lock(&on_lock); + debug_led(0,on); + spin_unlock(&on_lock); + } } -void blink( char id) { +void write( char id) { while(1) { - debug_led(id - 1,1); - WAIT_CYCLES((long) 30000); - debug_led(id - 1,0); - WAIT_CYCLES((long) 30000); + spin_lock(&on_lock); + on = !on; + WAIT_CYCLES(30000); + spin_unlock(&on_lock); } } -DECLARE_TASK(task1, DEFAULT_STACK_SIZE, blink, 1); -DECLARE_TASK(task2, DEFAULT_STACK_SIZE, blink, 2); -DECLARE_TASK(task3, DEFAULT_STACK_SIZE, blink, 3); -DECLARE_TASK(task4, DEFAULT_STACK_SIZE, blink, 4); +DECLARE_TASK(task1, DEFAULT_STACK_SIZE, read, 1); +DECLARE_TASK(task2, DEFAULT_STACK_SIZE, write, 2); int main(int argc, char *argv[]) { cli(); tshield_init(); - spawn(&task1); spawn(&task2); - spawn(&task3); - spawn(&task4); sei(); clock_init(10, schedule); clock_start(); sched_init(); - - panic(); //should never reach here while(1){} } |