Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Semaphore.h Source File

Semaphore.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef SEMAPHORE_H
00023 #define SEMAPHORE_H
00024 
00025 #include <stdint.h>
00026 #include "rtos/mbed_rtos_types.h"
00027 #include "rtos/mbed_rtos1_types.h"
00028 #include "rtos/mbed_rtos_storage.h"
00029 #include "platform/mbed_toolchain.h"
00030 #include "platform/NonCopyable.h"
00031 
00032 namespace rtos {
00033 /** \addtogroup rtos-public-api */
00034 /** @{*/
00035 
00036 /**
00037  * \defgroup rtos_Semaphore Semaphore class
00038  * @{
00039  */
00040 
00041 /** The Semaphore class is used to manage and protect access to a set of shared resources.
00042  *
00043  * @note
00044  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
00045  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00046  */
00047 class Semaphore : private mbed::NonCopyable<Semaphore> {
00048 public:
00049     /** Create and Initialize a Semaphore object used for managing resources.
00050       @param count      number of available resources; maximum index value is (count-1). (default: 0).
00051 
00052       @note You cannot call this function from ISR context.
00053     */
00054     Semaphore(int32_t count = 0);
00055 
00056     /** Create and Initialize a Semaphore object used for managing resources.
00057       @param  count     number of available resources
00058       @param  max_count maximum number of available resources
00059 
00060       @note You cannot call this function from ISR context.
00061     */
00062     Semaphore(int32_t count, uint16_t max_count);
00063 
00064     /** Wait until a Semaphore resource becomes available.
00065 
00066       @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
00067 
00068       @param   millisec  timeout value. (default: osWaitForever).
00069       @return  number of available tokens, before taking one; or -1 in case of incorrect parameters
00070 
00071       @note You may call this function from ISR context if the millisec parameter is set to 0.
00072     */
00073     MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
00074     int32_t wait(uint32_t millisec = osWaitForever);
00075 
00076     /** Wait until a Semaphore resource becomes available.
00077 
00078       @deprecated Do not use this function. This function has been replaced with try_acquire_until().
00079 
00080       @param   millisec  absolute timeout time, referenced to Kernel::get_ms_count()
00081       @return  number of available tokens, before taking one; or -1 in case of incorrect parameters
00082       @note the underlying RTOS may have a limit to the maximum wait time
00083             due to internal 32-bit computations, but this is guaranteed to work if the
00084             wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00085             the acquire attempt will time out earlier than specified.
00086 
00087       @note You cannot call this function from ISR context.
00088     */
00089     MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
00090     int32_t wait_until(uint64_t millisec);
00091 
00092     /** Wait until a Semaphore resource becomes available.
00093 
00094       @note You cannot call this function from ISR context.
00095     */
00096     void acquire();
00097 
00098     /** Try to acquire a Semaphore resource, and return immediately
00099       @return true if a resource was acquired, false otherwise.
00100       @note equivalent to try_acquire_for(0)
00101 
00102     @note You may call this function from ISR context.
00103     */
00104     bool try_acquire();
00105 
00106     /** Wait until a Semaphore resource becomes available.
00107       @param   millisec  timeout value.
00108       @return true if a resource was acquired, false otherwise.
00109 
00110       @note You may call this function from ISR context if the millisec parameter is set to 0.
00111     */
00112     bool try_acquire_for(uint32_t millisec);
00113 
00114     /** Wait until a Semaphore resource becomes available.
00115       @param   millisec  absolute timeout time, referenced to Kernel::get_ms_count()
00116       @return true if a resource was acquired, false otherwise.
00117       @note the underlying RTOS may have a limit to the maximum wait time
00118             due to internal 32-bit computations, but this is guaranteed to work if the
00119             wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00120             the acquire attempt will time out earlier than specified.
00121 
00122       @note You cannot call this function from ISR context.
00123     */
00124     bool try_acquire_until(uint64_t millisec);
00125 
00126     /** Release a Semaphore resource that was obtain with Semaphore::acquire.
00127       @return status code that indicates the execution status of the function:
00128               @a osOK the token has been correctly released.
00129               @a osErrorResource the maximum token count has been reached.
00130               @a osErrorParameter internal error.
00131 
00132       @note You may call this function from ISR context.
00133     */
00134     osStatus release(void);
00135 
00136     /** Semaphore destructor
00137      *
00138      * @note You cannot call this function from ISR context.
00139      */
00140     ~Semaphore();
00141 
00142 private:
00143     void constructor(int32_t count, uint16_t max_count);
00144 
00145 #if MBED_CONF_RTOS_PRESENT
00146     int32_t _wait(uint32_t millisec);
00147 
00148     osSemaphoreId_t               _id;
00149     mbed_rtos_storage_semaphore_t _obj_mem;
00150 #else
00151     static bool semaphore_available(void *);
00152     int32_t _count;
00153     uint16_t _max_count;
00154 #endif
00155 };
00156 /** @}*/
00157 /** @}*/
00158 }
00159 #endif