From 89213982d5b38b2d8ad9d7b2ccc38a515df8ac6f Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Mon, 12 Dec 2011 00:03:50 +0900 Subject: [PATCH] Moving thread code to libtorrent. --- src/thread_base.cc | 25 +++++++------------------ src/thread_base.h | 19 +++---------------- src/thread_main.cc | 1 + src/thread_worker.cc | 1 + 4 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/thread_base.cc b/src/thread_base.cc index 936f382c..4996e223 100644 --- a/src/thread_base.cc +++ b/src/thread_base.cc @@ -108,13 +108,7 @@ public: void throw_shutdown_exception() { throw torrent::shutdown_exception(); } ThreadBase::ThreadBase() : - m_state(STATE_UNKNOWN), m_pollManager(NULL) { - // Init the poll manager in a special init function called by the - // thread itself. Need to be careful with what external stuff - // create_poll_manager calls in that case. - std::memset(&m_thread, 0, sizeof(pthread_t)); - m_taskShutdown.set_slot(rak::ptr_fn(&throw_shutdown_exception)); m_threadQueue = new thread_queue_hack; @@ -149,24 +143,19 @@ ThreadBase::client_next_timeout() { } void* -ThreadBase::event_loop(ThreadBase* threadBase) { - // Setup stuff... - threadBase->m_state = STATE_ACTIVE; - - // Set local poll and priority queue. +ThreadBase::event_loop(ThreadBase* thread) { + thread->m_state = STATE_ACTIVE; try { while (true) { // Check for new queued items set by other threads. - if (!threadBase->m_threadQueue->empty()) - threadBase->call_queued_items(); + if (!thread->m_threadQueue->empty()) + thread->call_queued_items(); - // // Remember to add global lock thing to the main poll loop ++. + rak::priority_queue_perform(&thread->m_taskScheduler, cachedTime); - rak::priority_queue_perform(&threadBase->m_taskScheduler, cachedTime); - - threadBase->m_pollManager->poll_simple(threadBase->client_next_timeout()); + thread->m_pollManager->poll_simple(thread->client_next_timeout()); } } catch (torrent::shutdown_exception& e) { @@ -175,7 +164,7 @@ ThreadBase::event_loop(ThreadBase* threadBase) { release_global_lock(); } - threadBase->m_state = STATE_INACTIVE; + thread->m_state = STATE_INACTIVE; __sync_synchronize(); return NULL; diff --git a/src/thread_base.h b/src/thread_base.h index 8b4013c2..0698d9d8 100644 --- a/src/thread_base.h +++ b/src/thread_base.h @@ -39,7 +39,7 @@ #include #include -#include +#include #include "rak/priority_queue_default.h" #include "core/poll_manager.h" @@ -50,25 +50,15 @@ struct thread_queue_hack; struct thread_queue_hack; -class ThreadBase : public torrent::ThreadBase { +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*); - enum state_type { - STATE_UNKNOWN, - STATE_INITIALIZED, - STATE_ACTIVE, - STATE_INACTIVE - }; - ThreadBase(); virtual ~ThreadBase(); - bool is_active() const { return m_state == STATE_ACTIVE; } - - torrent::Poll* poll() { return m_pollManager->get_torrent_poll(); } core::PollManager* poll_manager() { return m_pollManager; } priority_queue& task_scheduler() { return m_taskScheduler; } @@ -82,7 +72,7 @@ public: void queue_item(thread_base_func newFunc); - static void* event_loop(ThreadBase* threadBase); + 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 @@ -99,9 +89,6 @@ protected: // TODO: Add thread name. - pthread_t m_thread; - state_type m_state; - // The timer needs to be sync'ed when updated... core::PollManager* m_pollManager; diff --git a/src/thread_main.cc b/src/thread_main.cc index b87cae52..d41997b7 100644 --- a/src/thread_main.cc +++ b/src/thread_main.cc @@ -51,6 +51,7 @@ ThreadMain::init_thread() { m_pollManager = core::PollManager::create_poll_manager(); m_pollManager->get_torrent_poll()->set_flags(torrent::Poll::flag_waive_global_lock); + m_poll = m_pollManager->get_torrent_poll(); m_state = STATE_INITIALIZED; m_thread = pthread_self(); diff --git a/src/thread_worker.cc b/src/thread_worker.cc index cdfe3323..e955e10e 100644 --- a/src/thread_worker.cc +++ b/src/thread_worker.cc @@ -63,6 +63,7 @@ ThreadWorker::~ThreadWorker() { void ThreadWorker::init_thread() { m_pollManager = core::PollManager::create_poll_manager(); + m_poll = m_pollManager->get_torrent_poll(); m_state = STATE_INITIALIZED; }