Task Scheduler, log viewer pops up and disapperes. good good

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@308 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-02-26 21:58:59 +00:00
parent 9fe97ee490
commit ef3a8fcd83
15 changed files with 221 additions and 49 deletions
+3
View File
@@ -6,6 +6,9 @@ libsub_utils_a_SOURCES = \
functional.h \
parse.cc \
parse.h \
task.h \
task_schedule.cc \
task_schedule.h \
timer.h
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
+56
View File
@@ -0,0 +1,56 @@
#ifndef RTORRENT_UTILS_TASK_H
#define RTORRENT_UTILS_TASK_H
#include <sigc++/slot.h>
#include "timer.h"
#include "task_schedule.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;
};
}
#endif
+54
View File
@@ -0,0 +1,54 @@
#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);
}
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef RTORRENT_UTILS_TASK_SCHEDULE_H
#define RTORRENT_UTILS_TASK_SCHEDULE_H
#include <list>
#include "timer.h"
namespace utils {
class Task;
class TaskSchedule {
public:
friend class Task;
typedef std::list<Task*> Container;
typedef Container::iterator iterator;
static void perform(Timer t);
static Timer get_timeout();
protected:
static iterator end() { return m_container.end(); }
static iterator insert(Task* t);
static void erase(iterator itr) { m_container.erase(itr); }
private:
static inline void execute_task(Task* t);
static Container m_container;
};
}
#endif