diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2019-02-01 21:55:38 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2019-02-01 21:55:38 +0000 |
commit | b52a3881f04799d410f7ec70d022179c8d734459 (patch) | |
tree | eace57a9fb5df73173371815f0a0e1a5913a77a8 /libgo/go | |
parent | a53a893b4fe04ec966a4ec178ee8f394426a5dad (diff) | |
download | gcc-b52a3881f04799d410f7ec70d022179c8d734459.zip gcc-b52a3881f04799d410f7ec70d022179c8d734459.tar.gz gcc-b52a3881f04799d410f7ec70d022179c8d734459.tar.bz2 |
runtime, sync: use __atomic intrinsics instead of __sync
GCC has supported the __atomic intrinsics since 4.7. They are better
than the __sync intrinsics in that they specify a memory model and,
more importantly for our purposes, they are reliably implemented
either in the compiler or in libatomic.
Fixes https://gcc.gnu.org/PR52084
Reviewed-on: https://go-review.googlesource.com/c/160820
From-SVN: r268458
Diffstat (limited to 'libgo/go')
-rw-r--r-- | libgo/go/runtime/testdata/testprogcgo/lockosthread.c | 2 | ||||
-rw-r--r-- | libgo/go/runtime/testdata/testprogcgo/threadpprof.go | 4 | ||||
-rw-r--r-- | libgo/go/runtime/testdata/testprogcgo/tracebackctxt_c.c | 8 | ||||
-rw-r--r-- | libgo/go/sync/atomic/atomic.c | 97 | ||||
-rw-r--r-- | libgo/go/sync/cas.c | 17 |
5 files changed, 33 insertions, 95 deletions
diff --git a/libgo/go/runtime/testdata/testprogcgo/lockosthread.c b/libgo/go/runtime/testdata/testprogcgo/lockosthread.c index b10cc4f..9a8c057 100644 --- a/libgo/go/runtime/testdata/testprogcgo/lockosthread.c +++ b/libgo/go/runtime/testdata/testprogcgo/lockosthread.c @@ -9,5 +9,5 @@ uint32_t threadExited; void setExited(void *x) { - __sync_fetch_and_add(&threadExited, 1); + __atomic_add_fetch(&threadExited, 1, __ATOMIC_SEQ_CST); } diff --git a/libgo/go/runtime/testdata/testprogcgo/threadpprof.go b/libgo/go/runtime/testdata/testprogcgo/threadpprof.go index f803511..28b094f 100644 --- a/libgo/go/runtime/testdata/testprogcgo/threadpprof.go +++ b/libgo/go/runtime/testdata/testprogcgo/threadpprof.go @@ -50,13 +50,13 @@ void pprofCgoThreadTraceback(void* parg) { arg->buf[0] = (uintptr_t)(cpuHogThread) + 0x10; arg->buf[1] = (uintptr_t)(cpuHogThread2) + 0x4; arg->buf[2] = 0; - __sync_add_and_fetch(&cpuHogThreadCount, 1); + __atomic_add_fetch(&cpuHogThreadCount, 1, __ATOMIC_SEQ_CST); } // getCPUHogThreadCount fetches the number of times we've seen cpuHogThread // in the traceback. int getCPUHogThreadCount() { - return __sync_add_and_fetch(&cpuHogThreadCount, 0); + return __atomic_load(&cpuHogThreadCount, __ATOMIC_SEQ_CST); } static void* cpuHogDriver(void* arg __attribute__ ((unused))) { diff --git a/libgo/go/runtime/testdata/testprogcgo/tracebackctxt_c.c b/libgo/go/runtime/testdata/testprogcgo/tracebackctxt_c.c index f02b7ca..ff6895e 100644 --- a/libgo/go/runtime/testdata/testprogcgo/tracebackctxt_c.c +++ b/libgo/go/runtime/testdata/testprogcgo/tracebackctxt_c.c @@ -49,18 +49,18 @@ struct cgoSymbolizerArg { static int contextCount; int getContextCount() { - return __sync_add_and_fetch(&contextCount, 0); + return __atomic_load_n(&contextCount, __ATOMIC_SEQ_CST); } void tcContext(void* parg) { struct cgoContextArg* arg = (struct cgoContextArg*)(parg); if (arg->context == 0) { - arg->context = __sync_add_and_fetch(&contextCount, 1); + arg->context = __atomic_add_fetch(&contextCount, 1, __ATOMIC_SEQ_CST); } else { - if (arg->context != __sync_add_and_fetch(&contextCount, 0)) { + if (arg->context != __atomic_load_n(&contextCount, __ATOMIC_SEQ_CST)) abort(); } - __sync_sub_and_fetch(&contextCount, 1); + __atomic_sub_fetch(&contextCount, 1, __ATOMIC_SEQ_CST); } } diff --git a/libgo/go/sync/atomic/atomic.c b/libgo/go/sync/atomic/atomic.c index 6cc730f..25e439d 100644 --- a/libgo/go/sync/atomic/atomic.c +++ b/libgo/go/sync/atomic/atomic.c @@ -69,7 +69,8 @@ _Bool CompareAndSwapInt32 (int32_t *, int32_t, int32_t) _Bool CompareAndSwapInt32 (int32_t *val, int32_t old, int32_t new) { - return __sync_bool_compare_and_swap (val, old, new); + return __atomic_compare_exchange_n (val, &old, new, true, __ATOMIC_SEQ_CST, + __ATOMIC_RELAXED); } _Bool CompareAndSwapInt64 (int64_t *, int64_t, int64_t) @@ -81,7 +82,8 @@ CompareAndSwapInt64 (int64_t *val, int64_t old, int64_t new) { if (((uintptr_t) val & 7) != 0) val = NULL; - return __sync_bool_compare_and_swap (val, old, new); + return __atomic_compare_exchange_n (val, &old, new, true, __ATOMIC_SEQ_CST, + __ATOMIC_RELAXED); } _Bool CompareAndSwapUint32 (uint32_t *, uint32_t, uint32_t) @@ -91,7 +93,8 @@ _Bool CompareAndSwapUint32 (uint32_t *, uint32_t, uint32_t) _Bool CompareAndSwapUint32 (uint32_t *val, uint32_t old, uint32_t new) { - return __sync_bool_compare_and_swap (val, old, new); + return __atomic_compare_exchange_n (val, &old, new, true, __ATOMIC_SEQ_CST, + __ATOMIC_RELAXED); } _Bool CompareAndSwapUint64 (uint64_t *, uint64_t, uint64_t) @@ -103,7 +106,8 @@ CompareAndSwapUint64 (uint64_t *val, uint64_t old, uint64_t new) { if (((uintptr_t) val & 7) != 0) val = NULL; - return __sync_bool_compare_and_swap (val, old, new); + return __atomic_compare_exchange_n (val, &old, new, true, __ATOMIC_SEQ_CST, + __ATOMIC_RELAXED); } _Bool CompareAndSwapUintptr (uintptr_t *, uintptr_t, uintptr_t) @@ -113,7 +117,8 @@ _Bool CompareAndSwapUintptr (uintptr_t *, uintptr_t, uintptr_t) _Bool CompareAndSwapUintptr (uintptr_t *val, uintptr_t old, uintptr_t new) { - return __sync_bool_compare_and_swap (val, old, new); + return __atomic_compare_exchange_n (val, &old, new, true, __ATOMIC_SEQ_CST, + __ATOMIC_RELAXED); } int32_t AddInt32 (int32_t *, int32_t) @@ -123,7 +128,7 @@ int32_t AddInt32 (int32_t *, int32_t) int32_t AddInt32 (int32_t *val, int32_t delta) { - return __sync_add_and_fetch (val, delta); + return __atomic_add_fetch (val, delta, __ATOMIC_SEQ_CST); } uint32_t AddUint32 (uint32_t *, uint32_t) @@ -133,7 +138,7 @@ uint32_t AddUint32 (uint32_t *, uint32_t) uint32_t AddUint32 (uint32_t *val, uint32_t delta) { - return __sync_add_and_fetch (val, delta); + return __atomic_add_fetch (val, delta, __ATOMIC_SEQ_CST); } int64_t AddInt64 (int64_t *, int64_t) @@ -145,7 +150,7 @@ AddInt64 (int64_t *val, int64_t delta) { if (((uintptr_t) val & 7) != 0) val = NULL; - return __sync_add_and_fetch (val, delta); + return __atomic_add_fetch (val, delta, __ATOMIC_SEQ_CST); } uint64_t AddUint64 (uint64_t *, uint64_t) @@ -157,7 +162,7 @@ AddUint64 (uint64_t *val, uint64_t delta) { if (((uintptr_t) val & 7) != 0) val = NULL; - return __sync_add_and_fetch (val, delta); + return __atomic_add_fetch (val, delta, __ATOMIC_SEQ_CST); } uintptr_t AddUintptr (uintptr_t *, uintptr_t) @@ -167,7 +172,7 @@ uintptr_t AddUintptr (uintptr_t *, uintptr_t) uintptr_t AddUintptr (uintptr_t *val, uintptr_t delta) { - return __sync_add_and_fetch (val, delta); + return __atomic_add_fetch (val, delta, __ATOMIC_SEQ_CST); } int32_t LoadInt32 (int32_t *addr) @@ -177,12 +182,7 @@ int32_t LoadInt32 (int32_t *addr) int32_t LoadInt32 (int32_t *addr) { - int32_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } int64_t LoadInt64 (int64_t *addr) @@ -192,14 +192,9 @@ int64_t LoadInt64 (int64_t *addr) int64_t LoadInt64 (int64_t *addr) { - int64_t v; - if (((uintptr_t) addr & 7) != 0) panicmem (); - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } uint32_t LoadUint32 (uint32_t *addr) @@ -209,12 +204,7 @@ uint32_t LoadUint32 (uint32_t *addr) uint32_t LoadUint32 (uint32_t *addr) { - uint32_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } uint64_t LoadUint64 (uint64_t *addr) @@ -224,14 +214,9 @@ uint64_t LoadUint64 (uint64_t *addr) uint64_t LoadUint64 (uint64_t *addr) { - uint64_t v; - if (((uintptr_t) addr & 7) != 0) panicmem (); - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } uintptr_t LoadUintptr (uintptr_t *addr) @@ -241,12 +226,7 @@ uintptr_t LoadUintptr (uintptr_t *addr) uintptr_t LoadUintptr (uintptr_t *addr) { - uintptr_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } void *LoadPointer (void **addr) @@ -256,12 +236,7 @@ void *LoadPointer (void **addr) void * LoadPointer (void **addr) { - void *v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, v)) - v = *addr; - return v; + return __atomic_load_n (addr, __ATOMIC_SEQ_CST); } void StoreInt32 (int32_t *addr, int32_t val) @@ -271,11 +246,7 @@ void StoreInt32 (int32_t *addr, int32_t val) void StoreInt32 (int32_t *addr, int32_t val) { - int32_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, val)) - v = *addr; + __atomic_store_n (addr, val, __ATOMIC_SEQ_CST); } void StoreInt64 (int64_t *addr, int64_t val) @@ -285,13 +256,9 @@ void StoreInt64 (int64_t *addr, int64_t val) void StoreInt64 (int64_t *addr, int64_t val) { - int64_t v; - if (((uintptr_t) addr & 7) != 0) panicmem (); - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, val)) - v = *addr; + __atomic_store_n (addr, val, __ATOMIC_SEQ_CST); } void StoreUint32 (uint32_t *addr, uint32_t val) @@ -301,11 +268,7 @@ void StoreUint32 (uint32_t *addr, uint32_t val) void StoreUint32 (uint32_t *addr, uint32_t val) { - uint32_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, val)) - v = *addr; + __atomic_store_n (addr, val, __ATOMIC_SEQ_CST); } void StoreUint64 (uint64_t *addr, uint64_t val) @@ -315,13 +278,9 @@ void StoreUint64 (uint64_t *addr, uint64_t val) void StoreUint64 (uint64_t *addr, uint64_t val) { - uint64_t v; - if (((uintptr_t) addr & 7) != 0) panicmem (); - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, val)) - v = *addr; + __atomic_store_n (addr, val, __ATOMIC_SEQ_CST); } void StoreUintptr (uintptr_t *addr, uintptr_t val) @@ -331,9 +290,5 @@ void StoreUintptr (uintptr_t *addr, uintptr_t val) void StoreUintptr (uintptr_t *addr, uintptr_t val) { - uintptr_t v; - - v = *addr; - while (! __sync_bool_compare_and_swap (addr, v, val)) - v = *addr; + __atomic_store_n (addr, val, __ATOMIC_SEQ_CST); } diff --git a/libgo/go/sync/cas.c b/libgo/go/sync/cas.c deleted file mode 100644 index 7571c64..0000000 --- a/libgo/go/sync/cas.c +++ /dev/null @@ -1,17 +0,0 @@ -/* cas.c -- implement sync.cas for Go. - - Copyright 2009 The Go Authors. All rights reserved. - Use of this source code is governed by a BSD-style - license that can be found in the LICENSE file. */ - -#include <stdint.h> - -#include "runtime.h" - -_Bool cas (int32_t *, int32_t, int32_t) __asm__ (GOSYM_PREFIX "libgo_sync.sync.cas"); - -_Bool -cas (int32_t *ptr, int32_t old, int32_t new) -{ - return __sync_bool_compare_and_swap (ptr, old, new); -} |