2 * Copyright 2010, Lucian Adrian Grijincu, lucian.grijincu@gmail.com.
3 * Distributed under the terms of the MIT License.
9 #include <arch_cpu_defs.h>
11 #include "pthread_private.h"
19 pthread_spin_init(pthread_spinlock_t
* lock
, int pshared
)
21 // This implementation of spinlocks doesn't differentiate
22 // between spin locks used by threads in the same process or
23 // between threads of different processes.
25 lock
->lock
= UNLOCKED
;
31 pthread_spin_destroy(pthread_spinlock_t
* lock
)
38 pthread_spin_lock(pthread_spinlock_t
* lock
)
40 while (atomic_test_and_set((int32
*)&lock
->lock
, LOCKED
, UNLOCKED
)
42 SPINLOCK_PAUSE(); // spin
43 // TODO: On UP machines we should thread_yield() in the loop.
50 pthread_spin_trylock(pthread_spinlock_t
* lock
)
52 if (atomic_test_and_set((int32
*)&lock
->lock
, LOCKED
, UNLOCKED
) == LOCKED
)
59 pthread_spin_unlock(pthread_spinlock_t
* lock
)
61 lock
->lock
= UNLOCKED
;