2 * Copyright (c) 2011 The Regents of the University of California
3 * Barret Rhoden <brho@cs.berkeley.edu>
4 * Kevin Klues <klueska@cs.berkeley.edu>
6 * This file is part of Parlib.
8 * Parlib is free software: you can redistribute it and/or modify
9 * it under the terms of the Lesser GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Parlib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * Lesser GNU General Public License for more details.
18 * See COPYING.LESSER for details on the GNU Lesser General Public License.
19 * See COPYING for details on the GNU General Public License.
27 void spinlock_init(spinlock_t *lock)
30 *lock = SPINLOCK_UNLOCKED;
34 int spinlock_trylock(spinlock_t *lock)
37 if (*lock == SPINLOCK_LOCKED)
40 return (int)atomic_cas(lock, (long)SPINLOCK_LOCKED, (long)SPINLOCK_UNLOCKED);
44 void spinlock_lock(spinlock_t *lock)
47 while (spinlock_trylock(lock) != (int)SPINLOCK_UNLOCKED)
52 void spinlock_unlock(spinlock_t *lock)
55 *lock = SPINLOCK_UNLOCKED;