OSDN Git Service

2010-11-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / thread
1 // <thread> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/thread
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _GLIBCXX_THREAD
30 #define _GLIBCXX_THREAD 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <chrono>
39 #include <functional>
40 #include <memory>
41 #include <mutex>
42 #include <condition_variable>
43 #include <bits/functexcept.h>
44 #include <bits/functional_hash.h>
45 #include <bits/gthr.h>
46
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
48
49 _GLIBCXX_BEGIN_NAMESPACE(std)
50
51   /**
52    * @defgroup threads Threads
53    * @ingroup concurrency
54    *
55    * Classes for thread support.
56    * @{
57    */
58
59   /// thread
60   class thread
61   {
62   public:
63     typedef __gthread_t                 native_handle_type;
64     struct _Impl_base;
65     typedef shared_ptr<_Impl_base>      __shared_base_type;
66
67     /// thread::id
68     class id
69     {
70       native_handle_type        _M_thread;
71
72     public:
73       id() : _M_thread() { }
74
75       explicit
76       id(native_handle_type __id) : _M_thread(__id) { }
77
78     private:
79       friend class thread;
80       friend class hash<thread::id>;
81
82       friend bool
83       operator==(thread::id __x, thread::id __y)
84       { return __gthread_equal(__x._M_thread, __y._M_thread); }
85
86       friend bool
87       operator<(thread::id __x, thread::id __y)
88       { return __x._M_thread < __y._M_thread; }
89
90       template<class _CharT, class _Traits>
91         friend basic_ostream<_CharT, _Traits>&
92         operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id);
93     };
94
95     // Simple base type that the templatized, derived class containing
96     // an arbitrary functor can be converted to and called.
97     struct _Impl_base
98     {
99       __shared_base_type        _M_this_ptr;
100
101       inline virtual ~_Impl_base();
102
103       virtual void _M_run() = 0;
104     };
105
106     template<typename _Callable>
107       struct _Impl : public _Impl_base
108       {
109         _Callable               _M_func;
110
111         _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
112         { }
113
114         void
115         _M_run() { _M_func(); }
116       };
117
118   private:
119     id                          _M_id;
120
121   public:
122     thread() = default;
123     thread(thread&) = delete;
124     thread(const thread&) = delete;
125
126     thread(thread&& __t)
127     { swap(__t); }
128
129     template<typename _Callable, typename... _Args>
130       explicit 
131       thread(_Callable&& __f, _Args&&... __args)
132       {
133         _M_start_thread(_M_make_routine(std::bind<void>(
134                 std::forward<_Callable>(__f),
135                 std::forward<_Args>(__args)...)));
136       }
137
138     ~thread()
139     {
140       if (joinable())
141         std::terminate();
142     }
143
144     thread& operator=(const thread&) = delete;
145
146     thread& operator=(thread&& __t)
147     {
148       if (joinable())
149         std::terminate();
150       swap(__t);
151       return *this;
152     }
153
154     void
155     swap(thread& __t)
156     { std::swap(_M_id, __t._M_id); }
157
158     bool
159     joinable() const
160     { return !(_M_id == id()); }
161
162     void
163     join();
164
165     void
166     detach();
167
168     thread::id
169     get_id() const
170     { return _M_id; }
171
172     /** @pre thread is joinable
173      */
174     native_handle_type
175     native_handle()
176     { return _M_id._M_thread; }
177
178     // Returns a value that hints at the number of hardware thread contexts.
179     static unsigned int
180     hardware_concurrency()
181     { return 0; }
182
183   private:
184     void
185     _M_start_thread(__shared_base_type);
186
187     template<typename _Callable>
188       shared_ptr<_Impl<_Callable>>
189       _M_make_routine(_Callable&& __f)
190       {
191         // Create and allocate full data structure, not base.
192         return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
193       }
194   };
195
196   inline thread::_Impl_base::~_Impl_base() = default;
197
198   inline void
199   swap(thread& __x, thread& __y)
200   { __x.swap(__y); }
201
202   inline bool
203   operator!=(thread::id __x, thread::id __y)
204   { return !(__x == __y); }
205
206   inline bool
207   operator<=(thread::id __x, thread::id __y)
208   { return !(__y < __x); }
209
210   inline bool
211   operator>(thread::id __x, thread::id __y)
212   { return __y < __x; }
213
214   inline bool
215   operator>=(thread::id __x, thread::id __y)
216   { return !(__x < __y); }
217
218   // DR 889.
219   /// std::hash specialization for thread::id.
220   template<>
221     struct hash<thread::id>
222     : public __hash_base<size_t, thread::id>
223     {
224       size_t
225       operator()(const thread::id& __id) const
226       { return std::_Hash_impl::hash(__id._M_thread); }
227     };
228
229   template<class _CharT, class _Traits>
230     inline basic_ostream<_CharT, _Traits>&
231     operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
232     {
233       if (__id == thread::id())
234         return __out << "thread::id of a non-executing thread";
235       else
236         return __out << __id._M_thread;
237     }
238
239   /** @namespace std::this_thread
240    *  @brief ISO C++ 0x entities sub namespace for thread.
241    *  30.2.2 Namespace this_thread.
242    */
243   namespace this_thread
244   {
245     /// get_id
246     inline thread::id
247     get_id() { return thread::id(__gthread_self()); }
248
249 #ifdef _GLIBCXX_USE_SCHED_YIELD
250     /// yield
251     inline void
252     yield()
253     { __gthread_yield(); }
254 #endif
255
256 #ifdef _GLIBCXX_USE_NANOSLEEP
257     /// sleep_until
258     template<typename _Clock, typename _Duration>
259       inline void
260       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
261       { sleep_for(__atime - _Clock::now()); }
262
263     /// sleep_for
264     template<typename _Rep, typename _Period>
265       inline void
266       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
267       {
268         chrono::seconds __s =
269           chrono::duration_cast<chrono::seconds>(__rtime);
270
271         chrono::nanoseconds __ns =
272           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
273
274         __gthread_time_t __ts =
275           {
276             static_cast<std::time_t>(__s.count()),
277             static_cast<long>(__ns.count())
278           };
279
280         ::nanosleep(&__ts, 0);
281       }
282 #endif
283   }
284
285   // @} group threads
286 _GLIBCXX_END_NAMESPACE
287
288 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
289
290 #endif // __GXX_EXPERIMENTAL_CXX0X__
291
292 #endif // _GLIBCXX_THREAD