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

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/win_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
 
// win_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_WIN_THREAD_HPP
12
 
#define ASIO_DETAIL_WIN_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 <process.h>
35
 
#include "asio/detail/pop_options.hpp"
36
 
 
37
 
namespace asio {
38
 
namespace detail {
39
 
 
40
 
unsigned int __stdcall win_thread_function(void* arg);
41
 
 
42
 
class win_thread
43
 
  : private noncopyable
44
 
{
45
 
public:
46
 
  // The purpose of the thread.
47
 
  enum purpose { internal, external };
48
 
 
49
 
  // Constructor.
50
 
  template <typename Function>
51
 
  win_thread(Function f, purpose p = internal)
52
 
    : exit_event_(0)
53
 
  {
54
 
    std::auto_ptr<func_base> arg(new func<Function>(f));
55
 
 
56
 
    ::HANDLE entry_event = 0;
57
 
    if (p == internal)
58
 
    {
59
 
      arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
60
 
      if (!entry_event)
61
 
      {
62
 
        DWORD last_error = ::GetLastError();
63
 
        asio::system_error e(
64
 
            asio::error_code(last_error,
65
 
              asio::error::get_system_category()),
66
 
            "thread.entry_event");
67
 
        boost::throw_exception(e);
68
 
      }
69
 
 
70
 
      arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
71
 
      if (!exit_event_)
72
 
      {
73
 
        DWORD last_error = ::GetLastError();
74
 
        ::CloseHandle(entry_event);
75
 
        asio::system_error e(
76
 
            asio::error_code(last_error,
77
 
              asio::error::get_system_category()),
78
 
            "thread.exit_event");
79
 
        boost::throw_exception(e);
80
 
      }
81
 
    }
82
 
 
83
 
    unsigned int thread_id = 0;
84
 
    thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0, 0,
85
 
          win_thread_function, arg.get(), 0, &thread_id));
86
 
    if (!thread_)
87
 
    {
88
 
      DWORD last_error = ::GetLastError();
89
 
      if (entry_event)
90
 
        ::CloseHandle(entry_event);
91
 
      if (exit_event_)
92
 
        ::CloseHandle(exit_event_);
93
 
      asio::system_error e(
94
 
          asio::error_code(last_error,
95
 
            asio::error::get_system_category()),
96
 
          "thread");
97
 
      boost::throw_exception(e);
98
 
    }
99
 
    arg.release();
100
 
 
101
 
    if (entry_event)
102
 
    {
103
 
      ::WaitForSingleObject(entry_event, INFINITE);
104
 
      ::CloseHandle(entry_event);
105
 
    }
106
 
  }
107
 
 
108
 
  // Destructor.
109
 
  ~win_thread()
110
 
  {
111
 
    ::CloseHandle(thread_);
112
 
 
113
 
    // The exit_event_ handle is deliberately allowed to leak here since it
114
 
    // is an error for the owner of an internal thread not to join() it.
115
 
  }
116
 
 
117
 
  // Wait for the thread to exit.
118
 
  void join()
119
 
  {
120
 
    if (exit_event_)
121
 
    {
122
 
      ::WaitForSingleObject(exit_event_, INFINITE);
123
 
      ::CloseHandle(exit_event_);
124
 
      ::TerminateThread(thread_, 0);
125
 
    }
126
 
    else
127
 
    {
128
 
      ::WaitForSingleObject(thread_, INFINITE);
129
 
    }
130
 
  }
131
 
 
132
 
private:
133
 
  friend unsigned int __stdcall win_thread_function(void* arg);
134
 
 
135
 
  class func_base
136
 
  {
137
 
  public:
138
 
    virtual ~func_base() {}
139
 
    virtual void run() = 0;
140
 
    ::HANDLE entry_event_;
141
 
    ::HANDLE exit_event_;
142
 
  };
143
 
 
144
 
  template <typename Function>
145
 
  class func
146
 
    : public func_base
147
 
  {
148
 
  public:
149
 
    func(Function f)
150
 
      : f_(f)
151
 
    {
152
 
    }
153
 
 
154
 
    virtual void run()
155
 
    {
156
 
      f_();
157
 
    }
158
 
 
159
 
  private:
160
 
    Function f_;
161
 
  };
162
 
 
163
 
  ::HANDLE thread_;
164
 
  ::HANDLE exit_event_;
165
 
};
166
 
 
167
 
inline unsigned int __stdcall win_thread_function(void* arg)
168
 
{
169
 
  std::auto_ptr<win_thread::func_base> func(
170
 
      static_cast<win_thread::func_base*>(arg));
171
 
 
172
 
  if (func->entry_event_)
173
 
    ::SetEvent(func->entry_event_);
174
 
 
175
 
  func->run();
176
 
 
177
 
  if (HANDLE exit_event = func->exit_event_)
178
 
  {
179
 
    func.reset();
180
 
    ::SetEvent(exit_event);
181
 
    ::Sleep(INFINITE);
182
 
  }
183
 
 
184
 
  return 0;
185
 
}
186
 
 
187
 
} // namespace detail
188
 
} // namespace asio
189
 
 
190
 
#endif // defined(BOOST_WINDOWS) && !defined(UNDER_CE)
191
 
 
192
 
#include "asio/detail/pop_options.hpp"
193
 
 
194
 
#endif // ASIO_DETAIL_WIN_THREAD_HPP