Skip to content

Commit 5d7c8e5

Browse files
committed
#9 support for earlephilhower pico core.
1 parent cd79edf commit 5d7c8e5

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/SCCircularBuffer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
using namespace tccollection;
99

10-
SCCircularBuffer::SCCircularBuffer(uint16_t size) : readerPosition(0), writerPosition(0), bufferSize(size), buffer(new uint8_t[size]) {}
10+
SCCircularBuffer::SCCircularBuffer(uint16_t size) : readerPosition(0), writerPosition(0), bufferSize(size), buffer(new uint8_t[size]) {
11+
atomicInitialisationSupport();
12+
}
1113

1214
SCCircularBuffer::~SCCircularBuffer() {
1315
delete[] buffer;

src/SCThreadingSupport.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,45 @@ bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
4646
}
4747

4848
#endif
49+
50+
#ifdef SIMPLECOLLECTIONS_PICO_PHT_SUPPORT
51+
52+
// see https://raspberrypi.github.io/pico-sdk-doxygen/group__critical__section.html
53+
54+
class CircularBufferProtector {
55+
private:
56+
static critical_section_t* circularBufferProtection;
57+
public:
58+
static void initIfNeeded() {
59+
if(circularBufferProtection == nullptr) {
60+
circularBufferProtection = new critical_section_t;
61+
critical_section_init(circularBufferProtection);
62+
}
63+
}
64+
65+
CircularBufferProtector() {
66+
critical_section_enter_blocking(circularBufferProtection);
67+
}
68+
69+
~CircularBufferProtector() {
70+
critical_section_exit(circularBufferProtection);
71+
}
72+
};
73+
74+
critical_section_t* CircularBufferProtector::circularBufferProtection = nullptr;
75+
76+
bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
77+
CircularBufferProtector protector;
78+
auto ret = false;
79+
if(*ptr == expected) {
80+
*ptr = newVal;
81+
ret = true;
82+
}
83+
return ret;
84+
}
85+
86+
void atomicInitialisationSupport() {
87+
CircularBufferProtector::initIfNeeded();
88+
}
89+
90+
#endif

0 commit comments

Comments
 (0)