OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 #ifndef NATIVE_CLIENT_SRC_INCLUDE_LINUX_MIPS_ATOMIC_OPS_LINUX_MIPS_H_ |
| 8 #define NATIVE_CLIENT_SRC_INCLUDE_LINUX_MIPS_ATOMIC_OPS_LINUX_MIPS_H_ 1 |
| 9 |
| 10 /* |
| 11 * Used only by trusted code. Untrusted code uses gcc intrinsics. |
| 12 */ |
| 13 |
| 14 #include "native_client/src/include/portability.h" |
| 15 #include <stdint.h> |
| 16 |
| 17 typedef int32_t Atomic32; |
| 18 |
| 19 static INLINE Atomic32 CompareAndSwap(volatile Atomic32* ptr, |
| 20 Atomic32 old_value, |
| 21 Atomic32 new_value) { |
| 22 Atomic32 ret; |
| 23 |
| 24 __asm__ __volatile__("1:\n" |
| 25 "ll %0, %1\n" /* ret = *ptr */ |
| 26 "bne %0, %3, 2f\n" /* if (ret != old_value) goto 2 */ |
| 27 "nop\n" /* delay slot nop */ |
| 28 "sc %2, %1\n" /* *ptr = new_value (with atomic check) */ |
| 29 "beqz %2, 1b\n" /* start again on atomic error */ |
| 30 "nop\n" /* delay slot nop */ |
| 31 "2:\n" |
| 32 : "=&r" (ret), "=m" (*ptr), "+&r" (new_value) |
| 33 : "Ir" (old_value), "r" (new_value), "m" (*ptr) |
| 34 : "memory"); |
| 35 |
| 36 |
| 37 return ret; |
| 38 } |
| 39 |
| 40 static INLINE Atomic32 AtomicExchange(volatile Atomic32* ptr, |
| 41 Atomic32 new_value) { |
| 42 Atomic32 tmp, old; |
| 43 |
| 44 __asm__ __volatile__("1:\n" |
| 45 "ll %1, %2\n" /* old = *ptr */ |
| 46 "move %0, %3\n" /* tmp = new_value */ |
| 47 "sc %0, %2\n" /* *ptr = tmp (with atomic check) */ |
| 48 "beqz %0, 1b\n" /* start again on atomic error */ |
| 49 "nop\n" /* delay slot nop */ |
| 50 : "=&r" (tmp), "=&r" (old), "=m" (*ptr) |
| 51 : "r" (new_value), "m" (*ptr) |
| 52 : "memory"); |
| 53 |
| 54 return old; |
| 55 } |
| 56 |
| 57 static INLINE Atomic32 AtomicIncrement(volatile Atomic32* ptr, |
| 58 Atomic32 increment) { |
| 59 Atomic32 tmp, res; |
| 60 |
| 61 __asm__ __volatile__("1:\n" |
| 62 "ll %0, %2\n" /* tmp = *ptr */ |
| 63 "addu %0, %3\n" /* tmp = tmp + increment */ |
| 64 "move %1, %0\n" /* res = tmp */ |
| 65 "sc %0, %2\n" /* *ptr = tmp (with atomic check) */ |
| 66 "beqz %0, 1b\n" /* start again on atomic error */ |
| 67 "nop\n" /* delay slot nop */ |
| 68 : "=&r" (tmp), "=&r" (res), "=m" (*ptr) |
| 69 : "Ir" (increment), "m" (*ptr) |
| 70 : "memory"); |
| 71 /* res now holds the final value. */ |
| 72 |
| 73 return res; |
| 74 } |
| 75 |
| 76 #endif /* NATIVE_CLIENT_SRC_INCLUDE_LINUX_MIPS_ATOMIC_OPS_LINUX_MIPS_H_ */ |
OLD | NEW |