Parcourir la source

Fleshed out WorkerPool.hpp.

Added Started flags to WorkerPool and JobPool.

git-svn-id: https://svn.microneil.com/svn/SNF4CGP/trunk@12 59e8e3e7-56fa-483b-b4b4-fa6ab0af3dfc
master
madscientist il y a 15 ans
Parent
révision
22b1be6bb0
2 fichiers modifiés avec 56 ajouts et 0 suppressions
  1. 3
    0
      SNF4CGP/JobPool.hpp
  2. 53
    0
      SNF4CGP/WorkerPool.hpp

+ 3
- 0
SNF4CGP/JobPool.hpp Voir le fichier

@@ -75,6 +75,9 @@ class JobPool {
Mutex myMutex; // Syncrhonization point.
ProductionQueue<Job*> Jobs; // Recycled jobs holder.
unsigned int AllocatedJobs; // Count of jobs to clean up.
bool Started; // True if we're ready to go.
public:
JobPool(); // Basic construction.

+ 53
- 0
SNF4CGP/WorkerPool.hpp Voir le fichier

@@ -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

Chargement…
Annuler
Enregistrer