aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-08-21 15:47:20 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2007-08-21 15:47:20 +0000
commitce05f6b7ce5a001829ee6adf718f494125d7ce95 (patch)
tree257317f152c56872db66a3360be46365f763d9f2
parent120b0dd20cba3d4d9992a84728b12f91accecd10 (diff)
downloadpaludis-ce05f6b7ce5a001829ee6adf718f494125d7ce95.tar.gz
paludis-ce05f6b7ce5a001829ee6adf718f494125d7ce95.tar.xz
Change parallel_for_each
-rw-r--r--paludis/util/action_queue.cc10
-rw-r--r--paludis/util/action_queue.hh2
-rw-r--r--paludis/util/parallel_for_each.hh34
-rw-r--r--paludis/util/thread_pool.cc10
-rw-r--r--paludis/util/thread_pool.hh1
5 files changed, 52 insertions, 5 deletions
diff --git a/paludis/util/action_queue.cc b/paludis/util/action_queue.cc
index 82b51a1..551dafd 100644
--- a/paludis/util/action_queue.cc
+++ b/paludis/util/action_queue.cc
@@ -144,3 +144,13 @@ ActionQueue::forget_pending()
#endif
}
+unsigned
+ActionQueue::number_of_threads() const
+{
+#ifdef PALUDIS_ENABLE_THREADS
+ return _imp->threads.number_of_threads();
+#else
+ return 0;
+#endif
+}
+
diff --git a/paludis/util/action_queue.hh b/paludis/util/action_queue.hh
index 7b7684b..56571e3 100644
--- a/paludis/util/action_queue.hh
+++ b/paludis/util/action_queue.hh
@@ -35,6 +35,8 @@ namespace paludis
void enqueue(const tr1::function<void () throw ()> &);
void complete_pending();
void forget_pending();
+
+ unsigned number_of_threads() const;
};
}
diff --git a/paludis/util/parallel_for_each.hh b/paludis/util/parallel_for_each.hh
index 4bc5416..a1eccbe 100644
--- a/paludis/util/parallel_for_each.hh
+++ b/paludis/util/parallel_for_each.hh
@@ -25,14 +25,42 @@
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/tr1_memory.hh>
+#ifndef PALUDIS_ENABLE_THREADS
+# include <algorithm>
+#endif
+
namespace paludis
{
template <typename I_, typename P_>
- void parallel_for_each(I_ cur, const I_ & end, const P_ & op)
+ void parallel_for_each_worker(I_ cur, const I_ & end, const unsigned partition_size, const P_ & op)
{
+ while (cur != end)
+ {
+ op(*cur);
+
+ for (unsigned x(0) ; x < partition_size ; ++x, ++cur)
+ if (cur == end)
+ break;
+ }
+ }
+
+ template <typename I_, typename P_>
+ void parallel_for_each(I_ cur, const I_ & end, const P_ & op,
+ const unsigned partition_size = FutureActionQueue::get_instance()->number_of_threads())
+ {
+#ifdef PALUDIS_ENABLE_THREADS
Sequence<tr1::shared_ptr<Future<void> > > futures;
- for ( ; cur != end ; ++cur)
- futures.push_back(make_shared_ptr(new Future<void>(tr1::bind(op, tr1::ref(*cur)))));
+ for (unsigned x(0) ; x < partition_size ; ++x, ++cur)
+ {
+ if (cur == end)
+ break;
+
+ futures.push_back(make_shared_ptr(new Future<void>(
+ tr1::bind(parallel_for_each_worker<I_, P_>, I_(cur), tr1::cref(end), partition_size, tr1::cref(op)))));
+ }
+#else
+ std::for_each(cur, end, op);
+#endif
}
}
diff --git a/paludis/util/thread_pool.cc b/paludis/util/thread_pool.cc
index e009a9f..58f1e86 100644
--- a/paludis/util/thread_pool.cc
+++ b/paludis/util/thread_pool.cc
@@ -22,7 +22,7 @@
#include <paludis/util/tr1_memory.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
-#include <list>
+#include <deque>
using namespace paludis;
@@ -31,7 +31,7 @@ namespace paludis
template <>
struct Implementation<ThreadPool>
{
- std::list<tr1::shared_ptr<Thread> > threads;
+ std::deque<tr1::shared_ptr<Thread> > threads;
};
}
@@ -50,3 +50,9 @@ ThreadPool::create_thread(const tr1::function<void () throw ()> & f)
_imp->threads.push_back(make_shared_ptr(new Thread(f)));
}
+unsigned
+ThreadPool::number_of_threads() const
+{
+ return _imp->threads.size();
+}
+
diff --git a/paludis/util/thread_pool.hh b/paludis/util/thread_pool.hh
index 644c11f..a4e9e9e 100644
--- a/paludis/util/thread_pool.hh
+++ b/paludis/util/thread_pool.hh
@@ -34,6 +34,7 @@ namespace paludis
~ThreadPool();
void create_thread(const tr1::function<void () throw ()> &);
+ unsigned number_of_threads() const;
};
}