Base library for cellular modem implementations
Dependencies: Socket lwip-sys lwip
Dependents: CellularUSBModem CellularUSBModem
Deprecated
This is an mbed 2 networking library. For mbed 5, the networking libraries have been revised to better support additional network stacks and thread safety here.
core/MtxCircBuffer.h@8:944cd194963e, 2014-05-08 (annotated)
- Committer:
- mbed_official
- Date:
- Thu May 08 11:00:26 2014 +0100
- Revision:
- 8:944cd194963e
- Parent:
- 1:4a23efdf0da9
Synchronized with git revision df12bf01ac7dbb50751e2b16a351c894994e1dcf
Full URL: https://github.com/mbedmicro/mbed/commit/df12bf01ac7dbb50751e2b16a351c894994e1dcf/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 1:4a23efdf0da9 | 1 | /* MtxCircBuf.h */ |
bogdanm | 1:4a23efdf0da9 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
bogdanm | 1:4a23efdf0da9 | 3 | * |
bogdanm | 1:4a23efdf0da9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bogdanm | 1:4a23efdf0da9 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bogdanm | 1:4a23efdf0da9 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bogdanm | 1:4a23efdf0da9 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bogdanm | 1:4a23efdf0da9 | 8 | * furnished to do so, subject to the following conditions: |
bogdanm | 1:4a23efdf0da9 | 9 | * |
bogdanm | 1:4a23efdf0da9 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
bogdanm | 1:4a23efdf0da9 | 11 | * substantial portions of the Software. |
bogdanm | 1:4a23efdf0da9 | 12 | * |
bogdanm | 1:4a23efdf0da9 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bogdanm | 1:4a23efdf0da9 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bogdanm | 1:4a23efdf0da9 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bogdanm | 1:4a23efdf0da9 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bogdanm | 1:4a23efdf0da9 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bogdanm | 1:4a23efdf0da9 | 18 | */ |
bogdanm | 1:4a23efdf0da9 | 19 | |
bogdanm | 1:4a23efdf0da9 | 20 | #ifndef MTXCIRCBUFFER_H |
bogdanm | 1:4a23efdf0da9 | 21 | #define MTXCIRCBUFFER_H |
bogdanm | 1:4a23efdf0da9 | 22 | |
bogdanm | 1:4a23efdf0da9 | 23 | #include "rtos.h" |
bogdanm | 1:4a23efdf0da9 | 24 | |
bogdanm | 1:4a23efdf0da9 | 25 | //Mutex protected circualr buffer |
bogdanm | 1:4a23efdf0da9 | 26 | template<typename T, int size> |
bogdanm | 1:4a23efdf0da9 | 27 | class MtxCircBuffer |
bogdanm | 1:4a23efdf0da9 | 28 | { |
bogdanm | 1:4a23efdf0da9 | 29 | public: |
bogdanm | 1:4a23efdf0da9 | 30 | MtxCircBuffer() //: |
bogdanm | 1:4a23efdf0da9 | 31 | //mtx() |
bogdanm | 1:4a23efdf0da9 | 32 | { |
bogdanm | 1:4a23efdf0da9 | 33 | write = 0; |
bogdanm | 1:4a23efdf0da9 | 34 | read = 0; |
bogdanm | 1:4a23efdf0da9 | 35 | } |
bogdanm | 1:4a23efdf0da9 | 36 | |
bogdanm | 1:4a23efdf0da9 | 37 | bool isFull() |
bogdanm | 1:4a23efdf0da9 | 38 | { |
bogdanm | 1:4a23efdf0da9 | 39 | mtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 40 | bool r = (((write + 1) % size) == read); |
bogdanm | 1:4a23efdf0da9 | 41 | mtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 42 | return r; |
bogdanm | 1:4a23efdf0da9 | 43 | } |
bogdanm | 1:4a23efdf0da9 | 44 | |
bogdanm | 1:4a23efdf0da9 | 45 | bool isEmpty() |
bogdanm | 1:4a23efdf0da9 | 46 | { |
bogdanm | 1:4a23efdf0da9 | 47 | mtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 48 | bool r = (read == write); |
bogdanm | 1:4a23efdf0da9 | 49 | mtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 50 | return r; |
bogdanm | 1:4a23efdf0da9 | 51 | } |
bogdanm | 1:4a23efdf0da9 | 52 | |
bogdanm | 1:4a23efdf0da9 | 53 | void queue(T k) |
bogdanm | 1:4a23efdf0da9 | 54 | { |
bogdanm | 1:4a23efdf0da9 | 55 | mtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 56 | buf[write++] = k; |
bogdanm | 1:4a23efdf0da9 | 57 | write %= size; |
bogdanm | 1:4a23efdf0da9 | 58 | if (isFull()) |
bogdanm | 1:4a23efdf0da9 | 59 | { |
bogdanm | 1:4a23efdf0da9 | 60 | read++; |
bogdanm | 1:4a23efdf0da9 | 61 | read %= size; |
bogdanm | 1:4a23efdf0da9 | 62 | } |
bogdanm | 1:4a23efdf0da9 | 63 | mtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 64 | } |
bogdanm | 1:4a23efdf0da9 | 65 | |
bogdanm | 1:4a23efdf0da9 | 66 | uint16_t available() |
bogdanm | 1:4a23efdf0da9 | 67 | { |
bogdanm | 1:4a23efdf0da9 | 68 | mtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 69 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
bogdanm | 1:4a23efdf0da9 | 70 | mtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 71 | return a; |
bogdanm | 1:4a23efdf0da9 | 72 | } |
bogdanm | 1:4a23efdf0da9 | 73 | |
bogdanm | 1:4a23efdf0da9 | 74 | bool dequeue(T * c) |
bogdanm | 1:4a23efdf0da9 | 75 | { |
bogdanm | 1:4a23efdf0da9 | 76 | mtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 77 | bool empty = (read == write); |
bogdanm | 1:4a23efdf0da9 | 78 | if (!empty) |
bogdanm | 1:4a23efdf0da9 | 79 | { |
bogdanm | 1:4a23efdf0da9 | 80 | *c = buf[read++]; |
bogdanm | 1:4a23efdf0da9 | 81 | read %= size; |
bogdanm | 1:4a23efdf0da9 | 82 | } |
bogdanm | 1:4a23efdf0da9 | 83 | mtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 84 | return (!empty); |
bogdanm | 1:4a23efdf0da9 | 85 | } |
bogdanm | 1:4a23efdf0da9 | 86 | |
bogdanm | 1:4a23efdf0da9 | 87 | private: |
bogdanm | 1:4a23efdf0da9 | 88 | volatile uint16_t write; |
bogdanm | 1:4a23efdf0da9 | 89 | volatile uint16_t read; |
bogdanm | 1:4a23efdf0da9 | 90 | volatile T buf[size]; |
bogdanm | 1:4a23efdf0da9 | 91 | Mutex mtx; |
bogdanm | 1:4a23efdf0da9 | 92 | }; |
bogdanm | 1:4a23efdf0da9 | 93 | |
bogdanm | 1:4a23efdf0da9 | 94 | #endif |
bogdanm | 1:4a23efdf0da9 | 95 |