~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/wince_thread.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// wince_thread.hpp
3
 
// ~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 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_WINCE_THREAD_HPP
12
 
#define ASIO_DETAIL_WINCE_THREAD_HPP
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/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <boost/config.hpp>
22
 
#include "asio/detail/pop_options.hpp"
23
 
 
24
 
#if defined(BOOST_WINDOWS) && defined(UNDER_CE)
25
 
 
26
 
#include "asio/error.hpp"
27
 
#include "asio/system_error.hpp"
28
 
#include "asio/detail/noncopyable.hpp"
29
 
#include "asio/detail/socket_types.hpp"
30
 
 
31
 
#include "asio/detail/push_options.hpp"
32
 
#include <boost/throw_exception.hpp>
33
 
#include <memory>
34
 
#include "asio/detail/pop_options.hpp"
35
 
 
36
 
namespace asio {
37
 
namespace detail {
38
 
 
39
 
DWORD WINAPI wince_thread_function(LPVOID arg);
40
 
 
41
 
class wince_thread
42
 
  : private noncopyable
43
 
{
44
 
public:
45
 
  // The purpose of the thread.
46
 
  enum purpose { internal, external };
47
 
 
48
 
  // Constructor.
49
 
  template <typename Function>
50
 
  wince_thread(Function f, purpose = internal)
51
 
  {
52
 
    std::auto_ptr<func_base> arg(new func<Function>(f));
53
 
    DWORD thread_id = 0;
54
 
    thread_ = ::CreateThread(0, 0, wince_thread_function,
55
 
        arg.get(), 0, &thread_id);
56
 
    if (!thread_)
57
 
    {
58
 
      DWORD last_error = ::GetLastError();
59
 
      asio::system_error e(
60
 
          asio::error_code(last_error,
61
 
            asio::error::get_system_category()),
62
 
          "thread");
63
 
      boost::throw_exception(e);
64
 
    }
65
 
    arg.release();
66
 
  }
67
 
 
68
 
  // Destructor.
69
 
  ~wince_thread()
70
 
  {
71
 
    ::CloseHandle(thread_);
72
 
  }
73
 
 
74
 
  // Wait for the thread to exit.
75
 
  void join()
76
 
  {
77
 
    ::WaitForSingleObject(thread_, INFINITE);
78
 
  }
79
 
 
80
 
private:
81
 
  friend DWORD WINAPI wince_thread_function(LPVOID arg);
82
 
 
83
 
  class func_base
84
 
  {
85
 
  public:
86
 
    virtual ~func_base() {}
87
 
    virtual void run() = 0;
88
 
  };
89
 
 
90
 
  template <typename Function>
91
 
  class func
92
 
    : public func_base
93
 
  {
94
 
  public:
95
 
    func(Function f)
96
 
      : f_(f)
97
 
    {
98
 
    }
99
 
 
100
 
    virtual void run()
101
 
    {
102
 
      f_();
103
 
    }
104
 
 
105
 
  private:
106
 
    Function f_;
107
 
  };
108
 
 
109
 
  ::HANDLE thread_;
110
 
};
111
 
 
112
 
inline DWORD WINAPI wince_thread_function(LPVOID arg)
113
 
{
114
 
  std::auto_ptr<wince_thread::func_base> func(
115
 
      static_cast<wince_thread::func_base*>(arg));
116
 
  func->run();
117
 
  return 0;
118
 
}
119
 
 
120
 
} // namespace detail
121
 
} // namespace asio
122
 
 
123
 
#endif // defined(BOOST_WINDOWS) && defined(UNDER_CE)
124
 
 
125
 
#include "asio/detail/pop_options.hpp"
126
 
 
127
 
#endif // ASIO_DETAIL_WINCE_THREAD_HPP