OSDN Git Service

2009-01-29 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / thread
1 // <thread> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file thread
31  *  This is a Standard C++ Library header.
32  */
33
34 #ifndef _GLIBCXX_THREAD
35 #define _GLIBCXX_THREAD 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <chrono>
44 #include <functional>
45 #include <memory>
46 #include <mutex>
47 #include <condition_variable>
48 #include <cstddef>
49 #include <bits/functexcept.h>
50 #include <bits/gthr.h>
51
52 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
53
54 namespace std
55 {
56   class __thread_data_base;
57
58   typedef shared_ptr<__thread_data_base> __thread_data_ptr;
59
60   class __thread_data_base
61   {
62   public:
63     __thread_data_base() = default;
64     virtual ~__thread_data_base() = default;
65     
66     virtual void _M_run() = 0;
67     
68     __gthread_t         _M_thread_handle;
69     __thread_data_ptr   _M_this_ptr;
70   };
71   
72   template<typename _Callable>
73     class __thread_data : public __thread_data_base
74     {
75     public:
76       __thread_data(_Callable&& __f)
77       : _M_func(std::forward<_Callable>(__f))
78       { }
79
80       void _M_run()
81       { _M_func(); }
82
83     private:
84       _Callable _M_func;
85     };
86
87   /// thread
88   class thread
89   {
90   public:
91     // types
92     class id;
93     typedef __gthread_t native_handle_type;
94
95     // cons
96     thread() = default;
97     
98     template<typename _Callable>
99       explicit thread(_Callable __f)
100       : _M_thread_data(_M_make_thread_data(__f))
101       { _M_start_thread(); }
102
103     template<typename _Callable, typename... _Args>
104       thread(_Callable&& __f, _Args&&... __args)
105       : _M_thread_data(_M_make_thread_data(std::bind(__f, __args...)))
106       { _M_start_thread(); }
107
108     ~thread()
109     {
110       if (joinable())
111         detach();
112     }
113
114     thread(const thread&) = delete;
115     thread(thread&& __t)
116     { swap(__t); }
117
118     thread& operator=(const thread&) = delete;
119     thread& operator=(thread&& __t)
120     {
121       if (joinable())
122         detach();
123       swap(__t);
124       return *this;
125     }
126
127     // members
128     void 
129     swap(thread&& __t)
130     { std::swap(_M_thread_data, __t._M_thread_data); }
131
132     bool 
133     joinable() const
134     { return _M_thread_data; }
135
136     void 
137     join();
138
139     void 
140     detach();
141
142     thread::id
143     get_id() const;
144
145     /** @pre thread is joinable
146      */
147     native_handle_type 
148     native_handle()
149     { return _M_thread_data->_M_thread_handle; }
150
151     // static members
152     static unsigned hardware_concurrency();
153
154   private:
155     template<typename _Callable>
156       __thread_data_ptr 
157       _M_make_thread_data(_Callable&& __f)
158       { 
159         return make_shared<__thread_data<_Callable>>(
160             std::forward<_Callable>(__f));
161       }
162     
163     void _M_start_thread();
164
165     __thread_data_ptr   _M_thread_data;
166   };
167
168   inline void
169   swap(thread& __x, thread& __y)
170   { __x.swap(__y); }
171
172   inline void
173   swap(thread&& __x, thread& __y)
174   { __x.swap(__y); }
175   
176   inline void
177   swap(thread& __x, thread&& __y)
178   { __x.swap(__y); }
179
180   namespace this_thread
181   {
182     thread::id
183     get_id();
184
185 #ifdef _GLIBCXX_USE_SCHED_YIELD
186     inline void
187     yield()
188     { __gthread_yield(); }
189 #endif
190
191 #ifdef _GLIBCXX_USE_NANOSLEEP
192     template<typename _Clock, typename _Duration>
193       inline void
194       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
195       { sleep_for(__atime - _Clock::now()); }
196
197     template<typename _Rep, typename _Period>
198       inline void
199       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
200       {
201         chrono::seconds __s =
202           chrono::duration_cast<chrono::seconds>(__rtime);
203
204         chrono::nanoseconds __ns =
205           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
206
207         __gthread_time_t __ts = 
208           {
209             static_cast<std::time_t>(__s.count()),
210             static_cast<long>(__ns.count())
211           };
212
213         ::nanosleep(&__ts, 0);
214       }
215 #endif
216   }
217
218   /// thread::id
219   class thread::id
220   {
221   public:
222     id() : _M_thread_id() { }
223
224   private:
225     friend class thread;
226
227     friend thread::id this_thread::get_id();
228
229     friend bool 
230     operator==(thread::id __x, thread::id __y)
231     { return __gthread_equal(__x._M_thread_id, __y._M_thread_id); }
232
233     friend bool
234     operator<(thread::id __x, thread::id __y)
235     { return __x._M_thread_id < __y._M_thread_id; }
236
237     template<class _CharT, class _Traits>
238       friend basic_ostream<_CharT, _Traits>&
239       operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id); 
240
241     explicit 
242     id(__gthread_t __id)
243     : _M_thread_id(__id)
244     { }
245       
246     __gthread_t _M_thread_id;
247   };
248
249   inline bool
250   operator!=(thread::id __x, thread::id __y)
251   { return !(__x == __y); }
252   
253   inline bool
254   operator<=(thread::id __x, thread::id __y)
255   { return !(__y < __x); }
256   
257   inline bool
258   operator>(thread::id __x, thread::id __y)
259   { return __y < __x; }
260   
261   inline bool
262   operator>=(thread::id __x, thread::id __y)
263   { return !(__x < __y); }
264   
265   template<class _CharT, class _Traits>
266     inline basic_ostream<_CharT, _Traits>&
267     operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
268     {
269       if(__id == thread::id())
270         return __out << "non-executing thread";
271       else
272         return __out << __id._M_thread_id;
273     }  
274
275   inline thread::id
276   thread::get_id() const
277   {
278     if(_M_thread_data)
279       return thread::id(_M_thread_data->_M_thread_handle);
280     else
281       return thread::id();
282   }
283
284   namespace this_thread
285   {
286     inline thread::id
287     get_id()
288     { return thread::id(__gthread_self()); }
289   }
290 }
291
292 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
293
294 #endif // __GXX_EXPERIMENTAL_CXX0X__
295
296 #endif // _GLIBCXX_THREAD