public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration
@ 2021-11-11 10:54 s.zvyagin83 at gmail dot com
  2021-11-11 12:08 ` [Bug libstdc++/103187] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: s.zvyagin83 at gmail dot com @ 2021-11-11 10:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

            Bug ID: 103187
           Summary: std::counting_semaphore::try_acquire_for does not
                    unblock during wait duration
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: s.zvyagin83 at gmail dot com
  Target Milestone: ---

According to C++ standart https://eel.is/c++draft/thread.sema.cnt#18.2
try_acquire_for/until should block until counter is greater than zero or until
timeout expires. 

But semaphores implemented with atomic_wait blocks for whole timeout even if
semaphore released during wait. 

minimal repoduce code:
g++ -std=c++20 -lpthread

#include <thread>
#include <semaphore>
#include <time.h>
#include <stdio.h>

using namespace std::chrono_literals;

int main()
{
  std::binary_semaphore sem{0};
  auto begin = std::chrono::steady_clock::now();
  auto time = [&begin] {
      return
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-begin).count();
  };
  std::jthread jthr(
      [&sem, &time] {
        printf("[%ld] thread start\n", time());
        std::this_thread::sleep_for(1s);
        printf("[%ld] sem.release\n", time());
        sem.release();
      });
  printf("[%ld] sem.try_acquire_for\n", time());
  if (sem.try_acquire_for(10s))
    printf("[%ld] sem.acquired\n", time());
  else
    printf("[%ld] failed to acquire sem\n", time());
}

output:
[0] sem.try_acquire_for
[0] thread start
[1000] sem.release
[10000] sem.acquired

POSIX semaphores with same code work correctly and unblock immediately when
semaphore released
g++ -std=c++20 -lpthread -D_GLIBCXX_USE_POSIX_SEMAPHORE=1

output:
[0] sem.try_acquire_for
[0] thread start
[1000] sem.release
[1000] sem.acquired

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

* [Bug libstdc++/103187] std::counting_semaphore::try_acquire_for does not unblock during wait duration
  2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
@ 2021-11-11 12:08 ` redi at gcc dot gnu.org
  2021-11-11 12:17 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-11 12:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |11.2.1
      Known to work|                            |12.0
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-11-11
   Target Milestone|---                         |11.3
                 CC|                            |rodgertq at gcc dot gnu.org

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Already fixed on trunk, I think we just need a backport.

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

* [Bug libstdc++/103187] std::counting_semaphore::try_acquire_for does not unblock during wait duration
  2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
  2021-11-11 12:08 ` [Bug libstdc++/103187] " redi at gcc dot gnu.org
@ 2021-11-11 12:17 ` redi at gcc dot gnu.org
  2021-11-23 21:19 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-11 12:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The relevant commit seems to be g:aeaea265cea3a2b2e772af7825351a4ceef29aac but
that *should* only be an optimization to use futexes when available. The fact
it times out without that change suggests the non-futex code has a bug, and
it's probably still there on trunk.

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

* [Bug libstdc++/103187] std::counting_semaphore::try_acquire_for does not unblock during wait duration
  2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
  2021-11-11 12:08 ` [Bug libstdc++/103187] " redi at gcc dot gnu.org
  2021-11-11 12:17 ` redi at gcc dot gnu.org
@ 2021-11-23 21:19 ` redi at gcc dot gnu.org
  2022-04-21  7:50 ` rguenth at gcc dot gnu.org
  2022-08-04 13:29 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-23 21:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I've backported that fix for GCC 11.3 but I want to keep this open to get to
the bottom of the underlying bug.

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

* [Bug libstdc++/103187] std::counting_semaphore::try_acquire_for does not unblock during wait duration
  2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-11-23 21:19 ` redi at gcc dot gnu.org
@ 2022-04-21  7:50 ` rguenth at gcc dot gnu.org
  2022-08-04 13:29 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug libstdc++/103187] std::counting_semaphore::try_acquire_for does not unblock during wait duration
  2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
                   ` (3 preceding siblings ...)
  2022-04-21  7:50 ` rguenth at gcc dot gnu.org
@ 2022-08-04 13:29 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2022-08-04 13:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103187

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.3
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #2)
> The relevant commit seems to be g:aeaea265cea3a2b2e772af7825351a4ceef29aac
> but that *should* only be an optimization to use futexes when available.

Ah no, because the HAVE_PLATFORM_WAIT macro was being set, but too late to
affect the value of the __platform_wait_uses_type variable template, it meant
that _S_wait_addr always returned &_M_w._M_ver and did a futex wait on that,
but the semaphore release() was doing a futex wake on &_M_counter instead.

> The
> fact it times out without that change suggests the non-futex code has a bug,
> and it's probably still there on trunk.

I think it's fixed by the above commit, which is in GCC 11.3 and later.

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

end of thread, other threads:[~2022-08-04 13:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-11 10:54 [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration s.zvyagin83 at gmail dot com
2021-11-11 12:08 ` [Bug libstdc++/103187] " redi at gcc dot gnu.org
2021-11-11 12:17 ` redi at gcc dot gnu.org
2021-11-23 21:19 ` redi at gcc dot gnu.org
2022-04-21  7:50 ` rguenth at gcc dot gnu.org
2022-08-04 13:29 ` redi at gcc dot gnu.org

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).