OSDN Git Service

2009-01-22 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 <exception>
45 #include <functional>
46 #include <memory>
47 #include <mutex>
48 #include <condition_variable>
49 #include <type_traits>
50 #include <cstddef>
51 #include <bits/functexcept.h>
52 #include <bits/gthr.h>
53
54 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
55
56 namespace std
57 {
58   class __thread_data_base;
59
60   typedef shared_ptr<__thread_data_base> __thread_data_ptr;
61
62   class __thread_data_base : public enable_shared_from_this<__thread_data_base>
63   {
64   public:
65     __thread_data_base() = default;
66     virtual ~__thread_data_base() = default;
67     
68     virtual void _M_run() = 0;
69     
70     __gthread_t         _M_thread_handle;
71     __thread_data_ptr   _M_this_ptr;
72     mutex               _M_data_mutex;
73   };
74   
75   template<typename _Callable>
76     class __thread_data : public __thread_data_base
77     {
78     public:
79       __thread_data(_Callable&& __f)
80       : _M_func(std::forward<_Callable>(__f))
81       { }
82
83       void _M_run()
84       { _M_func(); }
85
86     private:
87       _Callable _M_func;
88     };
89
90   /// thread
91   class thread
92   {
93   public:
94     // types
95     class id;
96     typedef __gthread_t native_handle_type;
97
98     // cons
99     thread() = default;
100     
101     template<typename _Callable>
102       explicit thread(_Callable __f)
103       : _M_thread_data(_M_make_thread_data(__f))
104       { _M_start_thread(); }
105
106     template<typename _Callable, typename... _Args>
107       thread(_Callable&& __f, _Args&&... __args)
108       : _M_thread_data(_M_make_thread_data(std::bind(__f, __args...)))
109       { _M_start_thread(); }
110
111     ~thread()
112     { detach(); }
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
135     void 
136     join();
137
138     void 
139     detach();
140
141     thread::id
142     get_id() const;
143
144     native_handle_type 
145     native_handle()
146     { return _M_thread_data->_M_thread_handle; }
147
148     // static members
149     static unsigned hardware_concurrency();
150
151     __thread_data_ptr
152     _M_get_thread_data() const
153     {
154       lock_guard<mutex> __l(_M_thread_data_mutex);
155       return _M_thread_data;
156     }
157
158   private:
159     template<typename _Callable>
160       __thread_data_ptr 
161       _M_make_thread_data(_Callable&& __f)
162       { 
163         return __thread_data_ptr(
164           new __thread_data<_Callable>(std::forward<_Callable>(__f)));
165       }
166     
167     __thread_data_ptr
168     _M_make_thread_data(void(*__f)())
169     { return __thread_data_ptr(new __thread_data<void(*)()>(__f)); }
170     
171     void _M_start_thread();
172
173     __thread_data_ptr   _M_thread_data;
174     mutable mutex       _M_thread_data_mutex;
175   };
176
177   inline void
178   swap(thread& __x, thread& __y)
179   { __x.swap(__y); }
180
181   inline void
182   swap(thread&& __x, thread& __y)
183   { __x.swap(__y); }
184   
185   inline void
186   swap(thread& __x, thread&& __y)
187   { __x.swap(__y); }
188
189   namespace this_thread
190   {
191     thread::id
192     get_id();
193
194 #ifdef _GLIBCXX_USE_SCHED_YIELD
195     inline void
196     yield()
197     { __gthread_yield(); }
198 #endif
199
200 #ifdef _GLIBCXX_USE_NANOSLEEP
201     template<typename _Clock, typename _Duration>
202       inline void
203       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
204       { sleep_for(__atime - _Clock::now()); }
205
206     template<typename _Rep, typename _Period>
207       inline void
208       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
209       {
210         chrono::seconds __s =
211           chrono::duration_cast<chrono::seconds>(__rtime);
212
213         chrono::nanoseconds __ns =
214           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
215
216         __gthread_time_t __ts = 
217           {
218             static_cast<std::time_t>(__s.count()),
219             static_cast<long>(__ns.count())
220           };
221
222         ::nanosleep(&__ts, 0);
223       }
224 #endif
225   }
226
227   /// thread::id
228   class thread::id
229   {
230   public:
231     id() : _M_thread_id() { }
232
233   private:
234     friend class thread;
235
236     friend thread::id this_thread::get_id();
237
238     friend bool 
239     operator==(thread::id __x, thread::id __y)
240     { return static_cast<bool>(__gthread_equal(__x._M_thread_id,
241                                                __y._M_thread_id)); }
242
243     friend bool
244     operator<(thread::id __x, thread::id __y)
245     { return __x._M_thread_id < __y._M_thread_id; }
246
247     template<class _CharT, class _Traits>
248       friend basic_ostream<_CharT, _Traits>&
249       operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id); 
250
251     id(__gthread_t __id)
252     : _M_thread_id(__id)
253     { }
254       
255     __gthread_t _M_thread_id;
256   };
257
258   inline bool
259   operator!=(thread::id __x, thread::id __y)
260   { return !(__x == __y); }
261   
262   inline bool
263   operator<=(thread::id __x, thread::id __y)
264   { return !(__y < __x); }
265   
266   inline bool
267   operator>(thread::id __x, thread::id __y)
268   { return __y < __x; }
269   
270   inline bool
271   operator>=(thread::id __x, thread::id __y)
272   { return !(__x < __y); }
273   
274   template<class _CharT, class _Traits>
275     inline basic_ostream<_CharT, _Traits>&
276     operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
277     {
278       if(__id == thread::id())
279         return __out << "non-executing thread";
280       else
281         return __out << __id._M_thread_id;
282     }  
283
284   inline bool
285   thread::joinable() const
286   { return get_id() != thread::id(); }
287
288   inline thread::id
289   thread::get_id() const
290   {
291     if(_M_thread_data)
292       return thread::id(_M_thread_data->_M_thread_handle);
293     else
294       return thread::id();
295   }
296
297   namespace this_thread
298   {
299     inline thread::id
300     get_id()
301     { return thread::id(__gthread_self()); }
302   }
303 }
304
305 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
306
307 #endif // __GXX_EXPERIMENTAL_CXX0X__
308
309 #endif // _GLIBCXX_THREAD