2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2014, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
11 #include <ObjectList.h>
12 #include <Referenceable.h>
14 #include <util/DoublyLinkedList.h>
15 #include <util/OpenHashTable.h>
23 JOB_STATE_UNSCHEDULED
,
31 enum job_wait_status
{
32 JOB_DEPENDENCY_NOT_FOUND
,
33 JOB_DEPENDENCY_SUCCEEDED
,
34 JOB_DEPENDENCY_FAILED
,
35 JOB_DEPENDENCY_ABORTED
,
36 JOB_DEPENDENCY_ACTIVE
,
38 JOB_USER_INPUT_WAITING
47 virtual size_t HashValue() const = 0;
49 virtual bool operator==(const JobKey
& other
) const = 0;
53 struct SimpleJobKey
: public JobKey
{
58 SimpleJobKey(const void* object
, uint32 type
);
59 SimpleJobKey(const SimpleJobKey
& other
);
61 virtual size_t HashValue() const;
63 virtual bool operator==(const JobKey
& other
) const;
65 SimpleJobKey
& operator=(const SimpleJobKey
& other
);
71 virtual ~JobListener();
73 virtual void JobStarted(Job
* job
);
74 virtual void JobDone(Job
* job
);
75 virtual void JobWaitingForInput(Job
* job
);
76 virtual void JobFailed(Job
* job
);
77 virtual void JobAborted(Job
* job
);
81 typedef DoublyLinkedList
<Job
> JobList
;
84 class Job
: public BReferenceable
, public DoublyLinkedListLinkImpl
<Job
> {
89 virtual const JobKey
& Key() const = 0;
90 virtual status_t
Do() = 0;
92 Worker
* GetWorker() const { return fWorker
; }
93 job_state
State() const { return fState
; }
95 const BString
& GetDescription() const
96 { return fDescription
; }
99 job_wait_status
WaitFor(const JobKey
& key
);
100 status_t
WaitForUserInput();
101 void SetDescription(const char* format
, ...);
107 void SetWorker(Worker
* worker
);
108 void SetState(job_state state
);
110 Job
* Dependency() const { return fDependency
; }
111 void SetDependency(Job
* job
);
113 JobList
& DependentJobs() { return fDependentJobs
; }
115 job_wait_status
WaitStatus() const { return fWaitStatus
; }
116 void SetWaitStatus(job_wait_status status
);
118 status_t
AddListener(JobListener
* listener
);
119 void RemoveListener(JobListener
* listener
);
120 void NotifyListeners();
123 typedef BObjectList
<JobListener
> ListenerList
;
129 JobList fDependentJobs
;
130 job_wait_status fWaitStatus
;
131 ListenerList fListeners
;
132 BString fDescription
;
147 bool Lock() { return fLock
.Lock(); }
148 void Unlock() { fLock
.Unlock(); }
150 status_t
ScheduleJob(Job
* job
,
151 JobListener
* listener
= NULL
);
152 // always takes over reference
153 void AbortJob(const JobKey
& key
);
154 Job
* GetJob(const JobKey
& key
);
156 status_t
ResumeJob(Job
* job
);
157 // only valid for jobs that are
158 // suspended pending user input
160 bool HasPendingJobs();
162 status_t
AddListener(const JobKey
& key
,
163 JobListener
* listener
);
164 void RemoveListener(const JobKey
& key
,
165 JobListener
* listener
);
170 struct JobHashDefinition
{
171 typedef JobKey KeyType
;
172 typedef Job ValueType
;
174 size_t HashKey(const JobKey
& key
) const
176 return key
.HashValue();
179 size_t Hash(Job
* value
) const
181 return HashKey(value
->Key());
184 bool Compare(const JobKey
& key
, Job
* value
) const
186 return value
->Key() == key
;
189 Job
*& GetLink(Job
* value
) const
195 typedef BOpenHashTable
<JobHashDefinition
> JobTable
;
198 job_wait_status
WaitForJob(Job
* waitingJob
, const JobKey
& key
);
199 status_t
WaitForUserInput(Job
* waitingJob
);
201 static status_t
_WorkerLoopEntry(void* data
);
202 status_t
_WorkerLoop();
205 void _AbortJob(Job
* job
, bool removeFromTable
);
206 void _FinishJob(Job
* job
);
211 JobList fUnscheduledJobs
;
212 JobList fAbortedJobs
;
213 JobList fSuspendedJobs
;
215 thread_id fWorkerThread
;
216 volatile bool fTerminating
;