|
|
@@ -1,3 +1,56 @@ |
|
|
|
// SNF4CGP/WoorkerPool.hpp
|
|
|
|
// Copyright (C) 2009 ARM Research Labs, LLC
|
|
|
|
// See www.armresearch.com for more information.
|
|
|
|
//
|
|
|
|
// The worker pool provides threads for processing command jobs. A worker
|
|
|
|
// thread will process a Job until Job.doIt() returns. At that point the worker
|
|
|
|
// thread puts itself back into the pool.
|
|
|
|
//
|
|
|
|
// A production gateway keeps the worker blocked until there is something to do.
|
|
|
|
// For a worker thread there are really only two things to do. Do a job, or stop.
|
|
|
|
//
|
|
|
|
// When the worker pool is asked to provide a Worker it either gives the next
|
|
|
|
// worker it has handy, or if there are none it makes one and gives that away.
|
|
|
|
|
|
|
|
#ifndef IncludedWorkerPool
|
|
|
|
#define IncludedWorkerPool
|
|
|
|
|
|
|
|
#include "../SNF4CGP/CodeDweller/threading.hpp"
|
|
|
|
|
|
|
|
#include "JobPool.hpp"
|
|
|
|
|
|
|
|
class WorkerPool; // There will be a worker pool.
|
|
|
|
|
|
|
|
class Worker : private Thread { // This is a worker thread.
|
|
|
|
private:
|
|
|
|
WorkerPool& Workers; // Every worker knows where it lives.
|
|
|
|
ProductionGateway StoppingPoint; // Thread stops here and waits.
|
|
|
|
void myTask(); // Task for the thread. (my loop).
|
|
|
|
Job* myJob_; // The job to do (or 0 to stop).
|
|
|
|
|
|
|
|
public:
|
|
|
|
Worker(WorkerPool& W); // Initialize and start the thread.
|
|
|
|
virtual ~Worker(); // Cleanup on the way down.
|
|
|
|
void doJob(Job& J); // Give this worker a job to do.
|
|
|
|
stop(); // Stop the worker thread.
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerPool { // Worker pools look like this...
|
|
|
|
private:
|
|
|
|
Mutex myMutex; // Worker pool allocation control.
|
|
|
|
ProductionQueue<Worker*> RecycledWorkers; // Where do recyceled workers go.
|
|
|
|
unsigned int AllocatedWorkers; // Count of workers to clean up.
|
|
|
|
bool Started; // True if we're ready to go.
|
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerPool(); // Set startup defaults.
|
|
|
|
~WorkerPool(); // Cleanup if needed.
|
|
|
|
void init(); // Start making workers.
|
|
|
|
Worker& grab(); // Give me a worker to use.
|
|
|
|
void drop(Worker& W); // Take this worker back.
|
|
|
|
void stop(); // Destroy all workers and stop.
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|