| 1 | /* Low-level functions for atomic operations. Mips version. |
|---|
| 2 | Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
|---|
| 3 | This file is part of the GNU C Library. |
|---|
| 4 | |
|---|
| 5 | The GNU C Library is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU Lesser General Public |
|---|
| 7 | License as published by the Free Software Foundation; either |
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | Lesser General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU Lesser General Public |
|---|
| 16 | License along with the GNU C Library; if not, write to the Free |
|---|
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
|---|
| 18 | 02111-1307 USA. */ |
|---|
| 19 | |
|---|
| 20 | #ifndef _MIPS_ATOMICITY_H |
|---|
| 21 | #define _MIPS_ATOMICITY_H 1 |
|---|
| 22 | |
|---|
| 23 | #include <inttypes.h> |
|---|
| 24 | |
|---|
| 25 | static inline int |
|---|
| 26 | __attribute_used__ |
|---|
| 27 | exchange_and_add (volatile uint32_t *mem, int val) |
|---|
| 28 | { |
|---|
| 29 | int result, tmp; |
|---|
| 30 | |
|---|
| 31 | __asm__ __volatile__ |
|---|
| 32 | ("/* Inline exchange & add */\n" |
|---|
| 33 | "1:\n\t" |
|---|
| 34 | ".set push\n\t" |
|---|
| 35 | ".set mips2\n\t" |
|---|
| 36 | "ll %0,%3\n\t" |
|---|
| 37 | "addu %1,%4,%0\n\t" |
|---|
| 38 | "sc %1,%2\n\t" |
|---|
| 39 | ".set pop\n\t" |
|---|
| 40 | "beqz %1,1b\n\t" |
|---|
| 41 | "/* End exchange & add */" |
|---|
| 42 | : "=&r"(result), "=&r"(tmp), "=m"(*mem) |
|---|
| 43 | : "m" (*mem), "r"(val) |
|---|
| 44 | : "memory"); |
|---|
| 45 | |
|---|
| 46 | return result; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | static inline void |
|---|
| 50 | __attribute_used__ |
|---|
| 51 | atomic_add (volatile uint32_t *mem, int val) |
|---|
| 52 | { |
|---|
| 53 | int result; |
|---|
| 54 | |
|---|
| 55 | __asm__ __volatile__ |
|---|
| 56 | ("/* Inline atomic add */\n" |
|---|
| 57 | "1:\n\t" |
|---|
| 58 | ".set push\n\t" |
|---|
| 59 | ".set mips2\n\t" |
|---|
| 60 | "ll %0,%2\n\t" |
|---|
| 61 | "addu %0,%3,%0\n\t" |
|---|
| 62 | "sc %0,%1\n\t" |
|---|
| 63 | ".set pop\n\t" |
|---|
| 64 | "beqz %0,1b\n\t" |
|---|
| 65 | "/* End atomic add */" |
|---|
| 66 | : "=&r"(result), "=m"(*mem) |
|---|
| 67 | : "m" (*mem), "r"(val) |
|---|
| 68 | : "memory"); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | static inline int |
|---|
| 72 | __attribute_used__ |
|---|
| 73 | compare_and_swap (volatile long int *p, long int oldval, long int newval) |
|---|
| 74 | { |
|---|
| 75 | long int ret, temp; |
|---|
| 76 | |
|---|
| 77 | __asm__ __volatile__ |
|---|
| 78 | ("/* Inline compare & swap */\n" |
|---|
| 79 | "1:\n\t" |
|---|
| 80 | ".set push\n\t" |
|---|
| 81 | ".set mips2\n\t" |
|---|
| 82 | "ll %1,%5\n\t" |
|---|
| 83 | "move %0,$0\n\t" |
|---|
| 84 | "bne %1,%3,2f\n\t" |
|---|
| 85 | "move %0,%4\n\t" |
|---|
| 86 | "sc %0,%2\n\t" |
|---|
| 87 | ".set pop\n\t" |
|---|
| 88 | "beqz %0,1b\n" |
|---|
| 89 | "2:\n\t" |
|---|
| 90 | "/* End compare & swap */" |
|---|
| 91 | : "=&r" (ret), "=&r" (temp), "=m" (*p) |
|---|
| 92 | : "r" (oldval), "r" (newval), "m" (*p) |
|---|
| 93 | : "memory"); |
|---|
| 94 | |
|---|
| 95 | return ret; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | #endif /* atomicity.h */ |
|---|