2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-12-12 21:05:12 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/app/job_manager.h>
|
2017-12-12 21:05:12 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
|
|
|
|
#include <packager/media/chunking/sync_point_queue.h>
|
|
|
|
#include <packager/media/origin/origin_handler.h>
|
2017-12-12 21:05:12 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
Job::Job(const std::string& name,
|
|
|
|
std::shared_ptr<OriginHandler> work,
|
|
|
|
OnCompleteFunction on_complete)
|
|
|
|
: name_(name),
|
2018-02-06 19:47:41 +00:00
|
|
|
work_(std::move(work)),
|
2023-12-01 17:32:19 +00:00
|
|
|
on_complete_(on_complete),
|
|
|
|
status_(error::Code::UNKNOWN, "Job uninitialized") {
|
2018-02-06 19:47:41 +00:00
|
|
|
DCHECK(work_);
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
const Status& Job::Initialize() {
|
|
|
|
status_ = work_->Initialize();
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::Start() {
|
|
|
|
thread_.reset(new std::thread(&Job::Run, this));
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:05:12 +00:00
|
|
|
void Job::Cancel() {
|
|
|
|
work_->Cancel();
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
const Status& Job::Run() {
|
|
|
|
if (status_.ok()) // initialized correctly
|
|
|
|
status_ = work_->Run();
|
|
|
|
|
|
|
|
on_complete_(this);
|
|
|
|
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::Join() {
|
|
|
|
if (thread_) {
|
|
|
|
thread_->join();
|
|
|
|
thread_ = nullptr;
|
|
|
|
}
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:24:17 +00:00
|
|
|
JobManager::JobManager(std::unique_ptr<SyncPointQueue> sync_points)
|
|
|
|
: sync_points_(std::move(sync_points)) {}
|
|
|
|
|
2017-12-12 21:05:12 +00:00
|
|
|
void JobManager::Add(const std::string& name,
|
|
|
|
std::shared_ptr<OriginHandler> handler) {
|
2023-12-01 17:32:19 +00:00
|
|
|
jobs_.emplace_back(new Job(
|
|
|
|
name, std::move(handler),
|
|
|
|
std::bind(&JobManager::OnJobComplete, this, std::placeholders::_1)));
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status JobManager::InitializeJobs() {
|
|
|
|
Status status;
|
2023-12-01 17:32:19 +00:00
|
|
|
for (auto& job : jobs_)
|
|
|
|
status.Update(job->Initialize());
|
2017-12-12 21:05:12 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status JobManager::RunJobs() {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::set<Job*> active_jobs;
|
2017-12-12 21:05:12 +00:00
|
|
|
|
|
|
|
// Start every job and add it to the active jobs list so that we can wait
|
|
|
|
// on each one.
|
|
|
|
for (auto& job : jobs_) {
|
|
|
|
job->Start();
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
active_jobs.insert(job.get());
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
// Wait for all jobs to complete or any job to error.
|
2017-12-12 21:05:12 +00:00
|
|
|
Status status;
|
2023-12-01 17:32:19 +00:00
|
|
|
{
|
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
while (status.ok() && active_jobs.size()) {
|
|
|
|
// any_job_complete_ is protected by mutex_.
|
|
|
|
any_job_complete_.Wait(&mutex_);
|
|
|
|
|
|
|
|
// complete_ is protected by mutex_.
|
|
|
|
for (const auto& entry : complete_) {
|
|
|
|
Job* job = entry.first;
|
|
|
|
bool complete = entry.second;
|
|
|
|
if (complete) {
|
|
|
|
job->Join();
|
|
|
|
status.Update(job->status());
|
|
|
|
active_jobs.erase(job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the main loop has exited and there are still jobs running,
|
|
|
|
// we need to cancel them and clean-up.
|
2018-03-22 06:24:17 +00:00
|
|
|
if (sync_points_)
|
|
|
|
sync_points_->Cancel();
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
for (auto& job : active_jobs)
|
2017-12-12 21:05:12 +00:00
|
|
|
job->Cancel();
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
for (auto& job : active_jobs)
|
2017-12-12 21:05:12 +00:00
|
|
|
job->Join();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
void JobManager::OnJobComplete(Job* job) {
|
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
// These are both protected by mutex_.
|
|
|
|
complete_[job] = true;
|
|
|
|
any_job_complete_.Signal();
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:05:12 +00:00
|
|
|
void JobManager::CancelJobs() {
|
2018-03-22 06:24:17 +00:00
|
|
|
if (sync_points_)
|
|
|
|
sync_points_->Cancel();
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
for (auto& job : jobs_)
|
2017-12-12 21:05:12 +00:00
|
|
|
job->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|