From abbe4dd6752029fc47eeb6fbec48eea6439889ee Mon Sep 17 00:00:00 2001 From: rakshasa Date: Thu, 18 Aug 2005 00:46:54 +0000 Subject: [PATCH] * Changed the TaskScheduler so new tasks are inserted beyond the last due task when executing. * Moved stuff from torrent/torrent.cc to torrent::Manager. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@535 e378c898-3ddf-0310-93e7-cc216c733640 --- configure.ac | 4 ++-- src/utils/task_scheduler.cc | 41 ++++++++++++++++++++++--------------- src/utils/task_scheduler.h | 4 +++- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 872aa864..e72c22df 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT(rtorrent, 0.3.2, jaris@ifi.uio.no) +AC_INIT(rtorrent, 0.3.3, jaris@ifi.uio.no) AM_INIT_AUTOMAKE AM_CONFIG_HEADER(config.h) @@ -22,7 +22,7 @@ TORRENT_OTFD() TORRENT_WITHOUT_VARIABLE_FDSET() -PKG_CHECK_MODULES(STUFF, sigc++-2.0 libtorrent >= 0.7.2, +PKG_CHECK_MODULES(STUFF, sigc++-2.0 libtorrent >= 0.7.3, CXXFLAGS="$CXXFLAGS $STUFF_CFLAGS $CURL_CFLAGS"; LIBS="$LIBS $STUFF_LIBS $CURL_LIBS") diff --git a/src/utils/task_scheduler.cc b/src/utils/task_scheduler.cc index ffcc6e53..122410d8 100644 --- a/src/utils/task_scheduler.cc +++ b/src/utils/task_scheduler.cc @@ -43,24 +43,21 @@ 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))); + // 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 @@ -68,19 +65,29 @@ TaskScheduler::erase(TaskItem* task) { if (!is_scheduled(task)) return; - Base::erase(task->get_iterator()); + iterator itr = Base::erase(task->get_iterator()); + + if (task->get_iterator() == m_entry) + m_entry = itr; + task->set_iterator(end()); } void TaskScheduler::execute(Timer time) { - Base tmp; + m_entry = std::find_if(begin(), end(), rak::less_equal(time, rak::mem_ptr_ref(&value_type::first))); - tmp.splice(tmp.begin(), *this, - begin(), std::find_if(begin(), end(), rak::less_equal(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()(); - std::for_each(tmp.begin(), tmp.end(), - rak::bind1st(std::mem_fun(&TaskScheduler::execute_task), this)); + Base::pop_front(); + } } } diff --git a/src/utils/task_scheduler.h b/src/utils/task_scheduler.h index b829e587..f74ab4a7 100644 --- a/src/utils/task_scheduler.h +++ b/src/utils/task_scheduler.h @@ -58,6 +58,8 @@ public: using Base::rbegin; using Base::rend; + TaskScheduler() : m_entry(begin()) {} + void insert(TaskItem* task, Timer time); void erase(TaskItem* task); @@ -68,7 +70,7 @@ public: Timer get_next_timeout() const { return begin()->first; } private: - inline void execute_task(const value_type& v); + iterator m_entry; }; }