mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-21 16:46:18 +00:00
Moved code from ThreadBase to torrent::thread_base.
This commit is contained in:
+14
-39
@@ -117,55 +117,21 @@ ThreadBase::~ThreadBase() {
|
|||||||
delete m_threadQueue;
|
delete m_threadQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
// Move to libtorrent...
|
||||||
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.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ThreadBase::stop_thread(ThreadBase* thread) {
|
ThreadBase::stop_thread(ThreadBase* thread) {
|
||||||
if (!thread->m_taskShutdown.is_queued())
|
if (!thread->m_taskShutdown.is_queued())
|
||||||
priority_queue_insert(&thread->m_taskScheduler, &thread->m_taskShutdown, cachedTime);
|
priority_queue_insert(&thread->m_taskScheduler, &thread->m_taskShutdown, cachedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline rak::timer
|
int64_t
|
||||||
ThreadBase::client_next_timeout() {
|
ThreadBase::next_timeout_usec() {
|
||||||
if (m_taskScheduler.empty())
|
if (m_taskScheduler.empty())
|
||||||
return rak::timer::from_seconds(600);
|
return rak::timer::from_seconds(600).usec();
|
||||||
else if (m_taskScheduler.top()->time() <= cachedTime)
|
else if (m_taskScheduler.top()->time() <= cachedTime)
|
||||||
return 0;
|
return 0;
|
||||||
else
|
else
|
||||||
return m_taskScheduler.top()->time() - cachedTime;
|
return (m_taskScheduler.top()->time() - cachedTime).usec();
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -178,6 +144,15 @@ ThreadBase::call_queued_items() {
|
|||||||
(*first++)(this);
|
(*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
|
void
|
||||||
ThreadBase::queue_item(thread_base_func newFunc) {
|
ThreadBase::queue_item(thread_base_func newFunc) {
|
||||||
m_threadQueue->push_back(newFunc);
|
m_threadQueue->push_back(newFunc);
|
||||||
|
|||||||
+3
-7
@@ -54,16 +54,13 @@ class ThreadBase : public torrent::thread_base {
|
|||||||
public:
|
public:
|
||||||
typedef rak::priority_queue_default priority_queue;
|
typedef rak::priority_queue_default priority_queue;
|
||||||
typedef void (*thread_base_func)(ThreadBase*);
|
typedef void (*thread_base_func)(ThreadBase*);
|
||||||
typedef void* (*pthread_func)(void*);
|
|
||||||
|
|
||||||
ThreadBase();
|
ThreadBase();
|
||||||
virtual ~ThreadBase();
|
virtual ~ThreadBase();
|
||||||
|
|
||||||
priority_queue& task_scheduler() { return m_taskScheduler; }
|
priority_queue& task_scheduler() { return m_taskScheduler; }
|
||||||
|
|
||||||
virtual void init_thread() = 0;
|
// Throw torrent::shutdown_exception to stop the thread.
|
||||||
|
|
||||||
void start_thread();
|
|
||||||
static void stop_thread(ThreadBase* thread);
|
static void stop_thread(ThreadBase* thread);
|
||||||
|
|
||||||
// ATM, only interaction with a thread's allowed by other threads is
|
// ATM, only interaction with a thread's allowed by other threads is
|
||||||
@@ -71,8 +68,6 @@ public:
|
|||||||
|
|
||||||
void queue_item(thread_base_func newFunc);
|
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
|
// Only call this when global lock has been acquired, as it checks
|
||||||
// ThreadBase::is_main_polling() which is only guaranteed to remain
|
// ThreadBase::is_main_polling() which is only guaranteed to remain
|
||||||
// 'false' if global lock keeps main thread from entering polling
|
// 'false' if global lock keeps main thread from entering polling
|
||||||
@@ -82,9 +77,10 @@ public:
|
|||||||
static void interrupt_main_polling();
|
static void interrupt_main_polling();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline rak::timer client_next_timeout();
|
int64_t next_timeout_usec();
|
||||||
|
|
||||||
void call_queued_items();
|
void call_queued_items();
|
||||||
|
virtual void call_events();
|
||||||
|
|
||||||
// TODO: Add thread name.
|
// TODO: Add thread name.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user