Thread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  8. threadHandleSys = 0;
  9. threadHandle = 0;
  10. threadId = 0;
  11. thRegister->add( this );
  12. run = 0;
  13. }
  14. // Destruktor
  15. Thread::~Thread()
  16. {
  17. thRegister->remove( this );
  18. #ifdef WIN32
  19. if( GetCurrentThreadId() == GetThreadId( threadHandle ) )
  20. return;
  21. #else
  22. if( pthread_self() == threadHandle )
  23. return;
  24. #endif
  25. if( run )
  26. warteAufThread( 100000 );
  27. if( run )
  28. ende();
  29. }
  30. // nicht constant
  31. void Thread::start() // startet den Thread
  32. {
  33. if( !run )
  34. {
  35. #ifdef WIN32
  36. threadHandle = CreateThread( 0, 0, threadStart, this, 0, &threadId );
  37. #else
  38. pthread_create( &threadHandle, NULL, threadStart, this );
  39. #endif
  40. }
  41. run = 1;
  42. }
  43. #ifdef WIN32
  44. void Thread::pause() // pausiert den Thread
  45. {
  46. if( run )
  47. SuspendThread( threadHandle );
  48. run = 0;
  49. }
  50. void Thread::fortsetzen() // pausiert den Thread
  51. {
  52. if( !run )
  53. ResumeThread( threadHandle );
  54. run = 1;
  55. }
  56. #endif
  57. void Thread::ende() // beendet den Thread
  58. {
  59. if( run )
  60. {
  61. #ifdef WIN32
  62. #pragma warning(suppress: 6258)
  63. TerminateThread( threadHandle, 0 );
  64. #else
  65. if( pthread_self() == threadHandle )
  66. {
  67. thRegister->addClosedThread( threadHandle );
  68. run = 0;
  69. }
  70. pthread_cancel( threadHandle );
  71. #endif
  72. }
  73. run = 0;
  74. }
  75. void Thread::thread() // Thread
  76. {}
  77. void Thread::threadEnd()
  78. {
  79. run = 0;
  80. }
  81. // constant
  82. bool Thread::isRunning() const // prüft, ob der Thrad aktiv ist
  83. {
  84. return run;
  85. }
  86. int Thread::warteAufThread( int zeit ) // wartet zeit lang auf den Thread
  87. {
  88. #ifdef WIN32
  89. if( !run )
  90. return WAIT_OBJECT_0;
  91. if( GetCurrentThreadId() == GetThreadId( threadHandle ) )
  92. return WAIT_OBJECT_0;
  93. return WaitForSingleObject( threadHandle, zeit );
  94. #else
  95. if( !run )
  96. return 0;
  97. if( pthread_self() == threadHandle )
  98. return 0;
  99. if( threadHandleSys )
  100. *threadHandleSys = threadHandle;
  101. int ret = pthread_join( threadHandle, 0 );
  102. threadHandle = 0;
  103. return ret;
  104. #endif
  105. }
  106. // Legt einen Frameworkpointer auf ein Threadhandle fest, der auf 0 gesetzt wird, falls die Ressourcen des Threads bereits follstänfig aufgeräumt wurden
  107. // ths: Ein Zeiger auf ein Threadhandle, das verändert werden soll
  108. void Thread::setSystemHandlePointer( pthread_t *ths )
  109. {
  110. threadHandleSys = ths;
  111. *threadHandleSys = threadHandle;
  112. }
  113. pthread_t Thread::getThreadHandle() const
  114. {
  115. return threadHandle;
  116. }
  117. // funktionen
  118. #ifdef WIN32
  119. unsigned long __stdcall Framework::threadStart( void *param )
  120. {
  121. if( istThreadOk( (Thread *)param ) )
  122. ( (Thread *)param )->thread();
  123. if( istThreadOk( (Thread *)param ) )
  124. ( (Thread *)param )->threadEnd();
  125. return 0;
  126. }
  127. #else
  128. void *Framework::threadStart( void *param )
  129. {
  130. pthread_t handle = 0;
  131. pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, 0 );
  132. if( istThreadOk( (Thread *)param ) )
  133. {
  134. ( (Thread *)param )->setSystemHandlePointer( &handle );
  135. ( (Thread *)param )->thread();
  136. }
  137. if( istThreadOk( (Thread *)param ) )
  138. ( (Thread *)param )->threadEnd();
  139. thRegister->addClosedThread( handle );
  140. pthread_exit( 0 );
  141. return 0;
  142. }
  143. #endif
  144. // Konstruktor
  145. ThreadRegister::ThreadRegister()
  146. {
  147. InitializeCriticalSection( &cs );
  148. }
  149. // Destruktor
  150. ThreadRegister::~ThreadRegister()
  151. {
  152. DeleteCriticalSection( &cs );
  153. }
  154. // Inhalt der ThreadRegister Klasse aus Thread.h
  155. void ThreadRegister::add( Thread *t )
  156. {
  157. EnterCriticalSection( &cs );
  158. threads.add( t );
  159. LeaveCriticalSection( &cs );
  160. }
  161. void ThreadRegister::remove( Thread *t )
  162. {
  163. EnterCriticalSection( &cs );
  164. threads.remove( threads.getWertIndex( t ) );
  165. LeaveCriticalSection( &cs );
  166. }
  167. bool ThreadRegister::isThread( Thread *t )
  168. {
  169. EnterCriticalSection( &cs );
  170. bool ret = threads.hat( threads.getWertIndex( t ) );
  171. LeaveCriticalSection( &cs );
  172. return ret;
  173. }
  174. void ThreadRegister::addClosedThread( pthread_t handle )
  175. {
  176. EnterCriticalSection( &cs );
  177. if( handle )
  178. closedThreads.add( handle );
  179. LeaveCriticalSection( &cs );
  180. }
  181. // Löscht die bereits beendetetn Threads und gibt ihre Reccourcen wieder frei
  182. void ThreadRegister::cleanUpClosedThreads()
  183. {
  184. EnterCriticalSection( &cs );
  185. while( closedThreads.getEintragAnzahl() > 0 )
  186. {
  187. #ifndef WIN32
  188. pthread_join( closedThreads.get( 0 ), 0 );
  189. #endif
  190. closedThreads.remove( 0 );
  191. }
  192. LeaveCriticalSection( &cs );
  193. }