Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/wince_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_WINCE_THREAD_HPP
0012 #define BOOST_ASIO_DETAIL_WINCE_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) && defined(UNDER_CE)
0021 
0022 #include <boost/asio/detail/noncopyable.hpp>
0023 #include <boost/asio/detail/scoped_ptr.hpp>
0024 #include <boost/asio/detail/socket_types.hpp>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/error.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 DWORD WINAPI wince_thread_function(LPVOID arg);
0035 
0036 class wince_thread
0037   : private noncopyable
0038 {
0039 public:
0040   // Constructor.
0041   template <typename Function>
0042   wince_thread(Function f, unsigned int = 0)
0043   {
0044     scoped_ptr<func_base> arg(new func<Function>(f));
0045     DWORD thread_id = 0;
0046     thread_ = ::CreateThread(0, 0, wince_thread_function,
0047         arg.get(), 0, &thread_id);
0048     if (!thread_)
0049     {
0050       DWORD last_error = ::GetLastError();
0051       boost::system::error_code ec(last_error,
0052           boost::asio::error::get_system_category());
0053       boost::asio::detail::throw_error(ec, "thread");
0054     }
0055     arg.release();
0056   }
0057 
0058   // Destructor.
0059   ~wince_thread()
0060   {
0061     ::CloseHandle(thread_);
0062   }
0063 
0064   // Wait for the thread to exit.
0065   void join()
0066   {
0067     ::WaitForSingleObject(thread_, INFINITE);
0068   }
0069 
0070   // Get number of CPUs.
0071   static std::size_t hardware_concurrency()
0072   {
0073     SYSTEM_INFO system_info;
0074     ::GetSystemInfo(&system_info);
0075     return system_info.dwNumberOfProcessors;
0076   }
0077 
0078 private:
0079   friend DWORD WINAPI wince_thread_function(LPVOID arg);
0080 
0081   class func_base
0082   {
0083   public:
0084     virtual ~func_base() {}
0085     virtual void run() = 0;
0086   };
0087 
0088   template <typename Function>
0089   class func
0090     : public func_base
0091   {
0092   public:
0093     func(Function f)
0094       : f_(f)
0095     {
0096     }
0097 
0098     virtual void run()
0099     {
0100       f_();
0101     }
0102 
0103   private:
0104     Function f_;
0105   };
0106 
0107   ::HANDLE thread_;
0108 };
0109 
0110 inline DWORD WINAPI wince_thread_function(LPVOID arg)
0111 {
0112   scoped_ptr<wince_thread::func_base> func(
0113       static_cast<wince_thread::func_base*>(arg));
0114   func->run();
0115   return 0;
0116 }
0117 
0118 } // namespace detail
0119 } // namespace asio
0120 } // namespace boost
0121 
0122 #include <boost/asio/detail/pop_options.hpp>
0123 
0124 #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
0125 
0126 #endif // BOOST_ASIO_DETAIL_WINCE_THREAD_HPP