gnu/161499: [patch] Use FreeBSD's atomic.h if no cpu-specific code is available

Ian Lepore freebsd at damnhippie.dyndns.org
Tue Oct 11 19:40:09 UTC 2011


>Number:         161499
>Category:       gnu
>Synopsis:       [patch] Use FreeBSD's atomic.h if no cpu-specific code is available
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct 11 19:40:08 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Ian Lepore <freebsd at damnhippie.dyndns.org>
>Release:        FreeBSD 8.2-STABLE arm
>Organization:
Symmetricom, Inc.
>Environment:
FreeBSD tflex 8.2-STABLE FreeBSD 8.2-STABLE #29: Tue Oct 11 13:32:35 UTC 2011     root at revolution.hippie.lan:/usr/obj/arm/usr/src/sys/TFLEX  arm

>Description:
The attached patch adds contrib/libstdc++/config/os/bsd/freebsd/atomicity.h
which implements the libstdc++ atomic ops in terms of FreeBSD's atomic.h, 
and changes the Makefile to use this implementation when a cpu-specific 
implementation is not available.  On the ARM platform this give us fast
atomic ops using the ARM RAS routines instead of generic pthread mutexes.
It may confer similar benefits on other non-x86 platforms.

I created this file by cloning an existing atomicity.h and replacing the
guts of each function.  I left the original GPL license block from the file I
started with in place, but I'm not sure that's appropriate; perhaps it should
be BSD-licensed.  I personally don't wish to assert any copyright or license
of my own, so please feel free to change the license block to whatever makes
the most sense.

>How-To-Repeat:
N/A

>Fix:


--- libstdcpp_atomicity.diff begins here ---
diff -r 8c63128f0de0 -r 18d5166659aa gnu/lib/libstdc++/Makefile
--- gnu/lib/libstdc++/Makefile	Mon Aug 01 17:47:21 2011 -0600
+++ gnu/lib/libstdc++/Makefile	Tue Aug 02 09:55:50 2011 -0600
@@ -77,6 +77,8 @@ MARCHDIR=	${MACHINE_ARCH}
 
 .if exists(${SRCDIR}/config/cpu/${MARCHDIR}/atomicity.h)
 ATOMICITY_H=	${SRCDIR}/config/cpu/${MARCHDIR}/atomicity.h
+.elif exists(${SRCDIR}/config/os/bsd/freebsd/atomicity.h) 
+ATOMICITY_H=	${SRCDIR}/config/os/bsd/freebsd/atomicity.h
 .else
 ATOMICITY_H=	${SRCDIR}/config/cpu/generic/atomicity_mutex/atomicity.h
 .endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ contrib/libstdc++/config/os/bsd/freebsd/atomicity.h	Tue Aug 02 09:55:50 2011 -0600
@@ -0,0 +1,73 @@
+// Low-level functions for atomic operations: FreeBSD MI version  -*- C++ -*-
+
+// Copyright (C) 2000, 2001, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// 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.
+
+#include <ext/atomicity.h>
+
+/* The FreeBSD machine/atomic.h header defines fast atomic math operations
+ * appropriate to the target hardware (handling SMP vs. non-SMP, etc).  
+ * However, it defines everything in terms of unsigned 32 bit operands.
+ * We can safely cast away the unsignedness to keep the compiler happy,
+ * as long as GCC's _Atomic_word is 32 bits (hence the CTASSERT below).
+ * I'd like to use FreeBSD's CTASSERT macro, but it lives in a header
+ * that has eleventymillion dependencies, so it's just pasted in here.
+ */
+
+extern "C"
+{
+#include <sys/types.h>
+#include <sys/cdefs.h>
+#include <machine/atomic.h>
+} 
+
+#ifndef CTASSERT
+#define CTASSERT(x)             _CTASSERT(x, __LINE__)
+#define _CTASSERT(x, y)         __CTASSERT(x, y)
+#define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
+#endif
+
+CTASSERT(sizeof(_Atomic_word) == sizeof(uint32_t));
+
+
+_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
+
+  _Atomic_word
+  __attribute__ ((__unused__))
+  __exchange_and_add (volatile _Atomic_word* __mem, int __val)
+  {
+    return (_Atomic_word) ::atomic_fetchadd_32((uint32_t *)__mem, (uint32_t)__val);
+  }
+
+  void
+  __attribute__ ((__unused__))
+  __atomic_add (volatile _Atomic_word* __mem, int __val)
+  {
+    ::atomic_add_32((uint32_t *)__mem, (uint32_t)__val);
+  }
+
+_GLIBCXX_END_NAMESPACE
--- libstdcpp_atomicity.diff ends here ---

>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list