* Finished the foundation of threading.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1114 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2009-12-15 21:39:35 +00:00
parent 6a94909647
commit 2304983f4f
6 changed files with 15 additions and 53 deletions
+6 -1
View File
@@ -44,6 +44,7 @@
#include <torrent/torrent.h>
#include "poll_manager_epoll.h"
#include "thread_base.h"
namespace core {
@@ -67,7 +68,11 @@ PollManagerEPoll::poll(rak::timer timeout) {
torrent::perform();
timeout = std::min(timeout, rak::timer(torrent::next_timeout())) + 1000;
if (static_cast<torrent::PollEPoll*>(m_poll)->poll((timeout.usec() + 999) / 1000) == -1)
ThreadBase::release_global_lock();
int status = static_cast<torrent::PollEPoll*>(m_poll)->poll((timeout.usec() + 999) / 1000);
ThreadBase::acquire_global_lock();
if (status == -1)
return check_error();
torrent::perform();
+6 -1
View File
@@ -45,6 +45,7 @@
#include <torrent/torrent.h>
#include "poll_manager_kqueue.h"
#include "thread_base.h"
namespace core {
@@ -68,7 +69,11 @@ PollManagerKQueue::poll(rak::timer timeout) {
torrent::perform();
timeout = std::min(timeout, rak::timer(torrent::next_timeout())) + 1000;
if (static_cast<torrent::PollKQueue*>(m_poll)->poll((timeout.usec() + 999) / 1000) == -1)
ThreadBase::release_global_lock();
int status = static_cast<torrent::PollKQueue*>(m_poll)->poll((timeout.usec() + 999) / 1000);
ThreadBase::acquire_global_lock();
if (status == -1)
return check_error();
torrent::perform();
+1 -1
View File
@@ -95,7 +95,7 @@ PollManagerSelect::poll(rak::timer timeout) {
ThreadBase::release_global_lock();
int status = select(maxFd + 1, m_readSet, m_writeSet, m_errorSet, &t);
ThreadBase::release_global_lock();
ThreadBase::acquire_global_lock();
if (status == -1)
return check_error();
-6
View File
@@ -45,11 +45,6 @@
#include "globals.h"
// A preliminary implementation of a global lock, to be moved
// somewhere more appropriate when it's put to use.
ThreadBase::global_lock_type ThreadBase::m_global = { 0, PTHREAD_MUTEX_INITIALIZER };
// Temporarly injected into config.h.
/* temp hack */
//#define __cacheline_aligned __attribute__((__aligned__(128)))
@@ -115,7 +110,6 @@ ThreadBase::ThreadBase() :
}
ThreadBase::~ThreadBase() {
pthread_mutex_destroy(&ThreadBase::m_global.lock);
}
void
+2 -40
View File
@@ -39,6 +39,7 @@
#include <pthread.h>
#include <sys/types.h>
#include <torrent/thread_base.h>
#include "rak/priority_queue_default.h"
#include "core/poll_manager.h"
@@ -49,7 +50,7 @@ struct thread_queue_hack;
struct thread_queue_hack;
class ThreadBase {
class ThreadBase : public torrent::ThreadBase {
public:
typedef rak::priority_queue_default priority_queue;
typedef void (*thread_base_func)(ThreadBase*);
@@ -80,12 +81,6 @@ public:
static void* event_loop(ThreadBase* threadBase);
static inline int global_queue_size() { return m_global.waiting; }
static inline void acquire_global_lock();
static inline void release_global_lock();
static inline void waive_global_lock();
protected:
inline rak::timer client_next_timeout();
@@ -102,39 +97,6 @@ protected:
// Temporary hack to pass messages to a thread. This really needs to
// be cleaned up and/or integrated into the priority queue itself.
thread_queue_hack* m_threadQueue;
struct __cacheline_aligned global_lock_type {
int waiting;
pthread_mutex_t lock;
};
static global_lock_type m_global;
};
inline void
ThreadBase::acquire_global_lock() {
__sync_add_and_fetch(&ThreadBase::m_global.waiting, 1);
pthread_mutex_lock(&ThreadBase::m_global.lock);
// if (pthread_mutex_lock(&ThreadBase::m_global.lock))
// throw internal_error("Mutex failed.");
__sync_fetch_and_sub(&ThreadBase::m_global.waiting, 1);
}
inline void
ThreadBase::release_global_lock() {
pthread_mutex_unlock(&ThreadBase::m_global.lock);
}
inline void
ThreadBase::waive_global_lock() {
__sync_synchronize();
pthread_mutex_unlock(&ThreadBase::m_global.lock);
// Do we need to sleep here? Make a CppUnit test for this.
acquire_global_lock();
}
#endif
-4
View File
@@ -72,10 +72,6 @@ ThreadWorker::task_touch_log() {
priority_queue_insert(&m_taskScheduler, &m_taskTouchLog, cachedTime + rak::timer::from_seconds(1));
acquire_global_lock();
__sync_synchronize();
control->core()->push_log("Tick Tock.");
__sync_synchronize();
release_global_lock();
}