| 323 | } |
| 324 | |
| 325 | JobResult JobQueue::check() SKR_NOEXCEPT |
| 326 | { |
| 327 | skr_rw_mutex_acquire_w(&pending_queue_mutex); |
| 328 | SKR_DEFER({skr_rw_mutex_release_w(&pending_queue_mutex);}); |
| 329 | |
| 330 | auto need_cancel = false; |
| 331 | need_cancel = skr_atomic32_load_acquire(&cancel_requested); |
| 332 | if (need_cancel) |
| 333 | { |
| 334 | std::for_each(pending_queue.begin(), pending_queue.end(), |
| 335 | [](auto ptr) { |
| 336 | if (ptr->is_none() == false) |
| 337 | { |
| 338 | ptr->cancel(); |
| 339 | } |
| 340 | }); |
| 341 | skr_atomic32_store_release(&cancel_requested, false); |
| 342 | } |
| 343 | |
| 344 | auto it = pending_queue.begin(); |
| 345 | while (it != pending_queue.end()) { |
| 346 | // call finish() when finished |
| 347 | if ((*it)->is_none()) { |
| 348 | auto jobItemPtr = *it; |
| 349 | // finish is called while the lock is released |
| 350 | skr_rw_mutex_release_w(&pending_queue_mutex); |
| 351 | jobItemPtr->finish(jobItemPtr->get_result()); |
| 352 | skr_rw_mutex_acquire_w(&pending_queue_mutex); |
| 353 | |
| 354 | // Since enqueue may be done while unlocking, it cannot be used |
| 355 | // so re-search the list |
| 356 | it = std::find(pending_queue.begin(), pending_queue.end(), jobItemPtr); |
| 357 | it = pending_queue.erase(it); |
| 358 | } |
| 359 | else { |
| 360 | ++it; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return ASYNC_RESULT_OK; |
| 365 | } |
| 366 | |
| 367 | void JobQueue::get_descriptor(JobQueueDesc* pDesc) SKR_NOEXCEPT |
| 368 | { |