Lazuli
Loading...
Searching...
No Matches
mutex.c
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-only
3 * This file is part of Lazuli.
4 */
5
15#include <Lazuli/config.h>
16#include <Lazuli/mutex.h>
17
19#include <Lazuli/sys/kernel.h>
20#include <Lazuli/sys/memory.h>
22#include <Lazuli/sys/task.h>
23
30static void
31InitMutex(Lz_Mutex * const mutex, const Lz_Mutex * const initValue)
32{
34 if (NULL == mutex) {
36 }
37 }
38
39 Memory_Copy(initValue, mutex, sizeof(Lz_Mutex));
40}
41
47static void
57
61void
62Lz_Mutex_Init(Lz_Mutex * const mutex)
63{
64 const Lz_Mutex initValueUnlocked = LZ_MUTEX_INIT;
65
66 InitMutex(mutex, &initValueUnlocked);
67}
68
69void
71{
72 const Lz_Mutex initValueLocked = LZ_MUTEX_INIT_LOCKED;
73
74 InitMutex(mutex, &initValueLocked);
75}
76
77void
78Lz_Mutex_Lock(Lz_Mutex * const mutex)
79{
81 if (NULL == mutex) {
83 }
84 }
85
86 while (!Arch_TryAcquireLock(&(mutex->lock))) {
87 WaitMutex(mutex);
88 }
89}
90
91void
93{
95 if (NULL == mutex) {
97 }
98 }
99
101 mutex->lock = 0;
104}
105
Architecture Abstraction API.
void Arch_DisableInterrupts(void)
Disable all interrupts.
bool Arch_TryAcquireLock(volatile uint8_t *const lock)
Try to acquire a lock by atomically changing the value pointed by the lock parameter.
void Arch_EnableInterrupts(void)
Enable all interrupts.
#define NULL
NULL pointer.
Definition common.h:70
Include appropriate config file.
const bool LZ_CONFIG_CHECK_NULL_PARAMETERS_IN_MUTEXES
When 1, always check for NULL functions parameters in mutexes implementation.
void Kernel_ManageFailure(void)
Manage a failure that can happen in a function call.
Definition kernel.c:95
Kernel symbols definition.
void Memory_Copy(const void *source, void *destination, const size_t size)
Copy bytes from one location to another in main memory.
Definition memory.c:73
Memory management API.
void Lz_Mutex_Unlock(Lz_Mutex *const mutex)
Unlock the mutex and leave critical section.
Definition mutex.c:92
static void WaitMutex(Lz_Mutex *const mutex)
Set the task to wait for a given mutex to be unlocked.
Definition mutex.c:48
void Lz_Mutex_InitLocked(Lz_Mutex *const mutex)
Initialize an already allocated Lz_Mutex.
Definition mutex.c:70
void Lz_Mutex_Init(Lz_Mutex *const mutex)
Initialize an already allocated Lz_Mutex.
Definition mutex.c:62
static void InitMutex(Lz_Mutex *const mutex, const Lz_Mutex *const initValue)
Initialize a mutex in a given state.
Definition mutex.c:31
void Lz_Mutex_Lock(Lz_Mutex *const mutex)
Lock the mutex and enter critical section.
Definition mutex.c:78
Mutexes interface.
#define LZ_MUTEX_INIT_LOCKED
Define the value to initialize a Lz_Mutex in the locked state.
Definition mutex.h:47
#define LZ_MUTEX_INIT
Define the value to initialize a Lz_Mutex in the unlocked state.
Definition mutex.h:39
Task * Scheduler_GetCurrentTask(void)
Get a pointer to the current running task.
Definition scheduler.c:726
static Task * currentTask
A pointer to the current running task.
Definition scheduler.c:33
void Scheduler_SleepUntilEndOfTimeSlice(void)
Put the current task to sleep until the end of its time slice.
Definition scheduler.c:732
void Scheduler_WakeupTasksWaitingMutex(Lz_Mutex *const mutex)
Wake up all tasks waiting for a mutex.
Definition scheduler.c:700
Lazuli scheduler interface.
Represents a mutex.
Definition mutex.h:28
volatile uint8_t lock
The mutex lock
Definition mutex.h:29
Represents a task.
Definition task.h:82
void * taskToSchedulerMessageParameter
A parameter that can accompany a taskToSchedulerMessage.
Definition task.h:187
volatile lz_task_to_scheduler_message_t taskToSchedulerMessage
The message the task has to pass to the scheduler for the next scheduling operation (i....
Definition task.h:180
Base task definition and context description.
#define WAIT_MUTEX
Wait for a mutex to be unlocked.
Definition task.h:63