cc y / mbed

Fork of mbed by mbed official

Revision:
122:f9eeca106725
Parent:
101:7cff1c4259d7
--- a/CircularBuffer.h	Wed May 25 16:44:06 2016 +0100
+++ b/CircularBuffer.h	Thu Jul 07 14:34:11 2016 +0100
@@ -16,9 +16,13 @@
 #ifndef MBED_CIRCULARBUFFER_H
 #define MBED_CIRCULARBUFFER_H
 
+#include "critical.h"
+
 namespace mbed {
 
 /** Templated Circular buffer class
+ *
+ *  @Note Synchronization level: Interrupt safe
  */
 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
 class CircularBuffer {
@@ -35,6 +39,7 @@
      * @param data Data to be pushed to the buffer
      */
     void push(const T& data) {
+        core_util_critical_section_enter();
         if (full()) {
             _tail++;
             _tail %= BufferSize;
@@ -44,6 +49,7 @@
         if (_head == _tail) {
             _full = true;
         }
+        core_util_critical_section_exit();
     }
 
     /** Pop the transaction from the buffer
@@ -52,13 +58,16 @@
      * @return True if the buffer is not empty and data contains a transaction, false otherwise
      */
     bool pop(T& data) {
+        bool data_popped = false;
+        core_util_critical_section_enter();
         if (!empty()) {
             data = _pool[_tail++];
             _tail %= BufferSize;
             _full = false;
-            return true;
+            data_popped = true;
         }
-        return false;
+        core_util_critical_section_exit();
+        return data_popped;
     }
 
     /** Check if the buffer is empty
@@ -66,7 +75,10 @@
      * @return True if the buffer is empty, false if not
      */
     bool empty() {
-        return (_head == _tail) && !_full;
+        core_util_critical_section_enter();
+        bool is_empty = (_head == _tail) && !_full;
+        core_util_critical_section_exit();
+        return is_empty;
     }
 
     /** Check if the buffer is full
@@ -74,16 +86,21 @@
      * @return True if the buffer is full, false if not
      */
     bool full() {
-        return _full;
+        core_util_critical_section_enter();
+        bool full = _full;
+        core_util_critical_section_exit();
+        return full;
     }
 
     /** Reset the buffer
      *
      */
     void reset() {
+        core_util_critical_section_enter();
         _head = 0;
         _tail = 0;
         _full = false;
+        core_util_critical_section_exit();
     }
 
 private: