Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/rtos/Semaphore.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2006-2012 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
marcozecchini | 0:9fca2b23d0ba | 5 | * of this software and associated documentation files (the "Software"), to deal |
marcozecchini | 0:9fca2b23d0ba | 6 | * in the Software without restriction, including without limitation the rights |
marcozecchini | 0:9fca2b23d0ba | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
marcozecchini | 0:9fca2b23d0ba | 8 | * copies of the Software, and to permit persons to whom the Software is |
marcozecchini | 0:9fca2b23d0ba | 9 | * furnished to do so, subject to the following conditions: |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * The above copyright notice and this permission notice shall be included in |
marcozecchini | 0:9fca2b23d0ba | 12 | * all copies or substantial portions of the Software. |
marcozecchini | 0:9fca2b23d0ba | 13 | * |
marcozecchini | 0:9fca2b23d0ba | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
marcozecchini | 0:9fca2b23d0ba | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
marcozecchini | 0:9fca2b23d0ba | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
marcozecchini | 0:9fca2b23d0ba | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
marcozecchini | 0:9fca2b23d0ba | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
marcozecchini | 0:9fca2b23d0ba | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
marcozecchini | 0:9fca2b23d0ba | 20 | * SOFTWARE. |
marcozecchini | 0:9fca2b23d0ba | 21 | */ |
marcozecchini | 0:9fca2b23d0ba | 22 | #ifndef SEMAPHORE_H |
marcozecchini | 0:9fca2b23d0ba | 23 | #define SEMAPHORE_H |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | #include <stdint.h> |
marcozecchini | 0:9fca2b23d0ba | 26 | #include "cmsis_os2.h" |
marcozecchini | 0:9fca2b23d0ba | 27 | #include "mbed_rtos1_types.h" |
marcozecchini | 0:9fca2b23d0ba | 28 | #include "mbed_rtos_storage.h" |
marcozecchini | 0:9fca2b23d0ba | 29 | #include "platform/NonCopyable.h" |
marcozecchini | 0:9fca2b23d0ba | 30 | |
marcozecchini | 0:9fca2b23d0ba | 31 | namespace rtos { |
marcozecchini | 0:9fca2b23d0ba | 32 | /** \addtogroup rtos */ |
marcozecchini | 0:9fca2b23d0ba | 33 | /** @{*/ |
marcozecchini | 0:9fca2b23d0ba | 34 | /** |
marcozecchini | 0:9fca2b23d0ba | 35 | * \defgroup rtos_Semaphore Semaphore class |
marcozecchini | 0:9fca2b23d0ba | 36 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 37 | */ |
marcozecchini | 0:9fca2b23d0ba | 38 | |
marcozecchini | 0:9fca2b23d0ba | 39 | /** The Semaphore class is used to manage and protect access to a set of shared resources. |
marcozecchini | 0:9fca2b23d0ba | 40 | * |
marcozecchini | 0:9fca2b23d0ba | 41 | * @note |
marcozecchini | 0:9fca2b23d0ba | 42 | * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS |
marcozecchini | 0:9fca2b23d0ba | 43 | * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
marcozecchini | 0:9fca2b23d0ba | 44 | */ |
marcozecchini | 0:9fca2b23d0ba | 45 | class Semaphore : private mbed::NonCopyable<Semaphore> { |
marcozecchini | 0:9fca2b23d0ba | 46 | public: |
marcozecchini | 0:9fca2b23d0ba | 47 | /** Create and Initialize a Semaphore object used for managing resources. |
marcozecchini | 0:9fca2b23d0ba | 48 | @param count number of available resources; maximum index value is (count-1). (default: 0). |
marcozecchini | 0:9fca2b23d0ba | 49 | */ |
marcozecchini | 0:9fca2b23d0ba | 50 | Semaphore(int32_t count=0); |
marcozecchini | 0:9fca2b23d0ba | 51 | |
marcozecchini | 0:9fca2b23d0ba | 52 | /** Create and Initialize a Semaphore object used for managing resources. |
marcozecchini | 0:9fca2b23d0ba | 53 | @param count number of available resources |
marcozecchini | 0:9fca2b23d0ba | 54 | @param max_count maximum number of available resources |
marcozecchini | 0:9fca2b23d0ba | 55 | */ |
marcozecchini | 0:9fca2b23d0ba | 56 | Semaphore(int32_t count, uint16_t max_count); |
marcozecchini | 0:9fca2b23d0ba | 57 | |
marcozecchini | 0:9fca2b23d0ba | 58 | /** Wait until a Semaphore resource becomes available. |
marcozecchini | 0:9fca2b23d0ba | 59 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
marcozecchini | 0:9fca2b23d0ba | 60 | @return number of available tokens, before taking one; or -1 in case of incorrect parameters |
marcozecchini | 0:9fca2b23d0ba | 61 | */ |
marcozecchini | 0:9fca2b23d0ba | 62 | int32_t wait(uint32_t millisec=osWaitForever); |
marcozecchini | 0:9fca2b23d0ba | 63 | |
marcozecchini | 0:9fca2b23d0ba | 64 | /** Release a Semaphore resource that was obtain with Semaphore::wait. |
marcozecchini | 0:9fca2b23d0ba | 65 | @return status code that indicates the execution status of the function: |
marcozecchini | 0:9fca2b23d0ba | 66 | @a osOK the token has been correctly released. |
marcozecchini | 0:9fca2b23d0ba | 67 | @a osErrorResource the maximum token count has been reached. |
marcozecchini | 0:9fca2b23d0ba | 68 | @a osErrorParameter internal error. |
marcozecchini | 0:9fca2b23d0ba | 69 | */ |
marcozecchini | 0:9fca2b23d0ba | 70 | osStatus release(void); |
marcozecchini | 0:9fca2b23d0ba | 71 | |
marcozecchini | 0:9fca2b23d0ba | 72 | ~Semaphore(); |
marcozecchini | 0:9fca2b23d0ba | 73 | |
marcozecchini | 0:9fca2b23d0ba | 74 | private: |
marcozecchini | 0:9fca2b23d0ba | 75 | void constructor(int32_t count, uint16_t max_count); |
marcozecchini | 0:9fca2b23d0ba | 76 | |
marcozecchini | 0:9fca2b23d0ba | 77 | osSemaphoreId_t _id; |
marcozecchini | 0:9fca2b23d0ba | 78 | mbed_rtos_storage_semaphore_t _obj_mem; |
marcozecchini | 0:9fca2b23d0ba | 79 | }; |
marcozecchini | 0:9fca2b23d0ba | 80 | /** @}*/ |
marcozecchini | 0:9fca2b23d0ba | 81 | /** @}*/ |
marcozecchini | 0:9fca2b23d0ba | 82 | } |
marcozecchini | 0:9fca2b23d0ba | 83 | #endif |
marcozecchini | 0:9fca2b23d0ba | 84 | |
marcozecchini | 0:9fca2b23d0ba | 85 |