CMSDK  2.0.1
ThreadManager.h
1 #if !defined(_THREADMANAGER_H_)
2 #define _THREADMANAGER_H_
3 
4 #include "MemoryManager.h"
5 
6 namespace cmlabs {
7 
8 struct ThreadStats {
9  uint32 id; // ID of the thread
10  uint64 created; // Thread creation time
11  uint32 status; // Status of the thread
12  uint32 osID; // OS id of thread
13  ThreadHandle hThread; // Thread handle
14  THREAD_FUNCTION func; // Function to run
15  void* args; // Function argument
16  uint64 time[10]; // Last 10 timestamps
17  uint64 currentCPUTicks[10]; // Last 10 CPU usage readings
18 };
19 
21  uint32 size; // Total size of data in bytes
22  uint32 count; // Number of thread stats
23  uint32 activeCount; // Number of thread stats
24  uint32 statColPolicy; // How statistics are collected
25  uint64 time[10]; // Last 10 timestamps
26  uint64 currentCPUTicks[10]; // Last 10 CPU usage readings
27  uint32 bitFieldSize; // Number of bytes in the bitfield
28  // Bitfield
29  // Stats1, Stats2, ...
30 };
31 
32 
33 class Runnable {
34 public:
35  Runnable() {threadID = 0; shouldContinue = true; isRunning = false;}
36  virtual ~Runnable() {
37  stop();
38  }
39  virtual bool stop(uint32 timeout = 200) {
40  shouldContinue = false;
41  int32 timeleft = (int32)timeout;
42  while (isRunning) {
43  utils::Sleep(5);
44  if ( (timeleft -= 5) <= 0) {
45  LogPrint(0,LOG_SYSTEM,0,"Thread id %u didn't terminate...\n", threadID);
46  return false;
47  }
48  }
49  return true;
50  }
51 
52 protected:
53  uint32 threadID;
54  bool shouldContinue;
55  bool isRunning;
56 };
57 
58 #define GETTHREADSTATS(data,id) (ThreadStats*)(data + sizeof(ThreadDataHeader) + ((ThreadDataHeader*)data)->bitFieldSize + id*sizeof(ThreadStats))
59 
60 class ThreadManager : public Runnable {
61 public:
62  static ThreadManager* Singleton;
63 
64  // Create the ThreadManager Singleton
65  static bool CreateThreadManager();
66  // Create a new thread and start it
67  static bool CreateThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID = 0);
68  // Pause the thread wherever it is now
69  static bool PauseThread(uint32 id);
70  // Allow the thread to continue running
71  static bool ContinueThread(uint32 id);
72  // Terminate the thread and restart it from its base location
73  static bool InterruptThread(uint32 id);
74  // Terminate and destroy the thread
75  static bool TerminateThread(uint32 id);
76  // Has thread terminated / exited
77  static bool IsThreadRunning(uint32 id);
78 
79  // Add thread stats for local thread - needed for pthreads
80  static bool AddLocalThreadStats();
81  // Add thread stats for local thread - needed for pthreads
82  static bool GetLocalThreadID(uint32& id);
83 
84  // Get the statistics for local thread
85  static ThreadStats GetLocalThreadStats();
86  // Get the statistics for a thread
87  static ThreadStats GetThreadStats(uint32 id);
88  // Get the statistics for all threads
89  static ThreadStats* GetAllThreadStats(uint32& count);
90 
91  // Shutdown and delete the ThreadManager
92  static bool Shutdown();
93 
94  ThreadManager();
95  ~ThreadManager();
96  bool init();
97  bool shutdown();
98 
99  int32 threadMonitoring();
100 
101 protected:
102  // Create a new thread and start it
103  bool createThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID);
104  // Pause the thread wherever it is now
105  bool pauseThread(uint32 id);
106  // Allow the thread to continue running
107  bool continueThread(uint32 id);
108  // Terminate the thread and restart it from its base location
109  bool interruptThread(uint32 id);
110  // Terminate and destroy the thread
111  bool terminateThread(uint32 id);
112  // Has thread terminated / exited
113  bool isThreadRunning(uint32 id);
114 
115  // Add thread stats for local thread - needed for pthreads
116  bool addLocalThreadStats();
117  // Get the local thread ID
118  bool getLocalThreadID(uint32& id);
119 
120  // Get the statistics for a thread
121  ThreadStats getThreadStats(uint32 id);
122  // Get the statistics for all threads
123  ThreadStats* getAllThreadStats(uint32& count);
124 
125  // Resize the ThreadStats storage to be able to contain more threads
126  bool resizeThreadStorage(uint32 newCount);
127 
128  utils::Mutex mutex;
129  unsigned char* data;
130 };
131 
132 bool TestThreadManager();
133 
134 static THREAD_RET THREAD_FUNCTION_CALL ThreadMonitoring(void* arg);
135 
136 
137 } // namespace cmlabs
138 
139 #endif //_THREADMANAGER_H_
140 
Definition: Bitmap.h:7
Definition: ThreadManager.h:20
Definition: ThreadManager.h:33
Definition: ThreadManager.h:8
Definition: ThreadManager.h:60
Definition: Utils.h:276