Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 "fwk.h" 00024 00025 #include "rtos.h" 00026 00027 //Mutex protected circualr buffer 00028 template<typename T, int size> 00029 class MtxCircBuffer 00030 { 00031 public: 00032 MtxCircBuffer() //: 00033 //mtx() 00034 { 00035 write = 0; 00036 read = 0; 00037 } 00038 00039 bool isFull() 00040 { 00041 mtx.lock(); 00042 bool r = (((write + 1) % size) == read); 00043 mtx.unlock(); 00044 return r; 00045 } 00046 00047 bool isEmpty() 00048 { 00049 mtx.lock(); 00050 bool r = (read == write); 00051 mtx.unlock(); 00052 return r; 00053 } 00054 00055 void queue(T k) 00056 { 00057 mtx.lock(); 00058 while (((write + 1) % size) == read) //if (isFull()) 00059 { 00060 /*while((((write + 1) % size) == read)) 00061 {*/ 00062 mtx.unlock(); 00063 Thread::wait(10); 00064 mtx.lock(); 00065 /*}*/ 00066 //read++; 00067 //read %= size; 00068 } 00069 buf[write++] = k; 00070 write %= size; 00071 mtx.unlock(); 00072 } 00073 00074 uint16_t available() 00075 { 00076 mtx.lock(); 00077 uint16_t a = (write >= read) ? (write - read) : (size - read + write); 00078 mtx.unlock(); 00079 return a; 00080 } 00081 00082 bool dequeue(T * c) 00083 { 00084 mtx.lock(); 00085 bool empty = (read == write); 00086 if (!empty) 00087 { 00088 *c = buf[read++]; 00089 read %= size; 00090 } 00091 mtx.unlock(); 00092 return (!empty); 00093 } 00094 00095 private: 00096 volatile uint16_t write; 00097 volatile uint16_t read; 00098 volatile T buf[size]; 00099 Mutex mtx; 00100 }; 00101 00102 #endif 00103
Generated on Tue Jul 12 2022 17:44:10 by
1.7.2