Deleted Added
full compact
thr_mutex.c (22315) thr_mutex.c (31402)
1/*
2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 32 unchanged lines hidden (view full) ---

41 const pthread_mutexattr_t * mutex_attr)
42{
43 enum pthread_mutextype type;
44 pthread_mutex_t pmutex;
45 int ret = 0;
46 int status;
47
48 if (mutex == NULL) {
1/*
2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 32 unchanged lines hidden (view full) ---

41 const pthread_mutexattr_t * mutex_attr)
42{
43 enum pthread_mutextype type;
44 pthread_mutex_t pmutex;
45 int ret = 0;
46 int status;
47
48 if (mutex == NULL) {
49 errno = EINVAL;
50 ret = -1;
49 ret = EINVAL;
51 } else {
52 /* Check if default mutex attributes: */
53 if (mutex_attr == NULL || *mutex_attr == NULL) {
54 /* Default to a fast mutex: */
55 type = MUTEX_TYPE_FAST;
56 } else if ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX) {
57 /* Return an invalid argument error: */
50 } else {
51 /* Check if default mutex attributes: */
52 if (mutex_attr == NULL || *mutex_attr == NULL) {
53 /* Default to a fast mutex: */
54 type = MUTEX_TYPE_FAST;
55 } else if ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX) {
56 /* Return an invalid argument error: */
58 errno = EINVAL;
59 ret = -1;
57 ret = EINVAL;
60 } else {
61 /* Use the requested mutex type: */
62 type = (*mutex_attr)->m_type;
63 }
64
65 /* Check no errors so far: */
66 if (ret == 0) {
67 if ((pmutex = (pthread_mutex_t) malloc(sizeof(struct pthread_mutex))) == NULL) {
58 } else {
59 /* Use the requested mutex type: */
60 type = (*mutex_attr)->m_type;
61 }
62
63 /* Check no errors so far: */
64 if (ret == 0) {
65 if ((pmutex = (pthread_mutex_t) malloc(sizeof(struct pthread_mutex))) == NULL) {
68 errno = ENOMEM;
69 ret = -1;
66 ret = ENOMEM;
70 } else {
71 /* Reset the mutex flags: */
72 pmutex->m_flags = 0;
73
74 /* Block signals: */
75 _thread_kern_sig_block(&status);
76
77 /* Process according to mutex type: */

--- 7 unchanged lines hidden (view full) ---

85 case MUTEX_TYPE_COUNTING_FAST:
86 /* Reset the mutex count: */
87 pmutex->m_data.m_count = 0;
88 break;
89
90 /* Trap invalid mutex types: */
91 default:
92 /* Return an invalid argument error: */
67 } else {
68 /* Reset the mutex flags: */
69 pmutex->m_flags = 0;
70
71 /* Block signals: */
72 _thread_kern_sig_block(&status);
73
74 /* Process according to mutex type: */

--- 7 unchanged lines hidden (view full) ---

82 case MUTEX_TYPE_COUNTING_FAST:
83 /* Reset the mutex count: */
84 pmutex->m_data.m_count = 0;
85 break;
86
87 /* Trap invalid mutex types: */
88 default:
89 /* Return an invalid argument error: */
93 errno = EINVAL;
94 ret = -1;
90 ret = EINVAL;
95 break;
96 }
97 if (ret == 0) {
98 /* Initialise the rest of the mutex: */
99 _thread_queue_init(&pmutex->m_queue);
100 pmutex->m_flags |= MUTEX_FLAGS_INITED;
101 pmutex->m_owner = NULL;
102 pmutex->m_type = type;

--- 14 unchanged lines hidden (view full) ---

117
118int
119pthread_mutex_destroy(pthread_mutex_t * mutex)
120{
121 int ret = 0;
122 int status;
123
124 if (mutex == NULL || *mutex == NULL) {
91 break;
92 }
93 if (ret == 0) {
94 /* Initialise the rest of the mutex: */
95 _thread_queue_init(&pmutex->m_queue);
96 pmutex->m_flags |= MUTEX_FLAGS_INITED;
97 pmutex->m_owner = NULL;
98 pmutex->m_type = type;

--- 14 unchanged lines hidden (view full) ---

113
114int
115pthread_mutex_destroy(pthread_mutex_t * mutex)
116{
117 int ret = 0;
118 int status;
119
120 if (mutex == NULL || *mutex == NULL) {
125 errno = EINVAL;
126 ret = -1;
121 ret = EINVAL;
127 } else {
128 /* Block signals: */
129 _thread_kern_sig_block(&status);
130
131 /* Process according to mutex type: */
132 switch ((*mutex)->m_type) {
133 /* Fast mutex: */
134 case MUTEX_TYPE_FAST:

--- 4 unchanged lines hidden (view full) ---

139 case MUTEX_TYPE_COUNTING_FAST:
140 /* Reset the mutex count: */
141 (*mutex)->m_data.m_count = 0;
142 break;
143
144 /* Trap undefined mutex types: */
145 default:
146 /* Return an invalid argument error: */
122 } else {
123 /* Block signals: */
124 _thread_kern_sig_block(&status);
125
126 /* Process according to mutex type: */
127 switch ((*mutex)->m_type) {
128 /* Fast mutex: */
129 case MUTEX_TYPE_FAST:

--- 4 unchanged lines hidden (view full) ---

134 case MUTEX_TYPE_COUNTING_FAST:
135 /* Reset the mutex count: */
136 (*mutex)->m_data.m_count = 0;
137 break;
138
139 /* Trap undefined mutex types: */
140 default:
141 /* Return an invalid argument error: */
147 errno = EINVAL;
148 ret = -1;
142 ret = EINVAL;
149 break;
150 }
151
152 /* Clean up the mutex in case that others want to use it: */
153 _thread_queue_init(&(*mutex)->m_queue);
154 (*mutex)->m_owner = NULL;
155 (*mutex)->m_flags = 0;
156

--- 7 unchanged lines hidden (view full) ---

164
165int
166pthread_mutex_trylock(pthread_mutex_t * mutex)
167{
168 int ret = 0;
169 int status;
170
171 if (mutex == NULL || *mutex == NULL) {
143 break;
144 }
145
146 /* Clean up the mutex in case that others want to use it: */
147 _thread_queue_init(&(*mutex)->m_queue);
148 (*mutex)->m_owner = NULL;
149 (*mutex)->m_flags = 0;
150

--- 7 unchanged lines hidden (view full) ---

158
159int
160pthread_mutex_trylock(pthread_mutex_t * mutex)
161{
162 int ret = 0;
163 int status;
164
165 if (mutex == NULL || *mutex == NULL) {
172 errno = EINVAL;
173 ret = -1;
166 ret = EINVAL;
174 } else {
175 /* Block signals: */
176 _thread_kern_sig_block(&status);
177
178 /* Process according to mutex type: */
179 switch ((*mutex)->m_type) {
180 /* Fast mutex: */
181 case MUTEX_TYPE_FAST:
182 /* Check if this mutex is not locked: */
183 if ((*mutex)->m_owner == NULL) {
184 /* Lock the mutex for the running thread: */
185 (*mutex)->m_owner = _thread_run;
186 } else {
187 /* Return a busy error: */
167 } else {
168 /* Block signals: */
169 _thread_kern_sig_block(&status);
170
171 /* Process according to mutex type: */
172 switch ((*mutex)->m_type) {
173 /* Fast mutex: */
174 case MUTEX_TYPE_FAST:
175 /* Check if this mutex is not locked: */
176 if ((*mutex)->m_owner == NULL) {
177 /* Lock the mutex for the running thread: */
178 (*mutex)->m_owner = _thread_run;
179 } else {
180 /* Return a busy error: */
188 errno = EBUSY;
189 ret = -1;
181 ret = EBUSY;
190 }
191 break;
192
193 /* Counting mutex: */
194 case MUTEX_TYPE_COUNTING_FAST:
195 /* Check if this mutex is locked: */
196 if ((*mutex)->m_owner != NULL) {
197 /*
198 * Check if the mutex is locked by the running
199 * thread:
200 */
201 if ((*mutex)->m_owner == _thread_run) {
202 /* Increment the lock count: */
203 (*mutex)->m_data.m_count++;
204 } else {
205 /* Return a busy error: */
182 }
183 break;
184
185 /* Counting mutex: */
186 case MUTEX_TYPE_COUNTING_FAST:
187 /* Check if this mutex is locked: */
188 if ((*mutex)->m_owner != NULL) {
189 /*
190 * Check if the mutex is locked by the running
191 * thread:
192 */
193 if ((*mutex)->m_owner == _thread_run) {
194 /* Increment the lock count: */
195 (*mutex)->m_data.m_count++;
196 } else {
197 /* Return a busy error: */
206 errno = EBUSY;
207 ret = -1;
198 ret = EBUSY;
208 }
209 } else {
210 /* Lock the mutex for the running thread: */
211 (*mutex)->m_owner = _thread_run;
212 }
213 break;
214
215 /* Trap invalid mutex types: */
216 default:
217 /* Return an invalid argument error: */
199 }
200 } else {
201 /* Lock the mutex for the running thread: */
202 (*mutex)->m_owner = _thread_run;
203 }
204 break;
205
206 /* Trap invalid mutex types: */
207 default:
208 /* Return an invalid argument error: */
218 errno = EINVAL;
219 ret = -1;
209 ret = EINVAL;
220 break;
221 }
222
223 /* Unblock signals: */
224 _thread_kern_sig_unblock(status);
225 }
226
227 /* Return the completion status: */
228 return (ret);
229}
230
231int
232pthread_mutex_lock(pthread_mutex_t * mutex)
233{
234 int ret = 0;
235 int status;
236
237 if (mutex == NULL || *mutex == NULL) {
210 break;
211 }
212
213 /* Unblock signals: */
214 _thread_kern_sig_unblock(status);
215 }
216
217 /* Return the completion status: */
218 return (ret);
219}
220
221int
222pthread_mutex_lock(pthread_mutex_t * mutex)
223{
224 int ret = 0;
225 int status;
226
227 if (mutex == NULL || *mutex == NULL) {
238 errno = EINVAL;
239 ret = -1;
228 ret = EINVAL;
240 } else {
241 /* Block signals: */
242 _thread_kern_sig_block(&status);
243
244 /* Process according to mutex type: */
245 switch ((*mutex)->m_type) {
246 /* Fast mutexes do not check for any error conditions: */
247 case MUTEX_TYPE_FAST:

--- 53 unchanged lines hidden (view full) ---

301
302 /* Increment the lock count for this mutex: */
303 (*mutex)->m_data.m_count++;
304 break;
305
306 /* Trap invalid mutex types: */
307 default:
308 /* Return an invalid argument error: */
229 } else {
230 /* Block signals: */
231 _thread_kern_sig_block(&status);
232
233 /* Process according to mutex type: */
234 switch ((*mutex)->m_type) {
235 /* Fast mutexes do not check for any error conditions: */
236 case MUTEX_TYPE_FAST:

--- 53 unchanged lines hidden (view full) ---

290
291 /* Increment the lock count for this mutex: */
292 (*mutex)->m_data.m_count++;
293 break;
294
295 /* Trap invalid mutex types: */
296 default:
297 /* Return an invalid argument error: */
309 errno = EINVAL;
310 ret = -1;
298 ret = EINVAL;
311 break;
312 }
313
314 /* Unblock signals: */
315 _thread_kern_sig_unblock(status);
316 }
317
318 /* Return the completion status: */
319 return (ret);
320}
321
322int
323pthread_mutex_unlock(pthread_mutex_t * mutex)
324{
325 int ret = 0;
326 int status;
327
328 if (mutex == NULL || *mutex == NULL) {
299 break;
300 }
301
302 /* Unblock signals: */
303 _thread_kern_sig_unblock(status);
304 }
305
306 /* Return the completion status: */
307 return (ret);
308}
309
310int
311pthread_mutex_unlock(pthread_mutex_t * mutex)
312{
313 int ret = 0;
314 int status;
315
316 if (mutex == NULL || *mutex == NULL) {
329 errno = EINVAL;
330 ret = -1;
317 ret = EINVAL;
331 } else {
332 /* Block signals: */
333 _thread_kern_sig_block(&status);
334
335 /* Process according to mutex type: */
336 switch ((*mutex)->m_type) {
337 /* Fast mutexes do not check for any error conditions: */
338 case MUTEX_TYPE_FAST:
339 /* Check if the running thread is not the owner of the mutex: */
340 if ((*mutex)->m_owner != _thread_run) {
341 /* Return an invalid argument error: */
318 } else {
319 /* Block signals: */
320 _thread_kern_sig_block(&status);
321
322 /* Process according to mutex type: */
323 switch ((*mutex)->m_type) {
324 /* Fast mutexes do not check for any error conditions: */
325 case MUTEX_TYPE_FAST:
326 /* Check if the running thread is not the owner of the mutex: */
327 if ((*mutex)->m_owner != _thread_run) {
328 /* Return an invalid argument error: */
342 errno = EINVAL;
343 ret = -1;
329 ret = EINVAL;
344 }
345 /*
346 * Get the next thread from the queue of threads waiting on
347 * the mutex:
348 */
349 else if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) {
350 /* Allow the new owner of the mutex to run: */
351 PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING);
352 }
353 break;
354
355 /* Counting mutex: */
356 case MUTEX_TYPE_COUNTING_FAST:
357 /* Check if the running thread is not the owner of the mutex: */
358 if ((*mutex)->m_owner != _thread_run) {
359 /* Return an invalid argument error: */
330 }
331 /*
332 * Get the next thread from the queue of threads waiting on
333 * the mutex:
334 */
335 else if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) {
336 /* Allow the new owner of the mutex to run: */
337 PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING);
338 }
339 break;
340
341 /* Counting mutex: */
342 case MUTEX_TYPE_COUNTING_FAST:
343 /* Check if the running thread is not the owner of the mutex: */
344 if ((*mutex)->m_owner != _thread_run) {
345 /* Return an invalid argument error: */
360 errno = EINVAL;
361 ret = -1;
346 ret = EINVAL;
362 }
363 /* Check if there are still counts: */
364 else if ((*mutex)->m_data.m_count) {
365 /* Decrement the count: */
366 (*mutex)->m_data.m_count--;
367 }
368 /*
369 * Get the next thread from the queue of threads waiting on
370 * the mutex:
371 */
372 else if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) {
373 /* Allow the new owner of the mutex to run: */
374 PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING);
375 }
376 break;
377
378 /* Trap invalid mutex types: */
379 default:
380 /* Return an invalid argument error: */
347 }
348 /* Check if there are still counts: */
349 else if ((*mutex)->m_data.m_count) {
350 /* Decrement the count: */
351 (*mutex)->m_data.m_count--;
352 }
353 /*
354 * Get the next thread from the queue of threads waiting on
355 * the mutex:
356 */
357 else if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) {
358 /* Allow the new owner of the mutex to run: */
359 PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING);
360 }
361 break;
362
363 /* Trap invalid mutex types: */
364 default:
365 /* Return an invalid argument error: */
381 errno = EINVAL;
382 ret = -1;
366 ret = EINVAL;
383 break;
384 }
385
386 /* Unblock signals: */
387 _thread_kern_sig_unblock(status);
388 }
389
390 /* Return the completion status: */
391 return (ret);
392}
393#endif
367 break;
368 }
369
370 /* Unblock signals: */
371 _thread_kern_sig_unblock(status);
372 }
373
374 /* Return the completion status: */
375 return (ret);
376}
377#endif