missing project/build files
[client-tools.git] / src / external / 3rd / library / soePlatform / CSAssist / utils / Base / solaris / Thread.h
blob42056856b01b8c68a8c9f5577dee003e0cd37c72
1 ////////////////////////////////////////
2 // Thread.h
3 //
4 // Purpose:
5 // 1. Declair the CThread class that encapsulates threading functionality.
6 // This abstract base class in intended to be used to encapsulate
7 // individual tasks that require threading in derived classes.
8 //
9 // Revisions:
10 // 07/10/2001 Created
13 #ifndef BASE_SOLARIS_THREAD_H
14 #define BASE_SOLARIS_THREAD_H
16 #if !defined(_REENTRANT)
17 # pragma message( "Excluding Base::CThread - requires multi-threaded compile. (_REENTRANT)" )
18 #else
21 #pragma warning( disable : 4786)
23 #include <list>
24 #include <set>
25 #include "Platform.h"
26 #include "Mutex.h"
27 #include "Event.h"
29 namespace Base
32 ////////////////////////////////////////
33 // Class:
34 // CThread
36 // Purpose:
37 // Encapsulates threading functionality. Creating classes derived
38 // from CThread provides an easy way to encapsulate tasks that require
39 // their own thread.
41 // Public Methods:
42 // StartThread() : Creates the low-level thread handle and begins executing
43 // the CThread::ThreadProc() function within the new thread.
44 // StopThread() : Signals the ThreadProc() function to stop executing using
45 // the mThreadContinue member variable, and waits for the ThreadProc()
46 // function to exit. By default, the function will block for a maximum
47 // of 5 seconds before exiting without the thread halting.
48 // IsThreadActive() : Returns true if the physical thread is still executing
49 // within the ThreadProc() function, otherwise it returns false.
50 // ThreadProc() : Pure-virtual function that will be executed when the StartThread()
51 // function is called. Derived classes must implement this function. The
52 // mThreadContinue member variable should be used internal the the ThreadProc()
53 // function to indicate whether it should continue executing or exit.
54 // Protected Attributes:
55 // mThreadContinue : Boolean value indicating to the ThreadProc() function
56 // whether to continue executing or to exit. If mThreadContinue is true,
57 // ThreadProc() should continue, otherwise ThreadProc() should exit. It
58 // left up to the derived class to implement a ThreadProc() function that
59 // uses the mThreadContinue member.
60 //
62 class CThread
64 friend void * threadProc(void *);
66 public:
67 enum { eSTOP_SUCCESS, eSTOP_TIMEOUT };
68 public:
69 CThread();
70 virtual ~CThread();
72 void StartThread();
73 int32 StopThread(int timeout=5);
74 bool IsThreadActive() { return mThreadActive; }
76 protected:
77 virtual void ThreadProc() = 0;
79 protected:
80 bool mThreadContinue;
81 private:
82 bool mThreadActive;
83 pthread_t mThreadID;
87 class CThreadPool
89 private:
90 class CMember : public CThread
92 public:
93 CMember(CThreadPool * parent);
94 virtual ~CMember();
96 bool Execute(void( *function )( void * ), void * arg);
97 void Destroy();
99 protected:
100 virtual void ThreadProc();
102 private:
103 CThreadPool * mParent;
104 void( * mFunction )( void * );
105 void * mArgument;
106 CEvent mSemaphore;
108 friend class CMember;
110 public:
111 CThreadPool(uint32 maxThreads, uint32 minThreads=1, uint32 timeout=15*60);
112 ~CThreadPool();
114 bool Execute(void( *function )( void * ), void * arg);
116 private:
117 uint32 GetTimeOut();
118 void OnIdle(CMember * member);
119 void OnBusy(CMember * member);
120 void OnDestory(CMember * member);
122 private:
123 CMutex mMutex;
124 std::set<CMember *> mIdleMember;
125 std::set<CMember *> mBusyMember;
126 std::list<CMember *> mNullMember;
127 uint32 mThreadCount;
129 uint32 mMaxThreads;
130 uint32 mMinThreads;
131 uint32 mTimeOut;
137 #endif // #if defined(_MT)
139 #endif // BASE_SOLARIS_THREAD_H