diff --git a/src/core/poll_manager_epoll.cc b/src/core/poll_manager_epoll.cc index e8cd4f12..b0043b3d 100644 --- a/src/core/poll_manager_epoll.cc +++ b/src/core/poll_manager_epoll.cc @@ -44,6 +44,7 @@ #include #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(m_poll)->poll((timeout.usec() + 999) / 1000) == -1) + ThreadBase::release_global_lock(); + int status = static_cast(m_poll)->poll((timeout.usec() + 999) / 1000); + ThreadBase::acquire_global_lock(); + + if (status == -1) return check_error(); torrent::perform(); diff --git a/src/core/poll_manager_kqueue.cc b/src/core/poll_manager_kqueue.cc index 1c6e5197..e61fa2fd 100644 --- a/src/core/poll_manager_kqueue.cc +++ b/src/core/poll_manager_kqueue.cc @@ -45,6 +45,7 @@ #include #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(m_poll)->poll((timeout.usec() + 999) / 1000) == -1) + ThreadBase::release_global_lock(); + int status = static_cast(m_poll)->poll((timeout.usec() + 999) / 1000); + ThreadBase::acquire_global_lock(); + + if (status == -1) return check_error(); torrent::perform(); diff --git a/src/core/poll_manager_select.cc b/src/core/poll_manager_select.cc index 6decb9b7..47a148b2 100644 --- a/src/core/poll_manager_select.cc +++ b/src/core/poll_manager_select.cc @@ -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(); diff --git a/src/thread_base.cc b/src/thread_base.cc index 3235cd40..ebdce394 100644 --- a/src/thread_base.cc +++ b/src/thread_base.cc @@ -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 diff --git a/src/thread_base.h b/src/thread_base.h index d8249215..51667519 100644 --- a/src/thread_base.h +++ b/src/thread_base.h @@ -39,6 +39,7 @@ #include #include +#include #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 diff --git a/src/thread_worker.cc b/src/thread_worker.cc index eeceb8d8..073ab9ee 100644 --- a/src/thread_worker.cc +++ b/src/thread_worker.cc @@ -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(); }