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  // Create a new job entry by specifying the origin handler at the top of the
64  // chain and a name for the thread. This will only register the job. To start
65  // the job, you need to call |RunJobs|.
66  void Add(const std::string& name, std::shared_ptr<OriginHandler> handler);
67 
68  // Initialize all registered jobs. If any job fails to initialize, this will
69  // return the error and it will not be safe to call |RunJobs| as not all jobs
70  // will be properly initialized.
71  Status InitializeJobs();
72 
73  // Run all registered jobs. Before calling this make sure that
74  // |InitializedJobs| returned |Status::OK|. This call is blocking and will
75  // block until all jobs exit.
76  Status RunJobs();
77 
78  // Ask all jobs to stop running. This call is non-blocking and can be used to
79  // unblock a call to |RunJobs|.
80  void CancelJobs();
81 
82  SyncPointQueue* sync_points() { return sync_points_.get(); }
83 
84  private:
85  JobManager(const JobManager&) = delete;
86  JobManager& operator=(const JobManager&) = delete;
87 
88  struct JobEntry {
89  std::string name;
90  std::shared_ptr<OriginHandler> worker;
91  };
92  // Stores Job entries for delayed construction of Job object.
93  std::vector<JobEntry> job_entries_;
94  std::vector<std::unique_ptr<Job>> jobs_;
95  // Stored in JobManager so JobManager can cancel |sync_points| when any job
96  // fails or is cancelled.
97  std::unique_ptr<SyncPointQueue> sync_points_;
98 };
99 
100 } // namespace media
101 } // namespace shaka
102 
103 #endif // PACKAGER_APP_JOB_MANAGER_H_
All the methods that are virtual are virtual for mocking.
A synchronized queue for cue points.