Commit 0a8f4feb by Jonathan Wakely

libstdc++: Fix conformance issues in <stop_token> (PR92895)

Fix synchronization issues in <stop_token>. Replace shared_ptr with
_Stop_state_ref and a reference count embedded in the shared state.
Replace std::mutex with spinlock using one bit of a std::atomic<> that
also tracks whether a stop request has been made and how many
stop_source objects share ownership of the state.

	PR libstdc++/92895
	* include/std/stop_token (stop_token::stop_possible()): Call new
	_M_stop_possible() function.
	(stop_token::stop_requested()): Do not use stop_possible().
	(stop_token::binary_semaphore): New class, as temporary stand-in for
	std::binary_semaphore.
	(stop_token::_Stop_cb::_M_callback): Add noexcept to type.
	(stop_token::_Stop_cb::_M_destroyed, stop_token::_Stop_cb::_M_done):
	New data members for symchronization with stop_callback destruction.
	(stop_token::_Stop_cb::_Stop_cb): Make non-template.
	(stop_token::_Stop_cb::_M_linked, stop_token::_Stop_cb::_S_execute):
	Remove.
	(stop_token::_Stop_cb::_M_run): New member function.
	(stop_token::_Stop_state::_M_stopped, stop_token::_Stop_state::_M_mtx):
	Remove.
	(stop_token::_Stop_state::_M_owners): New data member to track
	reference count for ownership.
	(stop_token::_Stop_state::_M_value): New data member combining a
	spinlock, the stop requested flag, and the reference count for
	associated stop_source objects.
	(stop_token::_Stop_state::_M_requester): New data member for
	synchronization with stop_callback destruction.
	(stop_token::_Stop_state::_M_stop_possible()): New member function.
	(stop_token::_Stop_state::_M_stop_requested()): Inspect relevant bit
	of _M_value.
	(stop_token::_Stop_state::_M_add_owner)
	(stop_token::_Stop_state::_M_release_ownership)
	(stop_token::_Stop_state::_M_add_ssrc)
	(stop_token::_Stop_state::_M_sub_ssrc): New member functions for
	updating reference counts.
	(stop_token::_Stop_state::_M_lock, stop_token::_Stop_state::_M_unlock)
	(stop_token::_Stop_state::_M_lock, stop_token::_Stop_state::_M_unlock)
	(stop_token::_Stop_state::_M_try_lock)
	(stop_token::_Stop_state::_M_try_lock_and_stop)
	(stop_token::_Stop_state::_M_do_try_lock): New member functions for
	managing spinlock.
	(stop_token::_Stop_state::_M_request_stop): Use atomic operations to
	read and update state. Release lock while running callbacks. Use new
	data members to synchronize with callback destruction.
	(stop_token::_Stop_state::_M_remove_callback): Likewise.
	(stop_token::_Stop_state::_M_register_callback): Use atomic operations
	to read and update state.
	(stop_token::_Stop_state_ref): Handle type to manage _Stop_state,
	replacing shared_ptr.
	(stop_source::stop_source(const stop_source&)): Update reference count.
	(stop_source::operator=(const stop_source&)): Likewise.
	(stop_source::~stop_source()): Likewise.
	(stop_source::stop_source(stop_source&&)): Define as defaulted.
	(stop_source::operator=(stop_source&&)): Establish postcondition on
	parameter.
	(stop_callback): Enforce preconditions on template parameter. Replace
	base class with data member of new _Cb_impl type.
	(stop_callback::stop_callback(const stop_token&, Cb&&))
	(stop_callback::stop_callback(stop_token&&, Cb&&)): Fix TOCTTOU race.
	(stop_callback::_Cb_impl): New type wrapping _Callback member and
	defining the _S_execute member function.
	* testsuite/30_threads/stop_token/stop_callback/deadlock-mt.cc: New
	test.
	* testsuite/30_threads/stop_token/stop_callback/deadlock.cc: New test.
	* testsuite/30_threads/stop_token/stop_callback/destroy.cc: New test.
	* testsuite/30_threads/stop_token/stop_callback/destructible_neg.cc:
	New test.
	* testsuite/30_threads/stop_token/stop_callback/invocable_neg.cc: New
	test.
	* testsuite/30_threads/stop_token/stop_callback/invoke.cc: New test.
	* testsuite/30_threads/stop_token/stop_source/assign.cc: New test.
	* testsuite/30_threads/stop_token/stop_token/stop_possible.cc: New
	test.
parent 989a5fb3
2020-01-29 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/92895
* include/std/stop_token (stop_token::stop_possible()): Call new
_M_stop_possible() function.
(stop_token::stop_requested()): Do not use stop_possible().
(stop_token::binary_semaphore): New class, as temporary stand-in for
std::binary_semaphore.
(stop_token::_Stop_cb::_M_callback): Add noexcept to type.
(stop_token::_Stop_cb::_M_destroyed, stop_token::_Stop_cb::_M_done):
New data members for symchronization with stop_callback destruction.
(stop_token::_Stop_cb::_Stop_cb): Make non-template.
(stop_token::_Stop_cb::_M_linked, stop_token::_Stop_cb::_S_execute):
Remove.
(stop_token::_Stop_cb::_M_run): New member function.
(stop_token::_Stop_state::_M_stopped, stop_token::_Stop_state::_M_mtx):
Remove.
(stop_token::_Stop_state::_M_owners): New data member to track
reference count for ownership.
(stop_token::_Stop_state::_M_value): New data member combining a
spinlock, the stop requested flag, and the reference count for
associated stop_source objects.
(stop_token::_Stop_state::_M_requester): New data member for
synchronization with stop_callback destruction.
(stop_token::_Stop_state::_M_stop_possible()): New member function.
(stop_token::_Stop_state::_M_stop_requested()): Inspect relevant bit
of _M_value.
(stop_token::_Stop_state::_M_add_owner)
(stop_token::_Stop_state::_M_release_ownership)
(stop_token::_Stop_state::_M_add_ssrc)
(stop_token::_Stop_state::_M_sub_ssrc): New member functions for
updating reference counts.
(stop_token::_Stop_state::_M_lock, stop_token::_Stop_state::_M_unlock)
(stop_token::_Stop_state::_M_lock, stop_token::_Stop_state::_M_unlock)
(stop_token::_Stop_state::_M_try_lock)
(stop_token::_Stop_state::_M_try_lock_and_stop)
(stop_token::_Stop_state::_M_do_try_lock): New member functions for
managing spinlock.
(stop_token::_Stop_state::_M_request_stop): Use atomic operations to
read and update state. Release lock while running callbacks. Use new
data members to synchronize with callback destruction.
(stop_token::_Stop_state::_M_remove_callback): Likewise.
(stop_token::_Stop_state::_M_register_callback): Use atomic operations
to read and update state.
(stop_token::_Stop_state_ref): Handle type to manage _Stop_state,
replacing shared_ptr.
(stop_source::stop_source(const stop_source&)): Update reference count.
(stop_source::operator=(const stop_source&)): Likewise.
(stop_source::~stop_source()): Likewise.
(stop_source::stop_source(stop_source&&)): Define as defaulted.
(stop_source::operator=(stop_source&&)): Establish postcondition on
parameter.
(stop_callback): Enforce preconditions on template parameter. Replace
base class with data member of new _Cb_impl type.
(stop_callback::stop_callback(const stop_token&, Cb&&))
(stop_callback::stop_callback(stop_token&&, Cb&&)): Fix TOCTTOU race.
(stop_callback::_Cb_impl): New type wrapping _Callback member and
defining the _S_execute member function.
* testsuite/30_threads/stop_token/stop_callback/deadlock-mt.cc: New
test.
* testsuite/30_threads/stop_token/stop_callback/deadlock.cc: New test.
* testsuite/30_threads/stop_token/stop_callback/destroy.cc: New test.
* testsuite/30_threads/stop_token/stop_callback/destructible_neg.cc:
New test.
* testsuite/30_threads/stop_token/stop_callback/invocable_neg.cc: New
test.
* testsuite/30_threads/stop_token/stop_callback/invoke.cc: New test.
* testsuite/30_threads/stop_token/stop_source/assign.cc: New test.
* testsuite/30_threads/stop_token/stop_token/stop_possible.cc: New
test.
* libsupc++/compare (__detail::__3way_builtin_ptr_cmp): Use
three_way_comparable_with.
(__detail::__3way_cmp_with): Remove workaround for fixed bug.
......
// Copyright (C) 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.
// 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-options "-std=gnu++2a -pthread" }
// { dg-require-effective-target c++2a }
// { dg-require-effective-target pthread }
// { dg-require-gthreads "" }
#include <stop_token>
#include <memory>
#include <testsuite_hooks.h>
void
test01()
{
std::stop_source ssrc;
std::stop_token stok = ssrc.get_token();
using F = void(*)();
std::unique_ptr<std::stop_callback<F>> pcb;
auto dereg = [&pcb] { pcb.reset(); };
std::stop_callback cb1(stok, dereg);
pcb = std::make_unique<std::stop_callback<F>>(stok, []{});
std::stop_callback cb2(stok, dereg);
// PR libstdc++/92895
// Making a stop request runs the callbacks. Whichever of cb1 and cb2
// runs first will destroy *pcb, which will try to unregister it.
// This recursive access to the shared stop state within a callback must
// work without deadlock.
ssrc.request_stop();
}
int main()
{
test01();
}
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <stop_token>
#include <memory>
#include <testsuite_hooks.h>
void
test01()
{
std::stop_source ssrc;
std::stop_token stok = ssrc.get_token();
using F = void(*)();
std::unique_ptr<std::stop_callback<F>> pcb;
auto dereg = [&pcb] { pcb.reset(); };
std::stop_callback cb1(stok, dereg);
pcb = std::make_unique<std::stop_callback<F>>(stok, []{});
std::stop_callback cb2(stok, dereg);
// PR libstdc++/92895
// Making a stop request runs the callbacks. Whichever of cb1 and cb2
// runs first will destroy *pcb, which will try to unregister it.
// This recursive access to the shared stop state within a callback must
// work without deadlock.
ssrc.request_stop();
}
int main()
{
test01();
}
// Copyright (C) 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.
// 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-options "-std=gnu++2a -pthread" }
// { dg-require-effective-target c++2a }
// { dg-require-effective-target pthread }
// { dg-require-gthreads "" }
#include <stop_token>
#include <atomic>
#include <thread>
#include <testsuite_hooks.h>
struct F
{
static std::atomic<int> stage;
F(int) { }
~F()
{
// PR libstdc++/92895
// Callback function must not be destroyed while still executing.
VERIFY( stage == 4 );
}
void operator()() const noexcept
{
stage = 2; // trigger destructor of stop_callback that owns *this
while (stage == 2)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(std::chrono::milliseconds(500));
stage = 4; // destructor checks for this
}
};
std::atomic<int> F::stage{0};
void
test01()
{
std::stop_source ssrc;
std::stop_token stok = ssrc.get_token();
std::thread t1([&ssrc] {
while (F::stage == 0)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ssrc.request_stop();
while (F::stage != 5)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
});
std::thread t2([&ssrc] {
std::stop_callback<F> cb(ssrc.get_token(), 0);
F::stage = 1; // trigger stop request in other thread, which runs callback
while (F::stage == 1)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
F::stage = 3;
// stop_callback destructor should block until callback completes
});
t2.join();
F::stage = 5; // allow first thread to exit
t1.join();
}
int main()
{
test01();
}
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <stop_token>
struct F
{
F();
void operator()() const { }
private:
~F();
};
auto
test01(std::stop_token& tok, F& f)
{
auto ok = sizeof(std::stop_callback<F&>);
auto bad = sizeof(std::stop_callback<F>); // { dg-error "here" }
return ok + bad;
}
struct G
{
G();
~G() noexcept(false);
void operator()() const { }
};
auto
test02(std::stop_token& tok, G& g)
{
auto ok = sizeof(std::stop_callback<G&>);
auto bad = sizeof(std::stop_callback<G>); // { dg-error "here" }
return ok + bad;
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <stop_token>
struct F
{
};
auto
test01(std::stop_token& tok, F& f)
{
auto bad1 = sizeof(std::stop_callback<F&>); // { dg-error "here" }
auto bad2 = sizeof(std::stop_callback<F>); // { dg-error "here" }
return bad1 + bad2;
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <stop_token>
#include <testsuite_hooks.h>
int lval[5];
int rval[5];
void
test01()
{
std::stop_source ssrc;
std::stop_token stok = ssrc.get_token();
struct F
{
void operator()() const & { ++lval[i]; }
void operator()() && { ++rval[i]; }
int i;
};
std::stop_callback<F> cb0(stok, F{0});
std::stop_callback<F> cb1(stok, F{1});
std::stop_callback<F> cb2(stok, F{2});
F f3{3};
std::stop_callback<F&> cb3(stok, f3);
std::stop_callback<const F> cb4(stok, F{4});
// PR libstdc++/92895
// Callback should be invoked with correct value category.
ssrc.request_stop();
VERIFY( lval[0] == 0 && lval[1] == 0 && lval[2] == 0 );
VERIFY( lval[3] == 1 );
VERIFY( lval[4] == 1 );
VERIFY( rval[0] == 1 );
VERIFY( rval[1] == 1 );
VERIFY( rval[2] == 1 );
VERIFY( rval[3] == 0 && rval[4] == 0 );
}
int main()
{
test01();
}
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <stop_token>
#include <testsuite_hooks.h>
void
test01()
{
std::stop_source src1, src2;
const std::stop_source orig1(src1);
VERIFY( src1 != src2 );
src1 = src2;
VERIFY( src1 == src2 );
VERIFY( src1 != orig1 );
}
void
test02()
{
std::stop_source src1, src2;
const std::stop_source orig1(src1), orig2(src2), src0(std::nostopstate);
src1 = std::move(src2);
VERIFY( src1 == orig2 );
VERIFY( src2 == src0 );
VERIFY( src1 != orig1 );
VERIFY( src0 != orig1 );
}
int main()
{
test01();
test02();
}
// Copyright (C) 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.
// 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-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }
#include <stop_token>
#include <memory>
#include <testsuite_hooks.h>
void
test01()
{
std::stop_source ssrc;
std::stop_token tok = ssrc.get_token();
VERIFY(tok.stop_possible());
ssrc.request_stop();
VERIFY(tok.stop_possible());
}
void
test02()
{
std::stop_token tok = std::stop_source().get_token();
// PR libstdc++/92895
// stop_possible() is false when there is no associated stop_source
VERIFY(!tok.stop_possible());
}
int main()
{
test01();
test02();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment