-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmutex.c
More file actions
521 lines (422 loc) · 12.8 KB
/
mutex.c
File metadata and controls
521 lines (422 loc) · 12.8 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Mutex and Condition Variable Implementation
*
* This implementation provides non-recursive mutexes and condition variables
* that are independent of the semaphore module.
*/
#include <lib/libc.h>
#include <sys/mutex.h>
#include <sys/task.h>
#include "private/error.h"
#include "private/utils.h"
/* Validate mutex pointer and structure integrity */
static inline bool mutex_is_valid(const mutex_t *m)
{
return m && m->magic == MUTEX_MAGIC && m->waiters &&
(m->owner_tid == 0 || m->owner_tid < UINT16_MAX);
}
/* Validate condition variable pointer and structure integrity */
static inline bool cond_is_valid(const cond_t *c)
{
return c && c->magic == COND_MAGIC && c->waiters;
}
/* Invalidate mutex during destruction to prevent reuse */
static inline void mutex_invalidate(mutex_t *m)
{
if (m) {
m->magic = 0xDEADBEEF;
m->owner_tid = UINT16_MAX; /* Invalid TID */
}
}
/* Invalidate condition variable during destruction */
static inline void cond_invalidate(cond_t *c)
{
if (c)
c->magic = 0xDEADBEEF;
}
/* Remove current task from waiter list, avoiding the need to search through
* the entire list.
*/
static bool remove_self_from_waiters(list_t *waiters)
{
if (unlikely(!waiters || !kcb || !kcb->task_current ||
!kcb->task_current->data))
return false;
tcb_t *self = kcb->task_current->data;
/* Search for and remove self from waiters list */
list_node_t *curr = waiters->head->next;
while (curr && curr != waiters->tail) {
if (curr->data == self) {
list_remove(waiters, curr);
free(curr);
return true;
}
curr = curr->next;
}
return false;
}
int32_t mo_mutex_init(mutex_t *m)
{
if (unlikely(!m))
return ERR_FAIL;
/* Initialize to known safe state */
m->waiters = NULL;
m->owner_tid = 0;
m->magic = 0;
/* Create waiters list */
m->waiters = list_create();
if (unlikely(!m->waiters))
return ERR_FAIL;
/* Mark as valid atomically (last step) */
m->owner_tid = 0;
m->magic = MUTEX_MAGIC;
return ERR_OK;
}
int32_t mo_mutex_destroy(mutex_t *m)
{
if (!m)
return ERR_OK; /* Destroying NULL is no-op */
if (unlikely(!mutex_is_valid(m)))
return ERR_FAIL;
NOSCHED_ENTER();
/* Check if any tasks are waiting */
if (unlikely(!list_is_empty(m->waiters))) {
NOSCHED_LEAVE();
return ERR_TASK_BUSY;
}
/* Check if mutex is still owned */
if (unlikely(m->owner_tid != 0)) {
NOSCHED_LEAVE();
return ERR_TASK_BUSY;
}
/* Invalidate atomically and cleanup */
mutex_invalidate(m);
list_t *waiters = m->waiters;
m->waiters = NULL;
m->owner_tid = 0;
NOSCHED_LEAVE();
/* Clean up resources outside critical section */
list_destroy(waiters);
return ERR_OK;
}
int32_t mo_mutex_lock(mutex_t *m)
{
if (unlikely(!mutex_is_valid(m)))
panic(ERR_SEM_OPERATION); /* Invalid mutex is programming error */
uint16_t self_tid = mo_task_id();
NOSCHED_ENTER();
/* Non-recursive: reject if caller already owns it */
if (unlikely(m->owner_tid == self_tid)) {
NOSCHED_LEAVE();
return ERR_TASK_BUSY;
}
/* Fast path: mutex is free, acquire immediately */
if (likely(m->owner_tid == 0)) {
m->owner_tid = self_tid;
NOSCHED_LEAVE();
return ERR_OK;
}
/* Slow path: mutex is owned, must block atomically */
mutex_block_atomic(m->waiters);
/* When we return here, we've been woken by mo_mutex_unlock()
* and ownership has been transferred to us. */
return ERR_OK;
}
int32_t mo_mutex_trylock(mutex_t *m)
{
if (unlikely(!mutex_is_valid(m)))
return ERR_FAIL;
uint16_t self_tid = mo_task_id();
int32_t result = ERR_TASK_BUSY;
NOSCHED_ENTER();
if (unlikely(m->owner_tid == self_tid)) {
/* Already owned by caller (non-recursive) */
result = ERR_TASK_BUSY;
} else if (m->owner_tid == 0) {
/* Mutex is free, acquire it */
m->owner_tid = self_tid;
result = ERR_OK;
}
/* else: owned by someone else, return ERR_TASK_BUSY */
NOSCHED_LEAVE();
return result;
}
int32_t mo_mutex_timedlock(mutex_t *m, uint32_t ticks)
{
if (unlikely(!mutex_is_valid(m)))
return ERR_FAIL;
if (ticks == 0)
return mo_mutex_trylock(m); /* Zero timeout = try only */
uint16_t self_tid = mo_task_id();
NOSCHED_ENTER();
/* Non-recursive check */
if (unlikely(m->owner_tid == self_tid)) {
NOSCHED_LEAVE();
return ERR_TASK_BUSY;
}
/* Fast path: mutex is free */
if (m->owner_tid == 0) {
m->owner_tid = self_tid;
NOSCHED_LEAVE();
return ERR_OK;
}
/* Slow path: must block with timeout using delay mechanism */
tcb_t *self = kcb->task_current->data;
if (unlikely(!list_pushback(m->waiters, self))) {
NOSCHED_LEAVE();
panic(ERR_SEM_OPERATION);
}
/* Set up timeout using task delay mechanism */
self->delay = ticks;
self->state = TASK_BLOCKED;
NOSCHED_LEAVE();
/* Yield and let the scheduler handle timeout via delay mechanism */
mo_task_yield();
/* Check result after waking up */
int32_t result;
NOSCHED_ENTER();
if (self->state == TASK_BLOCKED) {
/* We woke up due to timeout, not mutex unlock */
if (remove_self_from_waiters(m->waiters)) {
self->state = TASK_READY;
result = ERR_TIMEOUT;
} else {
/* Race condition: we were both timed out and unlocked */
/* Check if we now own the mutex */
result = (m->owner_tid == self_tid) ? ERR_OK : ERR_TIMEOUT;
}
} else {
/* We were woken by mutex unlock - check ownership */
result = (m->owner_tid == self_tid) ? ERR_OK : ERR_FAIL;
}
NOSCHED_LEAVE();
return result;
}
int32_t mo_mutex_unlock(mutex_t *m)
{
if (unlikely(!mutex_is_valid(m)))
return ERR_FAIL;
uint16_t self_tid = mo_task_id();
NOSCHED_ENTER();
/* Verify caller owns the mutex */
if (unlikely(m->owner_tid != self_tid)) {
NOSCHED_LEAVE();
return ERR_NOT_OWNER;
}
/* Check for waiting tasks */
if (list_is_empty(m->waiters)) {
/* No waiters - mutex becomes free */
m->owner_tid = 0;
} else {
/* Transfer ownership to next waiter (FIFO) */
tcb_t *next_owner = (tcb_t *) list_pop(m->waiters);
if (likely(next_owner)) {
/* Validate task state before waking */
if (likely(next_owner->state == TASK_BLOCKED)) {
m->owner_tid = next_owner->id;
next_owner->state = TASK_READY;
/* Clear any pending timeout since we're granting ownership */
next_owner->delay = 0;
} else {
/* Task state inconsistency */
panic(ERR_SEM_OPERATION);
}
} else {
/* Should not happen if list was not empty */
m->owner_tid = 0;
}
}
NOSCHED_LEAVE();
return ERR_OK;
}
bool mo_mutex_owned_by_current(mutex_t *m)
{
if (unlikely(!mutex_is_valid(m)))
return false;
return (m->owner_tid == mo_task_id());
}
int32_t mo_mutex_waiting_count(mutex_t *m)
{
if (unlikely(!mutex_is_valid(m)))
return -1;
int32_t count;
NOSCHED_ENTER();
count = m->waiters ? (int32_t) m->waiters->length : 0;
NOSCHED_LEAVE();
return count;
}
int32_t mo_cond_init(cond_t *c)
{
if (unlikely(!c))
return ERR_FAIL;
/* Initialize to known safe state */
c->waiters = NULL;
c->magic = 0;
/* Create waiters list */
c->waiters = list_create();
if (unlikely(!c->waiters))
return ERR_FAIL;
/* Mark as valid atomically */
c->magic = COND_MAGIC;
return ERR_OK;
}
int32_t mo_cond_destroy(cond_t *c)
{
if (!c)
return ERR_OK; /* Destroying NULL is no-op */
if (unlikely(!cond_is_valid(c)))
return ERR_FAIL;
NOSCHED_ENTER();
/* Check if any tasks are waiting */
if (unlikely(!list_is_empty(c->waiters))) {
NOSCHED_LEAVE();
return ERR_TASK_BUSY;
}
/* Invalidate atomically and cleanup */
cond_invalidate(c);
list_t *waiters = c->waiters;
c->waiters = NULL;
NOSCHED_LEAVE();
/* Clean up resources outside critical section */
list_destroy(waiters);
return ERR_OK;
}
int32_t mo_cond_wait(cond_t *c, mutex_t *m)
{
if (unlikely(!cond_is_valid(c) || !mutex_is_valid(m))) {
/* Invalid parameters are programming errors */
panic(ERR_SEM_OPERATION);
}
/* Verify caller owns the mutex */
if (unlikely(!mo_mutex_owned_by_current(m)))
return ERR_NOT_OWNER;
tcb_t *self = kcb->task_current->data;
/* Atomically add to wait list */
NOSCHED_ENTER();
if (unlikely(!list_pushback(c->waiters, self))) {
NOSCHED_LEAVE();
panic(ERR_SEM_OPERATION);
}
self->state = TASK_BLOCKED;
NOSCHED_LEAVE();
/* Release mutex */
int32_t unlock_result = mo_mutex_unlock(m);
if (unlikely(unlock_result != ERR_OK)) {
/* Failed to unlock - remove from wait list and restore state */
NOSCHED_ENTER();
remove_self_from_waiters(c->waiters);
self->state = TASK_READY;
NOSCHED_LEAVE();
return unlock_result;
}
/* Yield and wait to be signaled */
mo_task_yield();
/* Re-acquire mutex before returning */
return mo_mutex_lock(m);
}
int32_t mo_cond_timedwait(cond_t *c, mutex_t *m, uint32_t ticks)
{
if (unlikely(!cond_is_valid(c) || !mutex_is_valid(m)))
panic(ERR_SEM_OPERATION);
if (unlikely(!mo_mutex_owned_by_current(m)))
return ERR_NOT_OWNER;
if (ticks == 0) {
/* Zero timeout - don't wait at all */
return ERR_TIMEOUT;
}
tcb_t *self = kcb->task_current->data;
/* Atomically add to wait list with timeout */
NOSCHED_ENTER();
if (unlikely(!list_pushback(c->waiters, self))) {
NOSCHED_LEAVE();
panic(ERR_SEM_OPERATION);
}
self->delay = ticks;
self->state = TASK_BLOCKED;
NOSCHED_LEAVE();
/* Release mutex */
int32_t unlock_result = mo_mutex_unlock(m);
if (unlikely(unlock_result != ERR_OK)) {
/* Failed to unlock - cleanup and restore */
NOSCHED_ENTER();
remove_self_from_waiters(c->waiters);
self->state = TASK_READY;
self->delay = 0;
NOSCHED_LEAVE();
return unlock_result;
}
/* Yield and wait for signal or timeout */
mo_task_yield();
/* Determine why we woke up */
int32_t wait_status;
NOSCHED_ENTER();
if (self->state == TASK_BLOCKED) {
/* Timeout occurred - remove from wait list */
remove_self_from_waiters(c->waiters);
self->state = TASK_READY;
self->delay = 0;
wait_status = ERR_TIMEOUT;
} else {
/* Signaled successfully */
wait_status = ERR_OK;
}
NOSCHED_LEAVE();
/* Re-acquire mutex regardless of timeout status */
int32_t lock_result = mo_mutex_lock(m);
/* Return timeout status if wait timed out, otherwise lock result */
return (wait_status == ERR_TIMEOUT) ? ERR_TIMEOUT : lock_result;
}
int32_t mo_cond_signal(cond_t *c)
{
if (unlikely(!cond_is_valid(c)))
return ERR_FAIL;
NOSCHED_ENTER();
if (!list_is_empty(c->waiters)) {
tcb_t *waiter = (tcb_t *) list_pop(c->waiters);
if (likely(waiter)) {
/* Validate task state before waking */
if (likely(waiter->state == TASK_BLOCKED)) {
waiter->state = TASK_READY;
/* Clear any pending timeout since we're signaling */
waiter->delay = 0;
} else {
/* Task state inconsistency */
panic(ERR_SEM_OPERATION);
}
}
}
NOSCHED_LEAVE();
return ERR_OK;
}
int32_t mo_cond_broadcast(cond_t *c)
{
if (unlikely(!cond_is_valid(c)))
return ERR_FAIL;
NOSCHED_ENTER();
/* Wake all waiting tasks */
while (!list_is_empty(c->waiters)) {
tcb_t *waiter = (tcb_t *) list_pop(c->waiters);
if (likely(waiter)) {
/* Validate task state before waking */
if (likely(waiter->state == TASK_BLOCKED)) {
waiter->state = TASK_READY;
/* Clear any pending timeout since we're broadcasting */
waiter->delay = 0;
} else {
/* Task state inconsistency */
panic(ERR_SEM_OPERATION);
}
}
}
NOSCHED_LEAVE();
return ERR_OK;
}
int32_t mo_cond_waiting_count(cond_t *c)
{
if (unlikely(!cond_is_valid(c)))
return -1;
int32_t count;
NOSCHED_ENTER();
count = c->waiters ? (int32_t) c->waiters->length : 0;
NOSCHED_LEAVE();
return count;
}