aboutsummaryrefslogtreecommitdiff
path: root/paludis/util
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2013-05-16 16:26:36 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2013-05-22 16:43:42 +0100
commit9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d (patch)
tree819d9ef6fabaa7a4c91b0beeaf1ac8a895cdaf29 /paludis/util
parenta61bc76abeb201333fc28276d3d87d3a128c6f0e (diff)
downloadpaludis-9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d.tar.gz
paludis-9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d.tar.xz
Use std::thread
Diffstat (limited to 'paludis/util')
-rw-r--r--paludis/util/executor.cc14
-rw-r--r--paludis/util/files.m41
-rw-r--r--paludis/util/process.cc14
-rw-r--r--paludis/util/singleton_TEST.cc8
-rw-r--r--paludis/util/string_list_stream_TEST.cc8
-rw-r--r--paludis/util/thread.cc72
-rw-r--r--paludis/util/thread.hh70
-rw-r--r--paludis/util/thread_TEST.cc44
-rw-r--r--paludis/util/thread_pool.cc11
9 files changed, 34 insertions, 208 deletions
diff --git a/paludis/util/executor.cc b/paludis/util/executor.cc
index b4e93e903..2b4836a92 100644
--- a/paludis/util/executor.cc
+++ b/paludis/util/executor.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2009, 2010, 2011 Ciaran McCreesh
+ * Copyright (c) 2009, 2010, 2011, 2012 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -21,11 +21,11 @@
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/condition_variable.hh>
-#include <paludis/util/thread.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <map>
#include <list>
+#include <thread>
using namespace paludis;
@@ -110,7 +110,7 @@ Executor::add(const std::shared_ptr<Executive> & x)
void
Executor::execute()
{
- typedef std::map<std::string, std::pair<std::shared_ptr<Thread>, std::shared_ptr<Executive> > > Running;
+ typedef std::map<std::string, std::pair<std::thread, std::shared_ptr<Executive> > > Running;
Running running;
Lock lock(_imp->mutex);
@@ -129,8 +129,7 @@ Executor::execute()
++_imp->active;
--_imp->pending;
(*q->second.begin())->pre_execute_exclusive();
- running.insert(std::make_pair(q->first, std::make_pair(std::make_shared<Thread>(
- std::bind(&Executor::_one, this, *q->second.begin())), *q->second.begin())));
+ running.insert(std::make_pair(q->first, std::make_pair(std::thread(std::bind(&Executor::_one, this, *q->second.begin())), *q->second.begin())));
q->second.erase(q->second.begin());
if (q->second.empty())
_imp->queues.erase(q++);
@@ -157,7 +156,9 @@ Executor::execute()
{
--_imp->active;
++_imp->done;
- running.erase((*p)->queue_name());
+ auto r = running.find((*p)->queue_name());
+ r->second.first.join();
+ running.erase(r);
(*p)->post_execute_exclusive();
}
@@ -175,3 +176,4 @@ namespace paludis
{
template class Pimp<Executor>;
}
+
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 3a5aa8c13..912fa7ebc 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -86,7 +86,6 @@ add(`strip', `hh', `cc', `gtest')
add(`system', `hh', `cc', `gtest')
add(`tail_output_stream', `hh', `cc', `fwd', `gtest')
add(`tee_output_stream', `hh', `cc', `fwd')
-add(`thread', `hh', `cc', `gtest')
add(`thread_pool', `hh', `cc', `gtest')
add(`timestamp', `hh', `cc', `fwd')
add(`tokeniser', `hh', `cc', `gtest')
diff --git a/paludis/util/process.cc b/paludis/util/process.cc
index 48bb92841..22f8cb52f 100644
--- a/paludis/util/process.cc
+++ b/paludis/util/process.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2010, 2011 Ciaran McCreesh
+ * Copyright (c) 2010, 2011, 2012 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -19,7 +19,6 @@
#include <paludis/util/process.hh>
#include <paludis/util/pimp-impl.hh>
-#include <paludis/util/thread.hh>
#include <paludis/util/pipe.hh>
#include <paludis/util/pty.hh>
#include <paludis/util/fs_path.hh>
@@ -36,6 +35,7 @@
#include <map>
#include <cstdlib>
#include <cstring>
+#include <thread>
#include <errno.h>
#include <unistd.h>
@@ -194,7 +194,7 @@ namespace paludis
bool as_main_process;
/* must be last, so the thread gets join()ed before its FDs vanish */
- std::unique_ptr<Thread> thread;
+ std::thread thread;
RunningProcessThread() :
ctl_pipe(true),
@@ -206,6 +206,12 @@ namespace paludis
{
}
+ ~RunningProcessThread()
+ {
+ if (thread.joinable())
+ thread.join();
+ }
+
void thread_func();
void start();
@@ -481,7 +487,7 @@ RunningProcessThread::thread_func()
void
RunningProcessThread::start()
{
- thread.reset(new Thread(std::bind(&RunningProcessThread::thread_func, this)));
+ thread = std::thread(std::bind(&RunningProcessThread::thread_func, this));
}
namespace paludis
diff --git a/paludis/util/singleton_TEST.cc b/paludis/util/singleton_TEST.cc
index 9a43488c7..9f856f4a2 100644
--- a/paludis/util/singleton_TEST.cc
+++ b/paludis/util/singleton_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007, 2008, 2010, 2011 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008, 2010, 2011, 2012 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -19,8 +19,8 @@
#include <paludis/util/singleton.hh>
#include <paludis/util/singleton-impl.hh>
-#include <paludis/util/thread.hh>
#include <paludis/util/mutex.hh>
+#include <paludis/util/thread_pool.hh>
#include <functional>
#include <algorithm>
@@ -139,9 +139,9 @@ TEST(Singleton, Threaded)
ASSERT_EQ(0, MyThreadedClass::instances);
ASSERT_TRUE(c == std::count(a.begin(), a.end(), static_cast<void *>(0)));
{
- std::vector<std::shared_ptr<Thread> > t(c);
+ ThreadPool p;
for (int x(0) ; x < c ; ++x)
- t[x] = std::make_shared<Thread>(std::bind(&thread_func, &a[x]));
+ p.create_thread(std::bind(&thread_func, &a[x]));
}
ASSERT_EQ(1, MyThreadedClass::instances);
ASSERT_TRUE(0 == std::count(a.begin(), a.end(), static_cast<void *>(0)));
diff --git a/paludis/util/string_list_stream_TEST.cc b/paludis/util/string_list_stream_TEST.cc
index f71a8f059..ab1df95c8 100644
--- a/paludis/util/string_list_stream_TEST.cc
+++ b/paludis/util/string_list_stream_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2009, 2011 Ciaran McCreesh
+ * Copyright (c) 2009, 2011, 2012 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -18,10 +18,10 @@
*/
#include <paludis/util/string_list_stream.hh>
-#include <paludis/util/thread.hh>
#include <paludis/util/stringify.hh>
#include <functional>
+#include <thread>
#include <gtest/gtest.h>
@@ -64,7 +64,7 @@ TEST(StringListStream, Works)
TEST(StringListStream, Threads)
{
StringListStream s;
- Thread t(std::bind(&write_to, std::ref(s)));
+ std::thread t(std::bind(&write_to, std::ref(s)));
std::string l;
for (int n(0) ; n < 100 ; ++n)
@@ -74,5 +74,7 @@ TEST(StringListStream, Threads)
}
ASSERT_TRUE(! std::getline(s, l));
+
+ t.join();
}
diff --git a/paludis/util/thread.cc b/paludis/util/thread.cc
deleted file mode 100644
index 19516ba1c..000000000
--- a/paludis/util/thread.cc
+++ /dev/null
@@ -1,72 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2007, 2008, 2010, 2012 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <paludis/util/thread.hh>
-#include <paludis/util/exception.hh>
-#include <paludis/util/stringify.hh>
-#include <paludis/util/log.hh>
-#include <string.h>
-#include <errno.h>
-
-#ifdef __linux__
-# include <sys/time.h>
-# include <sys/resource.h>
-# include <unistd.h>
-# include <sys/syscall.h>
-#endif
-
-using namespace paludis;
-
-Thread::Thread(const std::function<void () throw ()> & f) :
- _thread(new pthread_t),
- _func(f)
-{
- int err;
-
- if (0 != ((err = pthread_create(_thread, 0, &thread_func, this))))
- throw InternalError(PALUDIS_HERE, "pthread_create failed: " + stringify(strerror(err)));
-}
-
-void *
-Thread::thread_func(void * r)
-{
- try
- {
- static_cast<Thread *>(r)->_func();
- }
- catch (const Exception & e)
- {
- static_cast<Thread *>(r)->_exception = e.backtrace(": ") + e.message() + " (" + e.what() + ")";
- }
- catch (const std::exception & e)
- {
- static_cast<Thread *>(r)->_exception = e.what();
- }
- return 0;
-}
-
-Thread::~Thread()
-{
- pthread_join(*_thread, 0);
- delete _thread;
-
- if (! _exception.empty())
- throw InternalError(PALUDIS_HERE, "Exception '" + _exception + "' uncaught in child thread");
-}
-
diff --git a/paludis/util/thread.hh b/paludis/util/thread.hh
deleted file mode 100644
index adff17c65..000000000
--- a/paludis/util/thread.hh
+++ /dev/null
@@ -1,70 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2007, 2008, 2012 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef PALUDIS_GUARD_PALUDIS_UTIL_THREAD_HH
-#define PALUDIS_GUARD_PALUDIS_UTIL_THREAD_HH 1
-
-#include <paludis/util/attributes.hh>
-#include <functional>
-#include <string>
-#include <pthread.h>
-
-/** \file
- * Declarations for the Thread class.
- *
- * \ingroup g_threads
- *
- * \section Examples
- *
- * - None at this time.
- */
-
-namespace paludis
-{
- /**
- * A basic thread class.
- *
- * If threading is disabled, the threaded function is executed immediately
- * in the current context.
- *
- * \ingroup g_threads
- * \since 0.26
- * \nosubgrouping
- */
- class PALUDIS_VISIBLE Thread
- {
- private:
- pthread_t * const _thread;
- const std::function<void () throw ()> _func;
- std::string _exception;
-
- static void * thread_func(void *);
-
- public:
- ///\name Basic operations
- ///\{
-
- Thread(const std::function<void () throw ()> &);
- ~Thread();
-
- ///\}
- };
-}
-
-#endif
diff --git a/paludis/util/thread_TEST.cc b/paludis/util/thread_TEST.cc
deleted file mode 100644
index daf233655..000000000
--- a/paludis/util/thread_TEST.cc
+++ /dev/null
@@ -1,44 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2007, 2008, 2011 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <paludis/util/thread.hh>
-
-#include <time.h>
-
-#include <gtest/gtest.h>
-
-using namespace paludis;
-
-namespace
-{
- void make_true(bool & b) throw ()
- {
- b = true;
- }
-}
-
-TEST(Thread, Works)
-{
- bool x(false);
- {
- Thread t(std::bind(&make_true, std::ref(x)));
- }
- EXPECT_TRUE(x);
-}
-
diff --git a/paludis/util/thread_pool.cc b/paludis/util/thread_pool.cc
index cf1843e9f..dd9f5f9d7 100644
--- a/paludis/util/thread_pool.cc
+++ b/paludis/util/thread_pool.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2007, 2008, 2010, 2011 Ciaran McCreesh
+ * Copyright (c) 2007, 2008, 2010, 2011, 2012 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -18,10 +18,10 @@
*/
#include <paludis/util/thread_pool.hh>
-#include <paludis/util/thread.hh>
#include <paludis/util/pimp-impl.hh>
#include <memory>
#include <deque>
+#include <thread>
using namespace paludis;
@@ -30,7 +30,7 @@ namespace paludis
template <>
struct Imp<ThreadPool>
{
- std::deque<std::shared_ptr<Thread> > threads;
+ std::deque<std::thread> threads;
};
}
@@ -41,12 +41,14 @@ ThreadPool::ThreadPool() :
ThreadPool::~ThreadPool()
{
+ for (auto & t : _imp->threads)
+ t.join();
}
void
ThreadPool::create_thread(const std::function<void () throw ()> & f)
{
- _imp->threads.push_back(std::make_shared<Thread>(f));
+ _imp->threads.emplace_back(f);
}
unsigned
@@ -59,3 +61,4 @@ namespace paludis
{
template class Pimp<ThreadPool>;
}
+