timers_unix.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include <sys/time.h>
00004 #include <pthread.h> 
00005 #include <signal.h>
00006 
00007 #include "applicfg.h"
00008 #include "timer.h"
00009 
00010 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
00011 
00012 TASK_HANDLE TimerLoopThread;
00013 
00014 TIMEVAL last_time_set = TIMEVAL_MAX;
00015 
00016 struct timeval last_sig;
00017 
00018 timer_t timer;
00019 
00020 void EnterMutex(void)
00021 {
00022         pthread_mutex_lock(&CanFestival_mutex); 
00023 }
00024 
00025 void LeaveMutex(void)
00026 {
00027         pthread_mutex_unlock(&CanFestival_mutex);
00028 }
00029 
00030 void timer_notify(sigval_t val)
00031 {
00032         gettimeofday(&last_sig,NULL);
00033         EnterMutex();
00034         TimeDispatch();
00035         LeaveMutex();
00036 //      printf("getCurrentTime() return=%u\n", p.tv_usec);
00037 }
00038 
00039 void initTimer(void)
00040 {
00041         struct sigevent sigev;
00042 
00043         // Take first absolute time ref.
00044         gettimeofday(&last_sig,NULL);
00045 
00046         memset (&sigev, 0, sizeof (struct sigevent));
00047         sigev.sigev_value.sival_int = 0;
00048         sigev.sigev_notify = SIGEV_THREAD;
00049         sigev.sigev_notify_attributes = NULL;
00050         sigev.sigev_notify_function = timer_notify;
00051 
00052         timer_create (CLOCK_REALTIME, &sigev, &timer);
00053 }
00054 
00055 void StopTimerLoop(void)
00056 {
00057         EnterMutex();
00058         timer_delete (timer);
00059         LeaveMutex();
00060 }
00061 
00062 void StartTimerLoop(TimerCallback_t init_callback)
00063 {
00064         initTimer();
00065         EnterMutex();
00066         // At first, TimeDispatch will call init_callback.
00067         SetAlarm(NULL, 0, init_callback, 0, 0);
00068         LeaveMutex();
00069 }
00070 
00071 void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
00072 {
00073         pthread_create(Thread, NULL, ReceiveLoopPtr, (void*)port);
00074 }
00075 
00076 void WaitReceiveTaskEnd(TASK_HANDLE Thread)
00077 {
00078         pthread_kill(Thread, SIGTERM);
00079         pthread_join(Thread, NULL);
00080 }
00081 
00082 #define maxval(a,b) ((a>b)?a:b)
00083 void setTimer(TIMEVAL value)
00084 {
00085 //      printf("setTimer(TIMEVAL value=%d)\n", value);
00086         // TIMEVAL is us whereas setitimer wants ns...
00087         long tv_nsec = 1000 * (maxval(value,1)%1000000);
00088         time_t tv_sec = value/1000000;
00089         struct itimerspec timerValues;
00090         timerValues.it_value.tv_sec = tv_sec;
00091         timerValues.it_value.tv_nsec = tv_nsec;
00092         timerValues.it_interval.tv_sec = 0;
00093         timerValues.it_interval.tv_nsec = 0;
00094 
00095         timer_settime (timer, 0, &timerValues, NULL);
00096 }
00097 
00098 TIMEVAL getElapsedTime(void)
00099 {
00100         struct timeval p;
00101         gettimeofday(&p,NULL);
00102 //      printf("getCurrentTime() return=%u\n", p.tv_usec);
00103         return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;
00104 }

Generated on Mon Jul 2 19:10:16 2007 for CanFestival by  doxygen 1.5.1