forked from KariHaapalehto/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionVariable.cpp
153 lines (129 loc) · 4.24 KB
/
ConditionVariable.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* mbed Microcontroller Library
* Copyright (c) 2017-2017 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "rtos/ConditionVariable.h"
#include "rtos/Kernel.h"
#include "rtos/ThisThread.h"
#include "mbed_error.h"
#include "mbed_assert.h"
namespace rtos {
ConditionVariable::Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false)
{
// No initialization to do
}
ConditionVariable::ConditionVariable(Mutex &mutex): _mutex(mutex), _wait_list(NULL)
{
// No initialization to do
}
void ConditionVariable::wait()
{
wait_for(osWaitForever);
}
bool ConditionVariable::wait_for(uint32_t millisec)
{
Waiter current_thread;
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
MBED_ASSERT(_mutex._count == 1);
_add_wait_list(&_wait_list, ¤t_thread);
_mutex.unlock();
int32_t sem_count = current_thread.sem.wait(millisec);
bool timeout = (sem_count > 0) ? false : true;
_mutex.lock();
if (current_thread.in_list) {
_remove_wait_list(&_wait_list, ¤t_thread);
}
return timeout;
}
bool ConditionVariable::wait_until(uint64_t millisec)
{
uint64_t now = Kernel::get_ms_count();
if (now >= millisec) {
// Time has already passed - standard behaviour is to
// treat as a "try".
return wait_for(0);
} else if (millisec - now >= osWaitForever) {
// Exceeds maximum delay of underlying wait_for -
// spuriously wake after 49 days, indicating no timeout.
wait_for(osWaitForever - 1);
return false;
} else {
return wait_for(millisec - now);
}
}
void ConditionVariable::notify_one()
{
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
if (_wait_list != NULL) {
_wait_list->sem.release();
_remove_wait_list(&_wait_list, _wait_list);
}
}
void ConditionVariable::notify_all()
{
MBED_ASSERT(_mutex.get_owner() == ThisThread::get_id());
while (_wait_list != NULL) {
_wait_list->sem.release();
_remove_wait_list(&_wait_list, _wait_list);
}
}
void ConditionVariable::_add_wait_list(Waiter **wait_list, Waiter *waiter)
{
if (NULL == *wait_list) {
// Nothing in the list so add it directly.
// Update prev and next pointer to reference self
*wait_list = waiter;
waiter->next = waiter;
waiter->prev = waiter;
} else {
// Add after the last element
Waiter *first = *wait_list;
Waiter *last = (*wait_list)->prev;
// Update new entry
waiter->next = first;
waiter->prev = last;
// Insert into the list
first->prev = waiter;
last->next = waiter;
}
waiter->in_list = true;
}
void ConditionVariable::_remove_wait_list(Waiter **wait_list, Waiter *waiter)
{
Waiter *prev = waiter->prev;
Waiter *next = waiter->next;
// Remove from list
prev->next = waiter->next;
next->prev = waiter->prev;
*wait_list = waiter->next;
if (*wait_list == waiter) {
// This was the last element in the list
*wait_list = NULL;
}
// Invalidate pointers
waiter->next = NULL;
waiter->prev = NULL;
waiter->in_list = false;
}
ConditionVariable::~ConditionVariable()
{
MBED_ASSERT(NULL == _wait_list);
}
}