diff --git a/core/include/mutex.h b/core/include/mutex.h index 823ca81307..4d9acd1e34 100644 --- a/core/include/mutex.h +++ b/core/include/mutex.h @@ -105,11 +105,14 @@ #include #include -#include "irq.h" #include "kernel_defines.h" #include "list.h" #include "thread.h" +#ifndef __cplusplus +#include "irq.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -187,6 +190,25 @@ static inline mutex_cancel_t mutex_cancel_init(mutex_t *mutex) return result; } +/** + * @brief Tries to get a mutex, non-blocking. + * + * @internal + * @note This function is intended for use by languages incompatible + * with C (such as C++). Code in C should use @ref mutex_trylock + * instead + * + * @param[in,out] mutex Mutex object to lock. + * + * @retval 1 if mutex was unlocked, now it is locked. + * @retval 0 if the mutex was locked. + * + * @pre @p mutex is not `NULL` + * @pre Mutex at @p mutex has been initialized + * @pre Must be called in thread context + */ +int mutex_trylock_ffi(mutex_t *mutex); + /** * @brief Tries to get a mutex, non-blocking. * @@ -201,6 +223,9 @@ static inline mutex_cancel_t mutex_cancel_init(mutex_t *mutex) */ static inline int mutex_trylock(mutex_t *mutex) { +#ifdef __cplusplus + return mutex_trylock_ffi(mutex); +#else unsigned irq_state = irq_disable(); int retval = 0; @@ -210,6 +235,7 @@ static inline int mutex_trylock(mutex_t *mutex) } irq_restore(irq_state); return retval; +#endif } /** diff --git a/core/mutex.c b/core/mutex.c index f3b232a7f8..c8682614f7 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -211,3 +211,9 @@ void mutex_cancel(mutex_cancel_t *mc) irq_restore(irq_state); } + +/* Helper for compatibility with C++ or other non-C languages */ +int mutex_trylock_ffi(mutex_t *mutex) +{ + return mutex_trylock(mutex); +}