]> rtime.felk.cvut.cz Git - CanFestival-3.git/blobdiff - drivers/timers_unix/timers_unix.c
Added more correct signal handling to unix timers.
[CanFestival-3.git] / drivers / timers_unix / timers_unix.c
index 4f834fdda9fbd26dac0c3b2de712779b74de1eb6..a32313075b8a8f4019e478dd443738ac7e960606 100644 (file)
@@ -5,7 +5,6 @@
 #include <signal.h>
 
 #include "applicfg.h"
-#include "can_driver.h"
 #include "timer.h"
 
 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -18,6 +17,11 @@ struct timeval last_sig;
 
 timer_t timer;
 
+void TimerCleanup(void)
+{
+       /* only used in realtime apps */
+}
+
 void EnterMutex(void)
 {
        pthread_mutex_lock(&CanFestival_mutex); 
@@ -28,7 +32,7 @@ void LeaveMutex(void)
        pthread_mutex_unlock(&CanFestival_mutex);
 }
 
-void timer_notify(int val)
+void timer_notify(sigval_t val)
 {
        gettimeofday(&last_sig,NULL);
        EnterMutex();
@@ -37,7 +41,7 @@ void timer_notify(int val)
 //     printf("getCurrentTime() return=%u\n", p.tv_usec);
 }
 
-void initTimer(void)
+void TimerInit(void)
 {
        struct sigevent sigev;
 
@@ -53,26 +57,44 @@ void initTimer(void)
        timer_create (CLOCK_REALTIME, &sigev, &timer);
 }
 
-void StopTimerLoop(void)
+void StopTimerLoop(TimerCallback_t exitfunction)
 {
+       EnterMutex();
        timer_delete (timer);
+       exitfunction(NULL,0);
+       LeaveMutex();
 }
 
 void StartTimerLoop(TimerCallback_t init_callback)
 {
-       initTimer();
+       EnterMutex();
        // At first, TimeDispatch will call init_callback.
        SetAlarm(NULL, 0, init_callback, 0, 0);
+       LeaveMutex();
 }
 
-void ReceiveLoop(void* arg)
+void canReceiveLoop_signal(int sig)
+{
+}
+/* We assume that ReceiveLoop_task_proc is always the same */
+static void (*unixtimer_ReceiveLoop_task_proc)(CAN_PORT) = NULL;
+
+/**
+ * Enter in realtime and start the CAN receiver loop
+ * @param port
+ */
+void unixtimer_canReceiveLoop(CAN_PORT port)
 {
-       canReceiveLoop((CAN_HANDLE)arg);
+       
+    /*get signal*/
+    signal(SIGTERM, canReceiveLoop_signal);
+    unixtimer_ReceiveLoop_task_proc(port);
 }
 
-void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread)
+void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
 {
-       pthread_create(Thread, NULL, (void *)&ReceiveLoop, (void*)fd0);
+    unixtimer_ReceiveLoop_task_proc = ReceiveLoopPtr;
+       pthread_create(Thread, NULL, unixtimer_canReceiveLoop, (void*)port);
 }
 
 void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
@@ -81,7 +103,7 @@ void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
        pthread_join(*Thread, NULL);
 }
 
-#define maxval(a,b) a>b?a:b
+#define maxval(a,b) ((a>b)?a:b)
 void setTimer(TIMEVAL value)
 {
 //     printf("setTimer(TIMEVAL value=%d)\n", value);