Updated Portuguese translation for the BFD sub-directory
[binutils-gdb.git] / gdbsupport / thread-pool.cc
index be9ca22682a0bf59e13bd8bf8098be09cc2487ae..2bb75cc9cef4b6ab42e0779106063aa9e53fb38a 100644 (file)
@@ -1,6 +1,6 @@
 /* Thread pool
 
-   Copyright (C) 2019-2020 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -25,6 +25,7 @@
 #include "gdbsupport/alt-stack.h"
 #include "gdbsupport/block-signals.h"
 #include <algorithm>
+#include <system_error>
 
 /* On the off chance that we have the pthread library on a Windows
    host, but std::thread is not using it, avoid calling
@@ -102,8 +103,19 @@ thread_pool::set_thread_count (size_t num_threads)
       block_signals blocker;
       for (size_t i = m_thread_count; i < num_threads; ++i)
        {
-         std::thread thread (&thread_pool::thread_function, this);
-         thread.detach ();
+         try
+           {
+             std::thread thread (&thread_pool::thread_function, this);
+             thread.detach ();
+           }
+         catch (const std::system_error &)
+           {
+             /* libstdc++ may not implement std::thread, and will
+                throw an exception on use.  It seems fine to ignore
+                this, and any other sort of startup failure here.  */
+             num_threads = i;
+             break;
+           }
        }
     }
   /* If the new size is smaller, terminate some existing threads.  */
@@ -118,9 +130,9 @@ thread_pool::set_thread_count (size_t num_threads)
 }
 
 std::future<void>
-thread_pool::post_task (std::function<void ()> func)
+thread_pool::post_task (std::function<void ()> &&func)
 {
-  std::packaged_task<void ()> t (func);
+  std::packaged_task<void ()> t (std::move (func));
   std::future<void> f = t.get_future ();
 
   if (m_thread_count == 0)