OSDN Git Service

2009-02-13 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   /// thread
57   class thread
58   {
59   public:
60     typedef __gthread_t                 native_handle_type;
61     struct _Impl_base;
62     typedef shared_ptr<_Impl_base>      __shared_base_type;
63
64     /// thread::id
65     class id
66     {
67       native_handle_type        _M_thread;
68
69     public:
70       id() : _M_thread() { }
71
72       explicit
73       id(native_handle_type __id) : _M_thread(__id) { }
74
75     private:
76       friend class thread;
77
78       friend bool
79       operator==(thread::id __x, thread::id __y)
80       { return __gthread_equal(__x._M_thread, __y._M_thread); }
81
82       friend bool
83       operator<(thread::id __x, thread::id __y)
84       { return __x._M_thread < __y._M_thread; }
85
86       template<class _CharT, class _Traits>
87         friend basic_ostream<_CharT, _Traits>&
88         operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
89     };
90
91     // Simple base type that the templatized, derived class containing
92     // an abitrary functor can be converted to and called.
93     struct _Impl_base
94     {
95       __shared_base_type        _M_this_ptr;
96
97       virtual ~_Impl_base() = default;
98
99       virtual void _M_run() = 0;
100     };
101
102     template<typename _Callable>
103       struct _Impl : public _Impl_base
104       {
105         _Callable               _M_func;
106
107         _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
108         { }
109
110         void
111         _M_run() { _M_func(); }
112       };
113
114   private:
115     id                          _M_id;
116
117   public:
118     thread() = default;
119     thread(const thread&) = delete;
120
121     thread(thread&& __t)
122     { swap(__t); }
123
124     template<typename _Callable>
125       explicit thread(_Callable __f)
126       { _M_start_thread(_M_make_routine<_Callable>(__f)); }
127
128     template<typename _Callable, typename... _Args>
129       thread(_Callable&& __f, _Args&&... __args)
130       { _M_start_thread(_M_make_routine(std::bind(__f, __args...))); }
131
132     ~thread()
133     {
134       if (joinable())
135         detach();
136     }
137
138     thread& operator=(const thread&) = delete;
139
140     thread& operator=(thread&& __t)
141     {
142       if (joinable())
143         detach();
144       swap(__t);
145       return *this;
146     }
147
148     void
149     swap(thread&& __t)
150     { std::swap(_M_id, __t._M_id); }
151
152     bool
153     joinable() const
154     { return !(_M_id == id()); }
155
156     void
157     join();
158
159     void
160     detach();
161
162     thread::id
163     get_id() const
164     { return _M_id; }
165
166     /** @pre thread is joinable
167      */
168     native_handle_type
169     native_handle()
170     { return _M_id._M_thread; }
171
172     // Returns a value that hints at the number of hardware thread contexts.
173     static unsigned int
174     hardware_concurrency()
175     { return 0; }
176
177   private:
178     void
179     _M_start_thread(__shared_base_type);
180
181     template<typename _Callable>
182       shared_ptr<_Impl<_Callable>>
183       _M_make_routine(_Callable&& __f)
184       {
185         // Create and allocate full data structure, not base.
186         return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
187       }
188   };
189
190   inline void
191   swap(thread& __x, thread& __y)
192   { __x.swap(__y); }
193
194   inline void
195   swap(thread&& __x, thread& __y)
196   { __x.swap(__y); }
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   template<class _CharT, class _Traits>
219     inline basic_ostream<_CharT, _Traits>&
220     operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
221     {
222       if (__id == thread::id())
223         return __out << "thread::id of a non-executing thread";
224       else
225         return __out << __id._M_thread;
226     }
227
228   // 30.2.2 Namespace this_thread.
229   namespace this_thread
230   {
231     /// get_id
232     inline thread::id
233     get_id() { return thread::id(__gthread_self()); }
234
235 #ifdef _GLIBCXX_USE_SCHED_YIELD
236     /// yield
237     inline void
238     yield()
239     { __gthread_yield(); }
240 #endif
241
242 #ifdef _GLIBCXX_USE_NANOSLEEP
243     /// sleep_until
244     template<typename _Clock, typename _Duration>
245       inline void
246       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
247       { sleep_for(__atime - _Clock::now()); }
248
249     /// sleep_for
250     template<typename _Rep, typename _Period>
251       inline void
252       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
253       {
254         chrono::seconds __s =
255           chrono::duration_cast<chrono::seconds>(__rtime);
256
257         chrono::nanoseconds __ns =
258           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
259
260         __gthread_time_t __ts =
261           {
262             static_cast<std::time_t>(__s.count()),
263             static_cast<long>(__ns.count())
264           };
265
266         ::nanosleep(&__ts, 0);
267       }
268 #endif
269   }
270 }
271
272 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
273
274 #endif // __GXX_EXPERIMENTAL_CXX0X__
275
276 #endif // _GLIBCXX_THREAD