Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 1 | //===--------------------- TaskPool.cpp -------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Utility/TaskPool.h" |
| 11 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | namespace { |
| 13 | class TaskPoolImpl { |
| 14 | public: |
| 15 | static TaskPoolImpl &GetInstance(); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | void AddTask(std::function<void()> &&task_fn); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 18 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | private: |
| 20 | TaskPoolImpl(); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | static void Worker(TaskPoolImpl *pool); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | std::queue<std::function<void()>> m_tasks; |
| 25 | std::mutex m_tasks_mutex; |
| 26 | uint32_t m_thread_count; |
| 27 | }; |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 28 | |
| 29 | } // end of anonymous namespace |
| 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | TaskPoolImpl &TaskPoolImpl::GetInstance() { |
| 32 | static TaskPoolImpl g_task_pool_impl; |
| 33 | return g_task_pool_impl; |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | void TaskPool::AddTaskImpl(std::function<void()> &&task_fn) { |
| 37 | TaskPoolImpl::GetInstance().AddTask(std::move(task_fn)); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | TaskPoolImpl::TaskPoolImpl() : m_thread_count(0) {} |
| 41 | |
| 42 | void TaskPoolImpl::AddTask(std::function<void()> &&task_fn) { |
| 43 | static const uint32_t max_threads = std::thread::hardware_concurrency(); |
| 44 | |
| 45 | std::unique_lock<std::mutex> lock(m_tasks_mutex); |
| 46 | m_tasks.emplace(std::move(task_fn)); |
| 47 | if (m_thread_count < max_threads) { |
| 48 | m_thread_count++; |
| 49 | // Note that this detach call needs to happen with the m_tasks_mutex held. |
| 50 | // This prevents the thread |
| 51 | // from exiting prematurely and triggering a linux libc bug |
| 52 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=19951). |
| 53 | std::thread(Worker, this).detach(); |
| 54 | } |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | void TaskPoolImpl::Worker(TaskPoolImpl *pool) { |
| 58 | while (true) { |
| 59 | std::unique_lock<std::mutex> lock(pool->m_tasks_mutex); |
| 60 | if (pool->m_tasks.empty()) { |
| 61 | pool->m_thread_count--; |
| 62 | break; |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 63 | } |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | std::function<void()> f = pool->m_tasks.front(); |
| 66 | pool->m_tasks.pop(); |
| 67 | lock.unlock(); |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | f(); |
| 70 | } |
Tamas Berghammer | ccb3676 | 2015-10-20 12:42:05 +0000 | [diff] [blame] | 71 | } |