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