Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / system / libroot / posix / pthread / pthread_spinlock.c
blobb0167b53f71f0bc033a4f1a69e4a0e2c7db16e32
1 /*
2 * Copyright 2010, Lucian Adrian Grijincu, lucian.grijincu@gmail.com.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <pthread.h>
9 #include <arch_cpu_defs.h>
11 #include "pthread_private.h"
14 #define UNLOCKED 0
15 #define LOCKED 1
18 int
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;
26 return 0;
30 int
31 pthread_spin_destroy(pthread_spinlock_t* lock)
33 return 0;
37 int
38 pthread_spin_lock(pthread_spinlock_t* lock)
40 while (atomic_test_and_set((int32*)&lock->lock, LOCKED, UNLOCKED)
41 == LOCKED) {
42 SPINLOCK_PAUSE(); // spin
43 // TODO: On UP machines we should thread_yield() in the loop.
45 return 0;
49 int
50 pthread_spin_trylock(pthread_spinlock_t* lock)
52 if (atomic_test_and_set((int32*)&lock->lock, LOCKED, UNLOCKED) == LOCKED)
53 return EBUSY;
54 return 0;
58 int
59 pthread_spin_unlock(pthread_spinlock_t* lock)
61 lock->lock = UNLOCKED;
62 return 0;