jobList.cpp 931 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "jobList.hpp"
  2. JobList::JobList(Module mod, size_t numberOfJobs) {
  3. jobCount = numberOfJobs;
  4. pendingJobCount = numberOfJobs;
  5. for(size_t i=0; i<numberOfJobs; i++) {
  6. std::shared_ptr<Job> job(new Job(mod));
  7. job->setDoneCallback([this]{
  8. finishJob();
  9. });
  10. jobs.push_back(job);
  11. }
  12. }
  13. void JobList::waitAll() {
  14. std::unique_lock<std::mutex> lk(pendingJobCount_m);
  15. jobListDone.wait(lk, [this]{return pendingJobCount <= 0;});
  16. }
  17. void JobList::waitOne(microseconds us) {
  18. std::unique_lock<std::mutex> lk(pendingJobCount_m);
  19. jobListDone.wait_for(lk, us);
  20. }
  21. void JobList::finishJob() {
  22. std::lock_guard<std::mutex> lk(pendingJobCount_m);
  23. pendingJobCount--;
  24. jobListDone.notify_all();
  25. }
  26. JobContainer JobList::getJob(size_t i) {
  27. return JobContainer(jobs.at(i));
  28. }
  29. JobContainer JobList::getNextJob() {
  30. nextJobIndex = (nextJobIndex+1) % jobCount;
  31. return JobContainer(jobs.at(nextJobIndex));
  32. }