~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// deadline_timer_service.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_DEADLINE_TIMER_SERVICE_HPP
12
 
#define ASIO_DETAIL_DEADLINE_TIMER_SERVICE_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 <cstddef>
22
 
#include <boost/config.hpp>
23
 
#include <boost/date_time/posix_time/posix_time_types.hpp>
24
 
#include "asio/detail/pop_options.hpp"
25
 
 
26
 
#include "asio/error.hpp"
27
 
#include "asio/io_service.hpp"
28
 
#include "asio/detail/bind_handler.hpp"
29
 
#include "asio/detail/handler_base_from_member.hpp"
30
 
#include "asio/detail/noncopyable.hpp"
31
 
#include "asio/detail/service_base.hpp"
32
 
#include "asio/detail/socket_ops.hpp"
33
 
#include "asio/detail/socket_types.hpp"
34
 
#include "asio/detail/timer_queue.hpp"
35
 
 
36
 
namespace asio {
37
 
namespace detail {
38
 
 
39
 
template <typename Time_Traits, typename Timer_Scheduler>
40
 
class deadline_timer_service
41
 
  : public asio::detail::service_base<
42
 
      deadline_timer_service<Time_Traits, Timer_Scheduler> >
43
 
{
44
 
public:
45
 
  // The time type.
46
 
  typedef typename Time_Traits::time_type time_type;
47
 
 
48
 
  // The duration type.
49
 
  typedef typename Time_Traits::duration_type duration_type;
50
 
 
51
 
  // The implementation type of the timer. This type is dependent on the
52
 
  // underlying implementation of the timer service.
53
 
  struct implementation_type
54
 
    : private asio::detail::noncopyable
55
 
  {
56
 
    time_type expiry;
57
 
    bool might_have_pending_waits;
58
 
  };
59
 
 
60
 
  // Constructor.
61
 
  deadline_timer_service(asio::io_service& io_service)
62
 
    : asio::detail::service_base<
63
 
        deadline_timer_service<Time_Traits, Timer_Scheduler> >(io_service),
64
 
      scheduler_(asio::use_service<Timer_Scheduler>(io_service))
65
 
  {
66
 
    scheduler_.init_task();
67
 
    scheduler_.add_timer_queue(timer_queue_);
68
 
  }
69
 
 
70
 
  // Destructor.
71
 
  ~deadline_timer_service()
72
 
  {
73
 
    scheduler_.remove_timer_queue(timer_queue_);
74
 
  }
75
 
 
76
 
  // Destroy all user-defined handler objects owned by the service.
77
 
  void shutdown_service()
78
 
  {
79
 
  }
80
 
 
81
 
  // Construct a new timer implementation.
82
 
  void construct(implementation_type& impl)
83
 
  {
84
 
    impl.expiry = time_type();
85
 
    impl.might_have_pending_waits = false;
86
 
  }
87
 
 
88
 
  // Destroy a timer implementation.
89
 
  void destroy(implementation_type& impl)
90
 
  {
91
 
    asio::error_code ec;
92
 
    cancel(impl, ec);
93
 
  }
94
 
 
95
 
  // Cancel any asynchronous wait operations associated with the timer.
96
 
  std::size_t cancel(implementation_type& impl, asio::error_code& ec)
97
 
  {
98
 
    if (!impl.might_have_pending_waits)
99
 
    {
100
 
      ec = asio::error_code();
101
 
      return 0;
102
 
    }
103
 
    std::size_t count = scheduler_.cancel_timer(timer_queue_, &impl);
104
 
    impl.might_have_pending_waits = false;
105
 
    ec = asio::error_code();
106
 
    return count;
107
 
  }
108
 
 
109
 
  // Get the expiry time for the timer as an absolute time.
110
 
  time_type expires_at(const implementation_type& impl) const
111
 
  {
112
 
    return impl.expiry;
113
 
  }
114
 
 
115
 
  // Set the expiry time for the timer as an absolute time.
116
 
  std::size_t expires_at(implementation_type& impl,
117
 
      const time_type& expiry_time, asio::error_code& ec)
118
 
  {
119
 
    std::size_t count = cancel(impl, ec);
120
 
    impl.expiry = expiry_time;
121
 
    ec = asio::error_code();
122
 
    return count;
123
 
  }
124
 
 
125
 
  // Get the expiry time for the timer relative to now.
126
 
  duration_type expires_from_now(const implementation_type& impl) const
127
 
  {
128
 
    return Time_Traits::subtract(expires_at(impl), Time_Traits::now());
129
 
  }
130
 
 
131
 
  // Set the expiry time for the timer relative to now.
132
 
  std::size_t expires_from_now(implementation_type& impl,
133
 
      const duration_type& expiry_time, asio::error_code& ec)
134
 
  {
135
 
    return expires_at(impl,
136
 
        Time_Traits::add(Time_Traits::now(), expiry_time), ec);
137
 
  }
138
 
 
139
 
  // Perform a blocking wait on the timer.
140
 
  void wait(implementation_type& impl, asio::error_code& ec)
141
 
  {
142
 
    time_type now = Time_Traits::now();
143
 
    while (Time_Traits::less_than(now, impl.expiry))
144
 
    {
145
 
      boost::posix_time::time_duration timeout =
146
 
        Time_Traits::to_posix_duration(Time_Traits::subtract(impl.expiry, now));
147
 
      ::timeval tv;
148
 
      tv.tv_sec = timeout.total_seconds();
149
 
      tv.tv_usec = timeout.total_microseconds() % 1000000;
150
 
      asio::error_code ec;
151
 
      socket_ops::select(0, 0, 0, 0, &tv, ec);
152
 
      now = Time_Traits::now();
153
 
    }
154
 
    ec = asio::error_code();
155
 
  }
156
 
 
157
 
  template <typename Handler>
158
 
  class wait_handler : 
159
 
    public handler_base_from_member<Handler>
160
 
  {
161
 
  public:
162
 
    wait_handler(asio::io_service& io_service, Handler handler)
163
 
      : handler_base_from_member<Handler>(handler),
164
 
        io_service_(io_service),
165
 
        work_(io_service)
166
 
    {
167
 
    }
168
 
 
169
 
    void operator()(const asio::error_code& result)
170
 
    {
171
 
      io_service_.post(detail::bind_handler(this->handler_, result));
172
 
    }
173
 
 
174
 
  private:
175
 
    asio::io_service& io_service_;
176
 
    asio::io_service::work work_;
177
 
  };
178
 
 
179
 
  // Start an asynchronous wait on the timer.
180
 
  template <typename Handler>
181
 
  void async_wait(implementation_type& impl, Handler handler)
182
 
  {
183
 
    impl.might_have_pending_waits = true;
184
 
    scheduler_.schedule_timer(timer_queue_, impl.expiry,
185
 
        wait_handler<Handler>(this->get_io_service(), handler), &impl);
186
 
  }
187
 
 
188
 
private:
189
 
  // The queue of timers.
190
 
  timer_queue<Time_Traits> timer_queue_;
191
 
 
192
 
  // The object that schedules and executes timers. Usually a reactor.
193
 
  Timer_Scheduler& scheduler_;
194
 
};
195
 
 
196
 
} // namespace detail
197
 
} // namespace asio
198
 
 
199
 
#include "asio/detail/pop_options.hpp"
200
 
 
201
 
#endif // ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP