OSDN Git Service

e3797e66dca4a401eb305a13adf146e0d5b0dcae
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / thread.cc
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 along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // 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 #include <thread>
31 #include <cerrno>
32
33 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
34
35 namespace std
36 {
37   namespace 
38   {
39     extern "C"
40     {
41       void* __thread_proxy(void* __p)
42       {
43         thread::__thread_data_base* __t = 
44           static_cast<thread::__thread_data_base*>(__p);
45         thread::__thread_data_ptr __local_thread_data;
46         __local_thread_data.swap(__t->_M_this_ptr);
47
48         __try
49           {
50             __local_thread_data->_M_run();
51           }
52         __catch(...)
53           {
54             std::terminate();
55           }
56
57         return 0;
58       }
59     }
60   }
61
62   void
63   thread::join()
64   {
65     int __e = EINVAL;
66
67     if (_M_thread_data)
68     {
69       void* __r = 0;
70       __e = __gthread_join(_M_thread_data->_M_thread_handle, &__r);
71     }
72
73     if (__e)
74       __throw_system_error(__e);
75
76     _M_thread_data.reset();
77   }
78
79   void
80   thread::detach()
81   {    
82     int __e = EINVAL;
83
84     if (_M_thread_data)
85       __e = __gthread_detach(_M_thread_data->_M_thread_handle);
86
87     if (__e)
88       __throw_system_error(__e);
89
90     _M_thread_data.reset();
91   }
92
93   void 
94   thread::_M_start_thread()
95   {
96     _M_thread_data->_M_this_ptr = _M_thread_data;
97     int __e = __gthread_create(&_M_thread_data->_M_thread_handle, 
98                                &__thread_proxy, _M_thread_data.get());
99     if (__e)
100     {
101       _M_thread_data->_M_this_ptr.reset();
102       __throw_system_error(__e);
103     }
104   }
105 }
106
107 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1