1 | // |
---|---|
2 | // detail/impl/posix_thread.ipp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP |
12 | #define BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | |
20 | #if defined(BOOST_ASIO_HAS_PTHREADS) |
21 | |
22 | #include <boost/asio/detail/posix_thread.hpp> |
23 | #include <boost/asio/detail/throw_error.hpp> |
24 | #include <boost/asio/error.hpp> |
25 | |
26 | #include <boost/asio/detail/push_options.hpp> |
27 | |
28 | namespace boost { |
29 | namespace asio { |
30 | namespace detail { |
31 | |
32 | posix_thread::~posix_thread() |
33 | { |
34 | if (!joined_) |
35 | ::pthread_detach(th: thread_); |
36 | } |
37 | |
38 | void posix_thread::join() |
39 | { |
40 | if (!joined_) |
41 | { |
42 | ::pthread_join(th: thread_, thread_return: 0); |
43 | joined_ = true; |
44 | } |
45 | } |
46 | |
47 | void posix_thread::start_thread(func_base* arg) |
48 | { |
49 | int error = ::pthread_create(newthread: &thread_, attr: 0, |
50 | start_routine: boost_asio_detail_posix_thread_function, arg: arg); |
51 | if (error != 0) |
52 | { |
53 | delete arg; |
54 | boost::system::error_code ec(error, |
55 | boost::asio::error::get_system_category()); |
56 | boost::asio::detail::throw_error(err: ec, location: "thread"); |
57 | } |
58 | } |
59 | |
60 | void* boost_asio_detail_posix_thread_function(void* arg) |
61 | { |
62 | posix_thread::auto_func_base_ptr func = { |
63 | .ptr: static_cast<posix_thread::func_base*>(arg) }; |
64 | func.ptr->run(); |
65 | return 0; |
66 | } |
67 | |
68 | } // namespace detail |
69 | } // namespace asio |
70 | } // namespace boost |
71 | |
72 | #include <boost/asio/detail/pop_options.hpp> |
73 | |
74 | #endif // defined(BOOST_ASIO_HAS_PTHREADS) |
75 | |
76 | #endif // BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP |
77 |