demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
Utils/SafeCircBuf.h@33:8b9dcbf6d8ec, 2016-02-04 (annotated)
- Committer:
- henryrawas
- Date:
- Thu Feb 04 20:10:33 2016 +0000
- Revision:
- 33:8b9dcbf6d8ec
- Parent:
- 19:2f0ec9ac1238
update libs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryrawas | 19:2f0ec9ac1238 | 1 | // Copyright (c) Microsoft. All rights reserved. |
henryrawas | 19:2f0ec9ac1238 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
henryrawas | 4:36a4eceb1b7f | 3 | |
henryrawas | 4:36a4eceb1b7f | 4 | #ifndef __SAFECIRCBUF_H__ |
henryrawas | 4:36a4eceb1b7f | 5 | #define __SAFECIRCBUF_H__ |
henryrawas | 4:36a4eceb1b7f | 6 | |
henryrawas | 4:36a4eceb1b7f | 7 | #include "mbed.h" |
henryrawas | 4:36a4eceb1b7f | 8 | #include "rtos.h" |
henryrawas | 4:36a4eceb1b7f | 9 | |
henryrawas | 4:36a4eceb1b7f | 10 | #include <CircularBuffer.h> |
henryrawas | 4:36a4eceb1b7f | 11 | |
henryrawas | 4:36a4eceb1b7f | 12 | template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
henryrawas | 4:36a4eceb1b7f | 13 | class SafeCircBuf |
henryrawas | 4:36a4eceb1b7f | 14 | { |
henryrawas | 4:36a4eceb1b7f | 15 | public: |
henryrawas | 4:36a4eceb1b7f | 16 | SafeCircBuf() { |
henryrawas | 4:36a4eceb1b7f | 17 | } |
henryrawas | 4:36a4eceb1b7f | 18 | |
henryrawas | 4:36a4eceb1b7f | 19 | ~SafeCircBuf() { |
henryrawas | 4:36a4eceb1b7f | 20 | } |
henryrawas | 4:36a4eceb1b7f | 21 | |
henryrawas | 4:36a4eceb1b7f | 22 | /** Push the transaction to the buffer. This overwrites the buffer if it's |
henryrawas | 4:36a4eceb1b7f | 23 | * full |
henryrawas | 4:36a4eceb1b7f | 24 | * |
henryrawas | 4:36a4eceb1b7f | 25 | * @param data Data to be pushed to the buffer |
henryrawas | 4:36a4eceb1b7f | 26 | */ |
henryrawas | 4:36a4eceb1b7f | 27 | void push(const T& data) { |
henryrawas | 4:36a4eceb1b7f | 28 | _mutex.lock(); |
henryrawas | 4:36a4eceb1b7f | 29 | _circBuf.push(data); |
henryrawas | 4:36a4eceb1b7f | 30 | _mutex.unlock(); |
henryrawas | 4:36a4eceb1b7f | 31 | } |
henryrawas | 4:36a4eceb1b7f | 32 | |
henryrawas | 4:36a4eceb1b7f | 33 | /** Pop the transaction from the buffer |
henryrawas | 4:36a4eceb1b7f | 34 | * |
henryrawas | 4:36a4eceb1b7f | 35 | * @param data Data to be pushed to the buffer |
henryrawas | 4:36a4eceb1b7f | 36 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
henryrawas | 4:36a4eceb1b7f | 37 | */ |
henryrawas | 4:36a4eceb1b7f | 38 | bool pop(T& data) { |
henryrawas | 4:36a4eceb1b7f | 39 | bool rc; |
henryrawas | 4:36a4eceb1b7f | 40 | _mutex.lock(); |
henryrawas | 4:36a4eceb1b7f | 41 | rc = _circBuf.pop(data); |
henryrawas | 4:36a4eceb1b7f | 42 | _mutex.unlock(); |
henryrawas | 4:36a4eceb1b7f | 43 | return rc; |
henryrawas | 4:36a4eceb1b7f | 44 | } |
henryrawas | 4:36a4eceb1b7f | 45 | |
henryrawas | 4:36a4eceb1b7f | 46 | /** Check if the buffer is empty |
henryrawas | 4:36a4eceb1b7f | 47 | * |
henryrawas | 4:36a4eceb1b7f | 48 | * @return True if the buffer is empty, false if not |
henryrawas | 4:36a4eceb1b7f | 49 | */ |
henryrawas | 4:36a4eceb1b7f | 50 | bool empty() { |
henryrawas | 4:36a4eceb1b7f | 51 | bool rc; |
henryrawas | 4:36a4eceb1b7f | 52 | _mutex.lock(); |
henryrawas | 4:36a4eceb1b7f | 53 | rc = _circBuf.empty(); |
henryrawas | 4:36a4eceb1b7f | 54 | _mutex.unlock(); |
henryrawas | 4:36a4eceb1b7f | 55 | return rc; |
henryrawas | 4:36a4eceb1b7f | 56 | } |
henryrawas | 4:36a4eceb1b7f | 57 | |
henryrawas | 4:36a4eceb1b7f | 58 | /** Check if the buffer is full |
henryrawas | 4:36a4eceb1b7f | 59 | * |
henryrawas | 4:36a4eceb1b7f | 60 | * @return True if the buffer is full, false if not |
henryrawas | 4:36a4eceb1b7f | 61 | */ |
henryrawas | 4:36a4eceb1b7f | 62 | bool full() { |
henryrawas | 4:36a4eceb1b7f | 63 | bool rc; |
henryrawas | 4:36a4eceb1b7f | 64 | _mutex.lock(); |
henryrawas | 4:36a4eceb1b7f | 65 | rc = _circBuf.full(); |
henryrawas | 4:36a4eceb1b7f | 66 | _mutex.unlock(); |
henryrawas | 4:36a4eceb1b7f | 67 | return rc; |
henryrawas | 4:36a4eceb1b7f | 68 | } |
henryrawas | 4:36a4eceb1b7f | 69 | |
henryrawas | 4:36a4eceb1b7f | 70 | /** Reset the buffer |
henryrawas | 4:36a4eceb1b7f | 71 | * |
henryrawas | 4:36a4eceb1b7f | 72 | */ |
henryrawas | 4:36a4eceb1b7f | 73 | void reset() { |
henryrawas | 4:36a4eceb1b7f | 74 | _mutex.lock(); |
henryrawas | 4:36a4eceb1b7f | 75 | _circBuf.reset(); |
henryrawas | 4:36a4eceb1b7f | 76 | _mutex.unlock(); |
henryrawas | 4:36a4eceb1b7f | 77 | } |
henryrawas | 4:36a4eceb1b7f | 78 | |
henryrawas | 4:36a4eceb1b7f | 79 | private: |
henryrawas | 4:36a4eceb1b7f | 80 | Mutex _mutex; |
henryrawas | 4:36a4eceb1b7f | 81 | CircularBuffer<T, BufferSize, CounterType> _circBuf; |
henryrawas | 4:36a4eceb1b7f | 82 | }; |
henryrawas | 4:36a4eceb1b7f | 83 | |
henryrawas | 4:36a4eceb1b7f | 84 | #endif |