public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [libstdc++/65033] Give alignment info to libatomic
@ 2015-02-12 21:23 Richard Henderson
  2015-02-18 12:15 ` Jonathan Wakely
                   ` (2 more replies)
  0 siblings, 3 replies; 34+ messages in thread
From: Richard Henderson @ 2015-02-12 21:23 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1087 bytes --]

When we fixed PR54005, making sure that atomic_is_lock_free returns the same
value for all objects of a given type, we probably should have changed the
interface so that we would pass size and alignment rather than size and object
pointer.

Instead, we decided that passing null for the object pointer would be
sufficient.  But as this PR shows, we really do need to take alignment into
account.

The following patch constructs a fake object pointer that is maximally
misaligned.  This allows the interface to both the builtin and to libatomic to
remain unchanged.  Which probably makes this back-portable to maintenance
releases as well.

I believe that for all of our current systems, size_t == uintptr_t, so the
reinterpret_cast ought not generate warnings.

The test case is problematic, as there's currently no good place to put it.
The libstdc++ testsuite doesn't have the libatomic library path configured, and
the libatomic testsuite doesn't have the libstdc++ include paths configured.
Yet another example where we really need an install tree for testing.  Thoughts?


Ok?


r~

[-- Attachment #2: z --]
[-- Type: text/plain, Size: 2846 bytes --]

	* include/bits/atomic_base.h (__atomic_base<T>::is_lock_free): Build
	a fake pointer indicating type alignment.
	(__atomic_base<T *>::is_lock_free): Likewise.
	* include/std/atomic (atomic<T>::is_lock_free): Likewise.

diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
index fe6524f..8104c98 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -346,11 +346,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       bool
       is_lock_free() const noexcept
-      { return __atomic_is_lock_free(sizeof(_M_i), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_i));
+	return __atomic_is_lock_free(sizeof(_M_i), __a);
+      }
 
       bool
       is_lock_free() const volatile noexcept
-      { return __atomic_is_lock_free(sizeof(_M_i), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_i));
+	return __atomic_is_lock_free(sizeof(_M_i), __a);
+      }
 
       _GLIBCXX_ALWAYS_INLINE void
       store(__int_type __i, memory_order __m = memory_order_seq_cst) noexcept
@@ -653,11 +661,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       bool
       is_lock_free() const noexcept
-      { return __atomic_is_lock_free(sizeof(__pointer_type), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_p));
+	return __atomic_is_lock_free(sizeof(_M_p), __a);
+      }
 
       bool
       is_lock_free() const volatile noexcept
-      { return __atomic_is_lock_free(sizeof(__pointer_type), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_p));
+	return __atomic_is_lock_free(sizeof(_M_p), __a);
+      }
 
       _GLIBCXX_ALWAYS_INLINE void
       store(__pointer_type __p,
diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic
index 1a17427..cc4b5f1 100644
--- a/libstdc++-v3/include/std/atomic
+++ b/libstdc++-v3/include/std/atomic
@@ -198,11 +198,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       bool
       is_lock_free() const noexcept
-      { return __atomic_is_lock_free(sizeof(_M_i), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_i));
+	return __atomic_is_lock_free(sizeof(_M_i), __a);
+      }
 
       bool
       is_lock_free() const volatile noexcept
-      { return __atomic_is_lock_free(sizeof(_M_i), nullptr); }
+      {
+	// Produce a fake, minimally aligned pointer.
+	void *__a = reinterpret_cast<void *>(-__alignof(_M_i));
+	return __atomic_is_lock_free(sizeof(_M_i), __a);
+      }
 
       void
       store(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 65033.cc --]
[-- Type: text/x-c++src; name="65033.cc", Size: 1106 bytes --]

// Copyright (C) 2015 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 3, 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 COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-require-atomic-builtins "" }
// { dg-options "-std=gnu++11" }

#include <atomic>
#include <testsuite_hooks.h>

struct S2 { char a[2]; };
struct S3 { char a[3]; };

int
main()
{
  std::atomic<S2> s2;
  std::atomic<S3> s3;

  VERIFY( s2.is_lock_free() == false );
  VERIFY( s3.is_lock_free() == false );
}

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2015-04-13 17:53 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-12 21:23 [libstdc++/65033] Give alignment info to libatomic Richard Henderson
2015-02-18 12:15 ` Jonathan Wakely
2015-03-25 16:22   ` Jonathan Wakely
2015-03-25 18:36     ` Richard Henderson
2015-03-25 18:49       ` Jonathan Wakely
2015-03-25 19:04         ` Richard Henderson
2015-03-26 13:21           ` Jonathan Wakely
2015-03-31 13:41             ` Jonathan Wakely
2015-03-31 14:54               ` Richard Henderson
2015-03-31 15:03                 ` Jonathan Wakely
2015-03-31 15:13                   ` Richard Henderson
2015-03-31 15:41                     ` Jonathan Wakely
2015-04-06 22:59             ` Hans-Peter Nilsson
2015-04-13  4:45             ` patch fix issue 1 with "[libstdc++/65033] Give alignment info to libatomic" Hans-Peter Nilsson
2015-04-13 11:59               ` Jonathan Wakely
2015-04-13  5:59             ` Issue 2 " Hans-Peter Nilsson
2015-04-13 17:53               ` Joseph Myers
2015-03-25 18:39     ` [libstdc++/65033] Give alignment info to libatomic Richard Henderson
2015-04-03  3:04     ` Hans-Peter Nilsson
2015-03-26 11:54 ` Jonathan Wakely
2015-04-03  3:57 ` Hans-Peter Nilsson
2015-04-03  9:25   ` Hans-Peter Nilsson
2015-04-03 14:13     ` Jonathan Wakely
2015-04-03 19:13       ` Richard Henderson
2015-04-07 13:14         ` Jonathan Wakely
2015-04-09 11:17           ` Jonathan Wakely
2015-04-06  1:07       ` Hans-Peter Nilsson
2015-04-07  9:45         ` Jonathan Wakely
2015-04-07 10:52           ` Hans-Peter Nilsson
2015-04-07 13:12             ` Jonathan Wakely
2015-04-07 14:51               ` Hans-Peter Nilsson
2015-04-07 15:06                 ` Jonathan Wakely
2015-04-08  3:58                   ` Hans-Peter Nilsson
2015-04-08  9:35                     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).