job.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_JOB_H
00021 #define ARCHON_UTILITIES_JOB_H
00022 
00023 #include <string>
00024 #include <queue>
00025 #include <list>
00026 #include <deque>
00027 
00028 #include <archon/util/thread.H>
00029 
00030 namespace Archon
00031 {
00032   namespace Utilities
00033   {
00034     using namespace std;
00035 
00039     struct Job: virtual RefObjectBase
00040     {
00041       Job(): thread(0) {}
00042 
00043       virtual ~Job() {}
00044 
00045       virtual void main() = 0;
00046 
00050       struct Queue
00051       {
00052         Queue(int maxThreads = 8, Time maxIdleTime = Time(10.0));
00053 
00057         ~Queue();
00058 
00059         void add(Ref<Job>);
00060 
00065         void cancel(Ref<Job>);
00066 
00070         void wait();
00071 
00072       private:
00073         const int maxThreads;
00074         const Time maxIdleTime;
00075 
00076         Mutex mutex;
00077         Condition newJob;     // Signalled when a new job is added to the queue of pending jobs.
00078         Condition threadQuit; // Signalled each time a thread quits.
00079         Condition allDone;    // Signalled when all threads are idle and there is no pending jobs.
00080 
00081         unsigned long nextThreadId;
00082         list<Ref<Job> > pendingJobs;
00083         list<unsigned long> idleThreads;
00084         int activeThreads;
00085         bool shutdown;
00086 
00087         void worker();
00088         static void workerEntry(Queue *);
00089       };
00090 
00091     private:
00092       Thread *thread;
00093     };
00094   }
00095 }
00096 
00097 #endif // ARCHON_UTILITIES_JOB_H

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4