]> git.mxchange.org Git - flightgear.git/blob - src/Time/fg_timer.cxx
Replaced gdbm with metakit. Involves a new simgear version and a new database
[flightgear.git] / src / Time / fg_timer.cxx
1 // fg_timer.cxx -- time handling routines
2 //
3 // Written by Curtis Olson, started June 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <signal.h>    // for timer routines
29 #include <stdio.h>     // for printf()
30
31 #ifdef HAVE_STDLIB_H
32 #  include <stdlib.h>
33 #endif
34
35 #ifdef HAVE_SYS_TIME_H
36 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
37 #endif
38
39 #include "fg_time.hxx"
40 #include "fg_timer.hxx"
41 #include "timestamp.hxx"
42
43
44 unsigned long int fgSimTime;
45
46 #ifdef HAVE_SETITIMER
47   static struct itimerval t, ot;
48   static void (*callbackfunc)(int multi_loop);
49
50
51 // This routine catches the SIGALRM
52 void fgTimerCatch( int dummy ) {
53     int warning_avoider;
54
55     // get past a compiler warning
56     warning_avoider = dummy;
57
58     // ignore any SIGALRM's until we come back from our EOM iteration
59     signal(SIGALRM, SIG_IGN);
60
61     // printf("In fgTimerCatch()\n");
62
63     // -1 tells the routine to use default interval rather than
64     // something dynamically calculated based on frame rate
65     callbackfunc(-1); 
66
67     signal(SIGALRM, fgTimerCatch);
68 }
69
70
71 // this routine initializes the interval timer to generate a SIGALRM
72 // after the specified interval (dt)
73 void fgTimerInit(float dt, void (*f)( int )) {
74     int terr;
75     int isec;
76     int usec;
77
78     callbackfunc = f;
79
80     isec = (int) dt;
81     usec = 1000000 * ((int)dt - isec);
82
83     t.it_interval.tv_sec = isec;
84     t.it_interval.tv_usec = usec;
85     t.it_value.tv_sec = isec;
86     t.it_value.tv_usec = usec;
87     // printf("fgTimerInit() called\n");
88     fgTimerCatch(0);   // set up for SIGALRM signal catch
89     terr = setitimer( ITIMER_REAL, &t, &ot );
90     if (terr) {
91         printf("Error returned from setitimer");
92         exit(0);
93     }
94 }
95 #endif // HAVE_SETITIMER
96
97
98 // This function returns the number of microseconds since the last
99 // time it was called.
100 int fgGetTimeInterval( void ) {
101     int interval;
102     static int inited = 0;
103     static FGTimeStamp last;
104     FGTimeStamp current;
105
106     
107     if ( ! inited ) {
108         inited = 1;
109         last.stamp();
110         interval = 0;
111     } else {
112         current.stamp();
113         interval = current - last;
114         last = current;
115     }
116
117     return interval;
118 }
119
120