* Main loop consists of these steps: * - Wait until a new task arrives * - Process the task (i.e. maybe copy data into slot) * - Check if multitask is finished * - Update all slots */
| 1654 | * - Update all slots |
| 1655 | */ |
| 1656 | void start_loop() { |
| 1657 | running = true; |
| 1658 | |
| 1659 | while (true) { |
| 1660 | QUE_DBG("%s", "processing new tasks\n"); |
| 1661 | |
| 1662 | while (true) { |
| 1663 | std::unique_lock<std::mutex> lock(mutex_tasks); |
| 1664 | if (!running) { |
| 1665 | QUE_DBG("%s", "terminate\n"); |
| 1666 | return; |
| 1667 | } |
| 1668 | if (queue_tasks.empty()) { |
| 1669 | lock.unlock(); |
| 1670 | break; |
| 1671 | } |
| 1672 | server_task task = std::move(queue_tasks.front()); |
| 1673 | queue_tasks.pop_front(); |
| 1674 | lock.unlock(); |
| 1675 | |
| 1676 | QUE_DBG("processing task, id = %d\n", task.id); |
| 1677 | callback_new_task(std::move(task)); |
| 1678 | } |
| 1679 | |
| 1680 | // all tasks in the current loop is processed, slots data is now ready |
| 1681 | QUE_DBG("%s", "update slots\n"); |
| 1682 | |
| 1683 | callback_update_slots(); |
| 1684 | |
| 1685 | QUE_DBG("%s", "waiting for new tasks\n"); |
| 1686 | { |
| 1687 | std::unique_lock<std::mutex> lock(mutex_tasks); |
| 1688 | if (!running) { |
| 1689 | QUE_DBG("%s", "terminate\n"); |
| 1690 | return; |
| 1691 | } |
| 1692 | if (queue_tasks.empty()) { |
| 1693 | condition_tasks.wait(lock, [&]{ |
| 1694 | return (!queue_tasks.empty() || !running); |
| 1695 | }); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | private: |
| 1702 | void cleanup_pending_task(int id_target) { |