Converted to new TaskScheduler.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@474 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-06-23 19:23:36 +00:00
parent bc27fd19cb
commit 088398d6d6
10 changed files with 187 additions and 161 deletions
+3 -2
View File
@@ -9,8 +9,9 @@ libsub_utils_a_SOURCES = \
parse.cc \
parse.h \
task.h \
task_schedule.cc \
task_schedule.h \
task_item.h \
task_scheduler.cc \
task_scheduler.h \
timer.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+5 -49
View File
@@ -1,4 +1,4 @@
// rTorrent - BitTorrent client
// rTorrent - BitTorrent library
// Copyright (C) 2005, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
@@ -23,56 +23,12 @@
#ifndef RTORRENT_UTILS_TASK_H
#define RTORRENT_UTILS_TASK_H
#include <sigc++/slot.h>
#include "timer.h"
#include "task_schedule.h"
#include "task_scheduler.h"
namespace utils {
class Task {
public:
typedef sigc::slot<void> Slot;
Task(Slot s = Slot()) : m_slot(s) { clear_iterator(); }
~Task() { remove(); }
bool is_scheduled() { return m_itr != TaskSchedule::end(); }
void set_slot(Slot s) { m_slot = s; }
Slot& get_slot() { return m_slot; }
Timer get_time() { return m_time; }
void insert(Timer t) {
remove();
m_time = t;
m_itr = TaskSchedule::insert(this);
}
void remove() {
if (m_itr != TaskSchedule::end()) {
TaskSchedule::erase(m_itr);
clear_iterator();
}
}
protected:
friend class TaskSchedule;
TaskSchedule::iterator get_iterator() { return m_itr; }
void clear_iterator() { m_itr = TaskSchedule::end(); }
private:
Task(const Task&);
void operator () (const Task&);
Timer m_time;
TaskSchedule::iterator m_itr;
Slot m_slot;
};
extern TaskScheduler taskScheduler;
}
#endif
+60
View File
@@ -0,0 +1,60 @@
// 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
//
// 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 <sigc++/slot.h>
#include "utils/timer.h"
namespace utils {
// The user is responsible for removing TaskItem from the TaskScheduler.
class TaskItem {
public:
typedef sigc::slot<void> Slot;
typedef std::list<std::pair<Timer, TaskItem*> >::iterator iterator;
TaskItem(Slot s = Slot()) : m_slot(s) {}
Slot& get_slot() { return m_slot; }
void set_slot(Slot s) { m_slot = s; }
iterator get_iterator() { return m_iterator; }
void set_iterator(iterator itr) { m_iterator = itr; }
const Timer& get_time() { return m_iterator->first; }
private:
TaskItem(const TaskItem& t);
TaskItem& operator = (const TaskItem& t);
iterator m_iterator;
Slot m_slot;
};
}
#endif
-76
View File
@@ -1,76 +0,0 @@
// rTorrent - BitTorrent client
// 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
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
#include <functional>
#include "task.h"
#include "task_schedule.h"
namespace utils {
TaskSchedule::Container TaskSchedule::m_container;
// Remove this, replace with stuff.
struct task_comp {
task_comp(Timer t) : m_time(t) {}
bool operator () (Task* t) {
return m_time <= t->get_time();
}
Timer m_time;
};
inline void
TaskSchedule::execute_task(Task* t) {
t->clear_iterator();
t->get_slot()();
}
void
TaskSchedule::perform(Timer t) {
Container c;
c.splice(c.begin(), c, m_container.begin(), std::find_if(m_container.begin(), m_container.end(), task_comp(t)));
std::for_each(c.begin(), c.end(), std::ptr_fun(&TaskSchedule::execute_task));
}
Timer
TaskSchedule::get_timeout() {
if (!m_container.empty())
return std::max(m_container.front()->get_time() - Timer::current(), Timer());
else
return Timer((int64_t)(1 << 30) * 1000000);
}
TaskSchedule::iterator
TaskSchedule::insert(Task* t) {
iterator itr = std::find_if(m_container.begin(), m_container.end(), task_comp(t->get_time()));
return m_container.insert(itr, t);
}
}
+72
View File
@@ -0,0 +1,72 @@
// 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
//
// 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 {
inline void
TaskScheduler::execute_task(const value_type& v) {
if (!is_scheduled(v.second))
throw std::logic_error("TaskScheduler::execute_task(iterator) received an invalid iterator");
v.second->set_iterator(end());
v.second->get_slot()();
}
void
TaskScheduler::insert(TaskItem* task, Timer time) {
if (is_scheduled(task))
throw std::logic_error("TaskScheduler::insert(...) tried to insert an already inserted or invalid TaskItem");
iterator itr = std::find_if(begin(), end(),
rak::less_equal(time, rak::mem_ptr_ref(&value_type::first)));
task->set_iterator(Base::insert(itr, value_type(time, task)));
}
void
TaskScheduler::erase(TaskItem* task) {
if (!is_scheduled(task))
return;
Base::erase(task->get_iterator());
task->set_iterator(end());
}
void
TaskScheduler::execute(Timer time) {
Base tmp;
tmp.splice(tmp.begin(), *this,
begin(), std::find_if(begin(), end(), rak::less_equal(time, rak::mem_ptr_ref(&value_type::first))));
std::for_each(tmp.begin(), tmp.end(),
rak::bind1st(std::mem_fun(&TaskScheduler::execute_task), this));
}
}
@@ -1,4 +1,4 @@
// rTorrent - BitTorrent client
// rTorrent - BitTorrent library
// Copyright (C) 2005, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
@@ -20,38 +20,41 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_TASK_SCHEDULE_H
#define RTORRENT_UTILS_TASK_SCHEDULE_H
#ifndef RTORRENT_UTILS_TASK_SCHEDULER_H
#define RTORRENT_UTILS_TASK_SCHEDULER_H
#include <list>
#include "timer.h"
#include "task_item.h"
namespace utils {
class Task;
class TaskSchedule {
class TaskScheduler : private std::list<std::pair<Timer, TaskItem*> > {
public:
friend class Task;
typedef std::list<std::pair<Timer, TaskItem*> > Base;
typedef std::list<Task*> Container;
typedef Container::iterator iterator;
using Base::value_type;
using Base::reference;
static void perform(Timer t);
using Base::iterator;
using Base::reverse_iterator;
using Base::size;
using Base::empty;
static Timer get_timeout();
using Base::begin;
using Base::end;
using Base::rbegin;
using Base::rend;
protected:
static iterator end() { return m_container.end(); }
void insert(TaskItem* task, Timer time);
void erase(TaskItem* task);
static iterator insert(Task* t);
static void erase(iterator itr) { m_container.erase(itr); }
void execute(Timer time);
bool is_scheduled(TaskItem* task) { return task->get_iterator() != end(); }
Timer get_next_timeout() const { return begin()->first; }
private:
static inline void execute_task(Task* t);
static Container m_container;
inline void execute_task(const value_type& v);
};
}