OSDN Git Service

2009-02-05 Chris Fairles <cfairles@gcc.gnu.org>
[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
57   {
58   public:
59     class __thread_data_base;
60     
61     typedef shared_ptr<__thread_data_base> __thread_data_ptr;
62     
63     class __thread_data_base
64     {
65     public:
66       __thread_data_base() = default;
67       virtual ~__thread_data_base() = default;
68       
69       virtual void _M_run() = 0;
70       
71       __gthread_t       _M_thread_handle;
72       __thread_data_ptr         _M_this_ptr;
73     };
74     
75     // types
76     class id;
77     typedef __gthread_t native_handle_type;
78
79     // cons
80     thread() = default;
81     
82     template<typename _Callable>
83       explicit thread(_Callable __f)
84       : _M_thread_data(_M_make_thread_data(__f))
85       { _M_start_thread(); }
86
87     template<typename _Callable, typename... _Args>
88       thread(_Callable&& __f, _Args&&... __args)
89       : _M_thread_data(_M_make_thread_data(std::bind(__f, __args...)))
90       { _M_start_thread(); }
91
92     ~thread()
93     {
94       if (joinable())
95         detach();
96     }
97
98     thread(const thread&) = delete;
99     thread(thread&& __t)
100     { swap(__t); }
101
102     thread& operator=(const thread&) = delete;
103     thread& operator=(thread&& __t)
104     {
105       if (joinable())
106         detach();
107       swap(__t);
108       return *this;
109     }
110
111     // members
112     void 
113     swap(thread&& __t)
114     { std::swap(_M_thread_data, __t._M_thread_data); }
115
116     bool 
117     joinable() const
118     { return _M_thread_data; }
119
120     void 
121     join();
122
123     void 
124     detach();
125
126     thread::id
127     get_id() const;
128
129     /** @pre thread is joinable
130      */
131     native_handle_type 
132     native_handle()
133     { return _M_thread_data->_M_thread_handle; }
134
135     // static members
136     static unsigned hardware_concurrency();
137
138   private:
139     template<typename _Callable>
140       class __thread_data : public __thread_data_base
141       {
142       public:
143         __thread_data(_Callable&& __f)
144           : _M_func(std::forward<_Callable>(__f))
145         { }
146         
147         void _M_run()
148         { _M_func(); }
149         
150       private:
151         _Callable _M_func;
152       };
153
154     template<typename _Callable>
155       __thread_data_ptr 
156       _M_make_thread_data(_Callable&& __f)
157       { 
158         return make_shared<__thread_data<_Callable>>(
159             std::forward<_Callable>(__f));
160       }
161     
162     void _M_start_thread();
163
164     __thread_data_ptr   _M_thread_data;
165   };
166
167   inline void
168   swap(thread& __x, thread& __y)
169   { __x.swap(__y); }
170
171   inline void
172   swap(thread&& __x, thread& __y)
173   { __x.swap(__y); }
174   
175   inline void
176   swap(thread& __x, thread&& __y)
177   { __x.swap(__y); }
178
179   namespace this_thread
180   {
181     thread::id
182     get_id();
183
184 #ifdef _GLIBCXX_USE_SCHED_YIELD
185     inline void
186     yield()
187     { __gthread_yield(); }
188 #endif
189
190 #ifdef _GLIBCXX_USE_NANOSLEEP
191     template<typename _Clock, typename _Duration>
192       inline void
193       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
194       { sleep_for(__atime - _Clock::now()); }
195
196     template<typename _Rep, typename _Period>
197       inline void
198       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
199       {
200         chrono::seconds __s =
201           chrono::duration_cast<chrono::seconds>(__rtime);
202
203         chrono::nanoseconds __ns =
204           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
205
206         __gthread_time_t __ts = 
207           {
208             static_cast<std::time_t>(__s.count()),
209             static_cast<long>(__ns.count())
210           };
211
212         ::nanosleep(&__ts, 0);
213       }
214 #endif
215   }
216
217   /// thread::id
218   class thread::id
219   {
220   public:
221     id() : _M_thread_id() { }
222
223   private:
224     friend class thread;
225
226     friend thread::id this_thread::get_id();
227
228     friend bool 
229     operator==(thread::id __x, thread::id __y)
230     { return __gthread_equal(__x._M_thread_id, __y._M_thread_id); }
231
232     friend bool
233     operator<(thread::id __x, thread::id __y)
234     { return __x._M_thread_id < __y._M_thread_id; }
235
236     template<class _CharT, class _Traits>
237       friend basic_ostream<_CharT, _Traits>&
238       operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id); 
239
240     explicit 
241     id(__gthread_t __id)
242     : _M_thread_id(__id)
243     { }
244       
245     __gthread_t _M_thread_id;
246   };
247
248   inline bool
249   operator!=(thread::id __x, thread::id __y)
250   { return !(__x == __y); }
251   
252   inline bool
253   operator<=(thread::id __x, thread::id __y)
254   { return !(__y < __x); }
255   
256   inline bool
257   operator>(thread::id __x, thread::id __y)
258   { return __y < __x; }
259   
260   inline bool
261   operator>=(thread::id __x, thread::id __y)
262   { return !(__x < __y); }
263   
264   template<class _CharT, class _Traits>
265     inline basic_ostream<_CharT, _Traits>&
266     operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
267     {
268       if(__id == thread::id())
269         return __out << "non-executing thread";
270       else
271         return __out << __id._M_thread_id;
272     }  
273
274   inline thread::id
275   thread::get_id() const
276   {
277     if(_M_thread_data)
278       return thread::id(_M_thread_data->_M_thread_handle);
279     else
280       return thread::id();
281   }
282
283   namespace this_thread
284   {
285     inline thread::id
286     get_id()
287     { return thread::id(__gthread_self()); }
288   }
289 }
290
291 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
292
293 #endif // __GXX_EXPERIMENTAL_CXX0X__
294
295 #endif // _GLIBCXX_THREAD