Thread.h

Go to the documentation of this file.
00001 
00002 //Luke Lenhart (2007)
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 #include "Types.h"
00008 #include "Locks.h"
00009 #include <list>
00010 
00011 namespace MPMA
00012 {
00013     // -- Thread Class
00014 
00016     union ThreadParam
00017     {
00019         void *ptr;
00021         int val;
00022 
00023         //handy contructors
00024         inline ThreadParam(): val(0) 
00025             {}
00026         inline ThreadParam(void *somePtr): val(0) 
00027             { ptr=somePtr; }
00028         inline ThreadParam(int someInt): ptr(0) 
00029             { val=someInt; }
00030         inline ThreadParam(const ThreadParam &p)
00031             { ptr=p.ptr; val=p.val; }
00032     };
00033 
00036     typedef void (*ThreadFunc)(class Thread&, ThreadParam);
00037 
00039     class Thread
00040     {
00041     public:
00043         Thread(ThreadFunc proc, ThreadParam param);
00044 
00046         ~Thread();
00047 
00049         void SetPriorityLow(bool isLowPriority);
00050 
00052         bool IsRunning() const;
00053 
00055         bool IsEnding() const;
00056 
00057     protected:
00058         void SetEnding();
00059 
00060     private:
00061         //you cannot duplicate a thread
00062         Thread(const Thread&);
00063         const Thread& operator=(const Thread&);
00064 
00065         //internal use to store thread state
00066         void *internal;
00067 
00068         friend class ThreadPool;
00069     };
00070 
00071 
00072     // -- Threadpool class
00073 
00075     class ThreadPool
00076     {
00077     public:
00079         ThreadPool(uint initialThreads=1, uint maxThreads=100);
00081         ~ThreadPool();
00082 
00084         void RunThread(ThreadFunc proc, ThreadParam param);
00085 
00086     private:
00087         class ThreadPoolThread
00088         {
00089         public:
00090             ThreadPool *pool;
00091             Thread *thread;
00092             ThreadFunc runFunc;
00093             ThreadParam runParam;
00094             BlockingObject block;
00095             bool isEnding;
00096             bool isAssigned;
00097         };
00098 
00099         void GrowPool();
00100         ThreadPoolThread* GetThreadFromPool();
00101         void ReturnThreadToPool(ThreadPoolThread *thread);
00102 
00103         static void ThreadProc(Thread &myThread, ThreadParam param);
00104 
00105         std::list<ThreadPoolThread*> availableThreads;
00106         std::list<ThreadPoolThread*> allThreads;
00107         uint threadLimit;
00108         uint threadCount;
00109         SpinLock listLock;
00110         BlockingObject threadReturnBlock;
00111 
00112         //you cannot duplicate a threadpool
00113         ThreadPool(const ThreadPool&);
00114         const ThreadPool& operator=(const ThreadPool&);
00115     };
00116 
00117 
00118     // -- Thread-related helpers
00119 
00121     void Sleep(uint time);
00122 }

Generated on Sat Aug 9 15:05:05 2008 for MPMA Framework by  doxygen 1.5.6