1 /* 2 * This file is part of ltrace. 3 * Copyright (C) 2012 Petr Machata, Red Hat Inc. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation; either version 2 of the 8 * License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 18 * 02110-1301 USA 19 */ 20 21 #include <stdint.h> 22 23 __attribute__((noinline, optimize(3))) void 24 atomic_add(uint32_t *a, uint32_t b) 25 { 26 __asm__ volatile("lwarx 9,0,%0\n" 27 "add 9,9,%2\n" 28 "stwcx. 9,0,%0\n" 29 "bne- atomic_add\n" 30 : "=r"(a) 31 : "0"(a), "r"(b) 32 : "%r9"); 33 } 34 35 uint32_t a = 0; 36 37 __attribute__((optimize(0))) int 38 main(int argc, char **argv) 39 { 40 atomic_add(&a, 5); 41 atomic_add(&a, 10); 42 atomic_add(&a, 15); 43 return a; 44 } 45