Lazuli
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 
18 #include <Lazuli/sys/arch/arch.h>
19 #include <Lazuli/sys/memory.h>
20 #include <Lazuli/sys/scheduler.h>
21 #include <Lazuli/sys/task.h>
22 
29 static void
30 InitMutex(Lz_Mutex * const mutex, const Lz_Mutex * const initValue)
31 {
33  if (NULL == mutex) {
34  Task_Abort();
35  }
36  }
37 
38  Memory_Copy(initValue, mutex, sizeof(Lz_Mutex));
39 }
40 
46 static void
47 WaitMutex(Lz_Mutex * const mutex)
48 {
50 
51  currentTask->taskToSchedulerMessage = WAIT_MUTEX;
52  currentTask->taskToSchedulerMessageParameter = mutex;
53 
55 }
56 
60 void
61 Lz_Mutex_Init(Lz_Mutex * const mutex)
62 {
63  const Lz_Mutex initValueUnlocked = LZ_MUTEX_INIT;
64 
65  InitMutex(mutex, &initValueUnlocked);
66 }
67 
68 void
70 {
71  const Lz_Mutex initValueLocked = LZ_MUTEX_INIT_LOCKED;
72 
73  InitMutex(mutex, &initValueLocked);
74 }
75 
76 void
77 Lz_Mutex_Lock(Lz_Mutex * const mutex)
78 {
80  if (NULL == mutex) {
81  Task_Abort();
82  }
83  }
84 
85  while (!Arch_TryAcquireLock(&(mutex->lock))) {
86  WaitMutex(mutex);
87  }
88 }
89 
90 void
91 Lz_Mutex_Unlock(Lz_Mutex * const mutex)
92 {
94  if (NULL == mutex) {
95  Task_Abort();
96  }
97  }
98 
100  mutex->lock = 0;
103 }
104 
const bool LZ_CONFIG_CHECK_NULL_PARAMETERS_IN_MUTEXES
When 1, always check for NULL functions parameters in mutexes implementation.
Task * Scheduler_GetCurrentTask(void)
Get a pointer to the current running task.
Definition: scheduler.c:720
Memory management API.
void Task_Abort(void)
Abort the calling task.
void Arch_DisableInterrupts(void)
Disable all interrupts.
static void InitMutex(Lz_Mutex *const mutex, const Lz_Mutex *const initValue)
Initialize a mutex in a given state.
Definition: mutex.c:30
void Arch_EnableInterrupts(void)
Enable all interrupts.
void Scheduler_SleepUntilEndOfTimeSlice(void)
Put the current task to sleep until the end of its time slice.
Definition: scheduler.c:726
Include appropriate config file.
#define LZ_MUTEX_INIT
Define the value to initialize a Lz_Mutex in the unlocked state.
Definition: mutex.h:39
#define WAIT_MUTEX
Wait for a mutex to be unlocked.
Definition: task.h:63
static Task * currentTask
A pointer to the current running task.
Definition: scheduler.c:33
Lazuli scheduler interface.
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:174
bool Arch_TryAcquireLock(volatile uint8_t *const lock)
Try to acquire a lock by atomically changing the value pointed by the lock parameter.
void Lz_Mutex_Init(Lz_Mutex *const mutex)
Initialize an already allocated Lz_Mutex.
Definition: mutex.c:61
void Lz_Mutex_Lock(Lz_Mutex *const mutex)
Lock the mutex and enter critical section.
Definition: mutex.c:77
static void WaitMutex(Lz_Mutex *const mutex)
Set the task to wait for a given mutex to be unlocked.
Definition: mutex.c:47
volatile uint8_t lock
The mutex lock.
Definition: mutex.h:29
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
Mutexes interface.
void Lz_Mutex_Unlock(Lz_Mutex *const mutex)
Unlock the mutex and leave critical section.
Definition: mutex.c:91
void Lz_Mutex_InitLocked(Lz_Mutex *const mutex)
Initialize an already allocated Lz_Mutex.
Definition: mutex.c:69
#define NULL
NULL pointer.
Definition: common.h:68
Base task definition and context description.
Architecture Abstraction API.
Represents a mutex.
Definition: mutex.h:28
void Scheduler_WakeupTasksWaitingMutex(Lz_Mutex *const mutex)
Wake up all tasks waiting for a mutex.
Definition: scheduler.c:694
#define LZ_MUTEX_INIT_LOCKED
Define the value to initialize a Lz_Mutex in the locked state.
Definition: mutex.h:47
Represents a task.
Definition: task.h:76
void * taskToSchedulerMessageParameter
A parameter that can accompany a taskToSchedulerMessage.
Definition: task.h:181