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 ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
12#define 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 "asio/detail/config.hpp"
19
20#if defined(ASIO_HAS_PTHREADS)
21
22#include "asio/detail/posix_thread.hpp"
23#include "asio/detail/throw_error.hpp"
24#include "asio/error.hpp"
25
26#include "asio/detail/push_options.hpp"
27
28namespace clmdep_asio {
29namespace detail {
30
31posix_thread::~posix_thread()
32{
33  if (!joined_)
34    ::pthread_detach(thread_);
35}
36
37void posix_thread::join()
38{
39  if (!joined_)
40  {
41    ::pthread_join(thread_, 0);
42    joined_ = true;
43  }
44}
45
46void posix_thread::start_thread(func_base* arg)
47{
48  int error = ::pthread_create(&thread_, 0,
49        clmdep_asio_detail_posix_thread_function, arg);
50  if (error != 0)
51  {
52    delete arg;
53    clmdep_asio::error_code ec(error,
54        clmdep_asio::error::get_system_category());
55    clmdep_asio::detail::throw_error(ec, "thread");
56  }
57}
58
59void* clmdep_asio_detail_posix_thread_function(void* arg)
60{
61  posix_thread::auto_func_base_ptr func = {
62      static_cast<posix_thread::func_base*>(arg) };
63  func.ptr->run();
64  return 0;
65}
66
67} // namespace detail
68} // namespace clmdep_asio
69
70#include "asio/detail/pop_options.hpp"
71
72#endif // defined(ASIO_HAS_PTHREADS)
73
74#endif // ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
75