]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/TaskPool.cpp
Import mandoc 1.4.1rc2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / TaskPool.cpp
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
12 namespace
13 {
14     class TaskPoolImpl
15     {
16     public:
17         static TaskPoolImpl&
18         GetInstance();
19
20         void
21         AddTask(std::function<void()>&& task_fn);
22
23     private:
24         TaskPoolImpl();
25
26         static void
27         Worker(TaskPoolImpl* pool);
28
29         std::queue<std::function<void()>> m_tasks;
30         std::mutex                        m_tasks_mutex;
31         uint32_t                          m_thread_count;
32     };
33
34 } // end of anonymous namespace
35
36 TaskPoolImpl&
37 TaskPoolImpl::GetInstance()
38 {
39     static TaskPoolImpl g_task_pool_impl;
40     return g_task_pool_impl;
41 }
42
43 void
44 TaskPool::AddTaskImpl(std::function<void()>&& task_fn)
45 {
46     TaskPoolImpl::GetInstance().AddTask(std::move(task_fn));
47 }
48
49 TaskPoolImpl::TaskPoolImpl() :
50     m_thread_count(0)
51 {
52 }
53
54 void
55 TaskPoolImpl::AddTask(std::function<void()>&& task_fn)
56 {
57     static const uint32_t max_threads = std::thread::hardware_concurrency();
58
59     std::unique_lock<std::mutex> lock(m_tasks_mutex);
60     m_tasks.emplace(std::move(task_fn));
61     if (m_thread_count < max_threads)
62     {
63         m_thread_count++;
64         // Note that this detach call needs to happen with the m_tasks_mutex held. This prevents the thread
65         // from exiting prematurely and triggering a linux libc bug
66         // (https://sourceware.org/bugzilla/show_bug.cgi?id=19951).
67         std::thread (Worker, this).detach();
68     }
69 }
70
71 void
72 TaskPoolImpl::Worker(TaskPoolImpl* pool)
73 {
74     while (true)
75     {
76         std::unique_lock<std::mutex> lock(pool->m_tasks_mutex);
77         if (pool->m_tasks.empty())
78         {
79             pool->m_thread_count--;
80             break;
81         }
82
83         std::function<void()> f = pool->m_tasks.front();
84         pool->m_tasks.pop();
85         lock.unlock();
86
87         f();
88     }
89 }