-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpicoRTOS_cond.c
106 lines (97 loc) · 2.76 KB
/
picoRTOS_cond.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "picoRTOS_cond.h"
#include "picoRTOS_port.h"
/* Function: picoRTOS_cond_init
* Initialises a condition
*
* Conditions are used to synchronize threads. They work in pair with mutexes
*
* Parameters:
* cond - A pointer to the condition to initialize
*
* Remarks:
* Another way to init a condition at startup is the following:
* (start code)
* struct picoRTOS_cond cond = PICORTOS_COND_ININTIALIZER;
* (end)
*/
void picoRTOS_cond_init(struct picoRTOS_cond *cond)
{
cond->act = PICORTOS_COND_NONE;
cond->count = (size_t)0;
picoRTOS_flush_dcache(cond, sizeof(*cond));
}
/* Function: picoRTOS_cond_signal
* Signals a condition to a single waiting thread
*
* Parameters:
* cond - A pointer to the condition you want to signal
*/
void picoRTOS_cond_signal(struct picoRTOS_cond *cond)
{
picoRTOS_invalidate_dcache(cond, sizeof(*cond));
cond->act = PICORTOS_COND_SIGNAL;
picoRTOS_flush_dcache(cond, sizeof(*cond));
}
/* Function: picoRTOS_cond_broadcast
* Broadcasts a condition to a all waiting threads
*
* Parameters:
* cond - A pointer to the condition you want to broadcast
*/
void picoRTOS_cond_broadcast(struct picoRTOS_cond *cond)
{
picoRTOS_invalidate_dcache(cond, sizeof(*cond));
cond->act = PICORTOS_COND_BROADCAST;
picoRTOS_flush_dcache(cond, sizeof(*cond));
}
/* Function: picoRTOS_cond_wait
* Waits for a condition to be signaled
*
* Parameters:
* cond - A pointer to the condition you want to wait for
* mutex - A previously acquired mutex that is shared with the signaling thread
*
* After this call the mutex is released, allowing the signaling task to take it back.
*
* Right out of this call, you own the mutex again and need to release it.
*
* This is how conditions are typically used:
* Thread A:
* (start code)
* picoRTOS_mutex_lock(&mutex);
* picoRTOS_cond_wait(&cond, &mutex);
*
* do_something();
*
* picoRTOS_mutex_unlock(&mutex);
* (end)
* Thread B:
* (start code)
* picoRTOS_mutex_lock(&mutex);
*
* use_shared_resource();
*
* picoRTOS_cond_signal(&cond, mutex);
* picoRTOS_mutex_unlock(&mutex);
* (end)
*/
void picoRTOS_cond_wait(struct picoRTOS_cond *cond, struct picoRTOS_mutex *mutex)
{
picoRTOS_invalidate_dcache(cond, sizeof(*cond));
picoRTOS_assert_fatal(cond->count < (size_t)CONFIG_TASK_COUNT, return );
/* we already own the mutex */
cond->count++;
for (;;) {
picoRTOS_invalidate_dcache(cond, sizeof(*cond));
if (cond->act != PICORTOS_COND_NONE)
break;
picoRTOS_mutex_unlock(mutex);
picoRTOS_schedule();
picoRTOS_mutex_lock(mutex);
}
/* reset */
if (--cond->count == 0 ||
cond->act == PICORTOS_COND_SIGNAL)
cond->act = PICORTOS_COND_NONE;
picoRTOS_flush_dcache(cond, sizeof(*cond));
}