Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:42

0001 //
0002 // detail/posix_thread.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
0012 #define BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_HAS_PTHREADS)
0021 
0022 #include <cstddef>
0023 #include <pthread.h>
0024 #include <boost/asio/detail/noncopyable.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031 
0032 extern "C"
0033 {
0034   BOOST_ASIO_DECL void* boost_asio_detail_posix_thread_function(void* arg);
0035 }
0036 
0037 class posix_thread
0038   : private noncopyable
0039 {
0040 public:
0041   // Constructor.
0042   template <typename Function>
0043   posix_thread(Function f, unsigned int = 0)
0044     : joined_(false)
0045   {
0046     start_thread(new func<Function>(f));
0047   }
0048 
0049   // Destructor.
0050   BOOST_ASIO_DECL ~posix_thread();
0051 
0052   // Wait for the thread to exit.
0053   BOOST_ASIO_DECL void join();
0054 
0055   // Get number of CPUs.
0056   BOOST_ASIO_DECL static std::size_t hardware_concurrency();
0057 
0058 private:
0059   friend void* boost_asio_detail_posix_thread_function(void* arg);
0060 
0061   class func_base
0062   {
0063   public:
0064     virtual ~func_base() {}
0065     virtual void run() = 0;
0066   };
0067 
0068   struct auto_func_base_ptr
0069   {
0070     func_base* ptr;
0071     ~auto_func_base_ptr() { delete ptr; }
0072   };
0073 
0074   template <typename Function>
0075   class func
0076     : public func_base
0077   {
0078   public:
0079     func(Function f)
0080       : f_(f)
0081     {
0082     }
0083 
0084     virtual void run()
0085     {
0086       f_();
0087     }
0088 
0089   private:
0090     Function f_;
0091   };
0092 
0093   BOOST_ASIO_DECL void start_thread(func_base* arg);
0094 
0095   ::pthread_t thread_;
0096   bool joined_;
0097 };
0098 
0099 } // namespace detail
0100 } // namespace asio
0101 } // namespace boost
0102 
0103 #include <boost/asio/detail/pop_options.hpp>
0104 
0105 #if defined(BOOST_ASIO_HEADER_ONLY)
0106 # include <boost/asio/detail/impl/posix_thread.ipp>
0107 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0108 
0109 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
0110 
0111 #endif // BOOST_ASIO_DETAIL_POSIX_THREAD_HPP