Hi ports --

Some context: gcc and clang both support two different atomic builtin function families. The first are the __sync functions, which as far as I can tell appeared in gcc-4.1. The second is the __atomic functions, which appeared in gcc-4.7.

Not all architectures and OSes have support for the __sync function. hppa is one such architecture. However, all architectures have support for the __atomic functions, as those are the builtins that the C11 and C++11 stdatomic functions are implemented in. The __sync functions are considered obsolete and may be removed from gcc at some point. So all new code should be __atomic only.

The gcc documentation says "It is*always*safe to replace a __sync call with an __atomic call using the __ATOMIC_SEQ_CST memory model. This is in fact what happens internally right now. (__sync calls are processed by the new __atomic routines)" [0]. Therefore, we can replace the __sync calls in libuv with the equivalent __atomic calls and it will build on hppa.

This change affects all archs except amd64 and i386, as those two have hand-written assembly functions that the libuv team wrote. The __atomic routines are used on all other archs do.

Tested on amd64 (no change) and hppa with gcc-8.3.0 (works now). With this change, CMake works again on hppa.

In the long run, I think I would like to change all references of __sync to __atomic. But that's a conversation for another email.

Tests on sparc64 and macppc and mips64* would be nice as a layer of safety.

OK for libuv?

~Brian

[0] https://gcc.gnu.org/wiki/Atomic/GCCMM

Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/libuv/Makefile,v
retrieving revision 1.9
diff -u -p -r1.9 Makefile
--- Makefile	4 Jan 2019 08:53:54 -0000	1.9
+++ Makefile	15 Apr 2019 19:01:20 -0000
@@ -7,6 +7,7 @@ COMMENT =	multi-platform library for asy
 VER =		1.24.1
 DISTNAME =	libuv-v${VER}
 PKGNAME =	libuv-${VER}
+REVISION =	0
 CATEGORIES =	devel
 
 SHARED_LIBS =	uv 2.0	# 1.0
@@ -19,6 +20,8 @@ MASTER_SITES =	https://dist.libuv.org/di
 PERMIT_PACKAGE_CDROM = Yes
 
 WANTLIB += pthread
+
+COMPILER =	base-clang ports-gcc
 
 BUILD_DEPENDS =	devel/libtool \
 		${MODGNU_AUTOCONF_DEPENDS} \
Index: patches/patch-src_unix_atomic-ops_h
===================================================================
RCS file: patches/patch-src_unix_atomic-ops_h
diff -N patches/patch-src_unix_atomic-ops_h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_unix_atomic-ops_h	15 Apr 2019 19:01:20 -0000
@@ -0,0 +1,34 @@
+$OpenBSD$
+
+Replace obsolete __sync functions with standard __atomic functions.
+
+Index: src/unix/atomic-ops.h
+--- src/unix/atomic-ops.h.orig
++++ src/unix/atomic-ops.h
+@@ -22,6 +22,8 @@
+ #include <atomic.h>
+ #endif
+ 
++#include <stdbool.h>
++
+ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval));
+ UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval));
+ UV_UNUSED(static void cpu_relax(void));
+@@ -51,7 +53,7 @@ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, in
+ #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
+   return atomic_cas_uint(ptr, oldval, newval);
+ #else
+-  return __sync_val_compare_and_swap(ptr, oldval, newval);
++  return __atomic_compare_exchange_n(ptr, &oldval, newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+ #endif
+ }
+ 
+@@ -87,7 +89,7 @@ UV_UNUSED(static long cmpxchgl(long* ptr, long oldval,
+ #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
+   return atomic_cas_ulong(ptr, oldval, newval);
+ #else
+-  return __sync_val_compare_and_swap(ptr, oldval, newval);
++  return __atomic_compare_exchange_n(ptr, &oldval, newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+ #endif
+ }
+ 

Reply via email to