Skip to content

Commit

Permalink
Merge pull request #18619 from maribu/core/mutex/cleanup
Browse files Browse the repository at this point in the history
core/mutex: clean up
  • Loading branch information
maribu authored Oct 3, 2022
2 parents b31eb6b + 2d2bb4b commit 3227fb3
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 79 deletions.
80 changes: 28 additions & 52 deletions core/include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,12 @@

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#include "kernel_defines.h"
#include "list.h"
#include "thread.h"

#ifndef __cplusplus
#include "irq.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -153,6 +150,27 @@ typedef struct {
#endif
} mutex_t;

/**
* @brief Internal function implementing @ref mutex_lock and
* @ref mutex_trylock
*
* @details Do not call this function, use @ref mutex_lock or @ref mutex_trylock
* instead
*
* @param[in,out] mutex Mutex object to lock.
* @param[in] block Whether to block
*
* @pre @p mutex is not `NULL`
* @pre Mutex at @p mutex has been initialized
* @pre Must be called in thread context
*
* @post The mutex @p is locked and held by the calling thread.
*
* @retval true Mutex obtained
* @retval false Mutex not obtained (only possible if @p block is `false`)
*/
bool mutex_lock_internal(mutex_t *mutex, bool block);

/**
* @brief A cancellation structure for use with @ref mutex_lock_cancelable
* and @ref mutex_cancel
Expand Down Expand Up @@ -219,25 +237,6 @@ 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.
*
Expand All @@ -252,28 +251,7 @@ int mutex_trylock_ffi(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;

if (mutex->queue.next == NULL) {
mutex->queue.next = MUTEX_LOCKED;
#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
mutex->owner = KERNEL_PID_UNDEF;
thread_t *t = thread_get_active();
/* in case mutex_trylock() is not called from thread context */
if (t) {
mutex->owner = t->pid;
mutex->owner_original_priority = t->priority;
}
#endif
retval = 1;
}
irq_restore(irq_state);
return retval;
#endif
return mutex_lock_internal(mutex, false);
}

/**
Expand All @@ -287,14 +265,12 @@ static inline int mutex_trylock(mutex_t *mutex)
*
* @post The mutex @p is locked and held by the calling thread.
*/
#if (MAXTHREADS > 1) || DOXYGEN
void mutex_lock(mutex_t *mutex);
#else
/**
* @brief dummy implementation for when no scheduler is used
*/
static inline void mutex_lock(mutex_t *mutex)
{
#if (MAXTHREADS > 1)
mutex_lock_internal(mutex, true);
#else
/* dummy implementation for when no scheduler is used */
/* (ab)use next pointer as lock variable */
volatile uintptr_t *lock = (void *)&mutex->queue.next;

Expand All @@ -310,8 +286,8 @@ static inline void mutex_lock(mutex_t *mutex)

/* set lock variable */
*lock = 1;
}
#endif
}

/**
* @brief Locks a mutex, blocking. This function can be canceled.
Expand Down
17 changes: 9 additions & 8 deletions core/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ static inline __attribute__((always_inline)) void _block(mutex_t *mutex,
/* We were woken up by scheduler. Waker removed us from queue. */
}

void mutex_lock(mutex_t *mutex)
bool mutex_lock_internal(mutex_t *mutex, bool block)
{
unsigned irq_state = irq_disable();

DEBUG("PID[%" PRIkernel_pid "] mutex_lock().\n", thread_getpid());
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_internal(block=%u).\n",
thread_getpid(), (unsigned)block);

if (mutex->queue.next == NULL) {
/* mutex is unlocked. */
Expand All @@ -101,8 +102,14 @@ void mutex_lock(mutex_t *mutex)
irq_restore(irq_state);
}
else {
if (!block) {
irq_restore(irq_state);
return false;
}
_block(mutex, irq_state);
}

return true;
}

int mutex_lock_cancelable(mutex_cancel_t *mc)
Expand Down Expand Up @@ -254,12 +261,6 @@ 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);
}

#else /* MAXTHREADS < 2 */
typedef int dont_be_pedantic;
#endif
7 changes: 4 additions & 3 deletions cpu/atxmega/periph/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
* @}
*/

#include <stdio.h>
#include <avr/interrupt.h>
#include <stdio.h>

#include "bitarithm.h"
#include "cpu.h"
#include "periph_conf.h"
#include "irq.h"
#include "periph/gpio.h"
#include "bitarithm.h"
#include "periph_conf.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down
5 changes: 3 additions & 2 deletions cpu/atxmega/periph/pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

#include <avr/sleep.h>

#include "periph_conf.h"
#include "periph/pm.h"
#include "cpu_pm.h"
#include "irq.h"
#include "periph/pm.h"
#include "periph_conf.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down
10 changes: 4 additions & 6 deletions cpu/atxmega/periph/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
* @}
*/

#include <avr/interrupt.h>

#include <assert.h>
#include <avr/interrupt.h>

#include "board.h"
#include "cpu.h"
#include "cpu_pm.h"
#include "thread.h"

#include "irq.h"
#include "periph/timer.h"

#include "board.h"
#include "periph_conf.h"
#include "thread.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down
5 changes: 3 additions & 2 deletions cpu/atxmega/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
#include "board.h"
#include "cpu.h"
#include "cpu_pm.h"
#include "irq.h"
#include "periph/gpio.h"
#include "periph/uart.h"
#include "sched.h"
#include "thread.h"
#include "periph/uart.h"
#include "periph/gpio.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down
1 change: 1 addition & 0 deletions drivers/atwinc15x0/atwinc15x0_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "driver/include/m2m_wifi.h"

#include "assert.h"
#include "irq.h"
#include "log.h"
#include "net/netdev/eth.h"
#include "od.h"
Expand Down
4 changes: 2 additions & 2 deletions drivers/sgp30/sgp30.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*/
#include <string.h>

#include "checksum/crc8.h"
#include "irq.h"
#include "sgp30.h"
#include "sgp30_constants.h"
#include "sgp30_params.h"

#include "checksum/crc8.h"
#include "ztimer.h"

#define ENABLE_DEBUG 0
Expand Down
5 changes: 2 additions & 3 deletions drivers/sm_pwm_01c/sm_pwm_01c.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
#include <assert.h>
#include <string.h>

#include "irq.h"
#include "log.h"
#include "ztimer.h"

#include "periph/gpio.h"

#include "sm_pwm_01c.h"
#include "sm_pwm_01c_params.h"
#include "ztimer.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down
1 change: 1 addition & 0 deletions pkg/driver_bme680/contrib/bme680_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "bme680.h"
#include "bme680_hal.h"
#include "irq.h"

#ifdef MODULE_PERIPH_I2C
#include "periph/i2c.h"
Expand Down
2 changes: 2 additions & 0 deletions sys/riotboot/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

#include <string.h>

#include "irq.h"
#include "periph/pm.h"
#include "riotboot/magic.h"

Expand Down
3 changes: 2 additions & 1 deletion tests/periph_gpio_ll/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include <string.h>
#include <stdlib.h>

#include "irq.h"
#include "mutex.h"
#include "periph/gpio_ll.h"
#include "periph/gpio_ll_irq.h"
#include "test_utils/expect.h"
#include "ztimer.h"
#include "timex.h"
#include "ztimer.h"

#ifndef LOW_ROM
#define LOW_ROM 0
Expand Down

0 comments on commit 3227fb3

Please # to comment.