~ubuntu-branches/ubuntu/oneiric/libtorrent/oneiric

« back to all changes in this revision

Viewing changes to src/data/hash_queue.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <functional>
 
40
 
 
41
#include "torrent/exceptions.h"
 
42
 
 
43
#include "hash_queue.h"
 
44
#include "hash_chunk.h"
 
45
#include "chunk.h"
 
46
#include "chunk_list_node.h"
 
47
#include "globals.h"
 
48
 
 
49
namespace torrent {
 
50
 
 
51
struct HashQueueEqual {
 
52
  HashQueueEqual(HashQueueNode::id_type id, uint32_t index) : m_id(id), m_index(index) {}
 
53
 
 
54
  bool operator () (const HashQueueNode& q) const { return m_id == q.id() && m_index == q.get_index(); }
 
55
 
 
56
  HashQueueNode::id_type m_id;
 
57
  uint32_t              m_index;
 
58
};
 
59
 
 
60
struct HashQueueWillneed {
 
61
  HashQueueWillneed(int bytes) : m_bytes(bytes) {}
 
62
 
 
63
  bool operator () (HashQueueNode& q) { return (m_bytes -= q.call_willneed()) <= 0; }
 
64
 
 
65
  int m_bytes;
 
66
};
 
67
 
 
68
// If madvise is not available it will always mark the pages as being
 
69
// in memory, thus we don't need to modify m_maxTries to have full
 
70
// disk usage. But this may cause too much blocking as it will think
 
71
// everything is in memory, thus we need to throttle.
 
72
 
 
73
HashQueue::HashQueue() :
 
74
  m_readAhead(10 << 20),
 
75
  m_interval(5000),
 
76
  m_maxTries(5) {
 
77
 
 
78
  m_taskWork.set_slot(rak::mem_fn(this, &HashQueue::work));
 
79
}
 
80
 
 
81
 
 
82
inline void
 
83
HashQueue::willneed(int bytes) {
 
84
  std::find_if(begin(), end(), HashQueueWillneed(bytes));
 
85
}
 
86
 
 
87
// If we're done immediately, move the chunk to the front of the list so
 
88
// the next work cycle gets stuff done.
 
89
void
 
90
HashQueue::push_back(ChunkHandle handle, slot_done_type d) {
 
91
  if (!handle.is_valid())
 
92
    throw internal_error("HashQueue::add(...) received an invalid chunk");
 
93
 
 
94
  HashChunk* hc = new HashChunk(handle);
 
95
 
 
96
  if (empty()) {
 
97
    if (m_taskWork.is_queued())
 
98
      throw internal_error("Empty HashQueue is still in task schedule");
 
99
 
 
100
    m_tries = 0;
 
101
    priority_queue_insert(&taskScheduler, &m_taskWork, cachedTime + 1);
 
102
  }
 
103
 
 
104
  base_type::push_back(HashQueueNode(hc, d));
 
105
  willneed(m_readAhead);
 
106
}
 
107
 
 
108
bool
 
109
HashQueue::has(HashQueueNode::id_type id) {
 
110
  return std::find_if(begin(), end(), rak::equal(id, std::mem_fun_ref(&HashQueueNode::id))) != end();
 
111
}
 
112
 
 
113
bool
 
114
HashQueue::has(HashQueueNode::id_type id, uint32_t index) {
 
115
  return std::find_if(begin(), end(), HashQueueEqual(id, index)) != end();
 
116
}
 
117
 
 
118
void
 
119
HashQueue::remove(HashQueueNode::id_type id) {
 
120
  iterator itr = begin();
 
121
  
 
122
  while ((itr = std::find_if(itr, end(), rak::equal(id, std::mem_fun_ref(&HashQueueNode::id)))) != end()) {
 
123
    itr->slot_done()(*itr->get_chunk()->chunk(), NULL);
 
124
 
 
125
    itr->clear();
 
126
    itr = erase(itr);
 
127
  }
 
128
 
 
129
  if (empty())
 
130
    priority_queue_erase(&taskScheduler, &m_taskWork);
 
131
}
 
132
 
 
133
void
 
134
HashQueue::clear() {
 
135
  if (!empty())
 
136
    throw internal_error("HashQueue::clear() called but valid nodes were found.");
 
137
 
 
138
  // Replace with a dtor check to ensure it is empty?
 
139
//   std::for_each(begin(), end(), std::mem_fun_ref(&HashQueueNode::clear));
 
140
//   base_type::clear();
 
141
//   priority_queue_erase(&taskScheduler, &m_taskWork);
 
142
}
 
143
 
 
144
void
 
145
HashQueue::work() {
 
146
  if (empty())
 
147
    return;
 
148
 
 
149
  if (!check(++m_tries >= m_maxTries))
 
150
    return priority_queue_insert(&taskScheduler, &m_taskWork, cachedTime + m_interval);
 
151
 
 
152
  if (!empty() && !m_taskWork.is_queued())
 
153
    priority_queue_insert(&taskScheduler, &m_taskWork, cachedTime + 1);
 
154
 
 
155
  m_tries = std::min(0, m_tries - 2);
 
156
}
 
157
 
 
158
bool
 
159
HashQueue::check(bool force) {
 
160
  if (!base_type::front().perform(force)) {
 
161
    willneed(m_readAhead);
 
162
    return false;
 
163
  }
 
164
 
 
165
  HashChunk* chunk                       = base_type::front().get_chunk();
 
166
  HashQueueNode::slot_done_type slotDone = base_type::front().slot_done();
 
167
 
 
168
  base_type::pop_front();
 
169
 
 
170
  char buffer[20];
 
171
  chunk->hash_c(buffer);
 
172
 
 
173
  slotDone(*chunk->chunk(), buffer);
 
174
  delete chunk;
 
175
 
 
176
  // This should be a few chunks ahead.
 
177
  if (!empty())
 
178
    willneed(m_readAhead);
 
179
 
 
180
  return true;
 
181
}
 
182
 
 
183
}