public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Thomas Rodgers <trodgers@redhat.com>
Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org
Subject: Re: [PATCH] Add c++2a binary_semaphore
Date: Tue, 18 Feb 2020 14:25:00 -0000	[thread overview]
Message-ID: <20200218142541.GD9441@redhat.com> (raw)
In-Reply-To: <1291008602.5930779.1582008369882.JavaMail.zimbra@redhat.com>

On 18/02/20 01:46 -0500, Thomas Rodgers wrote:
>This patch adds the c++2a semaphore header and binary_semaphore type. The implementation is not complete, this patch is just to solicit initial feedback.

Here is some initial feedback on comments and whitespace :-)

>diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
>index 89835759069..2dbb7d3a6b1 100644
>--- a/libstdc++-v3/include/Makefile.am
>+++ b/libstdc++-v3/include/Makefile.am
>@@ -69,6 +69,7 @@ std_headers = \
> 	${std_srcdir}/ratio \
> 	${std_srcdir}/regex \
> 	${std_srcdir}/scoped_allocator \
>+  ${std_srcdir}/semaphore \

Indentation is borked.

> 	${std_srcdir}/set \
> 	${std_srcdir}/shared_mutex \
> 	${std_srcdir}/span \
>diff --git a/libstdc++-v3/include/std/semaphore b/libstdc++-v3/include/std/semaphore
>new file mode 100644
>index 00000000000..e3e88a50eec
>--- /dev/null
>+++ b/libstdc++-v3/include/std/semaphore
>@@ -0,0 +1,131 @@
>+// <stop_token> -*- C++ -*-

Wrong header name in comment.

>+
>+// Copyright (C) 2019-2020 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.
>+
>+// Under Section 7 of GPL version 3, you are granted additional
>+// permissions described in the GCC Runtime Library Exception, version
>+// 3.1, as published by the Free Software Foundation.
>+
>+// You should have received a copy of the GNU General Public License and
>+// a copy of the GCC Runtime Library Exception along with this program;
>+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
>+// <http://www.gnu.org/licenses/>.
>+
>+/** @file include/stop_token

Wrong name again.

>+ *  This is a Standard C++ Library header.
>+ */
>+
>+#ifndef _GLIBCXX_SEMAPHORE
>+#define _GLIBCXX_SEMAPHORE
>+
>+#if __cplusplus > 201703L
>+#define __cpp_lib_semaphore 201907L

We shouldn't define this until the file is complete, i.e. when it
provides binary_semaphore and counting_semaphore.

>+
>+#include <cstddef>

Is this header needed? ptrdiff_t and size_t are both defined in
<bits/c++config.h> which every header already includes.

>+#include <atomic>
>+#include <chrono>
>+
>+namespace std _GLIBCXX_VISIBILITY(default)
>+{
>+_GLIBCXX_BEGIN_NAMESPACE_VERSION
>+
>+  // TODO: replace this with a real implementation of std::binary_semaphore
>+  struct binary_semaphore
>+{

Indentation is borked here too.

>+  explicit binary_semaphore(int __d) : _M_counter(__d > 0) { }
>+
>+  static constexpr std::ptrdiff_t
>+  max() noexcept
>+  {
>+   return 1;
>+  }
>+
>+  void release() { _M_counter.fetch_add(1, memory_order::release); }
>+
>+  void acquire()
>+  {
>+   while (!_M_try_acquire())
>+     {
>+      _S_yield();
>+     }
>+  }
>+
>+  bool
>+  try_acquire() noexcept
>+  {
>+   return _M_try_acquire(1u);
>+  }
>+
>+  template<class _Rep, class _Period>
>+  bool try_acquire_for(const std::chrono::duration<_Rep, _Period>& __rel_time)
>+  {
>+   auto __abst = std::chrono::steady_clock::now() + __rel_time;
>+   return try_acquire_until(__abst);
>+  }
>+
>+  template<class _Clock, class _Duration>
>+  bool try_acquire_until(const std::chrono::time_point<_Clock, _Duration>& __abs_time)
>+  {
>+   do
>+     {
>+      if (_M_try_acquire())
>+        {
>+         return true;
>+        }
>+     } while (std::chrono::steady_clock::now() < __abs_time);
>+   return false;
>+  }
>+
>+private:
>+  static void
>+  _S_yield() noexcept
>+  {
>+#if defined __i386__ || defined __x86_64__
>+   __builtin_ia32_pause();
>+#elif defined _GLIBCXX_USE_SCHED_YIELD
>+   __gthread_yield();
>+#endif
>+  }
>+
>+  bool
>+  _M_try_acquire(unsigned __spin_ct)
>+  {
>+   int __old = 1;
>+   while (!_M_counter.compare_exchange_weak(__old, 0,
>+                                            memory_order::acquire,
>+                                            memory_order::relaxed))
>+     {
>+       if (--__spin_ct == 0)
>+         {
>+           return false;
>+         }
>+      __old = 1;
>+     }
>+   return true;
>+  }
>+
>+  static constexpr unsigned _S_spin_ct = 64u;
>+  bool
>+  _M_try_acquire()
>+  {
>+    return _M_try_acquire(1);
>+  }
>+
>+  atomic<int> _M_counter;
>+};
>+
>+_GLIBCXX_END_NAMESPACE_VERSION
>+} // namespace
>+#endif // __cplusplus > 201703L
>+#endif // _GLIBCXX_STOP_TOKEN

Wrong macro name in comment.

  parent reply	other threads:[~2020-02-18 14:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <381004109.5930741.1582008200673.JavaMail.zimbra@redhat.com>
2020-02-18  6:46 ` Thomas Rodgers
2020-02-18 13:49   ` Sebastian Huber
2020-02-18 14:31     ` Jonathan Wakely
2020-02-21 12:33       ` Sebastian Huber
2020-02-18 14:25   ` Jonathan Wakely [this message]
2020-02-20  3:19     ` Thomas Rodgers
2020-02-25  2:53       ` Thomas Rodgers
2020-02-25  8:53         ` Sebastian Huber
2020-02-25 12:33           ` Jonathan Wakely
2020-02-25 12:39             ` Jonathan Wakely
2020-02-26 16:07         ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200218142541.GD9441@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=trodgers@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).