blob: 7f4683a6d34432ac06c368d707a1f5d6c4928be1 (
plain)
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
|
#include <minix/mthread.h>
#include "global.h"
#include "proto.h"
#ifdef MTHREAD_STRICT
static struct __mthread_mutex *vm_front, *vm_rear;
static void mthread_mutex_add(mthread_mutex_t *m);
static void mthread_mutex_remove(mthread_mutex_t *m);
#else
# define mthread_mutex_add(m) ((*m)->mm_magic = MTHREAD_INIT_MAGIC)
# define mthread_mutex_remove(m) ((*m)->mm_magic = MTHREAD_NOT_INUSE)
#endif
/*===========================================================================*
* mthread_init_valid_mutexes *
*===========================================================================*/
void mthread_init_valid_mutexes(void)
{
#ifdef MTHREAD_STRICT
/* Initialize list of valid mutexes */
vm_front = vm_rear = NULL;
#endif
}
/*===========================================================================*
* mthread_mutex_add *
*===========================================================================*/
#ifdef MTHREAD_STRICT
static void mthread_mutex_add(m)
mthread_mutex_t *m;
{
/* Add mutex to list of valid, initialized mutexes */
if (vm_front == NULL) { /* Empty list */
vm_front = *m;
(*m)->mm_prev = NULL;
} else {
vm_rear->mm_next = *m;
(*m)->mm_prev = vm_rear;
}
(*m)->mm_next = NULL;
vm_rear = *m;
}
#endif
/*===========================================================================*
* mthread_mutex_destroy *
*===========================================================================*/
int mthread_mutex_destroy(mutex)
mthread_mutex_t *mutex;
{
/* Invalidate mutex and deallocate resources. */
mthread_thread_t t;
mthread_tcb_t *tcb;
if (mutex == NULL)
return(EINVAL);
if (!mthread_mutex_valid(mutex))
return(EINVAL);
else if ((*mutex)->mm_owner != NO_THREAD)
return(EBUSY);
/* Check if this mutex is not associated with a condition */
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
tcb = mthread_find_tcb(t);
if (tcb->m_state == MS_CONDITION) {
if (tcb->m_cond != NULL && tcb->m_cond->mc_mutex == *mutex)
return(EBUSY);
}
}
/* Not in use; invalidate it */
mthread_mutex_remove(mutex);
free(*mutex);
*mutex = NULL;
return(0);
}
/*===========================================================================*
* mthread_mutex_init *
*===========================================================================*/
int mthread_mutex_init(mutex, mattr)
mthread_mutex_t *mutex; /* Mutex that is to be initialized */
mthread_mutexattr_t *mattr; /* Mutex attribute */
{
/* Initialize the mutex to a known state. Attributes are not supported */
struct __mthread_mutex *m;
if (mutex == NULL)
return(EAGAIN);
else if (mattr != NULL)
return(ENOSYS);
#ifdef MTHREAD_STRICT
else if (mthread_mutex_valid(mutex))
return(EBUSY);
#endif
else if ((m = malloc(sizeof(struct __mthread_mutex))) == NULL)
return(ENOMEM);
mthread_queue_init(&m->mm_queue);
m->mm_owner = NO_THREAD;
*mutex = (mthread_mutex_t) m;
mthread_mutex_add(mutex); /* Validate mutex; mutex now in use */
return(0);
}
/*===========================================================================*
* mthread_mutex_lock *
*===========================================================================*/
int mthread_mutex_lock(mutex)
mthread_mutex_t *mutex; /* Mutex that is to be locked */
{
/* Try to lock this mutex. If already locked, append the current thread to
* FIFO queue associated with this mutex and suspend the thread. */
struct __mthread_mutex *m;
if (mutex == NULL)
return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
if (!mthread_mutex_valid(&m))
return(EINVAL);
else if (m->mm_owner == NO_THREAD) { /* Not locked */
m->mm_owner = current_thread;
} else if (m->mm_owner == current_thread) {
return(EDEADLK);
} else {
mthread_queue_add(&m->mm_queue, current_thread);
mthread_suspend(MS_MUTEX);
}
/* When we get here we acquired the lock. */
return(0);
}
/*===========================================================================*
* mthread_mutex_remove *
*===========================================================================*/
#ifdef MTHREAD_STRICT
static void mthread_mutex_remove(m)
mthread_mutex_t *m;
{
/* Remove mutex from list of valid, initialized mutexes */
if ((*m)->mm_prev == NULL)
vm_front = (*m)->mm_next;
else
(*m)->mm_prev->mm_next = (*m)->mm_next;
if ((*m)->mm_next == NULL)
vm_rear = (*m)->mm_prev;
else
(*m)->mm_next->mm_prev = (*m)->mm_prev;
}
#endif
/*===========================================================================*
* mthread_mutex_trylock *
*===========================================================================*/
int mthread_mutex_trylock(mutex)
mthread_mutex_t *mutex; /* Mutex that is to be locked */
{
/* Try to lock this mutex and return OK. If already locked, return error. */
struct __mthread_mutex *m;
if (mutex == NULL)
return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
if (!mthread_mutex_valid(&m))
return(EINVAL);
else if (m->mm_owner == current_thread)
return(EDEADLK);
else if (m->mm_owner == NO_THREAD) {
m->mm_owner = current_thread;
return(0);
}
return(EBUSY);
}
/*===========================================================================*
* mthread_mutex_unlock *
*===========================================================================*/
int mthread_mutex_unlock(mutex)
mthread_mutex_t *mutex; /* Mutex that is to be unlocked */
{
/* Unlock a previously locked mutex. If there is a pending lock for this mutex
* by another thread, mark that thread runnable. */
struct __mthread_mutex *m;
if (mutex == NULL)
return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
if (!mthread_mutex_valid(&m))
return(EINVAL);
else if (m->mm_owner != current_thread)
return(EPERM); /* Can't unlock a mutex locked by another thread. */
m->mm_owner = mthread_queue_remove(&m->mm_queue);
if (m->mm_owner != NO_THREAD) mthread_unsuspend(m->mm_owner);
return(0);
}
/*===========================================================================*
* mthread_mutex_valid *
*===========================================================================*/
#ifdef MTHREAD_STRICT
int mthread_mutex_valid(m)
mthread_mutex_t *m;
{
/* Check to see if mutex is on the list of valid mutexes */
struct __mthread_mutex *loopitem;
loopitem = vm_front;
while (loopitem != NULL) {
if (loopitem == *m)
return(1);
loopitem = loopitem->mm_next;
}
return(0);
}
#endif
/*===========================================================================*
* mthread_mutex_verify *
*===========================================================================*/
#ifdef MDEBUG
int mthread_mutex_verify(void)
{
/* Return true when no mutexes are in use */
int r = 1;
struct __mthread_mutex *loopitem;
#ifdef MTHREAD_STRICT
loopitem = vm_front;
while (loopitem != NULL) {
printf("mutex corruption: owner: %d\n", loopitem->mm_owner);
loopitem = loopitem->mm_next;
r = 0;
}
#endif
return(r);
}
#endif
|