MPMA Framework 0.4
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     enum ThreadPriority
00017     {
00018         THREAD_LOW, 
00019         THREAD_NORMAL, 
00020         THREAD_HIGH 
00021     };
00022 
00024     union ThreadParam
00025     {
00027         void *ptr;
00029         int val;
00030 
00031         //handy contructors
00032         inline ThreadParam(): val(0) 
00033             {}
00034         inline ThreadParam(void *somePtr): val(0) 
00035             { ptr=somePtr; }
00036         inline ThreadParam(int someInt): ptr(0) 
00037             { val=someInt; }
00038         inline ThreadParam(const ThreadParam &p)
00039             { ptr=p.ptr; val=p.val; }
00040     };
00041 
00044     typedef void (*ThreadFunc)(class Thread&, ThreadParam);
00045 
00047     class Thread
00048     {
00049     public:
00051         Thread(ThreadFunc proc, ThreadParam param);
00052 
00054         ~Thread();
00055 
00057         void SetPriority(ThreadPriority newPriority);
00058 
00060         bool IsRunning() const;
00061 
00063         bool IsEnding() const;
00064 
00065     protected:
00066         void SetEnding();
00067 
00068     private:
00069         //you cannot duplicate a thread
00070         Thread(const Thread&);
00071         const Thread& operator=(const Thread&);
00072 
00073         //internal use to store thread state
00074         void *internal;
00075 
00076         friend class ThreadPool;
00077     };
00078 
00079 
00080     // -- Threadpool class
00081 
00083     class ThreadPool
00084     {
00085     public:
00087         ThreadPool(nuint initialThreads=1, nuint maxThreads=100);
00089         ~ThreadPool();
00090 
00092         void RunThread(ThreadFunc proc, ThreadParam param);
00093 
00094     private:
00095         class ThreadPoolThread
00096         {
00097         public:
00098             ThreadPool *pool;
00099             Thread *thread;
00100             ThreadFunc runFunc;
00101             ThreadParam runParam;
00102             BlockingObject block;
00103             bool isEnding;
00104             bool isAssigned;
00105             bool isReadyToRun;
00106         };
00107 
00108         void GrowPool();
00109         ThreadPoolThread* GetThreadFromPool();
00110         void ReturnThreadToPool(ThreadPoolThread *thread);
00111 
00112         static void ThreadProc(Thread &myThread, ThreadParam param);
00113 
00114         std::list<ThreadPoolThread*> availableThreads;
00115         std::list<ThreadPoolThread*> allThreads;
00116         nuint threadLimit;
00117         nuint threadCount;
00118         SpinLock listLock;
00119         BlockingObject threadReturnBlock;
00120 
00121         //you cannot duplicate a threadpool
00122         ThreadPool(const ThreadPool&);
00123         const ThreadPool& operator=(const ThreadPool&);
00124     };
00125 
00126 
00127     // -- Thread-related helpers
00128 
00130     void Sleep(nuint time);
00131 
00133     nuint GetThreadUniqueIdentifier();
00134 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends