Shaka Packager SDK
Loading...
Searching...
No Matches
job_manager.h
1// Copyright 2017 Google LLC. 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 <functional>
11#include <map>
12#include <memory>
13#include <string>
14#include <thread>
15#include <vector>
16
17#include <absl/base/thread_annotations.h>
18#include <absl/synchronization/mutex.h>
19
20#include <packager/status.h>
21
22namespace shaka {
23namespace media {
24
25class OriginHandler;
26class SyncPointQueue;
27
28// A job is a single line of work that is expected to run in parallel with
29// other jobs.
30class Job {
31 public:
32 typedef std::function<void(Job*)> OnCompleteFunction;
33
34 Job(const std::string& name,
35 std::shared_ptr<OriginHandler> work,
36 OnCompleteFunction on_complete);
37
38 // Initialize the work object. Call before Start() or Run(). Updates status()
39 // and returns it for convenience.
40 const Status& Initialize();
41
42 // Begin the job in a new thread. This is only a request and will not block.
43 // If you want to wait for the job to complete, use |complete|.
44 // Use either Start() for threaded operation or Run() for non-threaded
45 // operation. DO NOT USE BOTH!
46 void Start();
47
48 // Run the job's work synchronously, blocking until complete. Updates status()
49 // and returns it for convenience.
50 // Use either Start() for threaded operation or Run() for non-threaded
51 // operation. DO NOT USE BOTH!
52 const Status& Run();
53
54 // Request that the job stops executing. This is only a request and will not
55 // block. If you want to wait for the job to complete, use |complete|.
56 void Cancel();
57
58 // Join the thread, if any was started. Blocks until the thread has stopped.
59 void Join();
60
61 // Get the current status of the job. If the job failed to initialize or
62 // encountered an error during execution this will return the error.
63 const Status& status() const { return status_; }
64
65 // The name given to this job in the constructor.
66 const std::string& name() const { return name_; }
67
68 private:
69 Job(const Job&) = delete;
70 Job& operator=(const Job&) = delete;
71
72 std::string name_;
73 std::shared_ptr<OriginHandler> work_;
74 OnCompleteFunction on_complete_;
75 std::unique_ptr<std::thread> thread_;
76 Status status_;
77};
78
79// Similar to a thread pool, JobManager manages multiple jobs that are expected
80// to run in parallel. It can be used to register, run, and stop a batch of
81// jobs.
83 public:
84 // @param sync_points is an optional SyncPointQueue used to synchronize and
85 // align cue points. JobManager cancels @a sync_points when any job
86 // fails or is cancelled. It can be NULL.
87 explicit JobManager(std::unique_ptr<SyncPointQueue> sync_points);
88
89 virtual ~JobManager() = default;
90
91 // Create a new job entry by specifying the origin handler at the top of the
92 // chain and a name for the thread. This will only register the job. To start
93 // the job, you need to call |RunJobs|.
94 void Add(const std::string& name, std::shared_ptr<OriginHandler> handler);
95
96 // Initialize all registered jobs. If any job fails to initialize, this will
97 // return the error and it will not be safe to call |RunJobs| as not all jobs
98 // will be properly initialized.
99 Status InitializeJobs();
100
101 // Run all registered jobs. Before calling this make sure that
102 // |InitializedJobs| returned |Status::OK|. This call is blocking and will
103 // block until all jobs exit.
104 virtual Status RunJobs();
105
106 // Ask all jobs to stop running. This call is non-blocking and can be used to
107 // unblock a call to |RunJobs|.
108 void CancelJobs();
109
110 SyncPointQueue* sync_points() { return sync_points_.get(); }
111
112 protected:
113 JobManager(const JobManager&) = delete;
114 JobManager& operator=(const JobManager&) = delete;
115
116 void OnJobComplete(Job* job);
117
118 // Stored in JobManager so JobManager can cancel |sync_points| when any job
119 // fails or is cancelled.
120 std::unique_ptr<SyncPointQueue> sync_points_;
121
122 std::vector<std::unique_ptr<Job>> jobs_;
123
124 absl::Mutex mutex_;
125 std::map<Job*, bool> complete_ ABSL_GUARDED_BY(mutex_);
126 absl::CondVar any_job_complete_ ABSL_GUARDED_BY(mutex_);
127};
128
129} // namespace media
130} // namespace shaka
131
132#endif // PACKAGER_APP_JOB_MANAGER_H_
A synchronized queue for cue points.
All the methods that are virtual are virtual for mocking.