| 68 | } |
| 69 | |
| 70 | void* queueTask(const std::function<void*(void*)>& func, void* arg, bool async) { |
| 71 | if (std::this_thread::get_id() == mainThreadID && (!async || taskQueue.locked())) return func(arg); |
| 72 | TaskQueueItem * task = new TaskQueueItem; |
| 73 | task->func = func; |
| 74 | task->data = arg; |
| 75 | task->async = async; |
| 76 | { |
| 77 | std::unique_lock<std::mutex> lock(taskQueue.getMutex()); |
| 78 | taskQueue->push(task); |
| 79 | if (selectedRenderer == 0 || selectedRenderer == 5) { |
| 80 | SDL_Event e; |
| 81 | e.type = task_event_type; |
| 82 | SDL_PushEvent(&e); |
| 83 | } |
| 84 | taskQueueReady = true; |
| 85 | taskQueueNotify.notify_all(); |
| 86 | } |
| 87 | if (async) return NULL; |
| 88 | { |
| 89 | std::unique_lock<std::mutex> lock(task->lock); |
| 90 | while (!task->ready) task->notify.wait(lock); |
| 91 | if (task->exception != nullptr) { |
| 92 | std::exception_ptr e = task->exception; |
| 93 | delete task; |
| 94 | std::rethrow_exception(e); |
| 95 | } |
| 96 | arg = task->data; |
| 97 | } |
| 98 | delete task; |
| 99 | return arg; |
| 100 | } |
| 101 | |
| 102 | void awaitTasks(const std::function<bool()>& predicate = []()->bool{return true;}) { |
| 103 | while (predicate()) { |
no test coverage detected