-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathOSMutex.c
96 lines (84 loc) · 2.13 KB
/
OSMutex.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
#include <dolphin/os/OSMutex.h>
void __OSUnlockAllMutex(OSThread* thread)
{
while (thread->queueMutex.head != NULL) {
OSMutex* head = thread->queueMutex.head;
OSMutex* next = head->link.next;
if (next == NULL) {
thread->queueMutex.tail = NULL;
} else {
next->link.prev = NULL;
}
thread->queueMutex.head = next;
head->count = 0;
head->thread = NULL;
OSWakeupThread(&head->queue);
}
}
bool __OSCheckMutex(OSMutex* mutex)
{
OSThread* thread;
OSThreadQueue* queue;
OSPriority priority = 0;
queue = &mutex->queue;
if (!(queue->head == NULL || queue->head->link.prev == NULL)) {
return false;
}
if (!(queue->tail == NULL || queue->tail->link.next == NULL)) {
return false;
}
for (thread = queue->head; thread; thread = thread->link.next) {
if (!(thread->link.next == NULL ||
thread == thread->link.next->link.prev))
{
return false;
}
if (!(thread->link.prev == NULL ||
thread == thread->link.prev->link.next))
{
return false;
}
if (thread->state != OS_THREAD_STATE_WAITING) {
return false;
}
if (thread->priority < priority) {
return false;
}
priority = thread->priority;
}
if (mutex->thread) {
if (mutex->count <= 0) {
return false;
}
} else {
if (0 != mutex->count) {
return false;
}
}
return true;
}
bool __OSCheckDeadLock(OSThread* thread)
{
OSMutex* mutex;
mutex = thread->mutex;
while (mutex && mutex->thread) {
if (mutex->thread == thread) {
return true;
}
mutex = mutex->thread->mutex;
}
return false;
}
bool __OSCheckMutexes(OSThread* thread)
{
OSMutex* mutex;
for (mutex = thread->queueMutex.head; mutex; mutex = mutex->link.next) {
if (mutex->thread != thread) {
return false;
}
if (!__OSCheckMutex(mutex)) {
return false;
}
}
return true;
}