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;
22 pthread_mutex_lock(&CanFestival_mutex);
27 pthread_mutex_unlock(&CanFestival_mutex);
30 void timer_notify(sigval_t val)
32 gettimeofday(&last_sig,NULL);
36 // printf("getCurrentTime() return=%u\n", p.tv_usec);
41 struct sigevent sigev;
43 // Take first absolute time ref.
44 gettimeofday(&last_sig,NULL);
46 memset (&sigev, 0, sizeof (struct sigevent));
47 sigev.sigev_value.sival_int = 0;
48 sigev.sigev_notify = SIGEV_THREAD;
49 sigev.sigev_notify_attributes = NULL;
50 sigev.sigev_notify_function = timer_notify;
52 timer_create (CLOCK_REALTIME, &sigev, &timer);
55 void StopTimerLoop(void)
62 void StartTimerLoop(TimerCallback_t init_callback)
66 // At first, TimeDispatch will call init_callback.
67 SetAlarm(NULL, 0, init_callback, 0, 0);
71 void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
73 pthread_create(Thread, NULL, ReceiveLoopPtr, (void*)port);
76 void WaitReceiveTaskEnd(TASK_HANDLE Thread)
78 pthread_kill(Thread, SIGTERM);
79 pthread_join(Thread, NULL);
82 #define maxval(a,b) ((a>b)?a:b)
83 void setTimer(TIMEVAL value)
85 // printf("setTimer(TIMEVAL value=%d)\n", value);
86 // TIMEVAL is us whereas setitimer wants ns...
87 long tv_nsec = 1000 * (maxval(value,1)%1000000);
88 time_t tv_sec = value/1000000;
89 struct itimerspec timerValues;
90 timerValues.it_value.tv_sec = tv_sec;
91 timerValues.it_value.tv_nsec = tv_nsec;
92 timerValues.it_interval.tv_sec = 0;
93 timerValues.it_interval.tv_nsec = 0;
95 timer_settime (timer, 0, &timerValues, NULL);
98 TIMEVAL getElapsedTime(void)
101 gettimeofday(&p,NULL);
102 // printf("getCurrentTime() return=%u\n", p.tv_usec);
103 return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;