10 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
12 TASK_HANDLE TimerLoopThread;
14 TIMEVAL last_time_set = TIMEVAL_MAX;
16 struct timeval last_sig;
20 void TimerCleanup(void)
22 /* only used in realtime apps */
27 pthread_mutex_lock(&CanFestival_mutex);
32 pthread_mutex_unlock(&CanFestival_mutex);
35 void timer_notify(sigval_t val)
37 gettimeofday(&last_sig,NULL);
41 // printf("getCurrentTime() return=%u\n", p.tv_usec);
46 struct sigevent sigev;
48 // Take first absolute time ref.
49 gettimeofday(&last_sig,NULL);
51 memset (&sigev, 0, sizeof (struct sigevent));
52 sigev.sigev_value.sival_int = 0;
53 sigev.sigev_notify = SIGEV_THREAD;
54 sigev.sigev_notify_attributes = NULL;
55 sigev.sigev_notify_function = timer_notify;
57 timer_create (CLOCK_REALTIME, &sigev, &timer);
60 void StopTimerLoop(TimerCallback_t exitfunction)
68 void StartTimerLoop(TimerCallback_t init_callback)
71 // At first, TimeDispatch will call init_callback.
72 SetAlarm(NULL, 0, init_callback, 0, 0);
76 void canReceiveLoop_signal(int sig)
79 /* We assume that ReceiveLoop_task_proc is always the same */
80 static void (*unixtimer_ReceiveLoop_task_proc)(CAN_PORT) = NULL;
83 * Enter in realtime and start the CAN receiver loop
86 void unixtimer_canReceiveLoop(CAN_PORT port)
90 signal(SIGTERM, canReceiveLoop_signal);
91 unixtimer_ReceiveLoop_task_proc(port);
94 void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
96 unixtimer_ReceiveLoop_task_proc = ReceiveLoopPtr;
97 pthread_create(Thread, NULL, unixtimer_canReceiveLoop, (void*)port);
100 void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
102 pthread_kill(*Thread, SIGTERM);
103 pthread_join(*Thread, NULL);
106 #define maxval(a,b) ((a>b)?a:b)
107 void setTimer(TIMEVAL value)
109 // printf("setTimer(TIMEVAL value=%d)\n", value);
110 // TIMEVAL is us whereas setitimer wants ns...
111 long tv_nsec = 1000 * (maxval(value,1)%1000000);
112 time_t tv_sec = value/1000000;
113 struct itimerspec timerValues;
114 timerValues.it_value.tv_sec = tv_sec;
115 timerValues.it_value.tv_nsec = tv_nsec;
116 timerValues.it_interval.tv_sec = 0;
117 timerValues.it_interval.tv_nsec = 0;
119 timer_settime (timer, 0, &timerValues, NULL);
122 TIMEVAL getElapsedTime(void)
125 gettimeofday(&p,NULL);
126 // printf("getCurrentTime() return=%u\n", p.tv_usec);
127 return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;