aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/thread
diff options
context:
space:
mode:
authorBenjamin Kosnik <bkoz@redhat.com>2009-02-07 21:56:55 +0000
committerBenjamin Kosnik <bkoz@gcc.gnu.org>2009-02-07 21:56:55 +0000
commitd7afcd2b9ba4534fce7ec4d6d31a508af312b928 (patch)
treeab7a64f83b4f9ff2ad08ff622a70bd0a6957bf87 /libstdc++-v3/include/std/thread
parent5a7e237c637967eed5edff0a889c643ced174b33 (diff)
downloadgcc-d7afcd2b9ba4534fce7ec4d6d31a508af312b928.zip
gcc-d7afcd2b9ba4534fce7ec4d6d31a508af312b928.tar.gz
gcc-d7afcd2b9ba4534fce7ec4d6d31a508af312b928.tar.bz2
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::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
Diffstat (limited to 'libstdc++-v3/include/std/thread')
-rw-r--r--libstdc++-v3/include/std/thread260
1 files changed, 124 insertions, 136 deletions
diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread
index 231d4b3..9ce5fdd 100644
--- a/libstdc++-v3/include/std/thread
+++ b/libstdc++-v3/include/std/thread
@@ -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
+ typedef __gthread_t native_handle_type;
+
+ /// thread::id
+ class id
{
+ native_handle_type _M_thread;
+
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;
+ 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;
};
-
- // types
- class id;
- typedef __gthread_t native_handle_type;
- // cons
+ 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