| 4 | #include "jobqueue.h" |
| 5 | |
| 6 | void JobQueue::QueueJob(std::function<void(void)> f) |
| 7 | { |
| 8 | _jobQueue.push_back(f); |
| 9 | if (!IsQueueRunning()) |
| 10 | { |
| 11 | runOnWorkerThread( |
| 12 | [this]() |
| 13 | { |
| 14 | auto fail = [&]() |
| 15 | { |
| 16 | runOnUiThread( |
| 17 | [&]() |
| 18 | { |
| 19 | _jobQueue.clear(); |
| 20 | OnQueueFailed(); |
| 21 | }); |
| 22 | }; |
| 23 | |
| 24 | for (;;) |
| 25 | { |
| 26 | std::function<void()> f; |
| 27 | runOnUiThread( |
| 28 | [&]() |
| 29 | { |
| 30 | if (!_jobQueue.empty()) |
| 31 | { |
| 32 | f = _jobQueue.front(); |
| 33 | _jobQueue.pop_front(); |
| 34 | } |
| 35 | }); |
| 36 | if (!f) |
| 37 | break; |
| 38 | |
| 39 | try |
| 40 | { |
| 41 | f(); |
| 42 | } |
| 43 | catch (const EmergencyStopException& e) |
| 44 | { |
| 45 | fail(); |
| 46 | throw e; |
| 47 | } |
| 48 | catch (const ErrorException& e) |
| 49 | { |
| 50 | fail(); |
| 51 | throw e; |
| 52 | } |
| 53 | |
| 54 | runOnUiThread( |
| 55 | [&]() |
| 56 | { |
| 57 | OnQueueEmpty(); |
| 58 | }); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 |
nothing calls this directly
no test coverage detected