General purpose ring buffer library. RTOS proof, mutex protected.

Dependents:   RingBuffer_RTOS_test

Fork of RingBuffer by Yoji KURODA

Files at this revision

API Documentation at this revision

Comitter:
ykuroda
Date:
Thu Sep 27 13:31:29 2012 +0000
Parent:
2:ea6d02ba96ae
Commit message:
RTOS support. Mutex protected edition.

Changed in this revision

RingBuffer.cpp Show annotated file Show diff for this revision Revisions of this file
RingBuffer.h Show annotated file Show diff for this revision Revisions of this file
--- a/RingBuffer.cpp	Thu Sep 27 13:15:54 2012 +0000
+++ b/RingBuffer.cpp	Thu Sep 27 13:31:29 2012 +0000
@@ -30,17 +30,21 @@
 int
 RingBuffer::save(unsigned char c)
 {
+    mutex.lock();
     if( (ep==sp-1)||
         ((sp==buf)&&
-            (ep==buf+bufsize-1)) )    /* buffer full */
+            (ep==buf+bufsize-1)) ) {    /* buffer full */
+        mutex.unlock();
         return 0;
-
+    }
+    
     *ep = c;
     ep++;
 
     if(ep > buf+bufsize)
         ep = buf;
 
+    mutex.unlock();
     return 1;
 }
 
@@ -49,9 +53,11 @@
 {
     unsigned char ret;
 
-    if(sp == ep)
+    mutex.lock();
+    if(sp == ep){
+        mutex.unlock();
         return 0;    /* buffer empty */
-
+    }
     ret = *sp;
     *sp = 0;
     sp++;
@@ -59,16 +65,19 @@
     if(sp > buf+bufsize)
         sp = buf;
 
+    mutex.unlock();
     return ret;
 }
 
 int
 RingBuffer::check(void)
 {
+    mutex.lock();
     int n = ep-sp;
     if(n<0)
         n = bufsize-n;
 
+    mutex.unlock();
     return n;
 }
 
--- a/RingBuffer.h	Thu Sep 27 13:15:54 2012 +0000
+++ b/RingBuffer.h	Thu Sep 27 13:31:29 2012 +0000
@@ -5,18 +5,23 @@
 //
 //  2009.11.13 ... Originally written in C by Y.Kuroda for Renesas H83664
 //  2012.08.31 ... Code convert for mbed in C++
-//
+//  2012.09.26 ... Pointer use
+//  2012.09.27 ... RTOS proof
 
 #ifndef _RINGBUFFER_H
 #define _RINGBUFFER_H
 
+
+#include "rtos.h"
+
+
 class RingBuffer {
 
   protected:
     unsigned char* buf;
     unsigned char* sp;
     unsigned char* ep;
-
+    Mutex mutex;
 
     int bufsize;