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 class SyncPointQueue;
21 
22 // A job is a single line of work that is expected to run in parallel with
23 // other jobs.
24 class Job : public base::SimpleThread {
25  public:
26  Job(const std::string& name, std::shared_ptr<OriginHandler> work);
27 
28  // Request that the job stops executing. This is only a request and
29  // will not block. If you want to wait for the job to complete, use
30  // |wait|.
31  void Cancel();
32 
33  // Get the current status of the job. If the job failed to initialize
34  // or encountered an error during execution this will return the error.
35  const Status& status() const { return status_; }
36 
37  // If you want to wait for this job to complete, this will return the
38  // WaitableEvent you can wait on.
39  base::WaitableEvent* wait() { return &wait_; }
40 
41  private:
42  Job(const Job&) = delete;
43  Job& operator=(const Job&) = delete;
44 
45  void Run() override;
46 
47  std::shared_ptr<OriginHandler> work_;
48  Status status_;
49 
50  base::WaitableEvent wait_;
51 };
52 
53 // Similar to a thread pool, JobManager manages multiple jobs that are expected
54 // to run in parallel. It can be used to register, run, and stop a batch of
55 // jobs.
56 class JobManager {
57  public:
58  // @param sync_points is an optional SyncPointQueue used to synchronize and
59  // align cue points. JobManager cancels @a sync_points when any job
60  // fails or is cancelled. It can be NULL.
61  explicit JobManager(std::unique_ptr<SyncPointQueue> sync_points);
62 
63  virtual ~JobManager() = default;
64 
65  // Create a new job entry by specifying the origin handler at the top of the
66  // chain and a name for the thread. This will only register the job. To start
67  // the job, you need to call |RunJobs|.
68  void Add(const std::string& name, std::shared_ptr<OriginHandler> handler);
69 
70  // Initialize all registered jobs. If any job fails to initialize, this will
71  // return the error and it will not be safe to call |RunJobs| as not all jobs
72  // will be properly initialized.
73  virtual Status InitializeJobs();
74 
75  // Run all registered jobs. Before calling this make sure that
76  // |InitializedJobs| returned |Status::OK|. This call is blocking and will
77  // block until all jobs exit.
78  virtual Status RunJobs();
79 
80  // Ask all jobs to stop running. This call is non-blocking and can be used to
81  // unblock a call to |RunJobs|.
82  void CancelJobs();
83 
84  SyncPointQueue* sync_points() { return sync_points_.get(); }
85 
86  protected:
87  JobManager(const JobManager&) = delete;
88  JobManager& operator=(const JobManager&) = delete;
89 
90  struct JobEntry {
91  std::string name;
92  std::shared_ptr<OriginHandler> worker;
93  };
94  // Stores Job entries for delayed construction of Job object.
95  std::vector<JobEntry> job_entries_;
96  std::vector<std::unique_ptr<Job>> jobs_;
97  // Stored in JobManager so JobManager can cancel |sync_points| when any job
98  // fails or is cancelled.
99  std::unique_ptr<SyncPointQueue> sync_points_;
100 };
101 
102 } // namespace media
103 } // namespace shaka
104 
105 #endif // PACKAGER_APP_JOB_MANAGER_H_
A synchronized queue for cue points.
All the methods that are virtual are virtual for mocking.