PatchworkOS  19e446b
A non-POSIX operating system.
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#include <sys/list.h>
10
17
18static bool initialized = false;
21
22static atomic_int32_t newMutexId = ATOMIC_VAR_INIT(1);
23
25{
26 if (!initialized)
27 {
29 initialized = true;
30 }
31
32 if (syncLevel < currentSyncLevel)
33 {
34 LOG_ERR("Attempted to acquire a mutex with a lower SyncLevel than the current SyncLevel\n");
35 errno = EDEADLK;
36 return ERR;
37 }
38
40 if (entry == NULL)
41 {
42 return ERR;
43 }
44 list_entry_init(&entry->entry);
45 entry->id = id;
46 entry->syncLevel = syncLevel;
47
49 currentSyncLevel = syncLevel;
50 return 0;
51}
52
54{
56 {
57 LOG_ERR("Attempted to release a mutex when none are held\n");
58 errno = EDEADLK;
59 return ERR;
60 }
61
63 if (topEntry->id != id)
64 {
65 LOG_ERR("Mutex release not in LIFO order\n");
66 errno = EDEADLK;
67 return ERR;
68 }
69
70 list_remove(&topEntry->entry);
71 free(topEntry);
72
74 {
76 }
77 else
78 {
80 currentSyncLevel = newTopEntry->syncLevel;
81 }
82 return 0;
83}
84
86{
87 *mutex = atomic_fetch_add(&newMutexId, 1);
88}
89
91{
92 *mutex = 0;
93}
94
96{
97 UNUSED(timeout); // We ignore timeouts since we have the big mutex.
98
99 if (mutex == NULL)
100 {
101 errno = EINVAL;
102 return ERR;
103 }
104
105 // As mentioned, mutexes arent implemented since we have the big mutex, so we just pretend that
106 // we acquired it immediately.
107
108 if (aml_mutex_stack_push(*mutex, syncLevel) == ERR)
109 {
110 return ERR;
111 }
112 return 0;
113}
114
116{
117 if (mutex == NULL)
118 {
119 errno = EINVAL;
120 return ERR;
121 }
122
123 if (aml_mutex_stack_pop(*mutex) == ERR)
124 {
125 return ERR;
126 }
127 return 0;
128}
static char * id
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:115
void aml_mutex_id_init(aml_mutex_id_t *mutex)
Create a new mutex and return its id.
Definition mutex.c:85
void aml_mutex_id_deinit(aml_mutex_id_t *mutex)
Destroy the mutex with the given id.
Definition mutex.c:90
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:95
#define LOG_ERR(format,...)
Definition log.h:93
#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
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:96
static void list_remove(list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:290
static list_entry_t * list_last(list_t *list)
Gets the last entry in the list without removing it.
Definition list.h:423
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:322
static bool list_is_empty(list_t *list)
Checks if a list is empty.
Definition list.h:210
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:173
static void list_init(list_t *list)
Initializes a list.
Definition list.h:185
#define NULL
Pointer error value.
Definition NULL.h:25
#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 aml_sync_level_t currentSyncLevel
Definition mutex.c:19
static list_t mutexStack
Definition mutex.c:20
static uint64_t aml_mutex_stack_push(aml_mutex_id_t id, aml_sync_level_t syncLevel)
Definition mutex.c:24
static bool initialized
Definition mutex.c:18
static uint64_t aml_mutex_stack_pop(aml_mutex_id_t id)
Definition mutex.c:53
static atomic_int32_t newMutexId
Definition mutex.c:22
#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:12
aml_sync_level_t syncLevel
Definition mutex.c:15
list_entry_t entry
Definition mutex.c:13
aml_mutex_id_t id
Definition mutex.c:14
A entry in a doubly linked list.
Definition list.h:37
A doubly linked list.
Definition list.h:46