1*7e321ac1Sguenther /* $OpenBSD: _atomic_lock.c,v 1.1 2017/08/15 06:13:24 guenther Exp $ */
2*7e321ac1Sguenther /*
3*7e321ac1Sguenther * Copyright (c) 2005 Marco Peereboom <marco@openbsd.org>
4*7e321ac1Sguenther *
5*7e321ac1Sguenther * Permission to use, copy, modify, and distribute this software for any
6*7e321ac1Sguenther * purpose with or without fee is hereby granted, provided that the above
7*7e321ac1Sguenther * copyright notice and this permission notice appear in all copies.
8*7e321ac1Sguenther *
9*7e321ac1Sguenther * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*7e321ac1Sguenther * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*7e321ac1Sguenther * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*7e321ac1Sguenther * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*7e321ac1Sguenther * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*7e321ac1Sguenther * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*7e321ac1Sguenther * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*7e321ac1Sguenther */
17*7e321ac1Sguenther
18*7e321ac1Sguenther #include <machine/spinlock.h>
19*7e321ac1Sguenther #ifdef DIAGNOSTIC
20*7e321ac1Sguenther #include <stdio.h>
21*7e321ac1Sguenther #include <stdlib.h>
22*7e321ac1Sguenther #endif
23*7e321ac1Sguenther
24*7e321ac1Sguenther int
_atomic_lock(volatile _atomic_lock_t * lock)25*7e321ac1Sguenther _atomic_lock(volatile _atomic_lock_t *lock)
26*7e321ac1Sguenther {
27*7e321ac1Sguenther volatile _atomic_lock_t old;
28*7e321ac1Sguenther
29*7e321ac1Sguenther #ifdef DIAGNOSTIC
30*7e321ac1Sguenther if ((unsigned long)lock & 0xf) {
31*7e321ac1Sguenther printf("lock not 16 byte aligned\n");
32*7e321ac1Sguenther abort();
33*7e321ac1Sguenther }
34*7e321ac1Sguenther #endif
35*7e321ac1Sguenther
36*7e321ac1Sguenther asm volatile ("ldcws 0(%2),%0"
37*7e321ac1Sguenther : "=&r" (old), "+m" (lock)
38*7e321ac1Sguenther : "r" (lock));
39*7e321ac1Sguenther
40*7e321ac1Sguenther return (old == _ATOMIC_LOCK_LOCKED);
41*7e321ac1Sguenther }
42