aboutsummaryrefslogtreecommitdiff
path: root/lib/libcompiler_rt
diff options
context:
space:
mode:
authorEd Schouten <ed@FreeBSD.org>2011-12-27 22:13:51 +0000
committerEd Schouten <ed@FreeBSD.org>2011-12-27 22:13:51 +0000
commit6d57c7509758223508c2019ba60176263fb72068 (patch)
tree55052b5fbd1048f4b1c9e2fdcd09939c5337b577 /lib/libcompiler_rt
parent3033610f5dfd03b68de35631a8693963a80099c2 (diff)
downloadsrc-6d57c7509758223508c2019ba60176263fb72068.tar.gz
src-6d57c7509758223508c2019ba60176263fb72068.zip
Add locally implemented atomic intrinsics to libcompiler_rt.
The built-in atomic operations are not implemented in our version of GCC 4.2 for the ARM and MIPS architectures. Instead of emitting locked instructions, they generate calls to functions that can be implemented in the C runtime. Only implement the atomic operations that are used by <stdatomic.h> for datatype sizes that are supported by atomic(9). This means that on these architectures, we can only use atomic operations on 32-bits and 64-bits variables, which is typically sufficient. This makes <stdatomic.h> work on all architectures except MIPS, since MIPS and SPARC64 still use libgcc. Converting these architectures to libcompiler_rt is on my todo list.
Notes
Notes: svn path=/head/; revision=228919
Diffstat (limited to 'lib/libcompiler_rt')
-rw-r--r--lib/libcompiler_rt/Makefile20
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_add_4.c6
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_add_8.c6
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_and_4.c7
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_and_8.c7
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_op_n.h47
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_or_4.c7
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_or_8.c7
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_sub_4.c6
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_sub_8.c6
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_xor_4.c7
-rw-r--r--lib/libcompiler_rt/__sync_fetch_and_xor_8.c7
-rw-r--r--lib/libcompiler_rt/__sync_lock_test_and_set_4.c7
-rw-r--r--lib/libcompiler_rt/__sync_lock_test_and_set_8.c7
-rw-r--r--lib/libcompiler_rt/__sync_val_compare_and_swap_4.c6
-rw-r--r--lib/libcompiler_rt/__sync_val_compare_and_swap_8.c6
-rw-r--r--lib/libcompiler_rt/__sync_val_compare_and_swap_n.h45
17 files changed, 204 insertions, 0 deletions
diff --git a/lib/libcompiler_rt/Makefile b/lib/libcompiler_rt/Makefile
index 3a501f5771b2..ce091da77bf7 100644
--- a/lib/libcompiler_rt/Makefile
+++ b/lib/libcompiler_rt/Makefile
@@ -144,6 +144,26 @@ SRCF+= adddf3 \
umodsi3
.endif
+# FreeBSD-specific atomic intrinsics.
+.if ${MACHINE_CPUARCH} == "arm" || ${MACHINE_CPUARCH} == "mips"
+SRCF+= __sync_fetch_and_add_4 \
+ __sync_fetch_and_and_4 \
+ __sync_fetch_and_or_4 \
+ __sync_fetch_and_sub_4 \
+ __sync_fetch_and_xor_4 \
+ __sync_lock_test_and_set_4 \
+ __sync_val_compare_and_swap_4
+.endif
+.if ${MACHINE_ARCH:Mmips64*} != ""
+SRCF+= __sync_fetch_and_add_8 \
+ __sync_fetch_and_and_8 \
+ __sync_fetch_and_or_8 \
+ __sync_fetch_and_sub_8 \
+ __sync_fetch_and_xor_8 \
+ __sync_lock_test_and_set_8 \
+ __sync_val_compare_and_swap_8
+.endif
+
.for file in ${SRCF}
. if ${MACHINE_CPUARCH} != "arm" && exists(${CRTSRC}/${CRTARCH}/${file}.S)
SRCS+= ${file}.S
diff --git a/lib/libcompiler_rt/__sync_fetch_and_add_4.c b/lib/libcompiler_rt/__sync_fetch_and_add_4.c
new file mode 100644
index 000000000000..3c11a3b87dd1
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_add_4.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_add_4
+#define TYPE uint32_t
+#define FETCHADD(x, y) atomic_fetchadd_32(x, y)
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_add_8.c b/lib/libcompiler_rt/__sync_fetch_and_add_8.c
new file mode 100644
index 000000000000..6157c151767a
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_add_8.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_add_8
+#define TYPE uint64_t
+#define FETCHADD(x, y) atomic_fetchadd_64(x, y)
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_and_4.c b/lib/libcompiler_rt/__sync_fetch_and_and_4.c
new file mode 100644
index 000000000000..1a488ec27887
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_and_4.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_and_4
+#define TYPE uint32_t
+#define CMPSET atomic_cmpset_32
+#define EXPRESSION t & value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_and_8.c b/lib/libcompiler_rt/__sync_fetch_and_and_8.c
new file mode 100644
index 000000000000..9923e31158bd
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_and_8.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_and_8
+#define TYPE uint64_t
+#define CMPSET atomic_cmpset_64
+#define EXPRESSION t & value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_op_n.h b/lib/libcompiler_rt/__sync_fetch_and_op_n.h
new file mode 100644
index 000000000000..f7f0e06e83a0
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_op_n.h
@@ -0,0 +1,47 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <machine/atomic.h>
+
+TYPE
+NAME(volatile TYPE *ptr, TYPE value)
+{
+ TYPE t;
+
+#ifdef FETCHADD
+ t = FETCHADD(ptr, value);
+#else
+ do {
+ t = *ptr;
+ } while (!CMPSET(ptr, t, EXPRESSION));
+#endif
+
+ return (t);
+}
diff --git a/lib/libcompiler_rt/__sync_fetch_and_or_4.c b/lib/libcompiler_rt/__sync_fetch_and_or_4.c
new file mode 100644
index 000000000000..1feeeb17a1d6
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_or_4.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_or_4
+#define TYPE uint32_t
+#define CMPSET atomic_cmpset_32
+#define EXPRESSION t | value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_or_8.c b/lib/libcompiler_rt/__sync_fetch_and_or_8.c
new file mode 100644
index 000000000000..7cb9403bed17
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_or_8.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_or_8
+#define TYPE uint64_t
+#define CMPSET atomic_cmpset_64
+#define EXPRESSION t | value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_sub_4.c b/lib/libcompiler_rt/__sync_fetch_and_sub_4.c
new file mode 100644
index 000000000000..a251adde54f0
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_sub_4.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_sub_4
+#define TYPE uint32_t
+#define FETCHADD(x, y) atomic_fetchadd_32(x, -(y))
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_sub_8.c b/lib/libcompiler_rt/__sync_fetch_and_sub_8.c
new file mode 100644
index 000000000000..5a93f977ec89
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_sub_8.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_sub_8
+#define TYPE uint64_t
+#define FETCHADD(x, y) atomic_fetchadd_64(x, -(y))
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_xor_4.c b/lib/libcompiler_rt/__sync_fetch_and_xor_4.c
new file mode 100644
index 000000000000..d5f732d65f6b
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_xor_4.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_xor_4
+#define TYPE uint32_t
+#define CMPSET atomic_cmpset_32
+#define EXPRESSION t ^ value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_fetch_and_xor_8.c b/lib/libcompiler_rt/__sync_fetch_and_xor_8.c
new file mode 100644
index 000000000000..610037ef28bc
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_fetch_and_xor_8.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_fetch_and_xor_8
+#define TYPE uint64_t
+#define CMPSET atomic_cmpset_64
+#define EXPRESSION t ^ value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_lock_test_and_set_4.c b/lib/libcompiler_rt/__sync_lock_test_and_set_4.c
new file mode 100644
index 000000000000..d4965f96484f
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_lock_test_and_set_4.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_lock_test_and_set_4
+#define TYPE uint32_t
+#define CMPSET atomic_cmpset_32
+#define EXPRESSION value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_lock_test_and_set_8.c b/lib/libcompiler_rt/__sync_lock_test_and_set_8.c
new file mode 100644
index 000000000000..1e02203d2be5
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_lock_test_and_set_8.c
@@ -0,0 +1,7 @@
+/* $FreeBSD$ */
+#define NAME __sync_lock_test_and_set_8
+#define TYPE uint64_t
+#define CMPSET atomic_cmpset_64
+#define EXPRESSION value
+
+#include "__sync_fetch_and_op_n.h"
diff --git a/lib/libcompiler_rt/__sync_val_compare_and_swap_4.c b/lib/libcompiler_rt/__sync_val_compare_and_swap_4.c
new file mode 100644
index 000000000000..e0ab11515efd
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_val_compare_and_swap_4.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_val_compare_and_swap_4
+#define TYPE uint32_t
+#define CMPSET atomic_cmpset_32
+
+#include "__sync_val_compare_and_swap_n.h"
diff --git a/lib/libcompiler_rt/__sync_val_compare_and_swap_8.c b/lib/libcompiler_rt/__sync_val_compare_and_swap_8.c
new file mode 100644
index 000000000000..c6f1101a72c0
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_val_compare_and_swap_8.c
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+#define NAME __sync_val_compare_and_swap_8
+#define TYPE uint64_t
+#define CMPSET atomic_cmpset_64
+
+#include "__sync_val_compare_and_swap_n.h"
diff --git a/lib/libcompiler_rt/__sync_val_compare_and_swap_n.h b/lib/libcompiler_rt/__sync_val_compare_and_swap_n.h
new file mode 100644
index 000000000000..cd4c9a3d1663
--- /dev/null
+++ b/lib/libcompiler_rt/__sync_val_compare_and_swap_n.h
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <machine/atomic.h>
+
+TYPE
+NAME(volatile TYPE *ptr, TYPE oldval, TYPE newval)
+{
+ TYPE t;
+
+ while (!CMPSET(ptr, oldval, newval)) {
+ t = *ptr;
+ if (t != oldval)
+ return (t);
+ }
+
+ return (oldval);
+}