ok

Dependents:   I2C

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-2012 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 "mbed_rtos_types.h"
00027 #include "mbed_rtos1_types.h"
00028 #include "mbed_rtos_storage.h"
00029 #include "platform/mbed_toolchain.h"
00030 #include "platform/NonCopyable.h"
00031 
00032 namespace rtos {
00033 /** \addtogroup rtos */
00034 /** @{*/
00035 /**
00036  * \defgroup rtos_Semaphore Semaphore class
00037  * @{
00038  */
00039 
00040 /** The Semaphore class is used to manage and protect access to a set of shared resources.
00041  *
00042  * @note
00043  * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
00044  * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00045  */
00046 class Semaphore : private mbed::NonCopyable<Semaphore> {
00047 public:
00048     /** Create and Initialize a Semaphore object used for managing resources.
00049       @param count      number of available resources; maximum index value is (count-1). (default: 0).
00050 
00051       @note You cannot call this function from ISR context.
00052     */
00053     Semaphore(int32_t count = 0);
00054 
00055     /** Create and Initialize a Semaphore object used for managing resources.
00056       @param  count     number of available resources
00057       @param  max_count maximum number of available resources
00058 
00059       @note You cannot call this function from ISR context.
00060     */
00061     Semaphore(int32_t count, uint16_t max_count);
00062 
00063     /** Wait until a Semaphore resource becomes available.
00064 
00065       @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
00066 
00067       @param   millisec  timeout value. (default: osWaitForever).
00068       @return  number of available tokens, before taking one; or -1 in case of incorrect parameters
00069 
00070       @note You may call this function from ISR context if the millisec parameter is set to 0.
00071     */
00072     MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
00073     int32_t wait(uint32_t millisec = osWaitForever);
00074 
00075     /** Wait until a Semaphore resource becomes available.
00076 
00077       @deprecated Do not use this function. This function has been replaced with try_acquire_until().
00078 
00079       @param   millisec  absolute timeout time, referenced to Kernel::get_ms_count()
00080       @return  number of available tokens, before taking one; or -1 in case of incorrect parameters
00081       @note the underlying RTOS may have a limit to the maximum wait time
00082             due to internal 32-bit computations, but this is guaranteed to work if the
00083             wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00084             the acquire attempt will time out earlier than specified.
00085 
00086       @note You cannot call this function from ISR context.
00087     */
00088     MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
00089     int32_t wait_until(uint64_t millisec);
00090 
00091     /** Wait until a Semaphore resource becomes available.
00092 
00093       @note You cannot call this function from ISR context.
00094     */
00095     void acquire();
00096 
00097     /** Try to acquire a Semaphore resource, and return immediately
00098       @return true if a resource was acquired, false otherwise.
00099       @note equivalent to try_acquire_for(0)
00100 
00101     @note You may call this function from ISR context.
00102     */
00103     bool try_acquire();
00104 
00105     /** Wait until a Semaphore resource becomes available.
00106       @param   millisec  timeout value.
00107       @return true if a resource was acquired, false otherwise.
00108 
00109       @note You may call this function from ISR context if the millisec parameter is set to 0.
00110     */
00111     bool try_acquire_for(uint32_t millisec);
00112 
00113     /** Wait until a Semaphore resource becomes available.
00114       @param   millisec  absolute timeout time, referenced to Kernel::get_ms_count()
00115       @return true if a resource was acquired, false otherwise.
00116       @note the underlying RTOS may have a limit to the maximum wait time
00117             due to internal 32-bit computations, but this is guaranteed to work if the
00118             wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
00119             the acquire attempt will time out earlier than specified.
00120 
00121       @note You cannot call this function from ISR context.
00122     */
00123     bool try_acquire_until(uint64_t millisec);
00124 
00125     /** Release a Semaphore resource that was obtain with Semaphore::acquire.
00126       @return status code that indicates the execution status of the function:
00127               @a osOK the token has been correctly released.
00128               @a osErrorResource the maximum token count has been reached.
00129               @a osErrorParameter internal error.
00130 
00131       @note You may call this function from ISR context.
00132     */
00133     osStatus release(void);
00134 
00135     /** Semaphore destructor
00136      *
00137      * @note You cannot call this function from ISR context.
00138      */
00139     ~Semaphore();
00140 
00141 private:
00142     void constructor(int32_t count, uint16_t max_count);
00143 
00144 #if MBED_CONF_RTOS_PRESENT
00145     int32_t _wait(uint32_t millisec);
00146 
00147     osSemaphoreId_t               _id;
00148     mbed_rtos_storage_semaphore_t _obj_mem;
00149 #else
00150     static bool semaphore_available(void *);
00151     int32_t _count;
00152     uint16_t _max_count;
00153 #endif
00154 };
00155 /** @}*/
00156 /** @}*/
00157 }
00158 #endif
00159 
00160