gcc50/csu: Skip depends step to avoid possible race
[dragonfly.git] / contrib / gcc-4.4 / 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 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 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 <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 <cstddef>
44 #include <bits/functexcept.h>
45 #include <bits/gthr.h>
46
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
48
49 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
81       friend bool
82       operator==(thread::id __x, thread::id __y)
83       { return __gthread_equal(__x._M_thread, __y._M_thread); }
84
85       friend bool
86       operator<(thread::id __x, thread::id __y)
87       { return __x._M_thread < __y._M_thread; }
88
89       template<class _CharT, class _Traits>
90         friend basic_ostream<_CharT, _Traits>&
91         operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id);
92     };
93
94     // Simple base type that the templatized, derived class containing
95     // an arbitrary functor can be converted to and called.
96     struct _Impl_base
97     {
98       __shared_base_type        _M_this_ptr;
99
100       virtual ~_Impl_base() = default;
101
102       virtual void _M_run() = 0;
103     };
104
105     template<typename _Callable>
106       struct _Impl : public _Impl_base
107       {
108         _Callable               _M_func;
109
110         _Impl(_Callable&& __f) : _M_func(std::forward<_Callable>(__f))
111         { }
112
113         void
114         _M_run() { _M_func(); }
115       };
116
117   private:
118     id                          _M_id;
119
120   public:
121     thread() = default;
122     thread(const thread&) = delete;
123
124     thread(thread&& __t)
125     { swap(__t); }
126
127     template<typename _Callable>
128       explicit thread(_Callable __f)
129       { _M_start_thread(_M_make_routine<_Callable>(__f)); }
130
131     template<typename _Callable, typename... _Args>
132       thread(_Callable&& __f, _Args&&... __args)
133       { _M_start_thread(_M_make_routine(std::bind(__f, __args...))); }
134
135     ~thread()
136     {
137       if (joinable())
138         std::terminate();
139     }
140
141     thread& operator=(const thread&) = delete;
142
143     thread& operator=(thread&& __t)
144     {
145       if (joinable())
146         std::terminate();
147       swap(__t);
148       return *this;
149     }
150
151     void
152     swap(thread&& __t)
153     { std::swap(_M_id, __t._M_id); }
154
155     bool
156     joinable() const
157     { return !(_M_id == id()); }
158
159     void
160     join();
161
162     void
163     detach();
164
165     thread::id
166     get_id() const
167     { return _M_id; }
168
169     /** @pre thread is joinable
170      */
171     native_handle_type
172     native_handle()
173     { return _M_id._M_thread; }
174
175     // Returns a value that hints at the number of hardware thread contexts.
176     static unsigned int
177     hardware_concurrency()
178     { return 0; }
179
180   private:
181     void
182     _M_start_thread(__shared_base_type);
183
184     template<typename _Callable>
185       shared_ptr<_Impl<_Callable>>
186       _M_make_routine(_Callable&& __f)
187       {
188         // Create and allocate full data structure, not base.
189         return make_shared<_Impl<_Callable>>(std::forward<_Callable>(__f));
190       }
191   };
192
193   inline void
194   swap(thread& __x, thread& __y)
195   { __x.swap(__y); }
196
197   inline void
198   swap(thread&& __x, thread& __y)
199   { __x.swap(__y); }
200
201   inline void
202   swap(thread& __x, thread&& __y)
203   { __x.swap(__y); }
204
205   inline bool
206   operator!=(thread::id __x, thread::id __y)
207   { return !(__x == __y); }
208
209   inline bool
210   operator<=(thread::id __x, thread::id __y)
211   { return !(__y < __x); }
212
213   inline bool
214   operator>(thread::id __x, thread::id __y)
215   { return __y < __x; }
216
217   inline bool
218   operator>=(thread::id __x, thread::id __y)
219   { return !(__x < __y); }
220
221   template<class _CharT, class _Traits>
222     inline basic_ostream<_CharT, _Traits>&
223     operator<<(basic_ostream<_CharT, _Traits>&& __out, thread::id __id)
224     {
225       if (__id == thread::id())
226         return __out << "thread::id of a non-executing thread";
227       else
228         return __out << __id._M_thread;
229     }
230
231   /** @namespace std::this_thread
232    *  @brief ISO C++ 0x entities sub namespace for thread.
233    *  30.2.2 Namespace this_thread.
234    */
235   namespace this_thread
236   {
237     /// get_id
238     inline thread::id
239     get_id() { return thread::id(__gthread_self()); }
240
241 #ifdef _GLIBCXX_USE_SCHED_YIELD
242     /// yield
243     inline void
244     yield()
245     { __gthread_yield(); }
246 #endif
247
248 #ifdef _GLIBCXX_USE_NANOSLEEP
249     /// sleep_until
250     template<typename _Clock, typename _Duration>
251       inline void
252       sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
253       { sleep_for(__atime - _Clock::now()); }
254
255     /// sleep_for
256     template<typename _Rep, typename _Period>
257       inline void
258       sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
259       {
260         chrono::seconds __s =
261           chrono::duration_cast<chrono::seconds>(__rtime);
262
263         chrono::nanoseconds __ns =
264           chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
265
266         __gthread_time_t __ts =
267           {
268             static_cast<std::time_t>(__s.count()),
269             static_cast<long>(__ns.count())
270           };
271
272         ::nanosleep(&__ts, 0);
273       }
274 #endif
275   }
276
277   // @} group threads
278 }
279
280 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
281
282 #endif // __GXX_EXPERIMENTAL_CXX0X__
283
284 #endif // _GLIBCXX_THREAD