c_jobmanager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #ifndef __C_JOBMANAGER_H__
  2. #define __C_JOBMANAGER_H__
  3. #include <vector>
  4. #ifdef _WIN32
  5. #include <wtypes.h>
  6. #include <winbase.h> // for mutex support
  7. #define T_MUTEX HANDLE
  8. #else // _WIN32
  9. #error "This won't compile under anything other than windows since I haven't implemented mutexing on anything else"
  10. #endif // _WIN32
  11. template<class T> class C_JOBMANAGER {
  12. public:
  13. typedef int (*T_JOBHANDLER)(int state, int last_state, T *userData);
  14. private:
  15. struct T_JOB {
  16. int state;
  17. int last_state;
  18. int suspended;
  19. T *userData;
  20. };
  21. struct T_HANDLER {
  22. int state;
  23. int last_state;
  24. T_JOBHANDLER jobHandler;
  25. };
  26. std::vector<T_JOB*> JobList;
  27. std::vector<T_HANDLER*> HandlerList;
  28. T_MUTEX mutex;
  29. protected:
  30. int waitForMutex() {
  31. #ifdef _WIN32
  32. if(WaitForSingleObject(mutex,INFINITE) == WAIT_OBJECT_0) return 1;
  33. #else // _WIN32
  34. // insert mutex magic here
  35. #endif // _WIN32
  36. return 0;
  37. }
  38. void releaseMutex() {
  39. #ifdef _WIN32
  40. ReleaseMutex(mutex);
  41. #else // _WIN32
  42. // insert mutex magic here
  43. #endif // _WIN32
  44. }
  45. public:
  46. C_JOBMANAGER() {
  47. #ifdef _WIN32
  48. mutex = CreateMutex(NULL,TRUE,NULL);
  49. ReleaseMutex(mutex);
  50. #else // _WIN32
  51. // insert mutex magic here
  52. #endif // _WIN32
  53. }
  54. virtual ~C_JOBMANAGER() {
  55. waitForMutex();
  56. #ifdef _WIN32
  57. CloseHandle(mutex);
  58. mutex = NULL;
  59. #else // _WIN32
  60. // insert mutex magic here
  61. #endif // _WIN32
  62. //JobList.deleteAll();
  63. for (auto job : JobList)
  64. {
  65. delete job;
  66. }
  67. JobList.clear();
  68. //HandlerList.deleteAll();
  69. for (auto handler : HandlerList)
  70. {
  71. delete handler;
  72. }
  73. HandlerList.clear();
  74. }
  75. T *operator[](int job) {
  76. if(!waitForMutex()) return NULL;
  77. T_JOB *j = JobList[job];
  78. T *val = NULL;
  79. if(j) val = j->userData;
  80. releaseMutex();
  81. return val;
  82. }
  83. virtual int AddJob(int state, T *userData, int suspended = 0, int isUserUnique = 1) {
  84. if(!waitForMutex()) return -1;
  85. int n = JobList.size();
  86. if(isUserUnique && n) {
  87. for(int i = n-1; i >= 0; i--) {
  88. T_JOB *item = JobList[i];
  89. if(item) {
  90. if(item->userData == userData) {
  91. releaseMutex();
  92. return -1;
  93. }
  94. }
  95. }
  96. }
  97. T_JOB *job = new T_JOB;
  98. job->last_state = OUT_DISCONNECTED;
  99. job->state = state;
  100. job->suspended = suspended;
  101. job->userData = userData;
  102. JobList.push_back(job);
  103. releaseMutex();
  104. return n;
  105. }
  106. virtual int GetJobState(int job) {
  107. int retval = -1;
  108. if(waitForMutex()) {
  109. int n = JobList.size();
  110. if(job < n && job >= 0) retval = JobList[job]->state;
  111. releaseMutex();
  112. }
  113. return retval;
  114. }
  115. virtual void SetJobState(int job, int state) {
  116. if(!waitForMutex()) return;
  117. int n = JobList.size();
  118. if(job < n && job >= 0) JobList[job]->state = state;
  119. releaseMutex();
  120. }
  121. virtual void SuspendJob(int job, int suspended) {
  122. if(!waitForMutex()) return;
  123. int n = JobList.size();
  124. if(job < n && job >= 0) JobList[job]->suspended = suspended;
  125. releaseMutex();
  126. }
  127. virtual void DelJob(int job) {
  128. if(!waitForMutex()) return;
  129. int n = JobList.size();
  130. if(job < n && job >= 0) {
  131. delete JobList[job];
  132. JobList.erase(JobList.begin() + job);
  133. }
  134. releaseMutex();
  135. }
  136. virtual void ClearJobs() {
  137. if(!waitForMutex())
  138. return;
  139. //JobList.deleteAll();
  140. for (auto job : JobList)
  141. {
  142. delete job;
  143. }
  144. JobList.clear();
  145. releaseMutex();
  146. }
  147. virtual void AddHandler(int state, T_JOBHANDLER jobHandler) {
  148. if(!waitForMutex()) return;
  149. int n = HandlerList.size();
  150. for(int i = n-1; i >= 0; i--) {
  151. T_HANDLER *item = HandlerList[i];
  152. if(item) {
  153. if(item->state == state) {
  154. releaseMutex();
  155. return;
  156. }
  157. }
  158. }
  159. T_HANDLER *handler = new T_HANDLER;
  160. handler->state = state;
  161. handler->jobHandler = jobHandler;
  162. HandlerList.push_back(handler);
  163. releaseMutex();
  164. }
  165. virtual void DelHandler(int state) {
  166. if(!waitForMutex()) return;
  167. int n = HandlerList.size();
  168. for(int i = n-1; i >= 0; i--) {
  169. T_HANDLER *item = HandlerList[i];
  170. if(item) {
  171. if(item->state == state) {
  172. delete HandlerList[i];
  173. HandlerList.erase(HandlerList.begin() + i);
  174. releaseMutex();
  175. return;
  176. }
  177. }
  178. }
  179. releaseMutex();
  180. }
  181. virtual void ClearHandlers() {
  182. if(!waitForMutex())
  183. return;
  184. //HandlerList.deleteAll();
  185. for (auto handler : HandlerList)
  186. {
  187. delete handler;
  188. }
  189. HandlerList.clear();
  190. releaseMutex();
  191. }
  192. virtual int GetNumJobs() {
  193. if(!waitForMutex()) return -1;
  194. int n = JobList.size();
  195. releaseMutex();
  196. return n;
  197. }
  198. virtual int GetNumHandlers() {
  199. if(!waitForMutex()) return -1;
  200. int n = HandlerList.size();
  201. releaseMutex();
  202. return n;
  203. }
  204. virtual void Run(int job) {
  205. if(!waitForMutex()) return;
  206. int nJ = JobList.size();
  207. int nH = HandlerList.size();
  208. if(job < nJ && job >= 0) {
  209. T_JOB *job_item = JobList[job];
  210. for(int i = nH-1; i >= 0; i--) {
  211. T_HANDLER *handler = HandlerList[i];
  212. if(handler) {
  213. if(handler->state == job_item->state) {
  214. if(!job_item->suspended) {
  215. int cur_state = job_item->state;
  216. job_item->state = handler->jobHandler(job_item->state,job_item->last_state,job_item->userData);
  217. job_item->last_state = cur_state;
  218. }
  219. break;
  220. }
  221. }
  222. }
  223. }
  224. releaseMutex();
  225. }
  226. };
  227. #endif // !__C_JOBMANAGER_H__