Skip to content

Commit

Permalink
Merge pull request #11977 from JulianHolzwarth/pr/xtimer_rmutex_lock_…
Browse files Browse the repository at this point in the history
…timeout

xtimer/xtimer.c: xtimer_rmutex_lock_timeout
  • Loading branch information
MichelRottleuthner authored May 5, 2020
2 parents ebc415b + 6c8b1f1 commit 50c7b1b
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sys/include/c11_atomics_compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
* atomic_int foo = ATOMIC_VAR_INIT(42);
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#ifndef ATOMIC_VAR_INIT
#define ATOMIC_VAR_INIT(x) { x }
#endif

/**
* @brief Type with the same alignment and size as `atomic_bool`
Expand Down
4 changes: 1 addition & 3 deletions sys/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@
* see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60932
*/
#ifdef __cplusplus
#include <atomic>
/* Make atomic_int available without namespace specifier */
using std::atomic_int;
#include "c11_atomics_compat.hpp"
#else
#include <stdatomic.h> /* for atomic_int */
#endif
Expand Down
13 changes: 13 additions & 0 deletions sys/include/xtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#endif /* MODULE_CORE_MSG */
#include "mutex.h"
#include "kernel_types.h"
#include "rmutex.h"

#ifdef MODULE_ZTIMER_XTIMER_COMPAT
#include "ztimer/xtimer_compat.h"
Expand Down Expand Up @@ -413,7 +414,19 @@ static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
*/
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);

/**
* @brief lock a rmutex but with timeout
*
* @param[in] rmutex rmutex to lock
* @param[in] us timeout in microseconds relative
*
* @return 0, when returned after rmutex was locked
* @return -1, when the timeout occcured
*/
int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us);

#if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)

/**
* @brief Set timeout thread flag after @p timeout
*
Expand Down
15 changes: 15 additions & 0 deletions sys/xtimer/xtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "xtimer.h"
#include "mutex.h"
#include "rmutex.h"
#include "thread.h"
#include "irq.h"
#include "div.h"
Expand Down Expand Up @@ -270,6 +271,20 @@ int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t timeout)
return -mt.dequeued;
}

int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t timeout)
{
if (rmutex_trylock(rmutex)) {
return 0;
}
if (xtimer_mutex_lock_timeout(&rmutex->mutex, timeout) == 0) {
atomic_store_explicit(&rmutex->owner,
thread_getpid(), memory_order_relaxed);
rmutex->refcount++;
return 0;
}
return -1;
}

#ifdef MODULE_CORE_THREAD_FLAGS
static void _set_timeout_flag_callback(void* arg)
{
Expand Down
6 changes: 6 additions & 0 deletions tests/xtimer_rmutex_lock_timeout/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include ../Makefile.tests_common

USEMODULE += xtimer
USEMODULE += shell

include $(RIOTBASE)/Makefile.include
Loading

0 comments on commit 50c7b1b

Please sign in to comment.