2009-06-19 19:09:56 +04:00
|
|
|
/*
|
2012-08-06 01:54:35 +04:00
|
|
|
* Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2009-06-19 19:09:56 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef WORKER_H
|
|
|
|
#define WORKER_H
|
|
|
|
|
|
|
|
#include <Locker.h>
|
|
|
|
|
|
|
|
#include <ObjectList.h>
|
2009-07-19 03:52:16 +04:00
|
|
|
#include <Referenceable.h>
|
2009-06-19 19:09:56 +04:00
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
class Job;
|
|
|
|
class Worker;
|
|
|
|
|
|
|
|
|
|
|
|
enum job_state {
|
|
|
|
JOB_STATE_UNSCHEDULED,
|
|
|
|
JOB_STATE_WAITING,
|
|
|
|
JOB_STATE_ACTIVE,
|
|
|
|
JOB_STATE_ABORTED,
|
|
|
|
JOB_STATE_FAILED,
|
|
|
|
JOB_STATE_SUCCEEDED
|
|
|
|
};
|
|
|
|
|
|
|
|
enum job_wait_status {
|
|
|
|
JOB_DEPENDENCY_NOT_FOUND,
|
|
|
|
JOB_DEPENDENCY_SUCCEEDED,
|
|
|
|
JOB_DEPENDENCY_FAILED,
|
|
|
|
JOB_DEPENDENCY_ABORTED,
|
|
|
|
JOB_DEPENDENCY_ACTIVE
|
|
|
|
// internal only
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-07-19 03:52:16 +04:00
|
|
|
class JobKey {
|
|
|
|
public:
|
|
|
|
virtual ~JobKey();
|
|
|
|
|
2012-08-06 01:54:35 +04:00
|
|
|
virtual size_t HashValue() const = 0;
|
2009-07-19 03:52:16 +04:00
|
|
|
|
|
|
|
virtual bool operator==(const JobKey& other) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct SimpleJobKey : public JobKey {
|
|
|
|
void* object;
|
|
|
|
uint32 type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SimpleJobKey(void* object, uint32 type);
|
|
|
|
SimpleJobKey(const SimpleJobKey& other);
|
|
|
|
|
2012-08-06 01:54:35 +04:00
|
|
|
virtual size_t HashValue() const;
|
2009-07-19 03:52:16 +04:00
|
|
|
|
|
|
|
virtual bool operator==(const JobKey& other) const;
|
|
|
|
|
|
|
|
SimpleJobKey& operator=(const SimpleJobKey& other);
|
2009-06-19 19:09:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class JobListener {
|
|
|
|
public:
|
|
|
|
virtual ~JobListener();
|
|
|
|
|
|
|
|
virtual void JobDone(Job* job);
|
|
|
|
virtual void JobFailed(Job* job);
|
|
|
|
virtual void JobAborted(Job* job);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef DoublyLinkedList<Job> JobList;
|
|
|
|
|
|
|
|
|
2010-12-16 16:50:30 +03:00
|
|
|
class Job : public BReferenceable, public DoublyLinkedListLinkImpl<Job> {
|
2009-06-19 19:09:56 +04:00
|
|
|
public:
|
|
|
|
Job();
|
|
|
|
virtual ~Job();
|
|
|
|
|
2009-07-19 03:52:16 +04:00
|
|
|
virtual const JobKey& Key() const = 0;
|
2009-06-19 19:09:56 +04:00
|
|
|
virtual status_t Do() = 0;
|
|
|
|
|
|
|
|
Worker* GetWorker() const { return fWorker; }
|
|
|
|
job_state State() const { return fState; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
job_wait_status WaitFor(const JobKey& key);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Worker;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SetWorker(Worker* worker);
|
|
|
|
void SetState(job_state state);
|
|
|
|
|
|
|
|
Job* Dependency() const { return fDependency; }
|
|
|
|
void SetDependency(Job* job);
|
|
|
|
|
|
|
|
JobList& DependentJobs() { return fDependentJobs; }
|
|
|
|
|
|
|
|
job_wait_status WaitStatus() const { return fWaitStatus; }
|
|
|
|
void SetWaitStatus(job_wait_status status);
|
|
|
|
|
|
|
|
status_t AddListener(JobListener* listener);
|
|
|
|
void RemoveListener(JobListener* listener);
|
|
|
|
void NotifyListeners();
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef BObjectList<JobListener> ListenerList;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Worker* fWorker;
|
|
|
|
job_state fState;
|
|
|
|
Job* fDependency;
|
|
|
|
JobList fDependentJobs;
|
|
|
|
job_wait_status fWaitStatus;
|
|
|
|
ListenerList fListeners;
|
2009-07-27 04:39:12 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
Job* fNext;
|
2009-06-19 19:09:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Worker {
|
|
|
|
public:
|
|
|
|
Worker();
|
|
|
|
~Worker();
|
|
|
|
|
|
|
|
status_t Init();
|
|
|
|
void ShutDown();
|
|
|
|
|
|
|
|
bool Lock() { return fLock.Lock(); }
|
|
|
|
void Unlock() { fLock.Unlock(); }
|
|
|
|
|
|
|
|
status_t ScheduleJob(Job* job,
|
|
|
|
JobListener* listener = NULL);
|
2009-07-19 03:52:16 +04:00
|
|
|
// always takes over reference
|
2009-06-19 19:09:56 +04:00
|
|
|
void AbortJob(const JobKey& key);
|
2009-06-20 20:49:31 +04:00
|
|
|
Job* GetJob(const JobKey& key);
|
2009-06-19 19:09:56 +04:00
|
|
|
|
|
|
|
status_t AddListener(const JobKey& key,
|
|
|
|
JobListener* listener);
|
|
|
|
void RemoveListener(const JobKey& key,
|
|
|
|
JobListener* listener);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Job;
|
|
|
|
|
|
|
|
struct JobHashDefinition {
|
|
|
|
typedef JobKey KeyType;
|
|
|
|
typedef Job ValueType;
|
|
|
|
|
|
|
|
size_t HashKey(const JobKey& key) const
|
|
|
|
{
|
|
|
|
return key.HashValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Hash(Job* value) const
|
|
|
|
{
|
|
|
|
return HashKey(value->Key());
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
bool Compare(const JobKey& key, Job* value) const
|
2009-06-19 19:09:56 +04:00
|
|
|
{
|
|
|
|
return value->Key() == key;
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
Job*& GetLink(Job* value) const
|
2009-06-19 19:09:56 +04:00
|
|
|
{
|
2009-07-27 04:39:12 +04:00
|
|
|
return value->fNext;
|
2009-06-19 19:09:56 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<JobHashDefinition> JobTable;
|
2009-06-19 19:09:56 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
job_wait_status WaitForJob(Job* waitingJob, const JobKey& key);
|
|
|
|
|
|
|
|
static status_t _WorkerLoopEntry(void* data);
|
|
|
|
status_t _WorkerLoop();
|
|
|
|
|
2012-07-20 04:21:06 +04:00
|
|
|
void _ProcessJobs();
|
2009-06-19 19:09:56 +04:00
|
|
|
void _AbortJob(Job* job, bool removeFromTable);
|
|
|
|
void _FinishJob(Job* job);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BLocker fLock;
|
|
|
|
JobTable fJobs;
|
|
|
|
JobList fUnscheduledJobs;
|
|
|
|
JobList fAbortedJobs;
|
|
|
|
sem_id fWorkToDoSem;
|
|
|
|
thread_id fWorkerThread;
|
|
|
|
volatile bool fTerminating;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // WORKER_H
|