PatchworkOS
Loading...
Searching...
No Matches
mutex.c
Go to the documentation of this file.
2
3#include <kernel/log/log.h>
6
7#include <errno.h>
8#include <stdlib.h>
9
16
17static bool initialized = false;
20
21static atomic_int32_t newMutexId = ATOMIC_VAR_INIT(1);
22
24{
25 if (!initialized)
26 {
28 initialized = true;
29 }
30
31 if (syncLevel < currentSyncLevel)
32 {
33 LOG_ERR("Attempted to acquire a mutex with a lower SyncLevel than the current SyncLevel\n");
34 errno = EDEADLK;
35 return ERR;
36 }
37
39 if (entry == NULL)
40 {
41 return ERR;
42 }
43 list_entry_init(&entry->entry);
44 entry->id = id;
45 entry->syncLevel = syncLevel;
46
47 list_push(&mutexStack, &entry->entry);
48 currentSyncLevel = syncLevel;
49 return 0;
50}
51
53{
54 if (list_length(&mutexStack) == 0)
55 {
56 LOG_ERR("Attempted to release a mutex when none are held\n");
57 errno = EDEADLK;
58 return ERR;
59 }
60
62 if (topEntry->id != id)
63 {
64 LOG_ERR("Mutex release not in LIFO order\n");
65 errno = EDEADLK;
66 return ERR;
67 }
68
69 list_remove(&mutexStack, &topEntry->entry);
70 free(topEntry);
71
72 if (list_length(&mutexStack) == 0)
73 {
75 }
76 else
77 {
79 currentSyncLevel = newTopEntry->syncLevel;
80 }
81 return 0;
82}
83
88
93
95{
96 (void)timeout; // We ignore timeouts since we have the big mutex.
97
98 if (mutex == NULL)
99 {
100 errno = EINVAL;
101 return ERR;
102 }
103
104 // As mentioned, mutexes arent implemented since we have the big mutex, so we just pretend that
105 // we acquired it immediately.
106
107 if (aml_mutex_stack_push(*mutex, syncLevel) == ERR)
108 {
109 return ERR;
110 }
111 return 0;
112}
113
115{
116 if (mutex == NULL)
117 {
118 errno = EINVAL;
119 return ERR;
120 }
121
123 {
124 return ERR;
125 }
126 return 0;
127}
static aml_sync_level_t currentSyncLevel
Definition mutex.c:18
static list_t mutexStack
Definition mutex.c:19
static uint64_t aml_mutex_stack_push(aml_mutex_id_t id, aml_sync_level_t syncLevel)
Definition mutex.c:23
static bool initialized
Definition mutex.c:17
static uint64_t aml_mutex_stack_pop(aml_mutex_id_t id)
Definition mutex.c:52
static atomic_int32_t newMutexId
Definition mutex.c:21
static char id[MAX_NAME]
Definition dwm.c:20
uint8_t aml_sync_level_t
Definition named.h:135
uint64_t aml_mutex_id_t
Mutex id.
Definition mutex.h:28
uint64_t aml_mutex_release(aml_mutex_id_t *mutex)
Release a mutex.
Definition mutex.c:114
void aml_mutex_id_init(aml_mutex_id_t *mutex)
Create a new mutex and return its id.
Definition mutex.c:84
void aml_mutex_id_deinit(aml_mutex_id_t *mutex)
Destroy the mutex with the given id.
Definition mutex.c:89
uint64_t aml_mutex_acquire(aml_mutex_id_t *mutex, aml_sync_level_t syncLevel, clock_t timeout)
Acquire a mutex, blocking until it is available or the timeout is reached.
Definition mutex.c:94
#define LOG_ERR(format,...)
Definition log.h:89
#define EINVAL
Invalid argument.
Definition errno.h:142
#define EDEADLK
Resource deadlock would occur.
Definition errno.h:207
#define errno
Error number variable.
Definition errno.h:27
static uint64_t list_length(list_t *list)
Gets the length of the list.
Definition list.h:248
static list_entry_t * list_last(list_t *list)
Gets the last entry in the list without removing it.
Definition list.h:400
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:317
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:345
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:184
static void list_init(list_t *list)
Initializes a list.
Definition list.h:198
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
#define CONTAINER_OF(ptr, type, member)
Container of macro.
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static mtx_t mutex
Definition heap.c:35
#define ATOMIC_VAR_INIT(value)
Definition stdatomic.h:74
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:283
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Definition mutex.c:11
aml_sync_level_t syncLevel
Definition mutex.c:14
list_entry_t entry
Definition mutex.c:12
aml_mutex_id_t id
Definition mutex.c:13
A entry in a doubly linked list.
Definition list.h:38
A doubly linked list.
Definition list.h:51