1 // fg_timer.cxx -- time handling routines
3 // Written by Curtis Olson, started June 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <signal.h> // for timer routines
29 #include <stdio.h> // for printf()
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
39 #include <simgear/timing/fg_time.hxx>
41 #include "fg_timer.hxx"
42 #include "timestamp.hxx"
45 unsigned long int fgSimTime;
48 static struct itimerval t, ot;
49 static void (*callbackfunc)(int multi_loop);
52 // This routine catches the SIGALRM
53 void fgTimerCatch( int dummy ) {
56 // get past a compiler warning
57 warning_avoider = dummy;
59 // ignore any SIGALRM's until we come back from our EOM iteration
60 signal(SIGALRM, SIG_IGN);
62 // printf("In fgTimerCatch()\n");
64 // -1 tells the routine to use default interval rather than
65 // something dynamically calculated based on frame rate
68 signal(SIGALRM, fgTimerCatch);
72 // this routine initializes the interval timer to generate a SIGALRM
73 // after the specified interval (dt)
74 void fgTimerInit(float dt, void (*f)( int )) {
82 usec = 1000000 * ((int)dt - isec);
84 t.it_interval.tv_sec = isec;
85 t.it_interval.tv_usec = usec;
86 t.it_value.tv_sec = isec;
87 t.it_value.tv_usec = usec;
88 // printf("fgTimerInit() called\n");
89 fgTimerCatch(0); // set up for SIGALRM signal catch
90 terr = setitimer( ITIMER_REAL, &t, &ot );
92 printf("Error returned from setitimer");
96 #endif // HAVE_SETITIMER
99 // This function returns the number of microseconds since the last
100 // time it was called.
101 int fgGetTimeInterval( void ) {
103 static int inited = 0;
104 static FGTimeStamp last;
114 interval = current - last;