|
14 | 14 | typedef volatile uint32_t* position_ptr_t; |
15 | 15 | typedef volatile uint32_t position_t; |
16 | 16 | inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) { |
17 | | - return core_util_atomic_cas_u32(ptr, &expected, newVal); |
| 17 | + uint32_t exp = expected; |
| 18 | + return core_util_atomic_cas_u32(ptr, &exp, newVal); |
18 | 19 | } |
19 | 20 | inline uint16_t readAtomic(position_ptr_t ptr) { return *(ptr); } |
20 | 21 | #elif defined(ESP8266) |
@@ -56,31 +57,41 @@ inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal |
56 | 57 | inline uint16_t readAtomic(position_ptr_t ptr) { return *ptr; } |
57 | 58 | #endif |
58 | 59 |
|
| 60 | +namespace tccollection { |
| 61 | + |
59 | 62 | /** |
60 | 63 | * Create a circular buffer that has a fixed size and can be filled and read at the same time one character at a time. |
61 | | - * It is very thread safe on ESP32 and mbed based boards, it is moderately thread safe on other boards. |
| 64 | + * It is very thread safe on ESP32 and mbed based boards, it is thread safe on other boards, unless you are using that |
| 65 | + * board with an unexpected RTOS, in which case you would need to determine suitability yourself. |
62 | 66 | * |
63 | 67 | * Imagine the buffer like a circle with a given number of segments, both the reader and writer pointers start at 0. |
64 | 68 | * As the data structure fills up the writer will move around, and the reader can try and "keep up". Once the reader |
65 | 69 | * or writer gets to the end it will go back to 0 (start). This is by design and therefore, IT'S POSSIBLE TO LOSE DATA. |
66 | 70 | */ |
67 | | -class CircularBuffer { |
68 | | -private: |
69 | | - position_t readerPosition; |
70 | | - position_t writerPosition; |
71 | | - const uint16_t bufferSize; |
72 | | - uint8_t *const buffer; |
73 | | - |
74 | | -public: |
75 | | - explicit CircularBuffer(uint16_t size); |
76 | | - ~CircularBuffer(); |
77 | | - |
78 | | - bool available() const { return readerPosition != writerPosition; } |
79 | | - void put(uint8_t by) { buffer[nextPosition(&writerPosition)] = by;} |
80 | | - uint8_t get() { return buffer[nextPosition(&readerPosition)]; } |
81 | | -private: |
82 | | - uint16_t nextPosition(position_ptr_t by); |
83 | | -}; |
| 71 | + class SCCircularBuffer { |
| 72 | + private: |
| 73 | + position_t readerPosition; |
| 74 | + position_t writerPosition; |
| 75 | + const uint16_t bufferSize; |
| 76 | + uint8_t *const buffer; |
| 77 | + |
| 78 | + public: |
| 79 | + explicit SCCircularBuffer(uint16_t size); |
| 80 | + |
| 81 | + ~SCCircularBuffer(); |
| 82 | + |
| 83 | + bool available() const { return readerPosition != writerPosition; } |
| 84 | + |
| 85 | + void put(uint8_t by) { buffer[nextPosition(&writerPosition)] = by; } |
| 86 | + |
| 87 | + uint8_t get() { return buffer[nextPosition(&readerPosition)]; } |
| 88 | + |
| 89 | + private: |
| 90 | + uint16_t nextPosition(position_ptr_t by); |
| 91 | + }; |
| 92 | + |
| 93 | +} |
| 94 | + |
84 | 95 |
|
85 | 96 |
|
86 | 97 | #endif //SIMPLECOLLECTIONS_CIRCULARBUFFER_H |
0 commit comments