Zephyr API Documentation  3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Mutex APIs

Data Structures

struct  k_mutex
 Mutex Structure. More...
 

Macros

#define K_MUTEX_DEFINE(name)
 Statically define and initialize a mutex.
 

Functions

int k_mutex_init (struct k_mutex *mutex)
 Initialize a mutex.
 
int k_mutex_lock (struct k_mutex *mutex, k_timeout_t timeout)
 Lock a mutex.
 
int k_mutex_unlock (struct k_mutex *mutex)
 Unlock a mutex.
 

Detailed Description

Macro Definition Documentation

◆ K_MUTEX_DEFINE

#define K_MUTEX_DEFINE (   name)

#include <zephyr/kernel.h>

Value:
Z_MUTEX_INITIALIZER(name)
#define STRUCT_SECTION_ITERABLE(struct_type, varname)
Defines a new element for an iterable section.
Definition: iterable_sections.h:216
Mutex Structure.
Definition: kernel.h:2917

Statically define and initialize a mutex.

The mutex can be accessed outside the module where it is defined using:

extern struct k_mutex <name>;
Parameters
nameName of the mutex.

Function Documentation

◆ k_mutex_init()

int k_mutex_init ( struct k_mutex mutex)

#include <zephyr/kernel.h>

Initialize a mutex.

This routine initializes a mutex object, prior to its first use.

Upon completion, the mutex is available and does not have an owner.

Parameters
mutexAddress of the mutex.
Return values
0Mutex object created

◆ k_mutex_lock()

int k_mutex_lock ( struct k_mutex mutex,
k_timeout_t  timeout 
)

#include <zephyr/kernel.h>

Lock a mutex.

This routine locks mutex. If the mutex is locked by another thread, the calling thread waits until the mutex becomes available or until a timeout occurs.

A thread is permitted to lock a mutex it has already locked. The operation completes immediately and the lock count is increased by 1.

Mutexes may not be locked in ISRs.

Parameters
mutexAddress of the mutex.
timeoutWaiting period to lock the mutex, or one of the special values K_NO_WAIT and K_FOREVER.
Return values
0Mutex locked.
-EBUSYReturned without waiting.
-EAGAINWaiting period timed out.

◆ k_mutex_unlock()

int k_mutex_unlock ( struct k_mutex mutex)

#include <zephyr/kernel.h>

Unlock a mutex.

This routine unlocks mutex. The mutex must already be locked by the calling thread.

The mutex cannot be claimed by another thread until it has been unlocked by the calling thread as many times as it was previously locked by that thread.

Mutexes may not be unlocked in ISRs, as mutexes must only be manipulated in thread context due to ownership and priority inheritance semantics.

Parameters
mutexAddress of the mutex.
Return values
0Mutex unlocked.
-EPERMThe current thread does not own the mutex
-EINVALThe mutex is not locked