Commit 9db7c931 by Jonathan Wakely Committed by Jonathan Wakely

Define *_at_thread_exit() functions.

	* config/abi/pre/gnu.ver: Add new exports.
	* include/std/condition_variable (notify_all_at_thread_exit): Declare.
	(__at_thread_exit_elt): New base class.
	* include/std/future: Add comments documenting the implementation.
	(__future_base::_State_baseV2::_State_baseV2()): Use brace-or-equal
	initializers and define constructor as defaulted.
	(__future_base::_State_baseV2::_M_ready): Replace member function
	with member variable.
	(__future_base::_State_baseV2::_M_set_result): Set _M_ready.
	(__future_base::_State_baseV2::_M_set_delayed_result): Define.
	(__future_base::_State_baseV2::_M_break_promise): Set _M_ready.
	(__future_base::_State_baseV2::_Make_ready): New helper class.
	(__future_base::_Deferred_state::_M_has_deferred): Remove requirement
	for caller to own mutex.
	(__future_base::_Async_state_impl::~_Async_state_impl): Call join
	directly.
	(__future_base::_Task_state_base::_M_run): Take arguments by
	reference.
	(__future_base::_Task_state_base::_M_run_delayed): Declare new pure
	virtual function.
	(__future_base::_Task_state::_M_run_delayed): Define override.
	(promise::set_value_at_thread_exit): Define.
	(promise::set_exception_at_thread_exit): Define.
	(packaged_task::make_ready_at_thread_exit): Define.
	* src/c++11/condition_variable.cc (notify_all_at_thread_exit): Define.
	* src/c++11/future.cc
	(__future_base::_State_baseV2::_Make_ready::_M_set): Define.
	* testsuite/30_threads/condition_variable/members/3.cc: New.
	* testsuite/30_threads/packaged_task/members/at_thread_exit.cc: New.
	* testsuite/30_threads/promise/members/at_thread_exit.cc: New.

From-SVN: r218255
parent 8581fd64
2014-12-02 Jonathan Wakely <jwakely@redhat.com>
* config/abi/pre/gnu.ver: Add new exports.
* include/std/condition_variable (notify_all_at_thread_exit): Declare.
(__at_thread_exit_elt): New base class.
* include/std/future: Add comments documenting the implementation.
(__future_base::_State_baseV2::_State_baseV2()): Use brace-or-equal
initializers and define constructor as defaulted.
(__future_base::_State_baseV2::_M_ready): Replace member function
with member variable.
(__future_base::_State_baseV2::_M_set_result): Set _M_ready.
(__future_base::_State_baseV2::_M_set_delayed_result): Define.
(__future_base::_State_baseV2::_M_break_promise): Set _M_ready.
(__future_base::_State_baseV2::_Make_ready): New helper class.
(__future_base::_Deferred_state::_M_has_deferred): Remove requirement
for caller to own mutex.
(__future_base::_Async_state_impl::~_Async_state_impl): Call join
directly.
(__future_base::_Task_state_base::_M_run): Take arguments by
reference.
(__future_base::_Task_state_base::_M_run_delayed): Declare new pure
virtual function.
(__future_base::_Task_state::_M_run_delayed): Define override.
(promise::set_value_at_thread_exit): Define.
(promise::set_exception_at_thread_exit): Define.
(packaged_task::make_ready_at_thread_exit): Define.
* src/c++11/condition_variable.cc (notify_all_at_thread_exit): Define.
* src/c++11/future.cc
(__future_base::_State_baseV2::_Make_ready::_M_set): Define.
* testsuite/30_threads/condition_variable/members/3.cc: New.
* testsuite/30_threads/packaged_task/members/at_thread_exit.cc: New.
* testsuite/30_threads/promise/members/at_thread_exit.cc: New.
2014-12-01 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/63840
......
......@@ -128,7 +128,8 @@ GLIBCXX_3.4 {
std::messages*;
std::money*;
# std::n[^u]*;
std::n[^aue]*;
std::n[^aueo]*;
std::nothrow;
std::nu[^m]*;
std::num[^e]*;
std::ostrstream*;
......@@ -1500,6 +1501,11 @@ GLIBCXX_3.4.21 {
# std::_Sp_locker::*
_ZNSt10_Sp_locker[CD]*;
# std::notify_all_at_thread_exit
_ZSt25notify_all_at_thread_exitRSt18condition_variableSt11unique_lockISt5mutexE;
# std::__future_base::_State_baseV2::_Make_ready::_M_set()
_ZNSt13__future_base13_State_baseV211_Make_ready6_M_setEv;
} GLIBCXX_3.4.20;
......
......@@ -170,6 +170,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
};
void
notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
struct __at_thread_exit_elt
{
__at_thread_exit_elt* _M_next;
void (*_M_cb)(void*);
};
inline namespace _V2 {
/// condition_variable_any
......
......@@ -77,6 +77,80 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__throw_system_error(__e);
}
extern void
__at_thread_exit(__at_thread_exit_elt*);
namespace
{
__gthread_key_t key;
void run(void* p)
{
auto elt = (__at_thread_exit_elt*)p;
while (elt)
{
auto next = elt->_M_next;
elt->_M_cb(elt);
elt = next;
}
}
void run()
{
auto elt = (__at_thread_exit_elt*)__gthread_getspecific(key);
__gthread_setspecific(key, nullptr);
run(elt);
}
struct notifier final : __at_thread_exit_elt
{
notifier(condition_variable& cv, unique_lock<mutex>& l)
: cv(&cv), mx(l.release())
{
_M_cb = &notifier::run;
__at_thread_exit(this);
}
~notifier()
{
mx->unlock();
cv->notify_all();
}
condition_variable* cv;
mutex* mx;
static void run(void* p) { delete static_cast<notifier*>(p); }
};
void key_init() {
struct key_s {
key_s() { __gthread_key_create (&key, run); }
~key_s() { __gthread_key_delete (key); }
};
static key_s ks;
// Also make sure the callbacks are run by std::exit.
std::atexit (run);
}
}
void
__at_thread_exit(__at_thread_exit_elt* elt)
{
static __gthread_once_t once = __GTHREAD_ONCE_INIT;
__gthread_once (&once, key_init);
elt->_M_next = (__at_thread_exit_elt*)__gthread_getspecific(key);
__gthread_setspecific(key, elt);
}
void
notify_all_at_thread_exit(condition_variable& cv, unique_lock<mutex> l)
{
(void) new notifier{cv, l};
}
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
......
......@@ -82,6 +82,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__future_base::_Result_base::_Result_base() = default;
__future_base::_Result_base::~_Result_base() = default;
void
__future_base::_State_baseV2::_Make_ready::_S_run(void* p)
{
unique_ptr<_Make_ready> mr{static_cast<_Make_ready*>(p)};
if (auto state = mr->_M_shared_state.lock())
{
{
lock_guard<mutex> __lock{state->_M_mutex};
state->_M_ready = true;
}
state->_M_cond.notify_all();
}
}
// defined in src/c++11/condition_variable.cc
extern void
__at_thread_exit(__at_thread_exit_elt* elt);
void
__future_base::_State_baseV2::_Make_ready::_M_set()
{
_M_cb = &_Make_ready::_S_run;
__at_thread_exit(this);
}
#endif
_GLIBCXX_END_NAMESPACE_VERSION
......
// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// Copyright (C) 2014 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/>.
#include <condition_variable>
#include <thread>
#include <mutex>
std::mutex mx;
std::condition_variable cv;
int counter = 0;
struct Inc
{
Inc() { ++counter; }
~Inc() { ++counter; }
};
void func()
{
std::unique_lock<std::mutex> lock{mx};
std::notify_all_at_thread_exit(cv, std::move(lock));
static thread_local Inc inc;
}
int main()
{
bool test __attribute__((unused)) = true;
std::unique_lock<std::mutex> lock{mx};
std::thread t{func};
cv.wait(lock, [&]{ return counter == 2; });
t.join();
}
// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" }
// Copyright (C) 2014 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/>.
#include <future>
#include <testsuite_hooks.h>
bool executed = false;
int execute(int i) { executed = true; return i + 1; }
std::future<int> f1;
bool ready(std::future<int>& f)
{
return f.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready;
}
void test01()
{
bool test __attribute__((unused)) = true;
std::packaged_task<int(int)> p1(execute);
f1 = p1.get_future();
p1.make_ready_at_thread_exit(1);
VERIFY( executed );
VERIFY( p1.valid() );
VERIFY( !ready(f1) );
}
int main()
{
std::thread t{test01};
t.join();
VERIFY( ready(f1) );
VERIFY( f1.get() == 2 );
}
// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
// { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" }
// Copyright (C) 2014 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/>.
#include <future>
#include <testsuite_hooks.h>
int copies;
int copies_cmp;
struct Obj
{
Obj() = default;
Obj(const Obj&) { ++copies; }
};
std::future<Obj> f1;
bool ready(std::future<Obj>& f)
{
return f.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready;
}
void test01()
{
bool test __attribute__((unused)) = true;
std::promise<Obj> p1;
f1 = p1.get_future();
p1.set_value_at_thread_exit( {} );
copies_cmp = copies;
VERIFY( !ready(f1) );
}
int main()
{
std::thread t{test01};
t.join();
VERIFY( ready(f1) );
VERIFY( copies == copies_cmp );
}
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