krnl 1
Loading...
Searching...
No Matches
rt-arduino-reader-writerf.ino
Go to the documentation of this file.
1#include <krnl.h>
2
3//Jens s2021
4// https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem
5
6
7struct rdWrLockTp {
8 k_t *rdSem, *wrSem, *fifoSem;
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->fifoSem = k_crt_sem(1, 10);
24 lock->rdCount = 0;
25 return 0; // ok - no real chk
26}
27
28void rdEnter(struct rdWrLockTp *lock)
29{
30 k_wait(lock->fifoSem, 0);
31
32 k_wait(lock->rdSem, 0);
33 lock->rdCount++;
34 if (lock->rdCount == 1) {
35 k_wait(lock->wrSem, 0); // wait until writer is finished
36 }
37 k_signal(lock->fifoSem);
38 k_signal(lock->rdSem);
39}
40
41void wrEnter(struct rdWrLockTp *lock)
42{
43 k_wait(lock->fifoSem, 0);
44 k_wait(lock->wrSem, 0);
45 k_signal(lock->fifoSem);
46}
47
48void wrLeave(struct rdWrLockTp *lock)
49{
50 k_signal(lock->wrSem);
51}
52
53void rdLeave(struct rdWrLockTp *lock)
54{
55 k_wait(lock->rdSem, 0);
56 lock->rdCount--;
58 if (lock->rdCount == 0) {
59 k_signal(lock->wrSem); // wait until writer is finished
60 }
61 k_signal(lock->rdSem);
62}
63
64
65void wr(void)
66{
67 int i = 0;
68 while (1) {
69 k_sleep( random(100, 1000)); // NB k_sleep and not k_eat_ticks bq then you will never release CPU
71 Serial.println("wr in");
72 k_eat_ticks( random(100, 2500));
73 // just chekking
74 if (0 < rdInsideFortest)
75 Serial.println("problemmmmmss");
76 Serial.println("wr leaving");
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_sleep( random(100, 1000)); // NB k_sleep and not k_eat_ticks bq then you will never release CPU
92 Serial.print("rd>> "); Serial.println(n);
93 k_eat_ticks( random(100, 500));
94 Serial.print("rd<<"); Serial.println(n);
96 }
97}
98void setup()
99{
100 Serial.begin(115200); // for output from task 1
101 delay(2000);
102 Serial.print("RW START");
103 k_init(4, 3, 0);
104 k_crt_task(wr, 12, 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_sleep(int time)
let task sleep for a number of milliseconds
Definition krnl.c:445
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)
volatile int rdInsideFortest
struct rdWrLockTp rdWrLock1
void rdEnter(struct rdWrLockTp *lock)
void rdLeave(struct rdWrLockTp *lock)
void rd(void)
int rdWrLockInit(struct rdWrLockTp *lock)
void wr(void)
void wrLeave(struct rdWrLockTp *lock)
void wrEnter(struct rdWrLockTp *lock)