}
[(set (attr "length") (const_int 32))])
+(define_insn "atomic_cas_value_exchange_7_<mode>"
+ [(set (match_operand:GPR 0 "register_operand" "=&r")
+ (match_operand:GPR 1 "memory_operand" "+ZC"))
+ (set (match_dup 1)
+ (unspec_volatile:GPR [(match_operand:GPR 2 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 3 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 4 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 5 "reg_or_0_operand" "rJ")
+ (match_operand:SI 6 "const_int_operand")] ;; model
+ UNSPEC_SYNC_EXCHANGE))
+ (clobber (match_scratch:GPR 7 "=&r"))]
+ ""
+{
+ return "%G6\\n\\t"
+ "1:\\n\\t"
+ "ll.<amo>\\t%0,%1\\n\\t"
+ "and\\t%7,%0,%z3\\n\\t"
+ "or%i5\\t%7,%7,%5\\n\\t"
+ "sc.<amo>\\t%7,%1\\n\\t"
+ "beqz\\t%7,1b\\n\\t";
+}
+ [(set (attr "length") (const_int 20))])
+
(define_expand "atomic_exchange<mode>"
[(set (match_operand:SHORT 0 "register_operand")
(unspec_volatile:SHORT
""
{
union loongarch_gen_fn_ptrs generator;
- generator.fn_7 = gen_atomic_cas_value_cmp_and_7_si;
+ generator.fn_7 = gen_atomic_cas_value_exchange_7_si;
loongarch_expand_atomic_qihi (generator, operands[0], operands[1],
- operands[1], operands[2], operands[3]);
+ const0_rtx, operands[2], operands[3]);
DONE;
})
--- /dev/null
+/* { dg-do run } */
+/* { dg-require-effective-target pthread } */
+/* { dg-options "-pthread" } */
+
+#include <pthread.h>
+
+char x, x1, x2;
+
+void *
+work1 (void *)
+{
+ for (int i = 0; i < 100; i++)
+ x1 = __atomic_exchange_n (&x, x1, __ATOMIC_SEQ_CST);
+ return NULL;
+}
+
+void *
+work2 (void *)
+{
+ for (int i = 0; i < 100; i++)
+ x2 = __atomic_exchange_n (&x, x2, __ATOMIC_SEQ_CST);
+ return NULL;
+}
+
+void
+test (void)
+{
+ x = 0;
+ x1 = 1;
+ x2 = 2;
+ pthread_t w1, w2;
+ if (pthread_create (&w1, NULL, work1, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_create (&w2, NULL, work2, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_join (w1, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_join (w2, NULL) != 0)
+ __builtin_abort ();
+ if ((x ^ x1 ^ x2) != 3)
+ __builtin_abort ();
+}
+
+int
+main ()
+{
+ int i;
+ for (i = 0; i < 10000; i++)
+ test ();
+}