Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/win_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_WIN_THREAD_HPP
0012 #define BOOST_ASIO_DETAIL_WIN_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_WINDOWS) \
0021   && !defined(BOOST_ASIO_WINDOWS_APP) \
0022   && !defined(UNDER_CE)
0023 
0024 #include <cstddef>
0025 #include <boost/asio/detail/noncopyable.hpp>
0026 #include <boost/asio/detail/socket_types.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
0035 
0036 #if defined(WINVER) && (WINVER < 0x0500)
0037 BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
0038 #else
0039 BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
0040 #endif
0041 
0042 template <typename T>
0043 class win_thread_base
0044 {
0045 public:
0046   static bool terminate_threads()
0047   {
0048     return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
0049   }
0050 
0051   static void set_terminate_threads(bool b)
0052   {
0053     ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
0054   }
0055 
0056 private:
0057   static long terminate_threads_;
0058 };
0059 
0060 template <typename T>
0061 long win_thread_base<T>::terminate_threads_ = 0;
0062 
0063 class win_thread
0064   : private noncopyable,
0065     public win_thread_base<win_thread>
0066 {
0067 public:
0068   // Constructor.
0069   template <typename Function>
0070   win_thread(Function f, unsigned int stack_size = 0)
0071     : thread_(0),
0072       exit_event_(0)
0073   {
0074     start_thread(new func<Function>(f), stack_size);
0075   }
0076 
0077   // Destructor.
0078   BOOST_ASIO_DECL ~win_thread();
0079 
0080   // Wait for the thread to exit.
0081   BOOST_ASIO_DECL void join();
0082 
0083   // Get number of CPUs.
0084   BOOST_ASIO_DECL static std::size_t hardware_concurrency();
0085 
0086 private:
0087   friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
0088 
0089 #if defined(WINVER) && (WINVER < 0x0500)
0090   friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
0091 #else
0092   friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
0093 #endif
0094 
0095   class func_base
0096   {
0097   public:
0098     virtual ~func_base() {}
0099     virtual void run() = 0;
0100     ::HANDLE entry_event_;
0101     ::HANDLE exit_event_;
0102   };
0103 
0104   struct auto_func_base_ptr
0105   {
0106     func_base* ptr;
0107     ~auto_func_base_ptr() { delete ptr; }
0108   };
0109 
0110   template <typename Function>
0111   class func
0112     : public func_base
0113   {
0114   public:
0115     func(Function f)
0116       : f_(f)
0117     {
0118     }
0119 
0120     virtual void run()
0121     {
0122       f_();
0123     }
0124 
0125   private:
0126     Function f_;
0127   };
0128 
0129   BOOST_ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
0130 
0131   ::HANDLE thread_;
0132   ::HANDLE exit_event_;
0133 };
0134 
0135 } // namespace detail
0136 } // namespace asio
0137 } // namespace boost
0138 
0139 #include <boost/asio/detail/pop_options.hpp>
0140 
0141 #if defined(BOOST_ASIO_HEADER_ONLY)
0142 # include <boost/asio/detail/impl/win_thread.ipp>
0143 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0144 
0145 #endif // defined(BOOST_ASIO_WINDOWS)
0146        // && !defined(BOOST_ASIO_WINDOWS_APP)
0147        // && !defined(UNDER_CE)
0148 
0149 #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP