thread (thread::id): Move definition inside thread.

2009-02-06  Benjamin Kosnik  <bkoz@redhat.com>

	* include/std/thread (thread::id): Move definition inside thread.
	Use native_handle_type. Remove this_thread::get_id friend.
	Change __thread_data_ptr to __shared_base_ptr.
	(thread:🆔:id(native_handle_type): Make public. Still explicit.
	Use native_handle_type. Change _M_thread_id to _M_thread.
	(thread::__thread_data_base): Rename to _Impl_base. Use id, change
	_M_thread_handle to _M_id.
	(thread::__thread_data): Rename to _Impl.
	Fixup for renames.
	(thread::_M_make_thread_data): Return derived type.
	(thread::hardware_concurrency): Add definition for default case.
	(thread::get_id): Now can define inline.
	(thread): Change _M_thread_data to _M_data.
	(this_thread::get_id): Now can define inline.
	* src/thread.cc (__thread_proxy): Rename to
	execute_native_thread_routine.
	Fixup for other renames.
	* testsuite/30_threads/thread/cons/assign_neg.cc: New.
	* testsuite/30_threads/thread/cons/copy_neg.cc: New.
	* testsuite/30_threads/thread/algorithm: Move to..
	* testsuite/30_threads/thread/swap: ...this.
	* testsuite/30_threads/thread/member/hardware_concurrency.cc: Add.
	* testsuite/30_threads/thread/id/operators.cc: New.

From-SVN: r144007
This commit is contained in:
Benjamin Kosnik 2009-02-07 21:56:55 +00:00 committed by Benjamin Kosnik
parent 5a7e237c63
commit d7afcd2b9b
9 changed files with 330 additions and 171 deletions

View File

@ -1,3 +1,29 @@
2009-02-06 Benjamin Kosnik <bkoz@redhat.com>
* include/std/thread (thread::id): Move definition inside thread.
Use native_handle_type. Remove this_thread::get_id friend.
Change __thread_data_ptr to __shared_base_ptr.
(thread::id::id(native_handle_type): Make public. Still explicit.
Use native_handle_type. Change _M_thread_id to _M_thread.
(thread::__thread_data_base): Rename to _Impl_base. Use id, change
_M_thread_handle to _M_id.
(thread::__thread_data): Rename to _Impl.
Fixup for renames.
(thread::_M_make_thread_data): Return derived type.
(thread::hardware_concurrency): Add definition for default case.
(thread::get_id): Now can define inline.
(thread): Change _M_thread_data to _M_data.
(this_thread::get_id): Now can define inline.
* src/thread.cc (__thread_proxy): Rename to
execute_native_thread_routine.
Fixup for other renames.
* testsuite/30_threads/thread/cons/assign_neg.cc: New.
* testsuite/30_threads/thread/cons/copy_neg.cc: New.
* testsuite/30_threads/thread/algorithm: Move to..
* testsuite/30_threads/thread/swap: ...this.
* testsuite/30_threads/thread/member/hardware_concurrency.cc: Add.
* testsuite/30_threads/thread/id/operators.cc: New.
2009-02-05 Chris Fairles <cfairles@gcc.gnu.org>
* include/std/tuple (_Head_base<>::_Head_base(_UHead&&)): Formatting.
@ -7,13 +33,13 @@
2009-02-05 Chris Fairles <cfairles@gcc.gnu.org>
Benjamin Kosnik <bkoz@redhat.com>
* include/std/thread (__thread_data_base): Nest class in std::thread.
(__thread_data): Likewise.
(__thread_data_ptr): Nest typedef in std::thread.
* src/thread.cc (__thread_proxy): Qualify the above names.
* config/abi/pre/gnu.ver: Remove unused exports.
2009-02-04 Benjamin Kosnik <bkoz@redhat.com>
* include/bits/unique_ptr.h: Remove private __this_type typedef.

View File

@ -53,115 +53,149 @@
namespace std
{
/// thread
class thread
{
public:
class __thread_data_base;
typedef shared_ptr<__thread_data_base> __thread_data_ptr;
class __thread_data_base
{
public:
__thread_data_base() = default;
virtual ~__thread_data_base() = default;
virtual void _M_run() = 0;
__gthread_t _M_thread_handle;
__thread_data_ptr _M_this_ptr;
};
// types
class id;
typedef __gthread_t native_handle_type;
typedef __gthread_t native_handle_type;
// cons
/// thread::id
class id
{
native_handle_type _M_thread;
public:
id() : _M_thread() { }
explicit
id(native_handle_type __id) : _M_thread(__id) { }
private:
friend class thread;
friend bool
operator==(thread::id __x, thread::id __y)
{ return __gthread_equal(__x._M_thread, __y._M_thread); }
friend bool
operator<(thread::id __x, thread::id __y)
{ return __x._M_thread < __y._M_thread; }
template<class _CharT, class _Traits>
friend basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
};
struct _Impl_base;
typedef shared_ptr<_Impl_base> __shared_base_type;
struct _Impl_base
{
id _M_id;
__shared_base_type _M_this_ptr;
_Impl_base() = default;
virtual ~_Impl_base() = default;
virtual void
_M_run() = 0;
};
template<typename _Callable>
class _Impl : public _Impl_base
{
_Callable _M_func;
public:
_Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
{ }
void
_M_run() { _M_func(); }
};
private:
// NB: Store the base type here.
__shared_base_type _M_data;
public:
thread() = default;
thread(const thread&) = delete;
thread(thread&& __t)
{ swap(__t); }
template<typename _Callable>
explicit thread(_Callable __f)
: _M_thread_data(_M_make_thread_data(__f))
: _M_data(_M_make_shared_data(__f))
{ _M_start_thread(); }
template<typename _Callable, typename... _Args>
thread(_Callable&& __f, _Args&&... __args)
: _M_thread_data(_M_make_thread_data(std::bind(__f, __args...)))
: _M_data(_M_make_shared_data(std::bind(__f, __args...)))
{ _M_start_thread(); }
~thread()
{
if (joinable())
detach();
detach();
}
thread(const thread&) = delete;
thread(thread&& __t)
{ swap(__t); }
thread& operator=(const thread&) = delete;
thread& operator=(thread&& __t)
{
if (joinable())
detach();
detach();
swap(__t);
return *this;
}
// members
void
void
swap(thread&& __t)
{ std::swap(_M_thread_data, __t._M_thread_data); }
{ std::swap(_M_data, __t._M_data); }
bool
bool
joinable() const
{ return _M_thread_data; }
{ return _M_data; }
void
void
join();
void
void
detach();
thread::id
get_id() const;
get_id() const
{
if (_M_data)
return thread::id(_M_data->_M_id._M_thread);
else
return thread::id();
}
/** @pre thread is joinable
*/
native_handle_type
native_handle_type
native_handle()
{ return _M_thread_data->_M_thread_handle; }
{ return _M_data->_M_id._M_thread; }
// static members
static unsigned hardware_concurrency();
// Returns a value that hints at the number of hardware thread contexts.
static unsigned int
hardware_concurrency()
{ return 0; }
private:
template<typename _Callable>
class __thread_data : public __thread_data_base
shared_ptr<_Impl<_Callable>>
_M_make_shared_data(_Callable&& __f)
{
public:
__thread_data(_Callable&& __f)
: _M_func(std::forward<_Callable>(__f))
{ }
void _M_run()
{ _M_func(); }
private:
_Callable _M_func;
};
template<typename _Callable>
__thread_data_ptr
_M_make_thread_data(_Callable&& __f)
{
return make_shared<__thread_data<_Callable>>(
std::forward<_Callable>(__f));
// Create and allocate full data structure, not base.
return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
}
void _M_start_thread();
__thread_data_ptr _M_thread_data;
void _M_start_thread();
};
inline void
@ -171,15 +205,42 @@ namespace std
inline void
swap(thread&& __x, thread& __y)
{ __x.swap(__y); }
inline void
swap(thread& __x, thread&& __y)
{ __x.swap(__y); }
inline bool
operator!=(thread::id __x, thread::id __y)
{ return !(__x == __y); }
inline bool
operator<=(thread::id __x, thread::id __y)
{ return !(__y < __x); }
inline bool
operator>(thread::id __x, thread::id __y)
{ return __y < __x; }
inline bool
operator>=(thread::id __x, thread::id __y)
{ return !(__x < __y); }
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
{
if (__id == thread::id())
return __out << "thread::id of a non-executing thread";
else
return __out << __id._M_thread;
}
// 30.2.2 Namespace this_thread.
namespace this_thread
{
thread::id
get_id();
get_id() { return thread::id(__gthread_self()); }
#ifdef _GLIBCXX_USE_SCHED_YIELD
inline void
@ -203,7 +264,7 @@ namespace std
chrono::nanoseconds __ns =
chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
__gthread_time_t __ts =
__gthread_time_t __ts =
{
static_cast<std::time_t>(__s.count()),
static_cast<long>(__ns.count())
@ -213,79 +274,6 @@ namespace std
}
#endif
}
/// thread::id
class thread::id
{
public:
id() : _M_thread_id() { }
private:
friend class thread;
friend thread::id this_thread::get_id();
friend bool
operator==(thread::id __x, thread::id __y)
{ return __gthread_equal(__x._M_thread_id, __y._M_thread_id); }
friend bool
operator<(thread::id __x, thread::id __y)
{ return __x._M_thread_id < __y._M_thread_id; }
template<class _CharT, class _Traits>
friend basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
explicit
id(__gthread_t __id)
: _M_thread_id(__id)
{ }
__gthread_t _M_thread_id;
};
inline bool
operator!=(thread::id __x, thread::id __y)
{ return !(__x == __y); }
inline bool
operator<=(thread::id __x, thread::id __y)
{ return !(__y < __x); }
inline bool
operator>(thread::id __x, thread::id __y)
{ return __y < __x; }
inline bool
operator>=(thread::id __x, thread::id __y)
{ return !(__x < __y); }
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
{
if(__id == thread::id())
return __out << "non-executing thread";
else
return __out << __id._M_thread_id;
}
inline thread::id
thread::get_id() const
{
if(_M_thread_data)
return thread::id(_M_thread_data->_M_thread_handle);
else
return thread::id();
}
namespace this_thread
{
inline thread::id
get_id()
{ return thread::id(__gthread_self()); }
}
}
#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1

View File

@ -34,28 +34,25 @@
namespace std
{
namespace
namespace
{
extern "C"
extern "C" void*
execute_native_thread_routine(void* __p)
{
void* __thread_proxy(void* __p)
{
thread::__thread_data_base* __t =
static_cast<thread::__thread_data_base*>(__p);
thread::__thread_data_ptr __local_thread_data;
__local_thread_data.swap(__t->_M_this_ptr);
thread::_Impl_base* __t = static_cast<thread::_Impl_base*>(__p);
thread::__shared_base_type __local;
__local.swap(__t->_M_this_ptr);
__try
{
__local_thread_data->_M_run();
}
__catch(...)
{
std::terminate();
}
__try
{
__local->_M_run();
}
__catch(...)
{
std::terminate();
}
return 0;
}
return 0;
}
}
@ -64,41 +61,42 @@ namespace std
{
int __e = EINVAL;
if (_M_thread_data)
if (_M_data)
{
void* __r = 0;
__e = __gthread_join(_M_thread_data->_M_thread_handle, &__r);
__e = __gthread_join(_M_data->_M_id._M_thread, &__r);
}
if (__e)
__throw_system_error(__e);
_M_thread_data.reset();
_M_data.reset();
}
void
thread::detach()
{
{
int __e = EINVAL;
if (_M_thread_data)
__e = __gthread_detach(_M_thread_data->_M_thread_handle);
if (_M_data)
__e = __gthread_detach(_M_data->_M_id._M_thread);
if (__e)
__throw_system_error(__e);
_M_thread_data.reset();
_M_data.reset();
}
void
void
thread::_M_start_thread()
{
_M_thread_data->_M_this_ptr = _M_thread_data;
int __e = __gthread_create(&_M_thread_data->_M_thread_handle,
&__thread_proxy, _M_thread_data.get());
// _M_data->_M_this_ptr = _M_data;
_M_data->_M_this_ptr = _M_data;
int __e = __gthread_create(&_M_data->_M_id._M_thread,
&execute_native_thread_routine, _M_data.get());
if (__e)
{
_M_thread_data->_M_this_ptr.reset();
_M_data->_M_this_ptr.reset();
__throw_system_error(__e);
}
}

View File

@ -0,0 +1,36 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// Copyright (C) 2009 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
#include <thread>
void test01()
{
// assign
typedef std::thread test_type;
test_type t1;
test_type t2;
t1 = t2;
}
// { dg-error "used here" "" { target *-*-* } 32 }
// { dg-error "deleted function" "" { target *-*-* } 145 }

View File

@ -0,0 +1,36 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// Copyright (C) 2009 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
#include <thread>
void test01()
{
// copy
typedef std::thread test_type;
test_type t1;
test_type t2(t1);
}
// { dg-error "here" "" { target *-*-* } 31 }
// { dg-error "deleted function" "" { target *-*-* } 124 }
// { dg-excess-errors "In file included from" }

View File

@ -0,0 +1,38 @@
// { dg-do compile }
// { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// Copyright (C) 2009 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
#include <thread>
void test01()
{
// thread::id operators
std::thread::id id1;
std::thread::id id2;
id1 == id2;
id1 != id2;
id1 < id2;
id1 > id2;
id1 >= id2;
id1 <= id2;
}

View File

@ -0,0 +1,37 @@
// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// Copyright (C) 2009 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
#include <thread>
#include <testsuite_hooks.h>
int main()
{
bool test __attribute__((unused)) = true;
// Current implementation punts on this.
VERIFY( std::thread::hardware_concurrency() == 0 );
return 0;
}