From eed8a97b2f37f5e37de2546cede17dc5aaa10024 Mon Sep 17 00:00:00 2001 From: James Cowgill Date: Sun, 15 Jan 2017 00:16:06 +0000 Subject: Use generic atomic functions Use atomic_fetch_add from C11's stdatomic if available or fallback to GCC's __atomic functions. Remove atomic_add because is can trivially be implemented by calling exchange_and_add instead. --- include/atomicity.h | 33 ++++++++++++++++++++------------- include/internal.h | 2 +- libjack/messagebuffer.c | 2 +- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/atomicity.h b/include/atomicity.h index edf4ada..4c935c5 100644 --- a/include/atomicity.h +++ b/include/atomicity.h @@ -20,19 +20,26 @@ #ifndef __jack_atomicity_h__ #define __jack_atomicity_h__ -/* - * Interface with various machine-dependent headers derived from the - * gcc/libstdc++.v3 sources. We try to modify the GCC sources as - * little as possible. The following include is resolved using the - * config/configure.hosts mechanism. It will use an OS-dependent - * version if available, otherwise the one for this CPU. Some of - * these files might not work with older GCC compilers. - */ -#include +#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__) + +#include + +typedef atomic_int _Atomic_word; + +static inline int exchange_and_add(volatile _Atomic_word* obj, int value) +{ + return atomic_fetch_add_explicit(obj, value, memory_order_relaxed); +} + +#else + +typedef int _Atomic_word; + +static inline int exchange_and_add(volatile _Atomic_word* obj, int value) +{ + return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); +} -/* These functions are defined for each platform. The C++ library - * function names start with "__" to avoid namespace pollution. */ -#define exchange_and_add __exchange_and_add -#define atomic_add __atomic_add +#endif #endif /* __jack_atomicity_h__ */ diff --git a/include/internal.h b/include/internal.h index e3574bd..396b456 100644 --- a/include/internal.h +++ b/include/internal.h @@ -60,7 +60,7 @@ void jack_set_clock_source (jack_timer_type_t); const char* jack_clock_source_name (jack_timer_type_t); #include -#include +#include "atomicity.h" #ifdef JACK_USE_MACH_THREADS #include diff --git a/libjack/messagebuffer.c b/libjack/messagebuffer.c index 5f9541a..082f2f1 100644 --- a/libjack/messagebuffer.c +++ b/libjack/messagebuffer.c @@ -161,7 +161,7 @@ jack_messagebuffer_add (const char *fmt, ...) pthread_cond_signal (&mb_ready_cond); pthread_mutex_unlock (&mb_write_lock); } else { /* lock collision */ - atomic_add (&mb_overruns, 1); + exchange_and_add (&mb_overruns, 1); } } -- cgit v1.2.1 From 1c86be1b4c8eb2977828e403daed5c315f6efefc Mon Sep 17 00:00:00 2001 From: James Cowgill Date: Sun, 15 Jan 2017 00:44:43 +0000 Subject: Remove now unused config/cpu directory --- config/Makefile.am | 2 +- config/cpu/Makefile.am | 2 -- config/cpu/generic/Makefile.am | 3 -- config/cpu/generic/atomicity.h | 39 -------------------- config/cpu/i386/Makefile.am | 3 -- config/cpu/i386/atomicity.h | 53 ---------------------------- config/cpu/powerpc/Makefile.am | 3 -- config/cpu/powerpc/atomicity.h | 80 ------------------------------------------ config/sysdeps/Makefile.am | 1 - config/sysdeps/atomicity.h | 24 ------------- configure.ac | 4 --- 11 files changed, 1 insertion(+), 213 deletions(-) delete mode 100644 config/cpu/Makefile.am delete mode 100644 config/cpu/generic/Makefile.am delete mode 100644 config/cpu/generic/atomicity.h delete mode 100644 config/cpu/i386/Makefile.am delete mode 100644 config/cpu/i386/atomicity.h delete mode 100644 config/cpu/powerpc/Makefile.am delete mode 100644 config/cpu/powerpc/atomicity.h delete mode 100644 config/sysdeps/atomicity.h diff --git a/config/Makefile.am b/config/Makefile.am index d608932..7f06fc8 100644 --- a/config/Makefile.am +++ b/config/Makefile.am @@ -1,7 +1,7 @@ # We don't actually build anything in the `cpu' and `os' # subdirectories. -DIST_SUBDIRS = cpu os sysdeps +DIST_SUBDIRS = os sysdeps EXTRA_DIST = depcomp MAINTAINERCLEANFILES = Makefile.in config.guess config.sub \ diff --git a/config/cpu/Makefile.am b/config/cpu/Makefile.am deleted file mode 100644 index 93dde0d..0000000 --- a/config/cpu/Makefile.am +++ /dev/null @@ -1,2 +0,0 @@ -MAINTAINERCLEANFILES = Makefile.in -DIST_SUBDIRS = generic i386 powerpc diff --git a/config/cpu/generic/Makefile.am b/config/cpu/generic/Makefile.am deleted file mode 100644 index 166d93d..0000000 --- a/config/cpu/generic/Makefile.am +++ /dev/null @@ -1,3 +0,0 @@ -MAINTAINERCLEANFILES = Makefile.in -noinst_HEADERS = atomicity.h - diff --git a/config/cpu/generic/atomicity.h b/config/cpu/generic/atomicity.h deleted file mode 100644 index b781c44..0000000 --- a/config/cpu/generic/atomicity.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Low-level functions for atomic operations. Stub version. - Copyright (C) 1997,2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifndef _ATOMICITY_H -#define _ATOMICITY_H 1 - -typedef int _Atomic_word; - -static inline _Atomic_word -__attribute__ ((__unused__)) -__exchange_and_add (volatile _Atomic_word* mem, int val) -{ - return __sync_fetch_and_add (mem, val); -} - -static inline void -__attribute__ ((__unused__)) -__atomic_add (volatile _Atomic_word* mem, int val) -{ - __sync_add_and_fetch (mem, val); -} - -#endif /* atomicity.h */ diff --git a/config/cpu/i386/Makefile.am b/config/cpu/i386/Makefile.am deleted file mode 100644 index 166d93d..0000000 --- a/config/cpu/i386/Makefile.am +++ /dev/null @@ -1,3 +0,0 @@ -MAINTAINERCLEANFILES = Makefile.in -noinst_HEADERS = atomicity.h - diff --git a/config/cpu/i386/atomicity.h b/config/cpu/i386/atomicity.h deleted file mode 100644 index b9cad70..0000000 --- a/config/cpu/i386/atomicity.h +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 2, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING. If not, write to the Free -// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, -// USA. - -// As a special exception, you may use this file as part of a free software -// library without restriction. Specifically, if other files instantiate -// templates or use macros or inline functions from this file, or you compile -// this file and link it with other files to produce an executable, this -// file does not by itself cause the resulting executable to be covered by -// the GNU General Public License. This exception does not however -// invalidate any other reasons why the executable file might be covered by -// the GNU General Public License. - -#ifndef _GLIBCXX_ATOMICITY_H -#define _GLIBCXX_ATOMICITY_H 1 - -typedef int _Atomic_word; - -static inline _Atomic_word -__attribute__ ((__unused__)) -__exchange_and_add (volatile _Atomic_word* __mem, int __val) -{ - register _Atomic_word __result; - - __asm__ __volatile__ ("lock; xaddl %0,%1" - : "=r" (__result), "=m" (*__mem) - : "0" (__val), "m" (*__mem)); - return __result; -} - -static inline void -__attribute__ ((__unused__)) -__atomic_add (volatile _Atomic_word* __mem, int __val) -{ - __asm__ __volatile__ ("lock; addl %1,%0" - : "=m" (*__mem) : "ir" (__val), "m" (*__mem)); -} - -#endif /* atomicity.h */ diff --git a/config/cpu/powerpc/Makefile.am b/config/cpu/powerpc/Makefile.am deleted file mode 100644 index 166d93d..0000000 --- a/config/cpu/powerpc/Makefile.am +++ /dev/null @@ -1,3 +0,0 @@ -MAINTAINERCLEANFILES = Makefile.in -noinst_HEADERS = atomicity.h - diff --git a/config/cpu/powerpc/atomicity.h b/config/cpu/powerpc/atomicity.h deleted file mode 100644 index 5a74a65..0000000 --- a/config/cpu/powerpc/atomicity.h +++ /dev/null @@ -1,80 +0,0 @@ -// Low-level functions for atomic operations: PowerPC version -*- C++ -*- - -// Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 2, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING. If not, write to the Free -// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, -// USA. - -// As a special exception, you may use this file as part of a free software -// library without restriction. Specifically, if other files instantiate -// templates or use macros or inline functions from this file, or you compile -// this file and link it with other files to produce an executable, this -// file does not by itself cause the resulting executable to be covered by -// the GNU General Public License. This exception does not however -// invalidate any other reasons why the executable file might be covered by -// the GNU General Public License. - -#ifndef _GLIBCXX_ATOMICITY_H -#define _GLIBCXX_ATOMICITY_H 1 - -#ifdef __PPC405__ -#define _STWCX "sync \n\tstwcx. " -#else -#define _STWCX "stwcx. " -#endif - -typedef int _Atomic_word; - -static inline _Atomic_word -__attribute__ ((__unused__)) -__exchange_and_add (volatile _Atomic_word* __mem, int __val) -{ - _Atomic_word __tmp, __res; - - __asm__ __volatile__ ( - "/* Inline exchange & add */\n" - "0:\t" - "lwarx %0,0,%3 \n\t" - "add%I4 %1,%0,%4 \n\t" - _STWCX " %1,0,%3 \n\t" - "bne- 0b \n\t" - "/* End exchange & add */" - : "=&b" (__res), "=&r" (__tmp), "=m" (*__mem) - : "r" (__mem), "Ir" (__val), "m" (*__mem) - : "cr0"); - return __res; -} - -static inline void -__attribute__ ((__unused__)) -__atomic_add (volatile _Atomic_word* __mem, int __val) -{ - _Atomic_word __tmp; - - __asm__ __volatile__ ( - "/* Inline atomic add */\n" - "0:\t" - "lwarx %0,0,%2 \n\t" - "add%I3 %0,%0,%3 \n\t" - _STWCX " %0,0,%2 \n\t" - "bne- 0b \n\t" - "/* End atomic add */" - : "=&b" (__tmp), "=m" (*__mem) - : "r" (__mem), "Ir" (__val), "m" (*__mem) - : "cr0"); -} - -#endif /* atomicity.h */ diff --git a/config/sysdeps/Makefile.am b/config/sysdeps/Makefile.am index ca6d438..db98e58 100644 --- a/config/sysdeps/Makefile.am +++ b/config/sysdeps/Makefile.am @@ -7,7 +7,6 @@ MAINTAINERCLEANFILES = Makefile.in noinst_HEADERS = \ systemtest.c \ sanitycheck.c \ - atomicity.h \ getopt.h \ ipc.h \ mach_port.h \ diff --git a/config/sysdeps/atomicity.h b/config/sysdeps/atomicity.h deleted file mode 100644 index ae86b12..0000000 --- a/config/sysdeps/atomicity.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _jack_sysdep_atomicity_h_ -#define _jack_sysdep_atomicity_h_ - -#if defined(__i386__) - -#include - -#elif defined(__x86_64) - -/* x86_64 can use rdtsc just like i[456]86 */ - -#include - -#elif defined(__powerpc__) || defined(__ppc__) /* linux and OSX use different tokens */ - -#include - -#else - -#include - -#endif /* processor selection */ - -#endif /* _jack_sysdep_atomicity_h_ */ diff --git a/configure.ac b/configure.ac index 4c0e910..9c7cbf1 100644 --- a/configure.ac +++ b/configure.ac @@ -964,10 +964,6 @@ AM_CONDITIONAL(HAVE_ZITA_BRIDGE_DEPS, $HAVE_ZITA_BRIDGE_DEPS) AC_OUTPUT( Makefile config/Makefile -config/cpu/Makefile -config/cpu/generic/Makefile -config/cpu/i386/Makefile -config/cpu/powerpc/Makefile config/os/Makefile config/os/generic/Makefile config/os/gnu-linux/Makefile -- cgit v1.2.1 From ddc60b08e2fc5a051a131fb468c2f5343bf9f335 Mon Sep 17 00:00:00 2001 From: James Cowgill Date: Sun, 15 Jan 2017 00:44:51 +0000 Subject: Remove atomics section from doc/porting.dox --- doc/porting.dox | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/doc/porting.dox b/doc/porting.dox index 011e2e9..d9c4633 100644 --- a/doc/porting.dox +++ b/doc/porting.dox @@ -88,25 +88,6 @@ Be sure to place any generic implementation alternative in the @c \#else or use an @c \#ifndef, so no other code needs to know your conditional labels. -@section portcpu Processor Dependencies - -JACK uses some low-level machine operations for thread-safe updates to -shared memory. A low-level implementation of @c -is provided for every target processor architecture. There is also a -generic implementation using POSIX spin locks, but that is not a good -enough solution for serious use. - -The GCC package provides versions that work on most modern hardware. -We've tried to keep things as close to the original as possible, while -removing a bunch of os-specific files that didn't seem relevant. A -primary goal has been to avoid changing the CPU-dependent @c - headers. - -The relevant GCC documentation provides some helpful background, -especially the @c atomicity.h discussion at -. - - @section portissues Issues Not Addressed - Cross-compilation has not been tested, or even thought through in -- cgit v1.2.1