2017-12-12 21:05:12 +00:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "packager/app/job_manager.h"
|
|
|
|
|
|
|
|
#include "packager/app/libcrypto_threading.h"
|
2018-03-22 06:24:17 +00:00
|
|
|
#include "packager/media/chunking/sync_point_queue.h"
|
2017-12-12 21:05:12 +00:00
|
|
|
#include "packager/media/origin/origin_handler.h"
|
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
Job::Job(const std::string& name, std::shared_ptr<OriginHandler> work)
|
|
|
|
: SimpleThread(name),
|
2018-02-06 19:47:41 +00:00
|
|
|
work_(std::move(work)),
|
2017-12-12 21:05:12 +00:00
|
|
|
wait_(base::WaitableEvent::ResetPolicy::MANUAL,
|
|
|
|
base::WaitableEvent::InitialState::NOT_SIGNALED) {
|
2018-02-06 19:47:41 +00:00
|
|
|
DCHECK(work_);
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Job::Cancel() {
|
|
|
|
work_->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::Run() {
|
|
|
|
status_ = work_->Run();
|
|
|
|
wait_.Signal();
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-02-06 19:47:41 +00:00
|
|
|
// Stores Job entries for delayed construction of Job objects, to avoid
|
|
|
|
// setting up SimpleThread until we know all workers can be initialized
|
|
|
|
// successfully.
|
|
|
|
job_entries_.push_back({name, std::move(handler)});
|
2017-12-12 21:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status JobManager::InitializeJobs() {
|
|
|
|
Status status;
|
2018-02-06 19:47:41 +00:00
|
|
|
for (const JobEntry& job_entry : job_entries_)
|
|
|
|
status.Update(job_entry.worker->Initialize());
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2017-12-12 21:05:12 +00:00
|
|
|
|
2018-02-06 19:47:41 +00:00
|
|
|
// Create Job objects after successfully initialized all workers.
|
|
|
|
for (const JobEntry& job_entry : job_entries_)
|
|
|
|
jobs_.emplace_back(new Job(job_entry.name, std::move(job_entry.worker)));
|
2017-12-12 21:05:12 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status JobManager::RunJobs() {
|
|
|
|
// We need to store the jobs and the waits separately in order to use the
|
|
|
|
// |WaitMany| function. |WaitMany| takes an array of WaitableEvents but we
|
|
|
|
// need to access the jobs in order to join the thread and check the status.
|
|
|
|
// The indexes needs to be check in sync or else we won't be able to relate a
|
|
|
|
// WaitableEvent back to the job.
|
|
|
|
std::vector<Job*> active_jobs;
|
|
|
|
std::vector<base::WaitableEvent*> active_waits;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
active_jobs.push_back(job.get());
|
|
|
|
active_waits.push_back(job->wait());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all jobs to complete or an error occurs.
|
|
|
|
Status status;
|
|
|
|
while (status.ok() && active_jobs.size()) {
|
|
|
|
// Wait for an event to finish and then update our status so that we can
|
|
|
|
// quit if something has gone wrong.
|
|
|
|
const size_t done =
|
|
|
|
base::WaitableEvent::WaitMany(active_waits.data(), active_waits.size());
|
|
|
|
Job* job = active_jobs[done];
|
|
|
|
|
|
|
|
job->Join();
|
|
|
|
status.Update(job->status());
|
|
|
|
|
|
|
|
// Remove the job and the wait from our tracking.
|
|
|
|
active_jobs.erase(active_jobs.begin() + done);
|
|
|
|
active_waits.erase(active_waits.begin() + done);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2017-12-12 21:05:12 +00:00
|
|
|
for (auto& job : active_jobs) {
|
|
|
|
job->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& job : active_jobs) {
|
|
|
|
job->Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JobManager::CancelJobs() {
|
2018-03-22 06:24:17 +00:00
|
|
|
if (sync_points_)
|
|
|
|
sync_points_->Cancel();
|
2017-12-12 21:05:12 +00:00
|
|
|
for (auto& job : jobs_) {
|
|
|
|
job->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|