Vodafone K3770/K3772-Z modems driver & networking library
This library is deprecated. Please use the newest production branch of the library from https://mbed.org/users/mbed_official/code/VodafoneUSBModem/
core/MtxCircBuffer.h@2:50930edb6eb3, 2012-11-01 (annotated)
- Committer:
- donatien
- Date:
- Thu Nov 01 09:57:11 2012 +0000
- Revision:
- 2:50930edb6eb3
- Parent:
- 0:bd5343539e38
Deprecated library, pointing out to new one
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bd5343539e38 | 1 | /* MtxCircBuf.h */ |
donatien | 0:bd5343539e38 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 0:bd5343539e38 | 3 | * |
donatien | 0:bd5343539e38 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 0:bd5343539e38 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 0:bd5343539e38 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 0:bd5343539e38 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 0:bd5343539e38 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 0:bd5343539e38 | 9 | * |
donatien | 0:bd5343539e38 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 0:bd5343539e38 | 11 | * substantial portions of the Software. |
donatien | 0:bd5343539e38 | 12 | * |
donatien | 0:bd5343539e38 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 0:bd5343539e38 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 0:bd5343539e38 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 0:bd5343539e38 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:bd5343539e38 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 0:bd5343539e38 | 18 | */ |
donatien | 0:bd5343539e38 | 19 | |
donatien | 0:bd5343539e38 | 20 | #ifndef MTXCIRCBUFFER_H |
donatien | 0:bd5343539e38 | 21 | #define MTXCIRCBUFFER_H |
donatien | 0:bd5343539e38 | 22 | |
donatien | 0:bd5343539e38 | 23 | #include "fwk.h" |
donatien | 0:bd5343539e38 | 24 | |
donatien | 0:bd5343539e38 | 25 | #include "rtos.h" |
donatien | 0:bd5343539e38 | 26 | |
donatien | 0:bd5343539e38 | 27 | //Mutex protected circualr buffer |
donatien | 0:bd5343539e38 | 28 | template<typename T, int size> |
donatien | 0:bd5343539e38 | 29 | class MtxCircBuffer |
donatien | 0:bd5343539e38 | 30 | { |
donatien | 0:bd5343539e38 | 31 | public: |
donatien | 0:bd5343539e38 | 32 | MtxCircBuffer() //: |
donatien | 0:bd5343539e38 | 33 | //mtx() |
donatien | 0:bd5343539e38 | 34 | { |
donatien | 0:bd5343539e38 | 35 | write = 0; |
donatien | 0:bd5343539e38 | 36 | read = 0; |
donatien | 0:bd5343539e38 | 37 | } |
donatien | 0:bd5343539e38 | 38 | |
donatien | 0:bd5343539e38 | 39 | bool isFull() |
donatien | 0:bd5343539e38 | 40 | { |
donatien | 0:bd5343539e38 | 41 | mtx.lock(); |
donatien | 0:bd5343539e38 | 42 | bool r = (((write + 1) % size) == read); |
donatien | 0:bd5343539e38 | 43 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 44 | return r; |
donatien | 0:bd5343539e38 | 45 | } |
donatien | 0:bd5343539e38 | 46 | |
donatien | 0:bd5343539e38 | 47 | bool isEmpty() |
donatien | 0:bd5343539e38 | 48 | { |
donatien | 0:bd5343539e38 | 49 | mtx.lock(); |
donatien | 0:bd5343539e38 | 50 | bool r = (read == write); |
donatien | 0:bd5343539e38 | 51 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 52 | return r; |
donatien | 0:bd5343539e38 | 53 | } |
donatien | 0:bd5343539e38 | 54 | |
donatien | 0:bd5343539e38 | 55 | void queue(T k) |
donatien | 0:bd5343539e38 | 56 | { |
donatien | 0:bd5343539e38 | 57 | mtx.lock(); |
donatien | 0:bd5343539e38 | 58 | while (((write + 1) % size) == read) //if (isFull()) |
donatien | 0:bd5343539e38 | 59 | { |
donatien | 0:bd5343539e38 | 60 | /*while((((write + 1) % size) == read)) |
donatien | 0:bd5343539e38 | 61 | {*/ |
donatien | 0:bd5343539e38 | 62 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 63 | Thread::wait(10); |
donatien | 0:bd5343539e38 | 64 | mtx.lock(); |
donatien | 0:bd5343539e38 | 65 | /*}*/ |
donatien | 0:bd5343539e38 | 66 | //read++; |
donatien | 0:bd5343539e38 | 67 | //read %= size; |
donatien | 0:bd5343539e38 | 68 | } |
donatien | 0:bd5343539e38 | 69 | buf[write++] = k; |
donatien | 0:bd5343539e38 | 70 | write %= size; |
donatien | 0:bd5343539e38 | 71 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 72 | } |
donatien | 0:bd5343539e38 | 73 | |
donatien | 0:bd5343539e38 | 74 | uint16_t available() |
donatien | 0:bd5343539e38 | 75 | { |
donatien | 0:bd5343539e38 | 76 | mtx.lock(); |
donatien | 0:bd5343539e38 | 77 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
donatien | 0:bd5343539e38 | 78 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 79 | return a; |
donatien | 0:bd5343539e38 | 80 | } |
donatien | 0:bd5343539e38 | 81 | |
donatien | 0:bd5343539e38 | 82 | bool dequeue(T * c) |
donatien | 0:bd5343539e38 | 83 | { |
donatien | 0:bd5343539e38 | 84 | mtx.lock(); |
donatien | 0:bd5343539e38 | 85 | bool empty = (read == write); |
donatien | 0:bd5343539e38 | 86 | if (!empty) |
donatien | 0:bd5343539e38 | 87 | { |
donatien | 0:bd5343539e38 | 88 | *c = buf[read++]; |
donatien | 0:bd5343539e38 | 89 | read %= size; |
donatien | 0:bd5343539e38 | 90 | } |
donatien | 0:bd5343539e38 | 91 | mtx.unlock(); |
donatien | 0:bd5343539e38 | 92 | return (!empty); |
donatien | 0:bd5343539e38 | 93 | } |
donatien | 0:bd5343539e38 | 94 | |
donatien | 0:bd5343539e38 | 95 | private: |
donatien | 0:bd5343539e38 | 96 | volatile uint16_t write; |
donatien | 0:bd5343539e38 | 97 | volatile uint16_t read; |
donatien | 0:bd5343539e38 | 98 | volatile T buf[size]; |
donatien | 0:bd5343539e38 | 99 | Mutex mtx; |
donatien | 0:bd5343539e38 | 100 | }; |
donatien | 0:bd5343539e38 | 101 | |
donatien | 0:bd5343539e38 | 102 | #endif |
donatien | 0:bd5343539e38 | 103 |