Thread.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "Thread.h"
  2. #include "Globals.h"
  3. using namespace Framework;
  4. // inhalt der Thread Klasse aus Thread.h
  5. // Konstruktor
  6. Thread::Thread()
  7. : ReferenceCounter()
  8. {
  9. threadHandleSys = 0;
  10. threadHandle = 0;
  11. threadId = 0;
  12. thRegister->add(this);
  13. run = 0;
  14. lockCount = 0;
  15. name = 0;
  16. }
  17. // Destruktor
  18. Thread::~Thread()
  19. {
  20. thRegister->remove(this);
  21. #ifdef WIN32
  22. if (threadHandle != 0 && GetCurrentThreadId() == GetThreadId(threadHandle))
  23. return;
  24. #else
  25. if (pthread_self() == threadHandle) return;
  26. #endif
  27. if (run) warteAufThread(100000);
  28. if (run) ende();
  29. }
  30. // nicht constant
  31. void Thread::start() // startet den Thread
  32. {
  33. if (!run)
  34. {
  35. getThis();
  36. #ifdef WIN32
  37. threadHandle = CreateThread(0, 0, threadStart, this, 0, &threadId);
  38. #else
  39. pthread_create(&threadHandle, NULL, threadStart, this);
  40. #endif
  41. }
  42. run = 1;
  43. }
  44. #ifdef WIN32
  45. void Thread::pause() // pausiert den Thread
  46. {
  47. if (run) SuspendThread(threadHandle);
  48. run = 0;
  49. }
  50. void Thread::fortsetzen() // pausiert den Thread
  51. {
  52. if (!run) ResumeThread(threadHandle);
  53. run = 1;
  54. }
  55. #endif
  56. void Thread::ende() // beendet den Thread
  57. {
  58. if (run)
  59. {
  60. while (lockCount > 0)
  61. Sleep(100);
  62. bool rel = run;
  63. #ifdef WIN32
  64. # pragma warning(suppress : 6258)
  65. TerminateThread(threadHandle, 0);
  66. #else
  67. if (pthread_self() == threadHandle)
  68. {
  69. thRegister->addClosedThread(threadHandle);
  70. run = 0;
  71. }
  72. pthread_cancel(threadHandle);
  73. #endif
  74. if (rel)
  75. {
  76. run = 0;
  77. release();
  78. return;
  79. }
  80. }
  81. run = 0;
  82. }
  83. void Thread::thread() // Thread
  84. {}
  85. void Thread::threadEnd()
  86. {
  87. run = 0;
  88. release();
  89. }
  90. // constant
  91. bool Thread::isRunning() const // prüft, ob der Thrad aktiv ist
  92. {
  93. return run;
  94. }
  95. int Thread::warteAufThread(int zeit) // wartet zeit lang auf den Thread
  96. {
  97. #ifdef WIN32
  98. if (!run) return WAIT_OBJECT_0;
  99. if (GetCurrentThreadId() == GetThreadId(threadHandle)) return WAIT_OBJECT_0;
  100. return WaitForSingleObject(threadHandle, zeit);
  101. #else
  102. if (!run) return 0;
  103. if (pthread_self() == threadHandle) return 0;
  104. timespec ts;
  105. ts.tv_sec = 0;
  106. ts.tv_nsec = 1000 * zeit;
  107. if (threadHandleSys) *threadHandleSys = 0;
  108. int ret = pthread_timedjoin_np(threadHandle, 0, &ts);
  109. if (!ret)
  110. threadHandle = 0;
  111. else
  112. *threadHandleSys = threadHandle;
  113. return ret;
  114. #endif
  115. }
  116. // Legt einen Frameworkpointer auf ein Threadhandle fest, der auf 0 gesetzt
  117. // wird, falls die Ressourcen des Threads bereits follstänfig aufgeräumt wurden
  118. // ths: Ein Zeiger auf ein Threadhandle, das verändert werden soll
  119. void Thread::setSystemHandlePointer(pthread_t* ths)
  120. {
  121. threadHandleSys = ths;
  122. *threadHandleSys = threadHandle;
  123. }
  124. pthread_t Thread::getThreadHandle() const
  125. {
  126. return threadHandle;
  127. }
  128. // set the name of the thread
  129. void Thread::setName(const char* name)
  130. {
  131. this->name = name;
  132. }
  133. // get the name of the thread
  134. const char* Thread::getName() const
  135. {
  136. return name ? name : typeid(*this).name();
  137. }
  138. void Thread::addCriticalLock()
  139. {
  140. lockCount++;
  141. }
  142. void Thread::removeCriticalLock()
  143. {
  144. lockCount--;
  145. }
  146. // funktionen
  147. #ifdef WIN32
  148. unsigned long __stdcall Framework::threadStart(void* param)
  149. {
  150. if (istThreadOk((Thread*)param))
  151. {
  152. const char* name = ((Thread*)param)->getName();
  153. wchar_t* wc = new wchar_t[textLength(name) + 1];
  154. size_t l;
  155. mbstowcs_s(&l, wc, textLength(name) + 1, name, textLength(name));
  156. SetThreadDescription(((Thread*)param)->getThreadHandle(), wc);
  157. delete[] wc;
  158. ((Thread*)param)->thread();
  159. }
  160. if (istThreadOk((Thread*)param)) ((Thread*)param)->threadEnd();
  161. return 0;
  162. }
  163. #else
  164. void* Framework::threadStart(void* param)
  165. {
  166. pthread_t handle = 0;
  167. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
  168. if (istThreadOk((Thread*)param))
  169. {
  170. pthread_setname_np(
  171. ((Thread*)param)->getThreadHandle(), ((Thread*)param)->getName());
  172. ((Thread*)param)->setSystemHandlePointer(&handle);
  173. ((Thread*)param)->thread();
  174. }
  175. if (istThreadOk((Thread*)param)) ((Thread*)param)->threadEnd();
  176. thRegister->addClosedThread(handle);
  177. pthread_exit(0);
  178. return 0;
  179. }
  180. #endif
  181. // Konstruktor
  182. ThreadRegister::ThreadRegister()
  183. {
  184. InitializeCriticalSection(&cs);
  185. }
  186. // Destruktor
  187. ThreadRegister::~ThreadRegister()
  188. {
  189. EnterCriticalSection(&cs);
  190. while (threads.hat(0))
  191. {
  192. LeaveCriticalSection(&cs);
  193. Sleep(1000); // wait until all threads are done
  194. EnterCriticalSection(&cs);
  195. }
  196. cleanUpClosedThreads();
  197. LeaveCriticalSection(&cs);
  198. DeleteCriticalSection(&cs);
  199. }
  200. // Inhalt der ThreadRegister Klasse aus Thread.h
  201. void ThreadRegister::add(Thread* t)
  202. {
  203. EnterCriticalSection(&cs);
  204. threads.add(t);
  205. LeaveCriticalSection(&cs);
  206. }
  207. void ThreadRegister::remove(Thread* t)
  208. {
  209. EnterCriticalSection(&cs);
  210. threads.remove(threads.getWertIndex(t));
  211. LeaveCriticalSection(&cs);
  212. }
  213. bool ThreadRegister::isThread(Thread* t)
  214. {
  215. EnterCriticalSection(&cs);
  216. bool ret = threads.hat(threads.getWertIndex(t));
  217. LeaveCriticalSection(&cs);
  218. return ret;
  219. }
  220. void ThreadRegister::addClosedThread(pthread_t handle)
  221. {
  222. EnterCriticalSection(&cs);
  223. if (handle) closedThreads.add(handle);
  224. LeaveCriticalSection(&cs);
  225. }
  226. // Sucht nach einem bestimmten Thread und gibt das zugehörige Objekt zurück
  227. // handle: Ein handle zu dem gesuchten Thread
  228. Thread* ThreadRegister::zThread(pthread_t handle)
  229. {
  230. EnterCriticalSection(&cs);
  231. for (auto i : threads)
  232. {
  233. if (i->getThreadHandle()
  234. && GetThreadId(i->getThreadHandle()) == GetThreadId(handle)
  235. && GetThreadId(handle) != 0)
  236. {
  237. LeaveCriticalSection(&cs);
  238. return i;
  239. }
  240. }
  241. LeaveCriticalSection(&cs);
  242. return 0;
  243. }
  244. // sperrt das register
  245. void ThreadRegister::lock()
  246. {
  247. EnterCriticalSection(&cs);
  248. }
  249. // entsperrt das register
  250. void ThreadRegister::unlock()
  251. {
  252. LeaveCriticalSection(&cs);
  253. }
  254. // Löscht die bereits beendetetn Threads und gibt ihre Reccourcen wieder frei
  255. void ThreadRegister::cleanUpClosedThreads()
  256. {
  257. EnterCriticalSection(&cs);
  258. while (closedThreads.getEintragAnzahl() > 0)
  259. {
  260. #ifndef WIN32
  261. pthread_join(closedThreads.get(0), 0);
  262. #endif
  263. closedThreads.remove(0);
  264. }
  265. LeaveCriticalSection(&cs);
  266. }