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