* Replaced TaskScheduler with rak::priority_queue.

* Fixed an exception caused by initial hash check not properly
cleaning up HashQueue when canceling.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@606 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-12-08 21:19:53 +00:00
parent ba7d61b532
commit 12ef47da3c
30 changed files with 362 additions and 367 deletions
+2 -3
View File
@@ -59,7 +59,6 @@ Control::Control() :
m_inputStdin->slot_pressed(sigc::mem_fun(m_input, &input::Manager::pressed));
m_taskShutdown.set_iterator(taskScheduler.end());
m_taskShutdown.set_slot(rak::mem_fn(this, &Control::receive_shutdown));
}
@@ -113,8 +112,8 @@ Control::receive_shutdown() {
m_core->shutdown(false);
m_shutdownReceived = true;
if (!taskScheduler.is_scheduled(&m_taskShutdown))
taskScheduler.insert(&m_taskShutdown, cachedTime + 5 * 1000000);
if (!m_taskShutdown.is_queued())
taskScheduler.push(m_taskShutdown.prepare(cachedTime + 5 * 1000000));
} else {
m_core->shutdown(true);
+1 -1
View File
@@ -89,7 +89,7 @@ private:
input::Manager* m_input;
input::InputEvent* m_inputStdin;
utils::TaskItem m_taskShutdown;
rak::priority_item m_taskShutdown;
};
#endif
+2 -5
View File
@@ -60,10 +60,7 @@ DownloadFactory::DownloadFactory(const std::string& uri, Manager* m) :
m_session(false),
m_start(false) {
m_taskLoad.set_iterator(taskScheduler.end());
m_taskLoad.set_slot(rak::mem_fn(this, &DownloadFactory::receive_load));
m_taskCommit.set_iterator(taskScheduler.end());
m_taskCommit.set_slot(rak::mem_fn(this, &DownloadFactory::receive_commit));
}
@@ -77,12 +74,12 @@ DownloadFactory::~DownloadFactory() {
void
DownloadFactory::load() {
taskScheduler.insert(&m_taskLoad, cachedTime);
taskScheduler.push(m_taskLoad.prepare(cachedTime));
}
void
DownloadFactory::commit() {
taskScheduler.insert(&m_taskCommit, cachedTime);
taskScheduler.push(m_taskCommit.prepare(cachedTime));
}
void
+3 -3
View File
@@ -39,8 +39,8 @@
#include <iosfwd>
#include <sigc++/slot.h>
#include <rak/priority_queue_default.h>
#include "utils/task_item.h"
#include "http_queue.h"
namespace core {
@@ -87,8 +87,8 @@ private:
bool m_start;
Slot m_slotFinished;
utils::TaskItem m_taskLoad;
utils::TaskItem m_taskCommit;
rak::priority_item m_taskLoad;
rak::priority_item m_taskCommit;
};
}
+6 -1
View File
@@ -98,7 +98,12 @@ void
Manager::do_update() {
Canvas::refresh_std();
displayScheduler.execute(cachedTime);
std::list<rak::priority_item*> workQueue;
std::copy(rak::queue_popper(displayScheduler, rak::priority_ready(cachedTime)), rak::queue_popper(), std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear_time));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::call));
std::for_each(begin(), end(), rak::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::refresh)));
Canvas::do_update();
-1
View File
@@ -50,7 +50,6 @@ Window::Window(Canvas* c, bool d, int h) :
m_dynamic(d),
m_minHeight(h) {
m_taskUpdate.set_iterator(displayScheduler.end());
m_taskUpdate.set_slot(rak::mem_fn(this, &Window::redraw));
}
+3 -3
View File
@@ -57,7 +57,7 @@ public:
bool is_active() { return m_active; }
bool is_dynamic() { return m_dynamic; }
bool is_dirty() { return displayScheduler.is_scheduled(&m_taskUpdate); }
bool is_dirty() { return m_taskUpdate.is_queued(); }
//utils::rak::timer get_next_draw() { return m_nextDraw; }
@@ -87,13 +87,13 @@ protected:
bool m_dynamic;
int m_minHeight;
utils::TaskItem m_taskUpdate;
rak::priority_item m_taskUpdate;
};
inline void
Window::mark_dirty() {
displayScheduler.erase(&m_taskUpdate);
displayScheduler.insert(&m_taskUpdate, cachedTime);
displayScheduler.push(m_taskUpdate.prepare(cachedTime));
}
}
+1 -1
View File
@@ -60,7 +60,7 @@ WindowDownloadList::~WindowDownloadList() {
void
WindowDownloadList::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
+1 -1
View File
@@ -54,7 +54,7 @@ WindowDownloadStatusbar::WindowDownloadStatusbar(core::Download* d) :
void
WindowDownloadStatusbar::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
+1 -1
View File
@@ -54,7 +54,7 @@ WindowFileList::WindowFileList(core::Download* d, unsigned int* focus) :
void
WindowFileList::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 10 * 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 10 * 1000000).round_seconds()));
m_canvas->erase();
if (m_download->get_download().size_file_entries() == 0 ||
+1 -1
View File
@@ -58,7 +58,7 @@ WindowHttpQueue::WindowHttpQueue(core::HttpQueue* q) :
void
WindowHttpQueue::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
cleanup_list();
+1 -1
View File
@@ -57,7 +57,7 @@ WindowPeerInfo::WindowPeerInfo(core::Download* d, PList* l, PList::iterator* f)
void
WindowPeerInfo::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
int y = 0;
+1 -1
View File
@@ -56,7 +56,7 @@ WindowPeerList::WindowPeerList(core::Download* d, PList* l, PList::iterator* f)
void
WindowPeerList::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
int x = 2;
+1 -1
View File
@@ -56,7 +56,7 @@ WindowStatusbar::WindowStatusbar(core::Manager* c) :
void
WindowStatusbar::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
+1 -1
View File
@@ -48,7 +48,7 @@ WindowTitle::WindowTitle(const std::string& s) :
void
WindowTitle::redraw() {
displayScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
m_canvas->erase();
m_canvas->print(std::max(0, (m_canvas->get_width() - (int)m_title.size()) / 2 - 4), 0,
+1 -1
View File
@@ -55,7 +55,7 @@ WindowTrackerList::WindowTrackerList(core::Download* d, unsigned int* focus) :
void
WindowTrackerList::redraw() {
// TODO: Make this depend on tracker signal.
displayScheduler.insert(&m_taskUpdate, (cachedTime + 10 * 1000000).round_seconds());
displayScheduler.push(m_taskUpdate.prepare((cachedTime + 10 * 1000000).round_seconds()));
m_canvas->erase();
int pos = 0;
+3 -3
View File
@@ -38,6 +38,6 @@
#include "globals.h"
utils::TaskScheduler taskScheduler;
utils::TaskScheduler displayScheduler;
rak::timer cachedTime;
rak::priority_queue_default taskScheduler;
rak::priority_queue_default displayScheduler;
rak::timer cachedTime;
+4 -5
View File
@@ -38,11 +38,10 @@
#define TORRENT_GLOBALS_H
#include <rak/timer.h>
#include <rak/priority_queue_default.h>
#include "utils/task_scheduler.h"
extern utils::TaskScheduler taskScheduler;
extern utils::TaskScheduler displayScheduler;
extern rak::timer cachedTime;
extern rak::priority_queue_default taskScheduler;
extern rak::priority_queue_default displayScheduler;
extern rak::timer cachedTime;
#endif
+8 -6
View File
@@ -220,18 +220,20 @@ main(int argc, char** argv) {
countTicks++;
cachedTime = rak::timer::current();
taskScheduler.execute(cachedTime);
std::list<rak::priority_item*> workQueue;
std::copy(rak::queue_popper(taskScheduler, rak::priority_ready(cachedTime)), rak::queue_popper(), std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear_time));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::call));
// This needs to be called every second or so. Currently done by
// the throttle task in libtorrent.
if (!displayScheduler.empty() &&
displayScheduler.get_next_timeout() <= cachedTime)
if (!displayScheduler.empty() && displayScheduler.top()->time() <= cachedTime)
uiControl.display()->do_update();
// Do shutdown check before poll, not after.
uiControl.core()->get_poll_manager()->poll(!taskScheduler.empty() ?
taskScheduler.get_next_timeout() - cachedTime :
60 * 1000000);
uiControl.core()->get_poll_manager()->poll(!taskScheduler.empty() ? taskScheduler.top()->time() - cachedTime : 60 * 1000000);
}
uiControl.cleanup();
+2 -3
View File
@@ -85,7 +85,6 @@ DownloadList::DownloadList(Control* c) :
m_uiArray[DISPLAY_LOG] = new ElementLogComplete(&m_control->core()->get_log_complete());
m_windowLog = new WLog(&m_control->core()->get_log_important());
m_taskUpdate.set_iterator(taskScheduler.end());
m_taskUpdate.set_slot(rak::mem_fn(this, &DownloadList::task_update)),
setup_keys();
@@ -112,7 +111,7 @@ DownloadList::activate() {
if (is_active())
throw std::logic_error("ui::Download::activate() called on an already activated object");
taskScheduler.insert(&m_taskUpdate, cachedTime + 1000000);
taskScheduler.push(m_taskUpdate.prepare(cachedTime + 1000000));
m_windowTextInput->set_active(false);
@@ -304,7 +303,7 @@ void
DownloadList::task_update() {
m_windowLog->receive_update();
taskScheduler.insert(&m_taskUpdate, (cachedTime + 1000000).round_seconds());
taskScheduler.push(m_taskUpdate.prepare((cachedTime + 1000000).round_seconds()));
}
void
+1 -1
View File
@@ -138,7 +138,7 @@ private:
WInput* m_windowTextInput;
WHttp* m_windowHttpQueue;
utils::TaskItem m_taskUpdate;
rak::priority_item m_taskUpdate;
Download* m_uiDownload;
+1 -4
View File
@@ -7,9 +7,6 @@ libsub_utils_a_SOURCES = \
file_stat.h \
list_focus.h \
parse.cc \
parse.h \
task_item.h \
task_scheduler.cc \
task_scheduler.h
parse.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
-74
View File
@@ -1,74 +0,0 @@
// rTorrent - BitTorrent library
// Copyright (C) 2005, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_TASK_ITEM_H
#define RTORRENT_UTILS_TASK_ITEM_H
#include <list>
#include <rak/timer.h>
#include <rak/functional_fun.h>
namespace utils {
// The user is responsible for removing TaskItem from the TaskScheduler.
class TaskItem {
public:
typedef rak::function<void> Slot;
typedef std::list<std::pair<rak::timer, TaskItem*> >::iterator iterator;
TaskItem() {}
Slot& get_slot() { return m_slot; }
void set_slot(Slot s) { m_slot = s; }
iterator get_iterator() { return m_iterator; }
const iterator get_iterator() const { return m_iterator; }
void set_iterator(iterator itr) { m_iterator = itr; }
const rak::timer& get_time() const { return m_iterator->first; }
private:
TaskItem(const TaskItem& t);
TaskItem& operator = (const TaskItem& t);
iterator m_iterator;
Slot m_slot;
};
}
#endif
-93
View File
@@ -1,93 +0,0 @@
// rTorrent - BitTorrent library
// Copyright (C) 2005, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <stdexcept>
#include "rak/functional.h"
#include "task_scheduler.h"
namespace utils {
void
TaskScheduler::insert(TaskItem* task, rak::timer time) {
if (is_scheduled(task))
throw std::logic_error("TaskScheduler::insert(...) tried to insert an already inserted or invalid TaskItem");
// Only insert at or after m_entry because if we might be in
// execute(...).
iterator itr = std::find_if(m_entry, end(), rak::less_equal(time, rak::mem_ptr_ref(&value_type::first)));
task->set_iterator(Base::insert(itr, value_type(time, task)));
// Make sure m_entry points to the right node if we try inserting
// before m_entry.
if (itr == m_entry)
m_entry = task->get_iterator();
}
void
TaskScheduler::erase(TaskItem* task) {
if (!is_scheduled(task))
return;
iterator itr = Base::erase(task->get_iterator());
if (task->get_iterator() == m_entry)
m_entry = itr;
task->set_iterator(end());
}
void
TaskScheduler::execute(rak::timer time) {
m_entry = std::find_if(begin(), end(), rak::less(time, rak::mem_ptr_ref(&value_type::first)));
// Since we are always using the front rather than a splice of the
// due tasks, it is safe to erase them from within other tasks.
while (begin() != m_entry) {
if (!is_scheduled(Base::front().second))
throw std::logic_error("TaskScheduler::execute_task(iterator) received an invalid iterator");
Base::front().second->set_iterator(end());
Base::front().second->get_slot()();
Base::pop_front();
}
}
}
-78
View File
@@ -1,78 +0,0 @@
// rTorrent - BitTorrent library
// Copyright (C) 2005, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_TASK_SCHEDULER_H
#define RTORRENT_UTILS_TASK_SCHEDULER_H
#include "task_item.h"
namespace utils {
class TaskScheduler : private std::list<std::pair<rak::timer, TaskItem*> > {
public:
typedef std::list<std::pair<rak::timer, TaskItem*> > Base;
using Base::value_type;
using Base::reference;
using Base::iterator;
using Base::reverse_iterator;
using Base::size;
using Base::empty;
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
TaskScheduler() : m_entry(begin()) {}
void insert(TaskItem* task, rak::timer time);
void erase(TaskItem* task);
void execute(rak::timer time);
bool is_scheduled(const TaskItem* task) const { return task->get_iterator() != end(); }
rak::timer get_next_timeout() const { return begin()->first; }
private:
iterator m_entry;
};
}
#endif