* Foundation for multi-threaded support.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1111 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2009-12-12 15:32:04 +00:00
parent d71e0ccf9a
commit ef6e4deca9
12 changed files with 254 additions and 123 deletions
+76 -63
View File
@@ -47,63 +47,64 @@
/* temp hack */
//#define __cacheline_aligned __attribute__((__aligned__(128)))
// class __cacheline_aligned thread_queue_hack {
// public:
// typedef ThreadBase::thread_base_func value_type;
// typedef ThreadBase::thread_base_func* iterator;
class __cacheline_aligned thread_queue_hack {
public:
typedef ThreadBase::thread_base_func value_type;
typedef ThreadBase::thread_base_func* iterator;
// static const unsigned int max_size = 32;
static const unsigned int max_size = 32;
// thread_queue_hack() { std::memset(m_queue, 0, sizeof(thread_queue_hack)); }
thread_queue_hack() { std::memset(m_queue, 0, sizeof(thread_queue_hack)); }
// void lock() { while (!__sync_bool_compare_and_swap(&m_lock, 0, 1)) usleep(0); }
// void unlock() { __sync_bool_compare_and_swap(&m_lock, 1, 0); }
void lock() { while (!__sync_bool_compare_and_swap(&m_lock, 0, 1)) usleep(0); }
void unlock() { __sync_bool_compare_and_swap(&m_lock, 1, 0); }
// iterator begin() { return m_queue; }
// iterator max_capacity() { return m_queue + max_size; }
iterator begin() { return m_queue; }
iterator max_capacity() { return m_queue + max_size; }
// iterator end_and_lock() { lock(); return std::find(begin(), max_capacity(), (value_type)NULL); }
iterator end_and_lock() { lock(); return std::find(begin(), max_capacity(), (value_type)NULL); }
// bool empty() const { return m_queue[0] == NULL; }
bool empty() const { return m_queue[0] == NULL; }
// void push_back(value_type v) {
// iterator itr = end_and_lock();
void push_back(value_type v) {
iterator itr = end_and_lock();
// if (itr == max_capacity())
// throw torrent::internal_error("Overflowed thread_queue.");
if (itr == max_capacity())
throw torrent::internal_error("Overflowed thread_queue.");
// __sync_bool_compare_and_swap(itr, NULL, v);
// __sync_bool_compare_and_swap(&m_lock, 1, 0);
// }
__sync_bool_compare_and_swap(itr, NULL, v);
__sync_bool_compare_and_swap(&m_lock, 1, 0);
}
// value_type* copy_and_clear(value_type* dest) {
// iterator itr = begin();
// lock();
value_type* copy_and_clear(value_type* dest) {
iterator itr = begin();
lock();
// while (*itr != NULL) *++dest = *++itr;
while (*itr != NULL) *++dest = *++itr;
// clear_and_unlock();
// return dest;
// }
clear_and_unlock();
return dest;
}
// void clear_and_unlock() {
// std::memset(m_queue, 0, sizeof(value_type) * (max_size + 1));
// m_lock = 0;
// __sync_synchronize();
// }
void clear_and_unlock() {
std::memset(m_queue, 0, sizeof(value_type) * (max_size + 1));
m_lock = 0;
__sync_synchronize();
}
// private:
// int m_lock;
// value_type m_queue[max_size + 1];
// };
private:
int m_lock;
value_type m_queue[max_size + 1];
};
ThreadBase::ThreadBase()
: m_pollManager(NULL) {
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.
// m_threadQueue = new thread_queue_hack;
m_threadQueue = new thread_queue_hack;
}
ThreadBase::~ThreadBase() {
@@ -118,38 +119,50 @@ ThreadBase::init_thread() {
m_pollManager = core::PollManager::create_poll_manager();
}
// void*
// ThreadBase::event_loop() {
// // Setup stuff...
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.");
}
// // Set local poll and priority queue.
void
ThreadBase::stop_thread() {
}
void*
ThreadBase::event_loop(ThreadBase* threadBase) {
// Setup stuff...
threadBase->m_state = STATE_ACTIVE;
// Set local poll and priority queue.
// while (true) {
// // Check for new queued items set by other threads.
// if (!m_threadQueue->empty())
// call_queued_items();
while (true) {
// Check for new queued items set by other threads.
if (!threadBase->m_threadQueue->empty())
threadBase->call_queued_items();
// // Remember to add global lock thing to the main poll loop ++.
// rak::priority_queue_perform(&taskScheduler, cachedTime);
// rak::priority_queue_perform(&threadBase->m_taskScheduler, cachedTime);
// m_pollManager->poll_simple(rak::timer::from_seconds(10));
// }
threadBase->m_pollManager->poll_simple(rak::timer::from_seconds(10));
}
// return NULL;
// }
return NULL;
}
// void
// ThreadBase::call_queued_items() {
// thread_base_func result[thread_queue_hack::max_size];
// thread_base_func* first = result;
// thread_base_func* last = m_threadQueue->copy_and_clear((thread_base_func*)result);
void
ThreadBase::call_queued_items() {
thread_base_func result[thread_queue_hack::max_size];
thread_base_func* first = result;
thread_base_func* last = m_threadQueue->copy_and_clear((thread_base_func*)result);
// while (first != last)
// (*first)(this);
// }
while (first != last)
(*first)(this);
}
// void
// ThreadBase::queue_item(thread_base_func newFunc) {
// m_threadQueue->push_back(newFunc);
// }
void
ThreadBase::queue_item(thread_base_func newFunc) {
m_threadQueue->push_back(newFunc);
}