forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.c
266 lines (227 loc) · 7.84 KB
/
mutex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
* 2013 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup core_sync
* @{
*
* @file
* @brief Kernel mutex implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
*
* @}
*/
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include "mutex.h"
#include "thread.h"
#include "sched.h"
#include "irq.h"
#include "list.h"
#define ENABLE_DEBUG 0
#include "debug.h"
#if MAXTHREADS > 1
/**
* @brief Block waiting for a locked mutex
* @pre IRQs are disabled
* @post IRQs are restored to @p irq_state
* @post The calling thread is no longer waiting for the mutex, either
* because it got the mutex, or because the operation was cancelled
* (only possible for @ref mutex_lock_cancelable)
*
* Most applications don't use @ref mutex_lock_cancelable. Inlining this
* function into both @ref mutex_lock and @ref mutex_lock_cancelable is,
* therefore, beneficial for the majority of applications.
*/
static inline __attribute__((always_inline)) void _block(mutex_t *mutex,
unsigned irq_state)
{
thread_t *me = thread_get_active();
/* Fail visibly even if a blocking action is called from somewhere where
* it's subtly not allowed, eg. board_init */
assert(me != NULL);
DEBUG("PID[%" PRIkernel_pid "] mutex_lock() Adding node to mutex queue: "
"prio: %" PRIu32 "\n", thread_getpid(), (uint32_t)me->priority);
sched_set_status(me, STATUS_MUTEX_BLOCKED);
if (mutex->queue.next == MUTEX_LOCKED) {
mutex->queue.next = (list_node_t *)&me->rq_entry;
mutex->queue.next->next = NULL;
}
else {
thread_add_to_list(&mutex->queue, me);
}
#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
thread_t *owner = thread_get(mutex->owner);
if ((owner) && (owner->priority > me->priority)) {
DEBUG("PID[%" PRIkernel_pid "] prio of %" PRIkernel_pid
": %u --> %u\n",
thread_getpid(), mutex->owner,
(unsigned)owner->priority, (unsigned)me->priority);
sched_change_priority(owner, me->priority);
}
#endif
irq_restore(irq_state);
thread_yield_higher();
/* We were woken up by scheduler. Waker removed us from queue. */
}
bool mutex_lock_internal(mutex_t *mutex, bool block)
{
unsigned irq_state = irq_disable();
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_internal(block=%u).\n",
thread_getpid(), (unsigned)block);
if (mutex->queue.next == NULL) {
/* mutex is unlocked. */
mutex->queue.next = MUTEX_LOCKED;
#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
thread_t *me = thread_get_active();
mutex->owner = me->pid;
mutex->owner_original_priority = me->priority;
#endif
DEBUG("PID[%" PRIkernel_pid "] mutex_lock(): early out.\n",
thread_getpid());
irq_restore(irq_state);
}
else {
if (!block) {
irq_restore(irq_state);
return false;
}
_block(mutex, irq_state);
}
return true;
}
int mutex_lock_cancelable(mutex_cancel_t *mc)
{
unsigned irq_state = irq_disable();
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable()\n",
thread_getpid());
if (mc->cancelled) {
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable cancelled "
"early.\n", thread_getpid());
irq_restore(irq_state);
return -ECANCELED;
}
mutex_t *mutex = mc->mutex;
if (mutex->queue.next == NULL) {
/* mutex is unlocked. */
mutex->queue.next = MUTEX_LOCKED;
#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
thread_t *me = thread_get_active();
mutex->owner = me->pid;
mutex->owner_original_priority = me->priority;
#endif
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable() early out.\n",
thread_getpid());
irq_restore(irq_state);
return 0;
}
else {
_block(mutex, irq_state);
if (mc->cancelled) {
DEBUG("PID[%" PRIkernel_pid "] mutex_lock_cancelable() "
"cancelled.\n", thread_getpid());
}
return (mc->cancelled) ? -ECANCELED : 0;
}
}
void mutex_unlock(mutex_t *mutex)
{
unsigned irqstate = irq_disable();
DEBUG("PID[%" PRIkernel_pid "] mutex_unlock(): queue.next: %p\n",
thread_getpid(), (void *)mutex->queue.next);
if (mutex->queue.next == NULL) {
/* the mutex was not locked */
irq_restore(irqstate);
return;
}
if (mutex->queue.next == MUTEX_LOCKED) {
mutex->queue.next = NULL;
/* the mutex was locked and no thread was waiting for it */
irq_restore(irqstate);
return;
}
list_node_t *next = list_remove_head(&mutex->queue);
thread_t *process = container_of((clist_node_t *)next, thread_t, rq_entry);
DEBUG("PID[%" PRIkernel_pid "] mutex_unlock(): waking up waiting thread %"
PRIkernel_pid "\n", thread_getpid(), process->pid);
sched_set_status(process, STATUS_PENDING);
if (!mutex->queue.next) {
mutex->queue.next = MUTEX_LOCKED;
}
uint16_t process_priority = process->priority;
#ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
thread_t *owner = thread_get(mutex->owner);
if ((owner) && (owner->priority != mutex->owner_original_priority)) {
DEBUG("PID[%" PRIkernel_pid "] prio %u --> %u\n",
owner->pid,
(unsigned)owner->priority, (unsigned)owner->priority);
sched_change_priority(owner, mutex->owner_original_priority);
}
#endif
irq_restore(irqstate);
sched_switch(process_priority);
}
void mutex_unlock_and_sleep(mutex_t *mutex)
{
DEBUG("PID[%" PRIkernel_pid "] mutex_unlock_and_sleep(): queue.next: %p\n",
thread_getpid(), (void *)mutex->queue.next);
unsigned irqstate = irq_disable();
if (mutex->queue.next) {
if (mutex->queue.next == MUTEX_LOCKED) {
mutex->queue.next = NULL;
}
else {
list_node_t *next = list_remove_head(&mutex->queue);
thread_t *process = container_of((clist_node_t *)next, thread_t,
rq_entry);
DEBUG("PID[%" PRIkernel_pid "] mutex_unlock_and_sleep(): waking up "
"waiter.\n", process->pid);
sched_set_status(process, STATUS_PENDING);
if (!mutex->queue.next) {
mutex->queue.next = MUTEX_LOCKED;
}
}
}
DEBUG("PID[%" PRIkernel_pid "] mutex_unlock_and_sleep(): going to sleep.\n",
thread_getpid());
sched_set_status(thread_get_active(), STATUS_SLEEPING);
irq_restore(irqstate);
thread_yield_higher();
}
void mutex_cancel(mutex_cancel_t *mc)
{
unsigned irq_state = irq_disable();
mc->cancelled = 1;
mutex_t *mutex = mc->mutex;
thread_t *thread = mc->thread;
if (thread_is_active(thread)) {
/* thread is still running or about to run, so it will check
* `mc-cancelled` in time */
irq_restore(irq_state);
return;
}
if ((mutex->queue.next != MUTEX_LOCKED)
&& (mutex->queue.next != NULL)
&& list_remove(&mutex->queue, (list_node_t *)&thread->rq_entry)) {
/* Thread was queued and removed from list, wake it up */
if (mutex->queue.next == NULL) {
mutex->queue.next = MUTEX_LOCKED;
}
sched_set_status(thread, STATUS_PENDING);
irq_restore(irq_state);
sched_switch(thread->priority);
return;
}
irq_restore(irq_state);
}
#else /* MAXTHREADS < 2 */
typedef int dont_be_pedantic;
#endif