From c5b6f1611ea1f4685ef02f65fc0362f9c22c344f Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Thu, 24 Mar 2022 19:21:12 -0700 Subject: Semaphore example in toy --- usr/main.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'usr') diff --git a/usr/main.c b/usr/main.c index 835cedf..0cc25d8 100644 --- a/usr/main.c +++ b/usr/main.c @@ -1,4 +1,6 @@ +#include #include +#include #include #include #include @@ -33,6 +35,25 @@ static struct UartInfo UART_INFO = { .priority = 2, }; +static unsigned long simulated = 0; + +void producer(void) +{ + draw_string(0, 15, "Producing..."); + sys1(SYS_SEMAPHORE_V, &simulated); + draw_string(0, 15, "Produced! "); +} + +void consumer(void) +{ + add_thread(producer, 0, 4); + while (1) { + draw_string(0, 16, "Consuming..."); + sys1(SYS_SEMAPHORE_P, &simulated); + draw_string(0, 16, "Consumed! "); + } +} + void main(void) { subscribe_irq(UART_IRQ, handle_data, &UART_INFO); @@ -40,5 +61,6 @@ void main(void) subscribe_irq(SYS_TIMER_1_IRQ, loopt, &stime_1); subscribe_irq(SYS_TIMER_2_IRQ, loopt, &stime_2); subscribe_irq(SYS_TIMER_3_IRQ, loopt, &stime_3); - add_thread(loop, 0, 0); + add_thread(loop, 0, 8); + add_thread(consumer, 0, 3); } -- cgit v1.2.1