Core networking libraries including LwIP implementation

Dependencies:   DebugLib lwip_ppp_ethernet

Dependents:   EthernetNetworkLib

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 
00029 #include "rtos.h"
00030 
00031 //Mutex protected circualr buffer
00032 template<typename T, int size>
00033 class MtxCircBuffer
00034 {
00035 public:
00036   MtxCircBuffer() //:
00037       //mtx()
00038   {
00039     write = 0;
00040     read = 0;
00041   }
00042 
00043   bool isFull()
00044   {
00045     mtx.lock();
00046     bool r = (((write + 1) % size) == read);
00047     mtx.unlock();
00048     return r;
00049   }
00050 
00051   bool isEmpty()
00052   {
00053     mtx.lock();
00054     bool r = (read == write);
00055     mtx.unlock();
00056     return r;
00057   }
00058 
00059   void queue(T k)
00060   {
00061     mtx.lock();
00062     while (((write + 1) % size) == read) //if (isFull())
00063     {
00064       /*while((((write + 1) % size) == read))
00065       {*/
00066         mtx.unlock();
00067         Thread::wait(10);
00068         mtx.lock();
00069       /*}*/
00070       //read++;
00071       //read %= size;
00072     }
00073     buf[write++] = k;
00074     write %= size;
00075     mtx.unlock();
00076   }
00077 
00078   uint16_t available()
00079   {
00080     mtx.lock();
00081     uint16_t a = (write >= read) ? (write - read) : (size - read + write);
00082     mtx.unlock();
00083     return a;
00084   }
00085 
00086   bool dequeue(T * c)
00087   {
00088     mtx.lock();
00089     bool empty = (read == write);
00090     if (!empty)
00091     {
00092       *c = buf[read++];
00093       read %= size;
00094     }
00095     mtx.unlock();
00096     return (!empty);
00097   }
00098 
00099 private:
00100   volatile uint16_t write;
00101   volatile uint16_t read;
00102   volatile T buf[size];
00103   Mutex mtx;
00104 };
00105 
00106 #endif
00107