-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathReadWrite.cpp
executable file
·223 lines (200 loc) · 6.86 KB
/
ReadWrite.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @file ReadWrite.cpp
* @brief FreeRTOS Read/Write Lock Wrapper
*
* This file contains a set of lightweight wrappers for mutexes using FreeRTOS
*
* @copyright (c) 2007-2024 Richard Damon
* @author Richard Damon <[email protected]>
* @parblock
* MIT License:
*
* 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.
*
* It is requested (but not required by license) that any bugs found or
* improvements made be shared, preferably to the author.
* @endparblock
*
* @ingroup FreeRTOSCpp
*/
#include <ReadWrite.h>
#if FREERTOSCPP_USE_NAMESPACE
namespace FreeRTOScpp {
#endif
/*
Different events we can be waiting for.
Right now minimal, might be able to expand bits so less tasks get woken prematurely
*/
constexpr unsigned read_bit = 1 << 0;
constexpr unsigned write_bit = 1 << 1;
bool Reader::take(TickType_t wait) {
return static_cast<ReadWriteLock*>(this)->readLock(wait);
}
bool Reader::give() {
return static_cast<ReadWriteLock*>(this)->readUnlock();
}
bool Writer::take(TickType_t wait) {
return static_cast<ReadWriteLock*>(this)->writeLock(wait);
}
bool Writer::give() {
return static_cast<ReadWriteLock*>(this)->writeUnlock();
}
ReadWriteLock::ReadWriteLock() {
}
ReadWriteLock::~ReadWriteLock() {
}
bool ReadWriteLock::readLock(TickType_t wait) {
TickType_t start = xTaskGetTickCount();
while(1) {
taskENTER_CRITICAL();
if(readCount >= 0 && (int)uxTaskPriorityGet(nullptr) > writeReq) {
// Lock is granted, record that.
readCount++;
taskEXIT_CRITICAL();
return true;
}
taskEXIT_CRITICAL();
TickType_t now = xTaskGetTickCount();
if(now-start >= wait) {
// Timed out.
return false;
}
event.wait(read_bit, true, true, 1);
}
}
bool ReadWriteLock::reservedLock(TickType_t wait) {
TickType_t start = xTaskGetTickCount();
TaskHandle_t task = xTaskGetCurrentTaskHandle();
while(1) {
taskENTER_CRITICAL();
if(readCount >= 0 && reserved == nullptr && (int)uxTaskPriorityGet(nullptr) > writeReq) {
// Lock is granted, record that, reserve our ability to promote
readCount++;
reserved = task;
taskEXIT_CRITICAL();
return true;
}
taskEXIT_CRITICAL();
TickType_t now = xTaskGetTickCount();
if(now-start >= wait) {
// Timed out.
return false;
}
event.wait(read_bit, true, true, 1);
}
}
bool ReadWriteLock::requestReserved() {
TaskHandle_t task = xTaskGetCurrentTaskHandle();
bool flag = false;
taskENTER_CRITICAL();
if( readCount > 0 && reserved == nullptr) {
reserved = task;
flag = true;
}
taskEXIT_CRITICAL();
return flag;
}
bool ReadWriteLock::releaseReserved() {
// Don't need a critical, as no one with change it to us
if(xTaskGetCurrentTaskHandle() == reserved) {
reserved = nullptr;
// Signal read event as another task may be waiting on it. This might be able to be a different bit.
event.set(read_bit);
return true;
}
return false;
}
bool ReadWriteLock::readUnlock() {
bool ret = true;
// IF we had the reservation, clear it.
if(xTaskGetCurrentTaskHandle() == reserved) {
reserved = nullptr;
// Signal read event as another task may be waiting on it. This might be able to be a different bit.
event.set(read_bit);
}
taskENTER_CRITICAL();
if (readCount > 0) {
readCount--;
} else {
// something is wrong with the unlock, as we aren't locked.
ret = false;
}
if (readCount == 0) {
reserved = nullptr; // just for safety.
}
taskEXIT_CRITICAL();
// Can be outside critical as false positive doesn't really hurt anything here.
if (readCount == 0) {
event.set(write_bit);
}
return ret;
}
bool ReadWriteLock::writeLock(TickType_t wait) {
TaskHandle_t task = xTaskGetCurrentTaskHandle();
TickType_t start = xTaskGetTickCount();
int priority = uxTaskPriorityGet(nullptr);
while(1) {
taskENTER_CRITICAL();
if (0 <= readCount && readCount <= (reserved == task)) {
readCount = -1;
writeReq = -1;
taskEXIT_CRITICAL();
return true;
}
taskEXIT_CRITICAL();
TickType_t now = xTaskGetTickCount();
if(now-start >= wait) {
// clear writeReq if it might have been us.
taskENTER_CRITICAL();
if (writeReq == priority) {
writeReq = -1;
taskEXIT_CRITICAL();
// We may have been blocking a reader, or removed another writers request.
event.set(read_bit|write_bit);
} else {
taskEXIT_CRITICAL();
}
return false;
}
taskENTER_CRITICAL();
// Update request priority if we are higher
if (writeReq < priority) {
writeReq = priority;
}
taskEXIT_CRITICAL();
event.wait(write_bit, true, true, 1);
}
}
bool ReadWriteLock::writeUnlock() {
// Assume we had the write lock until proven otherwise
// no one else touches things if readCount is -1
if (readCount >= 0) return false; // bad call
if (reserved == xTaskGetCurrentTaskHandle()) {
// We were reserved, convert the write lock to our previous reserved lock
readCount = 1;
} else {
readCount = 0;
}
// Let other readers in, and notify writers so they can add their requests
event.set(read_bit|write_bit);
return true;
}
#if FREERTOSCPP_USE_NAMESPACE
}
#endif