drivers/timers_unix/timers_unix.c
author edouard
Mon, 05 Oct 2009 14:31:55 +0200
changeset 599 b2d2c3fab094
parent 540 6857b6ffb7a7
child 607 5fec528f66cf
permissions -rw-r--r--
Added timeout for waiting timer thread end on windows.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
#include <sys/time.h>
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
     4
#include <pthread.h> 
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
#include <signal.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
#include "applicfg.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    12
TASK_HANDLE TimerLoopThread;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    13
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
TIMEVAL last_time_set = TIMEVAL_MAX;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
struct timeval last_sig;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    18
timer_t timer;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
454
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    20
void TimerCleanup(void)
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    21
{
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    22
	/* only used in realtime apps */
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    23
}
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    24
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
void EnterMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
	pthread_mutex_lock(&CanFestival_mutex); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
void LeaveMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
	pthread_mutex_unlock(&CanFestival_mutex);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 48
diff changeset
    35
void timer_notify(sigval_t val)
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    36
{
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    37
	gettimeofday(&last_sig,NULL);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    38
	EnterMutex();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    39
	TimeDispatch();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    40
	LeaveMutex();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    41
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    42
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    43
454
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    44
void TimerInit(void)
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    45
{
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    46
	struct sigevent sigev;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    47
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    48
	// Take first absolute time ref.
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    49
	gettimeofday(&last_sig,NULL);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    50
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    51
	memset (&sigev, 0, sizeof (struct sigevent));
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    52
	sigev.sigev_value.sival_int = 0;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    53
	sigev.sigev_notify = SIGEV_THREAD;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    54
	sigev.sigev_notify_attributes = NULL;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    55
	sigev.sigev_notify_function = timer_notify;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    56
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    57
	timer_create (CLOCK_REALTIME, &sigev, &timer);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    58
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    59
454
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    60
void StopTimerLoop(TimerCallback_t exitfunction)
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    61
{
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
    62
	EnterMutex();
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    63
	timer_delete (timer);
454
bc000083297a - add RTAI support
greg
parents: 401
diff changeset
    64
	exitfunction(NULL,0);
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
    65
	LeaveMutex();
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    66
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    67
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    68
void StartTimerLoop(TimerCallback_t init_callback)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
{
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
    70
	EnterMutex();
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
	// At first, TimeDispatch will call init_callback.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
	SetAlarm(NULL, 0, init_callback, 0, 0);
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
    73
	LeaveMutex();
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
507
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    76
void canReceiveLoop_signal(int sig)
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    77
{
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    78
}
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    79
/* We assume that ReceiveLoop_task_proc is always the same */
508
08adb8d4b098 Added more correct signal handling to unix timers.
etisserant
parents: 507
diff changeset
    80
static void (*unixtimer_ReceiveLoop_task_proc)(CAN_PORT) = NULL;
507
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    81
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    82
/**
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    83
 * Enter in realtime and start the CAN receiver loop
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    84
 * @param port
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    85
 */
540
6857b6ffb7a7 Removed some warnings.
etisserant
parents: 508
diff changeset
    86
void* unixtimer_canReceiveLoop(void* port)
507
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    87
{
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    88
       
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    89
    /*get signal*/
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    90
    signal(SIGTERM, canReceiveLoop_signal);
540
6857b6ffb7a7 Removed some warnings.
etisserant
parents: 508
diff changeset
    91
    unixtimer_ReceiveLoop_task_proc((CAN_PORT)port);
507
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    92
}
c613e6cd34fa Added more correct signal handling to unix timers.
etisserant
parents: 454
diff changeset
    93
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 48
diff changeset
    94
void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
{
508
08adb8d4b098 Added more correct signal handling to unix timers.
etisserant
parents: 507
diff changeset
    96
    unixtimer_ReceiveLoop_task_proc = ReceiveLoopPtr;
08adb8d4b098 Added more correct signal handling to unix timers.
etisserant
parents: 507
diff changeset
    97
	pthread_create(Thread, NULL, unixtimer_canReceiveLoop, (void*)port);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
401
2c90876b9751 Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents: 149
diff changeset
   100
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
{
401
2c90876b9751 Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents: 149
diff changeset
   102
	pthread_kill(*Thread, SIGTERM);
2c90876b9751 Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents: 149
diff changeset
   103
	pthread_join(*Thread, NULL);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
48
adc6572caf5d minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents: 38
diff changeset
   106
#define maxval(a,b) ((a>b)?a:b)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
//	printf("setTimer(TIMEVAL value=%d)\n", value);
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   110
	// TIMEVAL is us whereas setitimer wants ns...
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents: 35
diff changeset
   111
	long tv_nsec = 1000 * (maxval(value,1)%1000000);
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   112
	time_t tv_sec = value/1000000;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   113
	struct itimerspec timerValues;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   114
	timerValues.it_value.tv_sec = tv_sec;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   115
	timerValues.it_value.tv_nsec = tv_nsec;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
	timerValues.it_interval.tv_sec = 0;
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   117
	timerValues.it_interval.tv_nsec = 0;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   118
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
   119
 	timer_settime (timer, 0, &timerValues, NULL);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
	struct timeval p;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
	gettimeofday(&p,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
	return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
}