Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
Mutex APIs

Data Structures

struct  k_mutex
 

Macros

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

Functions

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

Detailed Description

Macro Definition Documentation

◆ K_MUTEX_DEFINE

#define K_MUTEX_DEFINE (   name)

#include <include/kernel.h>

Value:
Z_MUTEX_INITIALIZER(name)
#define STRUCT_SECTION_ITERABLE(struct_type, name)
Defines a new iterable section.
Definition: common.h:199
Definition: kernel.h:2680

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 <include/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 <include/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 <include/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