diff options
author | 2013-05-16 16:26:36 +0100 | |
---|---|---|
committer | 2013-05-22 16:43:42 +0100 | |
commit | 9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d (patch) | |
tree | 819d9ef6fabaa7a4c91b0beeaf1ac8a895cdaf29 | |
parent | a61bc76abeb201333fc28276d3d87d3a128c6f0e (diff) | |
download | paludis-9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d.tar.gz paludis-9c7d9f9aef92e9073ee9d295b1ed5ea779f8a96d.tar.xz |
Use std::thread
-rw-r--r-- | configure.ac | 14 | ||||
-rw-r--r-- | paludis/ipc_output_manager.cc | 11 | ||||
-rw-r--r-- | paludis/util/executor.cc | 14 | ||||
-rw-r--r-- | paludis/util/files.m4 | 1 | ||||
-rw-r--r-- | paludis/util/process.cc | 14 | ||||
-rw-r--r-- | paludis/util/singleton_TEST.cc | 8 | ||||
-rw-r--r-- | paludis/util/string_list_stream_TEST.cc | 8 | ||||
-rw-r--r-- | paludis/util/thread.cc | 72 | ||||
-rw-r--r-- | paludis/util/thread.hh | 70 | ||||
-rw-r--r-- | paludis/util/thread_TEST.cc | 44 | ||||
-rw-r--r-- | paludis/util/thread_pool.cc | 11 | ||||
-rw-r--r-- | ruby/repository.cc | 3 | ||||
-rw-r--r-- | src/clients/cave/cmd_sync.cc | 3 | ||||
-rw-r--r-- | src/clients/cave/resolve_common.cc | 24 |
14 files changed, 73 insertions, 224 deletions
diff --git a/configure.ac b/configure.ac index da77ac698..6c9a2818c 100644 --- a/configure.ac +++ b/configure.ac @@ -710,6 +710,20 @@ int main(int, char **) AC_MSG_ERROR([Your compiler does not support unordered containers])]) dnl }}} +dnl {{{ check for threads +AC_MSG_CHECKING([for threads]) +AC_COMPILE_IFELSE([AC_LANG_SOURCE([ +#include <thread> +int main(int, char **) +{ + std::thread t; +} +])], + [AC_MSG_RESULT([yes])], + [AC_MSG_RESULT([no]) + AC_MSG_ERROR([Your compiler does not support threads])]) +dnl }}} + dnl {{{ shared pointers AC_MSG_CHECKING([for std::shared_ptr<>]) AC_COMPILE_IFELSE([AC_LANG_SOURCE([ diff --git a/paludis/ipc_output_manager.cc b/paludis/ipc_output_manager.cc index 0ef41a618..db3d6f168 100644 --- a/paludis/ipc_output_manager.cc +++ b/paludis/ipc_output_manager.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 @@ -31,7 +31,6 @@ #include <paludis/util/log.hh> #include <paludis/util/iterator_funcs.hh> #include <paludis/util/join.hh> -#include <paludis/util/thread.hh> #include <paludis/util/pipe.hh> #include <paludis/util/mutex.hh> #include <paludis/util/options.hh> @@ -43,6 +42,7 @@ #include <vector> #include <cstdlib> #include <cstring> +#include <thread> #include <unistd.h> #include <fcntl.h> #include <errno.h> @@ -220,7 +220,7 @@ namespace paludis Pipe stdout_pipe, stderr_pipe, finished_pipe; - std::shared_ptr<Thread> copy_thread; + std::thread copy_thread; Imp(const Environment * const e, const std::function<void (const std::shared_ptr<OutputManager> &)> & c) : @@ -250,6 +250,9 @@ IPCInputManager::~IPCInputManager() char c('x'); if (1 != write(_imp->finished_pipe.write_fd(), &c, 1)) throw InternalError(PALUDIS_HERE, "write failed"); + + if (_imp->copy_thread.joinable()) + _imp->copy_thread.join(); } const std::function<std::string (const std::string &)> @@ -326,7 +329,7 @@ IPCInputManager::_pipe_command_handler(const std::string & s) Log::get_instance()->message("ipc_input_manager.pipe_command.starting_thread", ll_debug, lc_context) << "Starting copy thread"; - _imp->copy_thread = std::make_shared<Thread>(std::bind(&IPCInputManager::_copy_thread, this)); + _imp->copy_thread = std::thread(std::bind(&IPCInputManager::_copy_thread, this)); Log::get_instance()->message("ipc_input_manager.pipe_command.started_thread", ll_debug, lc_context) << "Started copy thread"; 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>; } + diff --git a/ruby/repository.cc b/ruby/repository.cc index 016aa8c09..a5733dab2 100644 --- a/ruby/repository.cc +++ b/ruby/repository.cc @@ -1,7 +1,7 @@ /* vim: set sw=4 sts=4 et foldmethod=syntax : */ /* - * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh + * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Ciaran McCreesh * Copyright (c) 2006, 2007, 2008 Richard Brown * Copyright (c) 2007 David Leverton * @@ -30,7 +30,6 @@ #include <paludis/util/set.hh> #include <paludis/util/sequence.hh> #include <paludis/util/mutex.hh> -#include <paludis/util/thread.hh> #include <paludis/util/condition_variable.hh> #include <paludis/util/make_named_values.hh> #include <ruby.h> diff --git a/src/clients/cave/cmd_sync.cc b/src/clients/cave/cmd_sync.cc index 2ed725899..e29bfe3bc 100644 --- a/src/clients/cave/cmd_sync.cc +++ b/src/clients/cave/cmd_sync.cc @@ -1,7 +1,7 @@ /* vim: set sw=4 sts=4 et foldmethod=syntax : */ /* - * Copyright (c) 2008, 2009, 2010, 2011 Ciaran McCreesh + * Copyright (c) 2008, 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 @@ -25,7 +25,6 @@ #include <paludis/util/named_value.hh> #include <paludis/util/make_named_values.hh> #include <paludis/util/condition_variable.hh> -#include <paludis/util/thread.hh> #include <paludis/util/return_literal_function.hh> #include <paludis/util/executor.hh> #include <paludis/util/timestamp.hh> diff --git a/src/clients/cave/resolve_common.cc b/src/clients/cave/resolve_common.cc index 630f7e39e..5043f2c1e 100644 --- a/src/clients/cave/resolve_common.cc +++ b/src/clients/cave/resolve_common.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 @@ -35,7 +35,6 @@ #include <paludis/util/indirect_iterator-impl.hh> #include <paludis/util/wrapped_output_iterator.hh> #include <paludis/util/string_list_stream.hh> -#include <paludis/util/thread.hh> #include <paludis/util/timestamp.hh> #include <paludis/util/map.hh> #include <paludis/util/make_shared_copy.hh> @@ -112,6 +111,7 @@ #include <cstdlib> #include <list> #include <map> +#include <thread> #include "config.h" @@ -249,7 +249,7 @@ namespace Context context("When displaying chosen resolution:"); StringListStream ser_stream; - Thread ser_thread(std::bind(&serialise_resolved, + std::thread ser_thread(std::bind(&serialise_resolved, std::ref(ser_stream), std::cref(*resolved))); @@ -271,6 +271,7 @@ namespace p != p_end ; ++p) args->push_back(p->first); + int result; if (program_options.a_display_resolution_program.specified()) { std::string command(program_options.a_display_resolution_program.argument()); @@ -292,10 +293,13 @@ namespace process .send_input_to_fd(ser_stream, -1, "PALUDIS_SERIALISED_RESOLUTION_FD"); - return process.run().wait(); + result = process.run().wait(); } else - return DisplayResolutionCommand().run(env, args, resolved); + result = DisplayResolutionCommand().run(env, args, resolved); + + ser_thread.join(); + return result; } int graph_jobs( @@ -313,7 +317,7 @@ namespace return 0; StringListStream ser_stream; - Thread ser_thread(std::bind(&serialise_resolved, + std::thread ser_thread(std::bind(&serialise_resolved, std::ref(ser_stream), std::cref(*resolved))); @@ -347,6 +351,7 @@ namespace p != p_end ; ++p) args->push_back(p->first); + int result; if (program_options.a_graph_jobs_program.specified()) { std::string command(program_options.a_graph_jobs_program.argument()); @@ -368,10 +373,13 @@ namespace process .send_input_to_fd(ser_stream, -1, "PALUDIS_SERIALISED_RESOLUTION_FD"); - return process.run().wait(); + result = process.run().wait(); } else - return GraphJobsCommand().run(env, args, resolved); + result = GraphJobsCommand().run(env, args, resolved); + + ser_thread.join(); + return result; } void serialise_job_lists(StringListStream & ser_stream, const JobLists & job_lists) |