Merge Network code with webserver etc
[clinton/Smoothieware.git] / src / libs / RingBuffer.h
index 65a6d81..20c1128 100644 (file)
@@ -20,15 +20,17 @@ template<class kind, int length> class RingBuffer {
         int          prev_block_index(int index);
         void         push_back(kind object);
         void         pop_front(kind &object);
+        kind*        get_tail_ref();
         void         get( int index, kind &object);
         kind*        get_ref( int index);
         void         delete_first();
 
         kind         buffer[length];
-        int          head;
-        int          tail;
+        volatile int          head;
+        volatile int          tail;
 };
 
+#include "sLPC17xx.h"
 
 template<class kind, int length> RingBuffer<kind, length>::RingBuffer(){
     this->head = this->tail = 0;
@@ -39,7 +41,11 @@ template<class kind, int length>  int RingBuffer<kind, length>::capacity(){
 }
 
 template<class kind, int length>  int RingBuffer<kind, length>::size(){
-return((this->head>this->tail)?length:0)+this->tail-head;
+       //return((this->head>this->tail)?length:0)+this->tail-head;
+       __disable_irq();
+       int i = tail - head + ((head > tail)?length:0);
+       __enable_irq();
+       return i;
 }
 
 template<class kind, int length> int RingBuffer<kind, length>::next_block_index(int index){
@@ -59,6 +65,10 @@ template<class kind, int length> void RingBuffer<kind, length>::push_back(kind o
     this->tail = (tail+1)&(length-1);
 }
 
+template<class kind, int length> kind* RingBuffer<kind, length>::get_tail_ref(){
+    return &(buffer[tail]);
+}
+
 template<class kind, int length> void RingBuffer<kind, length>::get(int index, kind &object){
     int j= 0;
     int k= this->head;