timers_unix: Fix termination problem of WaitReceiveTaskEnd
authorRobert Lehmann <robert.lehmann@sitec-systems.de>
Tue, 28 Jul 2015 16:36:55 +0200
changeset 793 72e9e1064432
parent 791 7740ac6fdedc
child 794 8bfe0ac00cdb
timers_unix: Fix termination problem of WaitReceiveTaskEnd

The function pthread_kill sends the Signal thread and to the own process.
If you use this construct than the application which calls uses the
canfestival api will terminate at the call of canClose. To avoid that
use pthread_cancel instead of pthread_kill. To use the pthread_cancel call
you need to set the cancel ability in the thread function. That means
you need to call pthread_setcancelstate and pthread_setcanceltype.
For the termination of the thread at any time it is important to set the
cancel type to PTHREAD_CANCEL_ASYNCHRONOUS.
drivers/timers_unix/timers_unix.c
--- a/drivers/timers_unix/timers_unix.c	Thu Jun 12 14:07:16 2014 +0200
+++ b/drivers/timers_unix/timers_unix.c	Tue Jul 28 16:36:55 2015 +0200
@@ -1,12 +1,12 @@
 #include <stdlib.h>
 
 #include <sys/time.h>
-#include <pthread.h> 
+#include <pthread.h>
 #include <signal.h>
 #include <time.h>
 
-#include "applicfg.h"
-#include "timer.h"
+#include <applicfg.h>
+#include <timer.h>
 
 static pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
 
@@ -104,6 +104,20 @@
   	//  if(signal(SIGTERM, canReceiveLoop_signal) == SIG_ERR) {
 	//		perror("signal()");
 	//}
+
+    // Set the cancelation state for immediatly cancel the task
+    int ret;
+
+    ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+
+    if (ret != 0)
+        perror("Can't enable the cancelation of the receiving task");
+
+    ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+
+    if (ret != 0)
+        perror("Can't set the asynchronous cancel typ");
+
     unixtimer_ReceiveLoop_task_proc((CAN_PORT)port);
 
     return NULL;
@@ -119,8 +133,8 @@
 
 void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
 {
-	if(pthread_kill(*Thread, SIGTERM)) {
-		perror("pthread_kill()");
+	if(pthread_cancel(*Thread)) {
+		perror("pthread_cancel()");
 	}
 	if(pthread_join(*Thread, NULL)) {
 		perror("pthread_join()");