1

Committer:
valeyev
Date:
Tue Mar 13 07:17:50 2018 +0000
Revision:
0:e056ac8fecf8
looking for...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
valeyev 0:e056ac8fecf8 1 /* mbed Microcontroller Library
valeyev 0:e056ac8fecf8 2 * Copyright (c) 2006-2012 ARM Limited
valeyev 0:e056ac8fecf8 3 *
valeyev 0:e056ac8fecf8 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
valeyev 0:e056ac8fecf8 5 * of this software and associated documentation files (the "Software"), to deal
valeyev 0:e056ac8fecf8 6 * in the Software without restriction, including without limitation the rights
valeyev 0:e056ac8fecf8 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
valeyev 0:e056ac8fecf8 8 * copies of the Software, and to permit persons to whom the Software is
valeyev 0:e056ac8fecf8 9 * furnished to do so, subject to the following conditions:
valeyev 0:e056ac8fecf8 10 *
valeyev 0:e056ac8fecf8 11 * The above copyright notice and this permission notice shall be included in
valeyev 0:e056ac8fecf8 12 * all copies or substantial portions of the Software.
valeyev 0:e056ac8fecf8 13 *
valeyev 0:e056ac8fecf8 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
valeyev 0:e056ac8fecf8 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
valeyev 0:e056ac8fecf8 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
valeyev 0:e056ac8fecf8 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
valeyev 0:e056ac8fecf8 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
valeyev 0:e056ac8fecf8 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
valeyev 0:e056ac8fecf8 20 * SOFTWARE.
valeyev 0:e056ac8fecf8 21 */
valeyev 0:e056ac8fecf8 22 #ifndef SEMAPHORE_H
valeyev 0:e056ac8fecf8 23 #define SEMAPHORE_H
valeyev 0:e056ac8fecf8 24
valeyev 0:e056ac8fecf8 25 #include <stdint.h>
valeyev 0:e056ac8fecf8 26 #include "cmsis_os2.h"
valeyev 0:e056ac8fecf8 27 #include "mbed_rtos1_types.h"
valeyev 0:e056ac8fecf8 28 #include "mbed_rtos_storage.h"
valeyev 0:e056ac8fecf8 29 #include "platform/NonCopyable.h"
valeyev 0:e056ac8fecf8 30
valeyev 0:e056ac8fecf8 31 namespace rtos {
valeyev 0:e056ac8fecf8 32 /** \addtogroup rtos */
valeyev 0:e056ac8fecf8 33 /** @{*/
valeyev 0:e056ac8fecf8 34 /**
valeyev 0:e056ac8fecf8 35 * \defgroup rtos_Semaphore Semaphore class
valeyev 0:e056ac8fecf8 36 * @{
valeyev 0:e056ac8fecf8 37 */
valeyev 0:e056ac8fecf8 38
valeyev 0:e056ac8fecf8 39 /** The Semaphore class is used to manage and protect access to a set of shared resources.
valeyev 0:e056ac8fecf8 40 *
valeyev 0:e056ac8fecf8 41 * @note
valeyev 0:e056ac8fecf8 42 * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
valeyev 0:e056ac8fecf8 43 * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
valeyev 0:e056ac8fecf8 44 */
valeyev 0:e056ac8fecf8 45 class Semaphore : private mbed::NonCopyable<Semaphore> {
valeyev 0:e056ac8fecf8 46 public:
valeyev 0:e056ac8fecf8 47 /** Create and Initialize a Semaphore object used for managing resources.
valeyev 0:e056ac8fecf8 48 @param count number of available resources; maximum index value is (count-1). (default: 0).
valeyev 0:e056ac8fecf8 49
valeyev 0:e056ac8fecf8 50 @note You cannot call this function from ISR context.
valeyev 0:e056ac8fecf8 51 */
valeyev 0:e056ac8fecf8 52 Semaphore(int32_t count=0);
valeyev 0:e056ac8fecf8 53
valeyev 0:e056ac8fecf8 54 /** Create and Initialize a Semaphore object used for managing resources.
valeyev 0:e056ac8fecf8 55 @param count number of available resources
valeyev 0:e056ac8fecf8 56 @param max_count maximum number of available resources
valeyev 0:e056ac8fecf8 57
valeyev 0:e056ac8fecf8 58 @note You cannot call this function from ISR context.
valeyev 0:e056ac8fecf8 59 */
valeyev 0:e056ac8fecf8 60 Semaphore(int32_t count, uint16_t max_count);
valeyev 0:e056ac8fecf8 61
valeyev 0:e056ac8fecf8 62 /** Wait until a Semaphore resource becomes available.
valeyev 0:e056ac8fecf8 63 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
valeyev 0:e056ac8fecf8 64 @return number of available tokens, before taking one; or -1 in case of incorrect parameters
valeyev 0:e056ac8fecf8 65
valeyev 0:e056ac8fecf8 66 @note You may call this function from ISR context if the millisec parameter is set to 0.
valeyev 0:e056ac8fecf8 67 */
valeyev 0:e056ac8fecf8 68 int32_t wait(uint32_t millisec=osWaitForever);
valeyev 0:e056ac8fecf8 69
valeyev 0:e056ac8fecf8 70 /** Wait until a Semaphore resource becomes available.
valeyev 0:e056ac8fecf8 71 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
valeyev 0:e056ac8fecf8 72 @return number of available tokens, before taking one; or -1 in case of incorrect parameters
valeyev 0:e056ac8fecf8 73 @note the underlying RTOS may have a limit to the maximum wait time
valeyev 0:e056ac8fecf8 74 due to internal 32-bit computations, but this is guaranteed to work if the
valeyev 0:e056ac8fecf8 75 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
valeyev 0:e056ac8fecf8 76 the acquire attempt will time out earlier than specified.
valeyev 0:e056ac8fecf8 77
valeyev 0:e056ac8fecf8 78 @note You cannot call this function from ISR context.
valeyev 0:e056ac8fecf8 79 */
valeyev 0:e056ac8fecf8 80 int32_t wait_until(uint64_t millisec);
valeyev 0:e056ac8fecf8 81
valeyev 0:e056ac8fecf8 82 /** Release a Semaphore resource that was obtain with Semaphore::wait.
valeyev 0:e056ac8fecf8 83 @return status code that indicates the execution status of the function:
valeyev 0:e056ac8fecf8 84 @a osOK the token has been correctly released.
valeyev 0:e056ac8fecf8 85 @a osErrorResource the maximum token count has been reached.
valeyev 0:e056ac8fecf8 86 @a osErrorParameter internal error.
valeyev 0:e056ac8fecf8 87
valeyev 0:e056ac8fecf8 88 @note You may call this function from ISR context.
valeyev 0:e056ac8fecf8 89 */
valeyev 0:e056ac8fecf8 90 osStatus release(void);
valeyev 0:e056ac8fecf8 91
valeyev 0:e056ac8fecf8 92 /** Semaphore destructor
valeyev 0:e056ac8fecf8 93 *
valeyev 0:e056ac8fecf8 94 * @note You cannot call this function from ISR context.
valeyev 0:e056ac8fecf8 95 */
valeyev 0:e056ac8fecf8 96 ~Semaphore();
valeyev 0:e056ac8fecf8 97
valeyev 0:e056ac8fecf8 98 private:
valeyev 0:e056ac8fecf8 99 void constructor(int32_t count, uint16_t max_count);
valeyev 0:e056ac8fecf8 100
valeyev 0:e056ac8fecf8 101 osSemaphoreId_t _id;
valeyev 0:e056ac8fecf8 102 mbed_rtos_storage_semaphore_t _obj_mem;
valeyev 0:e056ac8fecf8 103 };
valeyev 0:e056ac8fecf8 104 /** @}*/
valeyev 0:e056ac8fecf8 105 /** @}*/
valeyev 0:e056ac8fecf8 106 }
valeyev 0:e056ac8fecf8 107 #endif
valeyev 0:e056ac8fecf8 108
valeyev 0:e056ac8fecf8 109