annotate osdep/timer-lx.c @ 24817:d30cb71c0103

Get rid of void pointer arithmetic.
author zuxy
date Tue, 23 Oct 2007 08:28:11 +0000
parents 1f956f8f881e
children 531116b7693d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3 #include <unistd.h>
13612
c0bde085511c Zeta OS support, mostly working.
reimar
parents: 12955
diff changeset
4 #ifdef __BEOS__
c0bde085511c Zeta OS support, mostly working.
reimar
parents: 12955
diff changeset
5 #define usleep(t) snooze(t)
c0bde085511c Zeta OS support, mostly working.
reimar
parents: 12955
diff changeset
6 #endif
5297
f297030ef5ab include stdlib.h
arpi
parents: 4385
diff changeset
7 #include <stdlib.h>
2476
a6c5a537f30a a few warning fixes (missing #include's)
pl
parents: 2273
diff changeset
8 #include <time.h>
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
9 #include <sys/time.h>
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13612
diff changeset
10 #include "config.h"
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
11
12955
82fb5c71f776 And a tiny compile fix.
wight
parents: 12954
diff changeset
12 const char *timer_name =
12954
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
13 #ifdef HAVE_NANOSLEEP
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
14 "nanosleep()";
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
15 #else
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
16 "usleep()";
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
17 #endif
f9755d9c479a Native darwin timer update.
wight
parents: 9380
diff changeset
18
2273
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
19 int usec_sleep(int usec_delay)
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
20 {
3090
57abb24ac58b HAVE_NANOSLEEP
alex
parents: 2476
diff changeset
21 #ifdef HAVE_NANOSLEEP
2273
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
22 struct timespec ts;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
23 ts.tv_sec = usec_delay / 1000000;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
24 ts.tv_nsec = (usec_delay % 1000000) * 1000;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
25 return nanosleep(&ts, NULL);
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
26 #else
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
27 return usleep(usec_delay);
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
28 #endif
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
29 }
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
30
99
fb1fc94eaff0 removed redundancy...
arpi_esp
parents: 1
diff changeset
31 // Returns current time in microseconds
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 16985
diff changeset
32 unsigned int GetTimer(void){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
33 struct timeval tv;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 // float s;
18467
1f956f8f881e remove useless cruft (caused error tz undefined)
rfelker
parents: 17566
diff changeset
35 gettimeofday(&tv,NULL);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
36 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37 return (tv.tv_sec*1000000+tv.tv_usec);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39
4385
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
40 // Returns current time in milliseconds
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 16985
diff changeset
41 unsigned int GetTimerMS(void){
4385
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
42 struct timeval tv;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
43 // float s;
18467
1f956f8f881e remove useless cruft (caused error tz undefined)
rfelker
parents: 17566
diff changeset
44 gettimeofday(&tv,NULL);
4385
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
45 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
46 return (tv.tv_sec*1000+tv.tv_usec/1000);
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
47 }
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
48
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 static unsigned int RelativeTime=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
50
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 // Returns time spent between now and last call in seconds
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 16985
diff changeset
52 float GetRelativeTime(void){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 unsigned int t,r;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 t=GetTimer();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 // t*=16;printf("time=%ud\n",t);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56 r=t-RelativeTime;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57 RelativeTime=t;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 return (float)r * 0.000001F;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 // Initialize timer, must be called at least once at start
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 16985
diff changeset
62 void InitTimer(void){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 GetRelativeTime();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
66
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67 #if 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 void main(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 float t=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70 InitTimer();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74