CrystalSpace

Public API Reference

csutil/threadjobqueue.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2005 by Jorrit Tyberghein
00003               (C) 2005 by Frank Richter
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSUTIL_THREADJOBQUEUE_H__
00021 #define __CS_CSUTIL_THREADJOBQUEUE_H__
00022 
00027 #include "csextern.h"
00028 #include "csutil/fifo.h"
00029 #include "csutil/scf_implementation.h"
00030 #include "csutil/thread.h"
00031 #include "iutil/job.h"
00032 
00033 #include "csutil/threading/condition.h"
00034 #include "csutil/threading/mutex.h"
00035 #include "csutil/threading/thread.h"
00036 
00037 namespace CS
00038 {
00039 namespace Threading
00040 {
00041 
00042 class CS_CRYSTALSPACE_EXPORT ThreadedJobQueue :
00043   public scfImplementation1<ThreadedJobQueue, iJobQueue>
00044 {
00045 public:
00046   ThreadedJobQueue (size_t numWorkers = 1, ThreadPriority priority = THREAD_PRIO_NORMAL);
00047   virtual ~ThreadedJobQueue ();
00048 
00049   virtual void Enqueue (iJob* job);
00050   virtual void PullAndRun (iJob* job);
00051   virtual void Unqueue (iJob* job, bool waitIfCurrent = true);
00052 
00053   enum
00054   {
00055     MAX_WORKER_THREADS = 16
00056   };
00057 
00058 private:
00059   
00060   // Runnable
00061   struct ThreadState;
00062 
00063   class QueueRunnable : public Runnable
00064   {
00065   public:
00066     QueueRunnable (ThreadedJobQueue* queue, ThreadState* ts);
00067 
00068     virtual void Run ();
00069 
00070   private:
00071     ThreadedJobQueue* ownerQueue;
00072     ThreadState* threadState;
00073   };
00074 
00075   // Per thread state
00076   struct ThreadState
00077   {
00078     ThreadState (ThreadedJobQueue* queue)
00079     {
00080       runnable.AttachNew (new QueueRunnable (queue, this));
00081       threadObject.AttachNew (new Thread (runnable, false));
00082     }
00083 
00084     csRef<QueueRunnable> runnable;
00085     csRef<Thread> threadObject;
00086     csRef<iJob> currentJob;
00087     Condition jobFinished;
00088   };
00089 
00090   // Shared queue state
00091   typedef csFIFO<csRef<iJob> > JobFifo;
00092   JobFifo jobQueue;
00093   Mutex jobMutex;
00094   Condition newJob;
00095 
00096   ThreadState* allThreadState[MAX_WORKER_THREADS];
00097   ThreadGroup allThreads;
00098   Mutex threadStateMutex;
00099   Mutex jobFinishMutex;
00100 
00101   size_t numWorkerThreads;
00102   bool shutdownQueue;
00103 };
00104 
00105 }
00106 }
00107 
00108 typedef CS::Threading::ThreadedJobQueue csThreadJobQueue;
00109 
00110 
00111 #endif // __CS_CSUTIL_THREADJOBQUEUE_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3