2013-02-18 15:32:11 +00:00
/* mbed Microcontroller Library
2019-07-11 15:23:39 +00:00
* Copyright ( c ) 2006 - 2019 ARM Limited
2020-02-20 08:45:04 +00:00
* SPDX - License - Identifier : MIT
*
2013-02-18 15:32:11 +00:00
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
*/
# ifndef SEMAPHORE_H
# define SEMAPHORE_H
# include <stdint.h>
2020-02-12 14:12:56 +00:00
# include <chrono>
2019-03-14 11:36:02 +00:00
# include "rtos/mbed_rtos_types.h"
# include "rtos/mbed_rtos1_types.h"
# include "rtos/mbed_rtos_storage.h"
2020-02-12 14:12:56 +00:00
# include "rtos/Kernel.h"
2019-03-18 10:21:51 +00:00
# include "platform/mbed_toolchain.h"
2017-06-20 12:00:20 +00:00
# include "platform/NonCopyable.h"
2013-02-18 15:32:11 +00:00
namespace rtos {
2019-07-11 15:23:39 +00:00
/** \addtogroup rtos-public-api */
2016-10-04 20:02:44 +00:00
/** @{*/
2019-09-06 21:58:57 +00:00
2017-10-24 15:05:45 +00:00
/**
* \ defgroup rtos_Semaphore Semaphore class
* @ {
*/
2013-02-18 15:32:11 +00:00
2017-05-15 14:55:45 +00:00
/** The Semaphore class is used to manage and protect access to a set of shared resources.
*
* @ note
* Memory considerations : The semaphore control structures will be created on current thread ' s stack , both for the mbed OS
* and underlying RTOS objects ( static or dynamic RTOS memory pools are not being used ) .
*/
2017-06-20 12:00:20 +00:00
class Semaphore : private mbed : : NonCopyable < Semaphore > {
2013-02-18 15:32:11 +00:00
public :
2014-05-29 13:36:51 +00:00
/** Create and Initialize a Semaphore object used for managing resources.
2017-06-06 01:51:37 +00:00
@ param count number of available resources ; maximum index value is ( count - 1 ) . ( default : 0 ) .
2017-12-27 14:46:00 +00:00
2018-01-08 23:52:40 +00:00
@ note You cannot call this function from ISR context .
2013-02-18 15:32:11 +00:00
*/
2018-10-09 19:59:02 +00:00
Semaphore ( int32_t count = 0 ) ;
2014-05-29 13:36:51 +00:00
2017-05-15 14:55:45 +00:00
/** Create and Initialize a Semaphore object used for managing resources.
@ param count number of available resources
@ param max_count maximum number of available resources
2017-12-27 14:46:00 +00:00
2018-01-08 23:52:40 +00:00
@ note You cannot call this function from ISR context .
2017-05-15 14:55:45 +00:00
*/
Semaphore ( int32_t count , uint16_t max_count ) ;
2019-03-18 10:21:51 +00:00
/** Wait until a Semaphore resource becomes available.
@ note You cannot call this function from ISR context .
*/
void acquire ( ) ;
/** Try to acquire a Semaphore resource, and return immediately
@ return true if a resource was acquired , false otherwise .
@ note equivalent to try_acquire_for ( 0 )
@ note You may call this function from ISR context .
*/
bool try_acquire ( ) ;
/** Wait until a Semaphore resource becomes available.
@ param millisec timeout value .
@ return true if a resource was acquired , false otherwise .
@ note You may call this function from ISR context if the millisec parameter is set to 0.
2020-02-12 14:12:56 +00:00
@ deprecated Pass a chrono duration , not an integer millisecond count . For example use ` 5 s ` rather than ` 5000 ` .
2019-03-18 10:21:51 +00:00
*/
2020-02-12 14:12:56 +00:00
MBED_DEPRECATED_SINCE ( " mbed-os-6.0.0 " , " Pass a chrono duration, not an integer millisecond count. For example use `5s` rather than `5000`. " )
2019-03-18 10:21:51 +00:00
bool try_acquire_for ( uint32_t millisec ) ;
2020-02-12 14:12:56 +00:00
/** Wait until a Semaphore resource becomes available.
@ param rel_time timeout value .
@ return true if a resource was acquired , false otherwise .
@ note You may call this function from ISR context if the rel_time parameter is set to 0.
*/
bool try_acquire_for ( Kernel : : Clock : : duration_u32 rel_time ) ;
2019-03-18 10:21:51 +00:00
/** Wait until a Semaphore resource becomes available.
@ param millisec absolute timeout time , referenced to Kernel : : get_ms_count ( )
@ return true if a resource was acquired , false otherwise .
@ note the underlying RTOS may have a limit to the maximum wait time
due to internal 32 - bit computations , but this is guaranteed to work if the
wait is < = 0x7fffffff milliseconds ( ~ 24 days ) . If the limit is exceeded ,
the acquire attempt will time out earlier than specified .
@ note You cannot call this function from ISR context .
2020-02-12 14:12:56 +00:00
@ deprecated Pass a chrono time_point , not an integer millisecond count . For example use
` Kernel : : Clock : : now ( ) + 5 s ` rather than ` Kernel : : get_ms_count ( ) + 5000 ` .
2019-03-18 10:21:51 +00:00
*/
2020-02-12 14:12:56 +00:00
MBED_DEPRECATED_SINCE ( " mbed-os-6.0.0 " , " Pass a chrono time_point, not an integer millisecond count. For example use `Kernel::Clock::now() + 5s` rather than `Kernel::get_ms_count() + 5000`. " )
2019-03-18 10:21:51 +00:00
bool try_acquire_until ( uint64_t millisec ) ;
2020-02-12 14:12:56 +00:00
/** Wait until a Semaphore resource becomes available.
@ param millisec absolute timeout time , referenced to Kernel : : get_ms_count ( )
@ return true if a resource was acquired , false otherwise .
@ note the underlying RTOS may have a limit to the maximum wait time
due to internal 32 - bit computations , but this is guaranteed to work if the
wait is < = 0x7fffffff milliseconds ( ~ 24 days ) . If the limit is exceeded ,
the acquire attempt will time out earlier than specified .
@ note You cannot call this function from ISR context .
*/
bool try_acquire_until ( Kernel : : Clock : : time_point abs_time ) ;
2019-03-18 10:21:51 +00:00
/** Release a Semaphore resource that was obtain with Semaphore::acquire.
2017-07-11 14:01:33 +00:00
@ return status code that indicates the execution status of the function :
@ a osOK the token has been correctly released .
@ a osErrorResource the maximum token count has been reached .
@ a osErrorParameter internal error .
2017-12-27 14:46:00 +00:00
2018-01-08 23:52:40 +00:00
@ note You may call this function from ISR context .
2013-02-18 15:32:11 +00:00
*/
osStatus release ( void ) ;
2014-05-29 13:36:51 +00:00
2017-12-27 14:46:00 +00:00
/** Semaphore destructor
*
2018-01-08 23:52:40 +00:00
* @ note You cannot call this function from ISR context .
2017-12-27 14:46:00 +00:00
*/
2013-02-18 15:32:11 +00:00
~ Semaphore ( ) ;
private :
2017-05-15 14:55:45 +00:00
void constructor ( int32_t count , uint16_t max_count ) ;
2019-03-14 11:36:02 +00:00
# if MBED_CONF_RTOS_PRESENT
2019-03-18 10:21:51 +00:00
int32_t _wait ( uint32_t millisec ) ;
2017-05-15 14:55:45 +00:00
osSemaphoreId_t _id ;
mbed_rtos_storage_semaphore_t _obj_mem ;
2019-03-14 11:36:02 +00:00
# else
static bool semaphore_available ( void * ) ;
int32_t _count ;
uint16_t _max_count ;
# endif
2013-02-18 15:32:11 +00:00
} ;
2017-10-24 15:05:45 +00:00
/** @}*/
/** @}*/
2013-02-18 15:32:11 +00:00
}
# endif