Rev 1797 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1797 | Rev 1864 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
2 | * Copyright (C) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
Line 24... | Line 24... | ||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libcsparc64 |
29 | /** @addtogroup libcsparc64 |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #ifndef __sparc64_ATOMIC_H__ |
35 | #ifndef LIBC_sparc64_ATOMIC_H_ |
36 | #define __sparc64_ATOMIC_H__ |
36 | #define LIBC_sparc64_ATOMIC_H_ |
37 | 37 | ||
- | 38 | #include <types.h> |
|
- | 39 | ||
- | 40 | /** Atomic add operation. |
|
- | 41 | * |
|
- | 42 | * Use atomic compare and swap operation to atomically add signed value. |
|
- | 43 | * |
|
- | 44 | * @param val Atomic variable. |
|
- | 45 | * @param i Signed value to be added. |
|
- | 46 | * |
|
- | 47 | * @return Value of the atomic variable as it existed before addition. |
|
- | 48 | */ |
|
38 | static inline void atomic_inc(atomic_t *val) |
49 | static inline long atomic_add(atomic_t *val, int i) |
39 | { |
50 | { |
- | 51 | uint64_t a, b; |
|
- | 52 | volatile uint64_t x = (uint64_t) &val->count; |
|
- | 53 | ||
- | 54 | __asm__ volatile ( |
|
- | 55 | "0:\n" |
|
- | 56 | "ldx %0, %1\n" |
|
- | 57 | "add %1, %3, %2\n" |
|
- | 58 | "casx %0, %1, %2\n" |
|
- | 59 | "cmp %1, %2\n" |
|
- | 60 | "bne 0b\n" /* The operation failed and must be attempted again if a != b. */ |
|
- | 61 | "nop\n" |
|
- | 62 | : "=m" (*((uint64_t *)x)), "=r" (a), "=r" (b) |
|
- | 63 | : "r" (i) |
|
- | 64 | ); |
|
- | 65 | ||
- | 66 | return a; |
|
40 | } |
67 | } |
41 | 68 | ||
42 | static inline void atomic_dec(atomic_t *val) |
69 | static inline long atomic_preinc(atomic_t *val) |
43 | { |
70 | { |
- | 71 | return atomic_add(val, 1) + 1; |
|
44 | } |
72 | } |
45 | 73 | ||
46 | static inline long atomic_postinc(atomic_t *val) |
74 | static inline long atomic_postinc(atomic_t *val) |
47 | { |
75 | { |
48 | atomic_inc(val); |
76 | return atomic_add(val, 1); |
- | 77 | } |
|
- | 78 | ||
- | 79 | static inline long atomic_predec(atomic_t *val) |
|
- | 80 | { |
|
49 | return val->count - 1; |
81 | return atomic_add(val, -1) - 1; |
50 | } |
82 | } |
51 | 83 | ||
52 | static inline long atomic_postdec(atomic_t *val) |
84 | static inline long atomic_postdec(atomic_t *val) |
53 | { |
85 | { |
54 | atomic_dec(val); |
86 | return atomic_add(val, -1); |
55 | return val->count + 1; |
- | |
56 | } |
87 | } |
57 | 88 | ||
58 | static inline long atomic_preinc(atomic_t *val) |
89 | static inline void atomic_inc(atomic_t *val) |
59 | { |
90 | { |
60 | atomic_inc(val); |
91 | (void) atomic_add(val, 1); |
61 | return val->count; |
- | |
62 | } |
92 | } |
63 | 93 | ||
64 | static inline long atomic_predec(atomic_t *val) |
94 | static inline void atomic_dec(atomic_t *val) |
65 | { |
95 | { |
66 | atomic_dec(val); |
96 | (void) atomic_add(val, -1); |
67 | return val->count; |
- | |
68 | } |
97 | } |
69 | 98 | ||
70 | #endif |
99 | #endif |
71 | 100 | ||
72 | /** @} |
101 | /** @} |