Shaka Packager SDK
job_manager.h
1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_APP_JOB_MANAGER_H_
8 #define PACKAGER_APP_JOB_MANAGER_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "packager/base/threading/simple_thread.h"
14 #include "packager/status.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 class OriginHandler;
20 
21 // A job is a single line of work that is expected to run in parallel with
22 // other jobs.
23 class Job : public base::SimpleThread {
24  public:
25  Job(const std::string& name, std::shared_ptr<OriginHandler> work);
26 
27  // Request that the job stops executing. This is only a request and
28  // will not block. If you want to wait for the job to complete, use
29  // |wait|.
30  void Cancel();
31 
32  // Get the current status of the job. If the job failed to initialize
33  // or encountered an error during execution this will return the error.
34  const Status& status() const { return status_; }
35 
36  // If you want to wait for this job to complete, this will return the
37  // WaitableEvent you can wait on.
38  base::WaitableEvent* wait() { return &wait_; }
39 
40  private:
41  Job(const Job&) = delete;
42  Job& operator=(const Job&) = delete;
43 
44  void Run() override;
45 
46  std::shared_ptr<OriginHandler> work_;
47  Status status_;
48 
49  base::WaitableEvent wait_;
50 };
51 
52 // Similar to a thread pool, JobManager manages multiple jobs that are expected
53 // to run in parallel. It can be used to register, run, and stop a batch of
54 // jobs.
55 class JobManager {
56  public:
57  JobManager() = default;
58 
59  // Create a new job entry by specifying the origin handler at the top of the
60  // chain and a name for the thread. This will only register the job. To start
61  // the job, you need to call |RunJobs|.
62  void Add(const std::string& name, std::shared_ptr<OriginHandler> handler);
63 
64  // Initialize all registered jobs. If any job fails to initialize, this will
65  // return the error and it will not be safe to call |RunJobs| as not all jobs
66  // will be properly initialized.
67  Status InitializeJobs();
68 
69  // Run all registered jobs. Before calling this make sure that
70  // |InitializedJobs| returned |Status::OK|. This call is blocking and will
71  // block until all jobs exit.
72  Status RunJobs();
73 
74  // Ask all jobs to stop running. This call is non-blocking and can be used to
75  // unblock a call to |RunJobs|.
76  void CancelJobs();
77 
78  private:
79  JobManager(const JobManager&) = delete;
80  JobManager& operator=(const JobManager&) = delete;
81 
82  struct JobEntry {
83  std::string name;
84  std::shared_ptr<OriginHandler> worker;
85  };
86  // Stores Job entries for delayed construction of Job object.
87  std::vector<JobEntry> job_entries_;
88  std::vector<std::unique_ptr<Job>> jobs_;
89 };
90 
91 } // namespace media
92 } // namespace shaka
93 
94 #endif // PACKAGER_APP_JOB_MANAGER_H_
All the methods that are virtual are virtual for mocking.