Skip to content

Commit

Permalink
Safer interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
Koryphon committed Apr 26, 2018
1 parent 13328c2 commit 1d82fc0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/BufferInterrupt/BufferInterrupt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#include <RingBuffer.h>

RingBuffer<uint32_t, 10> comBuffer;
RingBuffer<uint32_t, 100> comBuffer;

void externalInterruptHandler()
{
Expand Down
26 changes: 9 additions & 17 deletions src/RingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class RingBuffer

public:
RingBuffer();
bool push(const rg_element_t inElement);
bool push(const rg_element_t * const inElement);
bool push(const rg_element_t inElement) __attribute__ ((noinline));
bool push(const rg_element_t * const inElement) __attribute__ ((noinline));
bool lockedPush(const rg_element_t inElement);
bool lockedPush(const rg_element_t * const inElement);
bool pop(rg_element_t &outElement);
bool pop(rg_element_t &outElement) __attribute__ ((noinline));
bool lockedPop(rg_element_t &outElement);
bool isFull() { return mSize == __maxSize__; }
bool isEmpty() { return mSize == 0; }
Expand Down Expand Up @@ -83,23 +83,19 @@ bool RingBuffer<rg_element_t, __maxSize__>::push(const rg_element_t * const inEl
template <typename rg_element_t, uint8_t __maxSize__>
bool RingBuffer<rg_element_t, __maxSize__>::lockedPush(const rg_element_t inElement)
{
if (isFull()) return false;
noInterrupts();
mBuffer[writeIndex()] = inElement;
mSize++;
bool result = push(inElement);
interrupts();
return true;
return result;
}

template <typename rg_element_t, uint8_t __maxSize__>
bool RingBuffer<rg_element_t, __maxSize__>::lockedPush(const rg_element_t * const inElement)
{
if (isFull()) return false;
noInterrupts();
mBuffer[writeIndex()] = *inElement;
mSize++;
bool result = push(inElement);
interrupts();
return true;
return result;
}

template <typename rg_element_t, uint8_t __maxSize__>
Expand All @@ -116,14 +112,10 @@ bool RingBuffer<rg_element_t, __maxSize__>::pop(rg_element_t &outElement)
template <typename rg_element_t, uint8_t __maxSize__>
bool RingBuffer<rg_element_t, __maxSize__>::lockedPop(rg_element_t &outElement)
{
if (isEmpty()) return false;
noInterrupts();
outElement = mBuffer[mReadIndex];
mReadIndex++;
mSize--;
if (mReadIndex == __maxSize__) mReadIndex = 0;
bool result = pop(outElement);
interrupts();
return true;
return result;
}

template <typename rg_element_t, uint8_t __maxSize__>
Expand Down

0 comments on commit 1d82fc0

Please sign in to comment.