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.
mbed-os/rtos/Mail.h@0:4beb2ea291ec, 2020-03-16 (annotated)
- Committer:
- boro
- Date:
- Mon Mar 16 13:12:31 2020 +0000
- Revision:
- 0:4beb2ea291ec
a
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| boro | 0:4beb2ea291ec | 1 | /* mbed Microcontroller Library |
| boro | 0:4beb2ea291ec | 2 | * Copyright (c) 2006-2017 ARM Limited |
| boro | 0:4beb2ea291ec | 3 | * |
| boro | 0:4beb2ea291ec | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| boro | 0:4beb2ea291ec | 5 | * of this software and associated documentation files (the "Software"), to deal |
| boro | 0:4beb2ea291ec | 6 | * in the Software without restriction, including without limitation the rights |
| boro | 0:4beb2ea291ec | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| boro | 0:4beb2ea291ec | 8 | * copies of the Software, and to permit persons to whom the Software is |
| boro | 0:4beb2ea291ec | 9 | * furnished to do so, subject to the following conditions: |
| boro | 0:4beb2ea291ec | 10 | * |
| boro | 0:4beb2ea291ec | 11 | * The above copyright notice and this permission notice shall be included in |
| boro | 0:4beb2ea291ec | 12 | * all copies or substantial portions of the Software. |
| boro | 0:4beb2ea291ec | 13 | * |
| boro | 0:4beb2ea291ec | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| boro | 0:4beb2ea291ec | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| boro | 0:4beb2ea291ec | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| boro | 0:4beb2ea291ec | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| boro | 0:4beb2ea291ec | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| boro | 0:4beb2ea291ec | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| boro | 0:4beb2ea291ec | 20 | * SOFTWARE. |
| boro | 0:4beb2ea291ec | 21 | */ |
| boro | 0:4beb2ea291ec | 22 | #ifndef MAIL_H |
| boro | 0:4beb2ea291ec | 23 | #define MAIL_H |
| boro | 0:4beb2ea291ec | 24 | |
| boro | 0:4beb2ea291ec | 25 | #include <stdint.h> |
| boro | 0:4beb2ea291ec | 26 | #include <string.h> |
| boro | 0:4beb2ea291ec | 27 | |
| boro | 0:4beb2ea291ec | 28 | #include "Queue.h" |
| boro | 0:4beb2ea291ec | 29 | #include "MemoryPool.h" |
| boro | 0:4beb2ea291ec | 30 | #include "cmsis_os2.h" |
| boro | 0:4beb2ea291ec | 31 | #include "mbed_rtos_storage.h" |
| boro | 0:4beb2ea291ec | 32 | #include "mbed_rtos1_types.h" |
| boro | 0:4beb2ea291ec | 33 | |
| boro | 0:4beb2ea291ec | 34 | #include "platform/NonCopyable.h" |
| boro | 0:4beb2ea291ec | 35 | |
| boro | 0:4beb2ea291ec | 36 | using namespace rtos; |
| boro | 0:4beb2ea291ec | 37 | |
| boro | 0:4beb2ea291ec | 38 | namespace rtos { |
| boro | 0:4beb2ea291ec | 39 | /** \addtogroup rtos */ |
| boro | 0:4beb2ea291ec | 40 | /** @{*/ |
| boro | 0:4beb2ea291ec | 41 | /** |
| boro | 0:4beb2ea291ec | 42 | * \defgroup rtos_Mail Mail class |
| boro | 0:4beb2ea291ec | 43 | * @{ |
| boro | 0:4beb2ea291ec | 44 | */ |
| boro | 0:4beb2ea291ec | 45 | |
| boro | 0:4beb2ea291ec | 46 | /** The Mail class allow to control, send, receive, or wait for mail. |
| boro | 0:4beb2ea291ec | 47 | A mail is a memory block that is send to a thread or interrupt service routine. |
| boro | 0:4beb2ea291ec | 48 | @tparam T data type of a single message element. |
| boro | 0:4beb2ea291ec | 49 | @tparam queue_sz maximum number of messages in queue. |
| boro | 0:4beb2ea291ec | 50 | |
| boro | 0:4beb2ea291ec | 51 | @note |
| boro | 0:4beb2ea291ec | 52 | Memory considerations: The mail data store and control structures will be created on current thread's stack, |
| boro | 0:4beb2ea291ec | 53 | both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
| boro | 0:4beb2ea291ec | 54 | */ |
| boro | 0:4beb2ea291ec | 55 | template<typename T, uint32_t queue_sz> |
| boro | 0:4beb2ea291ec | 56 | class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > { |
| boro | 0:4beb2ea291ec | 57 | public: |
| boro | 0:4beb2ea291ec | 58 | /** Create and Initialise Mail queue. */ |
| boro | 0:4beb2ea291ec | 59 | Mail() { }; |
| boro | 0:4beb2ea291ec | 60 | |
| boro | 0:4beb2ea291ec | 61 | /** Check if the mail queue is empty |
| boro | 0:4beb2ea291ec | 62 | * |
| boro | 0:4beb2ea291ec | 63 | * @return True if the mail queue is empty, false if not |
| boro | 0:4beb2ea291ec | 64 | */ |
| boro | 0:4beb2ea291ec | 65 | bool empty() const { |
| boro | 0:4beb2ea291ec | 66 | return _queue.empty(); |
| boro | 0:4beb2ea291ec | 67 | } |
| boro | 0:4beb2ea291ec | 68 | |
| boro | 0:4beb2ea291ec | 69 | /** Check if the mail queue is full |
| boro | 0:4beb2ea291ec | 70 | * |
| boro | 0:4beb2ea291ec | 71 | * @return True if the mail queue is full, false if not |
| boro | 0:4beb2ea291ec | 72 | */ |
| boro | 0:4beb2ea291ec | 73 | bool full() const { |
| boro | 0:4beb2ea291ec | 74 | return _queue.full(); |
| boro | 0:4beb2ea291ec | 75 | } |
| boro | 0:4beb2ea291ec | 76 | |
| boro | 0:4beb2ea291ec | 77 | /** Allocate a memory block of type T |
| boro | 0:4beb2ea291ec | 78 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
| boro | 0:4beb2ea291ec | 79 | @return pointer to memory block that can be filled with mail or NULL in case error. |
| boro | 0:4beb2ea291ec | 80 | */ |
| boro | 0:4beb2ea291ec | 81 | T* alloc(uint32_t millisec=0) { |
| boro | 0:4beb2ea291ec | 82 | return _pool.alloc(); |
| boro | 0:4beb2ea291ec | 83 | } |
| boro | 0:4beb2ea291ec | 84 | |
| boro | 0:4beb2ea291ec | 85 | /** Allocate a memory block of type T and set memory block to zero. |
| boro | 0:4beb2ea291ec | 86 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
| boro | 0:4beb2ea291ec | 87 | @return pointer to memory block that can be filled with mail or NULL in case error. |
| boro | 0:4beb2ea291ec | 88 | */ |
| boro | 0:4beb2ea291ec | 89 | T* calloc(uint32_t millisec=0) { |
| boro | 0:4beb2ea291ec | 90 | return _pool.calloc(); |
| boro | 0:4beb2ea291ec | 91 | } |
| boro | 0:4beb2ea291ec | 92 | |
| boro | 0:4beb2ea291ec | 93 | /** Put a mail in the queue. |
| boro | 0:4beb2ea291ec | 94 | @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. |
| boro | 0:4beb2ea291ec | 95 | @return status code that indicates the execution status of the function. |
| boro | 0:4beb2ea291ec | 96 | */ |
| boro | 0:4beb2ea291ec | 97 | osStatus put(T *mptr) { |
| boro | 0:4beb2ea291ec | 98 | return _queue.put(mptr); |
| boro | 0:4beb2ea291ec | 99 | } |
| boro | 0:4beb2ea291ec | 100 | |
| boro | 0:4beb2ea291ec | 101 | /** Get a mail from a queue. |
| boro | 0:4beb2ea291ec | 102 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
| boro | 0:4beb2ea291ec | 103 | @return event that contains mail information or error code. |
| boro | 0:4beb2ea291ec | 104 | */ |
| boro | 0:4beb2ea291ec | 105 | osEvent get(uint32_t millisec=osWaitForever) { |
| boro | 0:4beb2ea291ec | 106 | osEvent evt = _queue.get(millisec); |
| boro | 0:4beb2ea291ec | 107 | if (evt.status == osEventMessage) { |
| boro | 0:4beb2ea291ec | 108 | evt.status = osEventMail; |
| boro | 0:4beb2ea291ec | 109 | } |
| boro | 0:4beb2ea291ec | 110 | return evt; |
| boro | 0:4beb2ea291ec | 111 | } |
| boro | 0:4beb2ea291ec | 112 | |
| boro | 0:4beb2ea291ec | 113 | /** Free a memory block from a mail. |
| boro | 0:4beb2ea291ec | 114 | @param mptr pointer to the memory block that was obtained with Mail::get. |
| boro | 0:4beb2ea291ec | 115 | @return status code that indicates the execution status of the function. |
| boro | 0:4beb2ea291ec | 116 | */ |
| boro | 0:4beb2ea291ec | 117 | osStatus free(T *mptr) { |
| boro | 0:4beb2ea291ec | 118 | return _pool.free(mptr); |
| boro | 0:4beb2ea291ec | 119 | } |
| boro | 0:4beb2ea291ec | 120 | |
| boro | 0:4beb2ea291ec | 121 | private: |
| boro | 0:4beb2ea291ec | 122 | Queue<T, queue_sz> _queue; |
| boro | 0:4beb2ea291ec | 123 | MemoryPool<T, queue_sz> _pool; |
| boro | 0:4beb2ea291ec | 124 | }; |
| boro | 0:4beb2ea291ec | 125 | |
| boro | 0:4beb2ea291ec | 126 | /** @}*/ |
| boro | 0:4beb2ea291ec | 127 | /** @}*/ |
| boro | 0:4beb2ea291ec | 128 | |
| boro | 0:4beb2ea291ec | 129 | } |
| boro | 0:4beb2ea291ec | 130 | |
| boro | 0:4beb2ea291ec | 131 | #endif |
| boro | 0:4beb2ea291ec | 132 | |
| boro | 0:4beb2ea291ec | 133 | |
| boro | 0:4beb2ea291ec | 134 |