comment/style fixes
[official-gcc.git] / libjava / sysdep / sparc / locks.h
blob7c30d7c3b0ff00ea80812b1940cee5d59ba7b37d
1 // locks.h - Thread synchronization primitives. Sparc implementation.
3 /* Copyright (C) 2002, 2007 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #ifndef __SYSDEP_LOCKS_H__
12 #define __SYSDEP_LOCKS_H__
14 typedef size_t obj_addr_t; /* Integer type big enough for object */
15 /* address. */
17 #ifdef __arch64__
18 /* Sparc64 implementation, use cas instruction. */
19 inline static bool
20 compare_and_swap(volatile obj_addr_t *addr,
21 obj_addr_t old,
22 obj_addr_t new_val)
24 __asm__ __volatile__("casx [%2], %3, %0\n\t"
25 "membar #StoreLoad | #StoreStore"
26 : "=&r" (new_val)
27 : "0" (new_val), "r" (addr), "r" (old)
28 : "memory");
30 return (new_val == old) ? true : false;
33 inline static void
34 release_set(volatile obj_addr_t *addr, obj_addr_t new_val)
36 __asm__ __volatile__("membar #StoreStore | #LoadStore" : : : "memory");
37 *(addr) = new_val;
40 inline static bool
41 compare_and_swap_release(volatile obj_addr_t *addr, obj_addr_t old,
42 obj_addr_t new_val)
44 return compare_and_swap(addr, old, new_val);
47 inline static void
48 read_barrier()
50 __asm__ __volatile__("membar #LoadLoad | #LoadStore" : : : "memory");
53 inline static void
54 write_barrier()
56 __asm__ __volatile__("membar #StoreLoad | #StoreStore" : : : "memory");
58 #else
59 /* Sparc32 implementation, use a spinlock. */
60 static unsigned char __cas_lock = 0;
62 inline static void
63 __cas_start_atomic(void)
65 unsigned int tmp;
66 __asm__ __volatile__(
67 "1: ldstub [%1], %0\n"
68 " orcc %0, 0x0, %%g0\n"
69 " be 3f\n"
70 " nop\n"
71 "2: ldub [%1], %0\n"
72 " orcc %0, 0x0, %%g0\n"
73 " bne 2b\n"
74 " nop\n"
75 "3:" : "=&r" (tmp)
76 : "r" (&__cas_lock)
77 : "memory", "cc");
80 inline static void
81 __cas_end_atomic(void)
83 __asm__ __volatile__(
84 "stb %%g0, [%0]"
85 : /* no outputs */
86 : "r" (&__cas_lock)
87 : "memory");
90 inline static bool
91 compare_and_swap(volatile obj_addr_t *addr,
92 obj_addr_t old,
93 obj_addr_t new_val)
95 bool ret;
97 __cas_start_atomic ();
98 if (*addr != old)
100 ret = false;
102 else
104 *addr = new_val;
105 ret = true;
107 __cas_end_atomic ();
109 return ret;
112 inline static void
113 release_set(volatile obj_addr_t *addr, obj_addr_t new_val)
115 /* Technically stbar would be needed here but no sparc32
116 system actually requires it. Also the stbar would mean
117 this code would not work on sparcv7 chips. */
118 __asm__ __volatile__("" : : : "memory");
119 *(addr) = new_val;
122 inline static bool
123 compare_and_swap_release(volatile obj_addr_t *addr, obj_addr_t old,
124 obj_addr_t new_val)
126 return compare_and_swap(addr, old, new_val);
129 inline static void
130 read_barrier()
132 __asm__ __volatile__ ("" : : : "memory");
135 inline static void
136 write_barrier()
138 __asm__ __volatile__ ("" : : : "memory");
140 #endif /* __arch64__ */
142 #endif /* ! __SYSDEP_LOCKS_H__ */