krnl 1
Loading...
Searching...
No Matches
rt-arduino-reader-writer01.ino
Go to the documentation of this file.
1#include <krnl.h>
2
3//Jens s2021
4
5// freertos considerations
6
7struct rdWrLockTp {
9 int rdCount;
10};
11
12
14
15volatile int rdInsideFortest=0;
16
17// RD WR LOCK BY SEMAPHORE
18
19int rdWrLockInit(struct rdWrLockTp *lock)
20{
21 lock->rdSem = k_crt_sem(1, 10);
22 lock->wrSem = k_crt_sem(1, 10);
23 lock->rdCount = 0;
24 return 0; // ok - no real chk
25}
26
27void rdEnter(struct rdWrLockTp *lock)
28{
29 k_wait(lock->rdSem, 0);
30 lock->rdCount++;
31 if (lock->rdCount == 1) {
32 k_wait(lock->wrSem, 0); // wait until writer is finished
33 }
35 k_signal(lock->rdSem);
36}
37
38void wrEnter(struct rdWrLockTp *lock)
39{
40 k_wait(lock->rdSem, 0);
41 k_signal(lock->rdSem);
42 k_wait(lock->wrSem, 0);
43}
44
45void wrLeave(struct rdWrLockTp *lock)
46{
47 k_wait(lock->rdSem, 0);
48 k_signal(lock->rdSem);
49 k_signal(lock->wrSem);
50}
51
52void rdLeave(struct rdWrLockTp *lock)
53{
54 k_wait(lock->rdSem, 0);
55 lock->rdCount--;
57 if (lock->rdCount == 0) {
58 k_signal(lock->wrSem); // wait until writer is finished
59 }
60 k_signal(lock->rdSem);
61}
62
63
64void wr(void)
65{
66 int i = 0;
67 while (1) {
68 k_eat_ticks( random(100, 1000));
70 Serial.println("wr in");
71 k_eat_ticks( random(100, 2500));
72 Serial.println("wr leaving");
73 // just chekking
74 if (0 < rdInsideFortest)
75 Serial.println("problemmmmmss");
77
78 }
79}
80
81volatile int rdNr = 0;
82void rd(void)
83{
84 int n;
85 DI();
86 n = rdNr;
87 rdNr++;
88 EI();
89 while (1) {
90 k_eat_ticks( random(100, 1000));
92 Serial.print("rd>> "); Serial.println(n);
93 k_eat_ticks( random(100, 500));
94 Serial.print("rd<<"); Serial.println(n);
96
97 }
98}
99void setup()
100{
101 Serial.begin(115200); // for output from task 1
102 delay(3000);
103 k_init(4, 2, 0);
104 k_crt_task(wr, 11, 200);
105 k_crt_task(rd, 11, 200);
106 k_crt_task(rd, 11, 200);
107 k_crt_task(rd, 11, 200);
109 k_start(1); // start kernel with tick speed 1 milli seconds
110 Serial.println("if coming hre then init went wrong");
111}
112
113void loop() {
114 /* loop will never be called */
115}
volatile int i
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
#define DI()
Definition krnl.h:478
#define EI()
Definition krnl.h:479
void wr(void)
volatile int rdNr
void rd(void)
void rdEnter(struct rdWrLockTp *lock)
void rdLeave(struct rdWrLockTp *lock)
void rd(void)
volatile int rdInsideFortest
int rdWrLockInit(struct rdWrLockTp *lock)
struct rdWrLockTp rdWrLock1
void wr(void)
void wrLeave(struct rdWrLockTp *lock)
void wrEnter(struct rdWrLockTp *lock)
Definition krnl.h:323