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