Nicholas Herriot / VodafoneK3770Lib
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MtxCircBuffer.h Source File

MtxCircBuffer.h

00001 /* MtxCircBuf.h */
00002 /*
00003  Copyright (C) 2012 ARM Limited.
00004 
00005  Permission is hereby granted, free of charge, to any person obtaining a copy of
00006  this software and associated documentation files (the "Software"), to deal in
00007  the Software without restriction, including without limitation the rights to
00008  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
00009  of the Software, and to permit persons to whom the Software is furnished to do
00010  so, subject to the following conditions:
00011 
00012  The above copyright notice and this permission notice shall be included in all
00013  copies or substantial portions of the Software.
00014 
00015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021  SOFTWARE.
00022  */
00023 
00024 #ifndef MTXCIRCBUFFER_H
00025 #define MTXCIRCBUFFER_H
00026 
00027 //#include "fwk.h"
00028 #include "stdint.h"
00029 
00030 #include "rtos.h"
00031 
00032 //Mutex protected circualr buffer
00033 template<typename T, int size>
00034 class MtxCircBuffer
00035 {
00036 public:
00037   MtxCircBuffer() //:
00038       //mtx()
00039   {
00040     write = 0;
00041     read = 0;
00042   }
00043 
00044   bool isFull()
00045   {
00046     mtx.lock();
00047     bool r = (((write + 1) % size) == read);
00048     mtx.unlock();
00049     return r;
00050   }
00051 
00052   bool isEmpty()
00053   {
00054     mtx.lock();
00055     bool r = (read == write);
00056     mtx.unlock();
00057     return r;
00058   }
00059 
00060   void queue(T k)
00061   {
00062     mtx.lock();
00063     while (((write + 1) % size) == read) //if (isFull())
00064     {
00065       /*while((((write + 1) % size) == read))
00066       {*/
00067         mtx.unlock();
00068         Thread::wait(10);
00069         mtx.lock();
00070       /*}*/
00071       //read++;
00072       //read %= size;
00073     }
00074     buf[write++] = k;
00075     write %= size;
00076     mtx.unlock();
00077   }
00078 
00079   uint16_t available()
00080   {
00081     mtx.lock();
00082     uint16_t a = (write >= read) ? (write - read) : (size - read + write);
00083     mtx.unlock();
00084     return a;
00085   }
00086 
00087   bool dequeue(T * c)
00088   {
00089     mtx.lock();
00090     bool empty = (read == write);
00091     if (!empty)
00092     {
00093       *c = buf[read++];
00094       read %= size;
00095     }
00096     mtx.unlock();
00097     return (!empty);
00098   }
00099 
00100 private:
00101   volatile uint16_t write;
00102   volatile uint16_t read;
00103   volatile T buf[size];
00104   Mutex mtx;
00105 };
00106 
00107 #endif
00108