FreeRTOS Addons
Loading...
Searching...
No Matches
read_write_lock.hpp
1/****************************************************************************
2 *
3 * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4 *
5 * This file is part of the FreeRTOS Add-ons project.
6 *
7 * Source Code:
8 * https://github.com/michaelbecker/freertos-addons
9 *
10 * Project Page:
11 * http://michaelbecker.github.io/freertos-addons/
12 *
13 * On-line Documentation:
14 * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files
18 * (the "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so,subject to the
22 * following conditions:
23 *
24 * + The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
26 * + Credit is appreciated, but not required, if you find this project
27 * useful enough to include in your application, product, device, etc.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 ***************************************************************************/
38
39
40#include "freertos-config.h"
41
42#ifndef READ_WRITE_LOCK_HPP_
43#define READ_WRITE_LOCK_HPP_
44
45
53#ifndef CPP_FREERTOS_NO_EXCEPTIONS
54#include <exception>
55#include <string>
56#include <cstdio>
57#ifdef CPP_FREERTOS_NO_CPP_STRINGS
58#error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
59#endif
60#endif
61#include "FreeRTOS.h"
62#include "semphr.h"
63
64
65namespace cpp_freertos {
66
67
68#ifndef CPP_FREERTOS_NO_EXCEPTIONS
72class ReadWriteLockCreateException : public std::exception {
73
74 public:
79 {
80 sprintf(errorString, "ReadWriteLock Constructor Failed");
81 }
82
87 virtual const char *what() const throw()
88 {
89 return errorString;
90 }
91
92 private:
96 char errorString[80];
97};
98#endif
99
100
113
115 //
116 // Public API
117 //
119 public:
126
130 virtual ~ReadWriteLock();
131
136 virtual void ReaderLock() = 0;
137
141 virtual void ReaderUnlock() = 0;
142
147 virtual void WriterLock() = 0;
148
152 virtual void WriterUnlock() = 0;
153
155 //
156 // Protected API
157 // Not intended for use by application code.
158 //
160 protected:
165
169 SemaphoreHandle_t ReadLock;
170
175 SemaphoreHandle_t ResourceLock;
176};
177
178
185
187 //
188 // Public API
189 //
191 public:
196 virtual void ReaderLock();
197
201 virtual void ReaderUnlock();
202
207 virtual void WriterLock();
208
212 virtual void WriterUnlock();
213};
214
215
222
224 //
225 // Public API
226 //
228 public:
233
238
243 virtual void ReaderLock();
244
248 virtual void ReaderUnlock();
249
254 virtual void WriterLock();
255
259 virtual void WriterUnlock();
260
262 //
263 // Private API
264 // The internals of this wrapper class.
265 //
267 private:
272 int WriteCount;
273
277 SemaphoreHandle_t WriteLock;
278
282 SemaphoreHandle_t BlockReadersLock;
283};
284
285
286}
287#endif
Definition: read_write_lock.hpp:72
virtual const char * what() const
Definition: read_write_lock.hpp:87
ReadWriteLockCreateException()
Definition: read_write_lock.hpp:78
Definition: read_write_lock.hpp:112
SemaphoreHandle_t ResourceLock
Definition: read_write_lock.hpp:175
virtual ~ReadWriteLock()
Definition: cread_write_lock.cpp:82
int ReadCount
Definition: read_write_lock.hpp:164
virtual void WriterLock()=0
virtual void ReaderUnlock()=0
ReadWriteLock()
Definition: cread_write_lock.cpp:46
virtual void WriterUnlock()=0
SemaphoreHandle_t ReadLock
Definition: read_write_lock.hpp:169
virtual void ReaderLock()=0
Definition: read_write_lock.hpp:184
virtual void WriterLock()
Definition: cread_write_lock.cpp:115
virtual void ReaderLock()
Definition: cread_write_lock.cpp:89
virtual void ReaderUnlock()
Definition: cread_write_lock.cpp:102
virtual void WriterUnlock()
Definition: cread_write_lock.cpp:121
Definition: read_write_lock.hpp:221
ReadWriteLockPreferWriter()
Definition: cread_write_lock.cpp:127
virtual void ReaderUnlock()
Definition: cread_write_lock.cpp:186
virtual void ReaderLock()
Definition: cread_write_lock.cpp:171
virtual ~ReadWriteLockPreferWriter()
Definition: cread_write_lock.cpp:164
virtual void WriterUnlock()
Definition: cread_write_lock.cpp:214
virtual void WriterLock()
Definition: cread_write_lock.cpp:199
Definition: condition_variable.hpp:57