krnl 1
Loading...
Searching...
No Matches
semmutex2.ino
Go to the documentation of this file.
1#include <snot2.h>
2
3// mutual exclusion by semaphore :-)
4// two tasks want to acces serial line
5// they have to reserce the serial port by aquiring a semaphore and release afterwards
6//
7
8struct k_t *t1, *t2, *mutexSem;
9
10char stak1[100], stak2[100];
11
12void t1Code() {
13 while (1) {
14 k_wait(mutexSem,0); // lets get the serial port - we will wait forever until granted access
15 Serial.println("nr 1 :-)");
16 Serial.println("nr 1. :-)");
17 Serial.println("nr 1.. :-)");
18 Serial.println("nr 1... :-)");
19 k_signal(mutexSem); // release port again
20 }
21}
22
23void t2Code() {
24 while (1) {
26 // delay(1000); // w´busy waiting a second - not the way to wait
27 Serial.println("nr 2 :-)");
28 Serial.println("nr 2. :-)");
29 Serial.println("nr 2.. :-)");
30 Serial.println("nr 2... :-)");
32 }
33}
34
35void setup()
36{
37 k_init(5,5,5);
38 Serial.begin(9600);
39 mutexSem = k_crt_sem(1,10);
40 t1 = k_crt_task(t1Code,10,stak1,100);
41 t2 = k_crt_task(t2Code,10,stak2,100);
42 k_start(10);
43}
44void loop()
45{
46}
47
char stak1[STKSZ]
char stak2[STKSZ]
int k_signal(struct k_t *sem)
Signal a semaphore w eventually task shift.
Definition krnl.c:636
int k_wait(struct k_t *sem, int timeout)
stand wait on semaphore call with timeout facility
Definition krnl.c:683
struct k_t * k_crt_task(void(*pTask)(void), char prio, char *pStk, int stkSize)
create a task - only to be called before k_start creates a task and put it in the active Q
Definition krnl.c:328
struct k_t * k_crt_sem(int init_val, int maxvalue)
change task priority and reinserts it in task Q and do a task shift
Definition krnl.c:544
int k_init(int nrTask, int nrSem, int nrMsg)
Definition krnl.c:1108
int k_start()
Definition krnl.c:1149
struct k_t * mutexSem
Definition mutex.ino:3
struct k_t * t1
Definition krnlgen.ino:3
struct k_t * t2
Definition krnlgen.ino:3
void t2Code()
Definition semmutex2.ino:23
void setup()
Definition semmutex2.ino:35
void t1Code()
Definition semmutex2.ino:12
void loop()
Definition semmutex2.ino:44
void t2Code()
Definition semmutex.ino:24
void t1Code()
Definition semmutex.ino:13
Definition krnl.h:323