170 |
|
* Spinlocks |
171 |
|
*/ |
172 |
|
|
173 |
+ |
static int try_acquire_spinlock(int *lock) |
174 |
+ |
{ |
175 |
+ |
return test_and_set(lock, 1) == 0; |
176 |
+ |
} |
177 |
+ |
|
178 |
|
static void acquire_spinlock(volatile int *lock) |
179 |
|
{ |
180 |
|
do { |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
+ |
/* |
192 |
+ |
* Initialize mutex |
193 |
+ |
*/ |
194 |
+ |
|
195 |
+ |
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr) |
196 |
+ |
{ |
197 |
+ |
// pthread_init_lock |
198 |
+ |
mutex->__m_lock.__status = 0; |
199 |
+ |
mutex->__m_lock.__spinlock = 0; |
200 |
+ |
|
201 |
+ |
mutex->__m_kind = mutex_attr ? mutex_attr->__mutexkind : PTHREAD_MUTEX_TIMED_NP; |
202 |
+ |
mutex->__m_count = 0; |
203 |
+ |
mutex->__m_owner = NULL; |
204 |
+ |
return 0; |
205 |
+ |
} |
206 |
+ |
|
207 |
+ |
|
208 |
+ |
/* |
209 |
+ |
* Destroy mutex |
210 |
+ |
*/ |
211 |
+ |
|
212 |
+ |
int pthread_mutex_destroy(pthread_mutex_t *mutex) |
213 |
+ |
{ |
214 |
+ |
switch (mutex->__m_kind) { |
215 |
+ |
case PTHREAD_MUTEX_TIMED_NP: |
216 |
+ |
return (mutex->__m_lock.__status != 0) ? EBUSY : 0; |
217 |
+ |
default: |
218 |
+ |
return EINVAL; |
219 |
+ |
} |
220 |
+ |
} |
221 |
+ |
|
222 |
+ |
|
223 |
+ |
/* |
224 |
+ |
* Lock mutex |
225 |
+ |
*/ |
226 |
+ |
|
227 |
+ |
int pthread_mutex_lock(pthread_mutex_t *mutex) |
228 |
+ |
{ |
229 |
+ |
switch (mutex->__m_kind) { |
230 |
+ |
case PTHREAD_MUTEX_TIMED_NP: |
231 |
+ |
acquire_spinlock(&mutex->__m_lock.__spinlock); |
232 |
+ |
return 0; |
233 |
+ |
default: |
234 |
+ |
return EINVAL; |
235 |
+ |
} |
236 |
+ |
} |
237 |
+ |
|
238 |
+ |
|
239 |
+ |
/* |
240 |
+ |
* Try to lock mutex |
241 |
+ |
*/ |
242 |
+ |
|
243 |
+ |
int pthread_mutex_trylock(pthread_mutex_t *mutex) |
244 |
+ |
{ |
245 |
+ |
switch (mutex->__m_kind) { |
246 |
+ |
case PTHREAD_MUTEX_TIMED_NP: |
247 |
+ |
if (!try_acquire_spinlock(&mutex->__m_lock.__spinlock)) |
248 |
+ |
return EBUSY; |
249 |
+ |
return 0; |
250 |
+ |
default: |
251 |
+ |
return EINVAL; |
252 |
+ |
} |
253 |
+ |
} |
254 |
+ |
|
255 |
+ |
|
256 |
+ |
/* |
257 |
+ |
* Unlock mutex |
258 |
+ |
*/ |
259 |
+ |
|
260 |
+ |
int pthread_mutex_unlock(pthread_mutex_t *mutex) |
261 |
+ |
{ |
262 |
+ |
switch (mutex->__m_kind) { |
263 |
+ |
case PTHREAD_MUTEX_TIMED_NP: |
264 |
+ |
release_spinlock(&mutex->__m_lock.__spinlock); |
265 |
+ |
return 0; |
266 |
+ |
default: |
267 |
+ |
return EINVAL; |
268 |
+ |
} |
269 |
+ |
} |
270 |
+ |
|
271 |
+ |
|
272 |
+ |
/* |
273 |
+ |
* Create mutex attribute |
274 |
+ |
*/ |
275 |
+ |
|
276 |
+ |
int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
277 |
+ |
{ |
278 |
+ |
attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP; |
279 |
+ |
return 0; |
280 |
+ |
} |
281 |
+ |
|
282 |
+ |
|
283 |
+ |
/* |
284 |
+ |
* Destroy mutex attribute |
285 |
+ |
*/ |
286 |
+ |
|
287 |
+ |
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
288 |
+ |
{ |
289 |
+ |
return 0; |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
|
293 |
|
/* |
294 |
|
* Init semaphore |
295 |
|
*/ |