* Enqueues a task. Tasks are guaranteed to be executed in the order * they were enqueued in except if there is more than one worker thread. */
| 55 | * they were enqueued in except if there is more than one worker thread. |
| 56 | */ |
| 57 | void WorkQueue::EnqueueUnlocked(std::unique_lock<std::mutex>& lock, std::function<void ()>&& function, WorkQueuePriority priority) |
| 58 | { |
| 59 | if (!m_Spawned) { |
| 60 | Log(LogNotice, "WorkQueue") |
| 61 | << "Spawning WorkQueue threads for '" << m_Name << "'"; |
| 62 | |
| 63 | for (int i = 0; i < m_ThreadCount; i++) { |
| 64 | m_Threads.create_thread([this]() { WorkerThreadProc(); }); |
| 65 | } |
| 66 | |
| 67 | m_Spawned = true; |
| 68 | } |
| 69 | |
| 70 | bool wq_thread = IsWorkerThread(); |
| 71 | |
| 72 | if (!wq_thread) { |
| 73 | while (m_Tasks.size() >= m_MaxItems && m_MaxItems != 0) |
| 74 | m_CVFull.wait(lock); |
| 75 | } |
| 76 | |
| 77 | m_Tasks.emplace(std::move(function), priority, ++m_NextTaskID); |
| 78 | |
| 79 | m_CVEmpty.notify_one(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Enqueues a task. Tasks are guaranteed to be executed in the order |