From 1c1993acef94f8468d1df46f5bbfb08d123ca701 Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Wed, 14 Dec 2011 13:38:16 +0900 Subject: [PATCH 1/5] Cleaned up code. --- src/thread_base.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thread_base.cc b/src/thread_base.cc index 7080f8b0..9a42dc0d 100644 --- a/src/thread_base.cc +++ b/src/thread_base.cc @@ -82,7 +82,7 @@ public: throw torrent::internal_error("Overflowed thread_queue."); __sync_bool_compare_and_swap(itr, NULL, v); - __sync_bool_compare_and_swap(&m_lock, 1, 0); + unlock(); } value_type* copy_and_clear(value_type* dest) { From 5b7a2b5e95e6594693bcfa41962868406e256751 Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Mon, 19 Dec 2011 03:19:54 +0900 Subject: [PATCH 2/5] Moved setting of PTHREAD related variables outside of the macro. --- configure.ac | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index fa1329a3..0d03d2f8 100644 --- a/configure.ac +++ b/configure.ac @@ -27,9 +27,10 @@ TORRENT_WITHOUT_NCURSESW() TORRENT_WITHOUT_STATVFS() TORRENT_WITHOUT_STATFS() -ACX_PTHREAD(CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"; - LIBS="$PTHREAD_LIBS $LIBS" -) +ACX_PTHREAD() +CFLAGS="$CFLAGS $PTHREAD_CFLAGS" +CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS" +LIBS="$PTHREAD_LIBS $LIBS" PKG_CHECK_MODULES(sigc, sigc++-2.0, CXXFLAGS="$CXXFLAGS $sigc_CFLAGS"; From 007d83e90452410c60579d8cffeae0f518dc3154 Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Mon, 19 Dec 2011 03:20:42 +0900 Subject: [PATCH 3/5] Moved code from ThreadBase to torrent::thread_base. --- src/thread_base.cc | 53 ++++++++++++---------------------------------- src/thread_base.h | 10 +++------ 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/src/thread_base.cc b/src/thread_base.cc index 9a42dc0d..c886456b 100644 --- a/src/thread_base.cc +++ b/src/thread_base.cc @@ -117,55 +117,21 @@ ThreadBase::~ThreadBase() { delete m_threadQueue; } -void -ThreadBase::start_thread() { - if (m_state != STATE_INITIALIZED || - pthread_create(&m_thread, NULL, (pthread_func)&ThreadBase::event_loop, this)) - throw torrent::internal_error("Failed to create thread."); -} - +// Move to libtorrent... void ThreadBase::stop_thread(ThreadBase* thread) { if (!thread->m_taskShutdown.is_queued()) priority_queue_insert(&thread->m_taskScheduler, &thread->m_taskShutdown, cachedTime); } -inline rak::timer -ThreadBase::client_next_timeout() { +int64_t +ThreadBase::next_timeout_usec() { if (m_taskScheduler.empty()) - return rak::timer::from_seconds(600); + return rak::timer::from_seconds(600).usec(); else if (m_taskScheduler.top()->time() <= cachedTime) return 0; else - return m_taskScheduler.top()->time() - cachedTime; -} - -void* -ThreadBase::event_loop(ThreadBase* thread) { - thread->m_state = STATE_ACTIVE; - - try { - - while (true) { - // Check for new queued items set by other threads. - if (!thread->m_threadQueue->empty()) - thread->call_queued_items(); - - rak::priority_queue_perform(&thread->m_taskScheduler, cachedTime); - - thread->m_poll->do_poll(thread->client_next_timeout().usec(), torrent::Poll::poll_worker_thread); - } - - } catch (torrent::shutdown_exception& e) { - acquire_global_lock(); - lt_log_print(torrent::LOG_THREAD_NOTICE, "Shutting down thread."); - release_global_lock(); - } - - thread->m_state = STATE_INACTIVE; - __sync_synchronize(); - - return NULL; + return (m_taskScheduler.top()->time() - cachedTime).usec(); } void @@ -178,6 +144,15 @@ ThreadBase::call_queued_items() { (*first++)(this); } +void +ThreadBase::call_events() { + // Check for new queued items set by other threads. + if (!m_threadQueue->empty()) + call_queued_items(); + + rak::priority_queue_perform(&m_taskScheduler, cachedTime); +} + void ThreadBase::queue_item(thread_base_func newFunc) { m_threadQueue->push_back(newFunc); diff --git a/src/thread_base.h b/src/thread_base.h index 201a02bb..4cd38de5 100644 --- a/src/thread_base.h +++ b/src/thread_base.h @@ -54,16 +54,13 @@ class ThreadBase : public torrent::thread_base { public: typedef rak::priority_queue_default priority_queue; typedef void (*thread_base_func)(ThreadBase*); - typedef void* (*pthread_func)(void*); ThreadBase(); virtual ~ThreadBase(); priority_queue& task_scheduler() { return m_taskScheduler; } - virtual void init_thread() = 0; - - void start_thread(); + // Throw torrent::shutdown_exception to stop the thread. static void stop_thread(ThreadBase* thread); // ATM, only interaction with a thread's allowed by other threads is @@ -71,8 +68,6 @@ public: void queue_item(thread_base_func newFunc); - static void* event_loop(ThreadBase* thread); - // Only call this when global lock has been acquired, as it checks // ThreadBase::is_main_polling() which is only guaranteed to remain // 'false' if global lock keeps main thread from entering polling @@ -82,9 +77,10 @@ public: static void interrupt_main_polling(); protected: - inline rak::timer client_next_timeout(); + int64_t next_timeout_usec(); void call_queued_items(); + virtual void call_events(); // TODO: Add thread name. From 28634f2c8c2d710eaba87c86949899f8097f522f Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Wed, 21 Dec 2011 01:54:29 +0900 Subject: [PATCH 4/5] Added slot for poll creation. --- src/core/poll_manager.cc | 4 ++-- src/core/poll_manager.h | 8 ++++---- src/main.cc | 2 ++ src/thread_main.cc | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/core/poll_manager.cc b/src/core/poll_manager.cc index e9f64afc..1023b0f1 100644 --- a/src/core/poll_manager.cc +++ b/src/core/poll_manager.cc @@ -38,7 +38,7 @@ #include #include -#include +#include #include #include #include @@ -85,7 +85,7 @@ create_poll() { log->push_front("Using 'select' based polling."); else - throw std::runtime_error("Could not create any Poll object."); + throw torrent::internal_error("Could not create any Poll object."); return poll; } diff --git a/src/core/poll_manager.h b/src/core/poll_manager.h index cfd40fc6..bdbf5a0e 100644 --- a/src/core/poll_manager.h +++ b/src/core/poll_manager.h @@ -37,12 +37,12 @@ #ifndef RTORRENT_CORE_POLL_MANAGER_H #define RTORRENT_CORE_POLL_MANAGER_H -#include -#include -#include - #include "curl_stack.h" +namespace torrent { +class Poll; +} + namespace core { torrent::Poll* create_poll(); diff --git a/src/main.cc b/src/main.cc index 5dfdc987..6c85dc44 100644 --- a/src/main.cc +++ b/src/main.cc @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -195,6 +196,7 @@ main(int argc, char** argv) { // to process new non-socket events. SignalHandler::set_handler(SIGUSR1, sigc::ptr_fun(&do_nothing)); + torrent::Poll::slot_create_poll() = std::tr1::bind(&core::create_poll); torrent::initialize(main_thread->poll()); // Initialize option handlers after libtorrent to ensure diff --git a/src/thread_main.cc b/src/thread_main.cc index 47ffa25f..09c78989 100644 --- a/src/thread_main.cc +++ b/src/thread_main.cc @@ -40,6 +40,7 @@ #include "globals.h" #include +#include ThreadMain::~ThreadMain() { } From 4c29d3b71447a90ae30eafc56cb1f4a0bc35de3f Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Mon, 26 Dec 2011 14:32:45 +0900 Subject: [PATCH 5/5] Removed extra semicolon. --- src/command_download.cc | 4 ++-- src/display/text_element_value.cc | 2 +- src/rpc/fixed_key.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/command_download.cc b/src/command_download.cc index 91c35443..558d60af 100644 --- a/src/command_download.cc +++ b/src/command_download.cc @@ -849,8 +849,8 @@ initialize_command_download() { // std::bind(&core::Download::main, std::placeholders::_1)), // CG_GROUP_INDEX())); - CMD2_DL ("d.group", std::bind(&cg_d_group, std::placeholders::_1));; - CMD2_DL ("d.group.name", std::bind(&cg_d_group, std::placeholders::_1));; + CMD2_DL ("d.group", std::bind(&cg_d_group, std::placeholders::_1)); + CMD2_DL ("d.group.name", std::bind(&cg_d_group, std::placeholders::_1)); CMD2_DL_V ("d.group.set", std::bind(&cg_d_group_set, std::placeholders::_1, std::placeholders::_2)); CMD2_DL ("d.initialize_logs", std::bind(&cmd_d_initialize_logs, std::placeholders::_1)); diff --git a/src/display/text_element_value.cc b/src/display/text_element_value.cc index fe2d4674..bee86a49 100644 --- a/src/display/text_element_value.cc +++ b/src/display/text_element_value.cc @@ -112,7 +112,7 @@ TextElementValueBase::print(char* first, char* last, Canvas::attributes_list* at if (u == NULL) return first; - first += std::min(std::max(snprintf(first, last - first + 1, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)), 0), last - first + 1);; + first += std::min(std::max(snprintf(first, last - first + 1, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)), 0), last - first + 1); } else if (m_flags & flag_time) { time_t t = val; diff --git a/src/rpc/fixed_key.h b/src/rpc/fixed_key.h index 91b29d5a..4261ab2c 100644 --- a/src/rpc/fixed_key.h +++ b/src/rpc/fixed_key.h @@ -63,7 +63,7 @@ public: static fixed_key_type from_string(const std::string& str) { fixed_key_type k; k.set_c_str(str.c_str(), str.size()); return k; } static fixed_key_type from_raw_string(const torrent::raw_string& str) { fixed_key_type k; k.set_data(str.data(), str.size()); return k; } - bool empty() const { return m_size == 0;; } + bool empty() const { return m_size == 0; } size_type size() const { return m_size; } iterator begin() const { return m_data; }