]> git.mxchange.org Git - flightgear.git/blob - LaRCsim/ls_sync.c
Merged in make system changes from Bob Kuehne <rpk@sgi.com>
[flightgear.git] / LaRCsim / ls_sync.c
1 /***************************************************************************
2
3         TITLE:  ls_sync.c
4         
5 ----------------------------------------------------------------------------
6
7         FUNCTION:       Real-time synchronization routines for LaRCSIM
8
9 ----------------------------------------------------------------------------
10
11         MODULE STATUS:  Developmental
12
13 ----------------------------------------------------------------------------
14
15         GENEALOGY:      Written 921229 by Bruce Jackson
16
17 ----------------------------------------------------------------------------
18
19         DESIGNED BY:    EBJ
20         
21         CODED BY:       EBJ
22         
23         MAINTAINED BY:  EBJ
24
25 ----------------------------------------------------------------------------
26
27         MODIFICATION HISTORY:
28         
29         DATE    PURPOSE                                         BY
30         
31         930104  Added ls_resync() call to avoid having to pass DT as a
32                 global variable.                                EBJ
33         940204  Added calculation of sim_control variable overrun to
34                 indicate a frame overrun has occurred.          EBJ
35         940506  Added support for sim_control_.debug flag, which disables
36                 synchronization (there is still a local dbg flag that
37                 enables synch error logging)                    EBJ
38
39         CURRENT RCS HEADER:
40
41 $Header$
42 $Log$
43 Revision 1.1  1997/05/29 00:10:00  curt
44 Initial Flight Gear revision.
45
46  * Revision 1.7  1994/05/06  15:34:54  bjax
47  * Removed "freerun" variable, and substituted sim_control_.debug flag.
48  *
49  * Revision 1.6  1994/02/16  13:01:22  bjax
50  * Added logic to signal frame overrun; corrected syntax on ls_catch call
51  * (old style may be BSD format). EBJ
52  *
53  * Revision 1.5  1993/07/30  18:33:14  bjax
54  * Added 'dt' parameter to call to ls_sync from ls_resync routine.
55  *
56  * Revision 1.4  1993/03/15  14:56:13  bjax
57  * Removed call to ls_pause; this should be done in cockpit routine.
58  *
59  * Revision 1.3  93/03/13  20:34:09  bjax
60  * Modified to allow for sync times longer than a second; added ls_pause()  EBJ
61  * 
62  * Revision 1.2  93/01/06  09:50:47  bjax
63  * Added ls_resync() function.
64  * 
65  * Revision 1.1  92/12/30  13:19:51  bjax
66  * Initial revision
67  * 
68  * Revision 1.3  93/12/31  10:34:11  bjax
69  * Added $Log marker as well.
70  * 
71
72 ----------------------------------------------------------------------------
73
74         REFERENCES:
75
76 ----------------------------------------------------------------------------
77
78         CALLED BY:
79
80 ----------------------------------------------------------------------------
81
82         CALLS TO:
83
84 ----------------------------------------------------------------------------
85
86         INPUTS:
87
88 ----------------------------------------------------------------------------
89
90         OUTPUTS:
91
92 --------------------------------------------------------------------------*/
93 #include <sys/time.h>
94 #include <signal.h>
95 #include <stdio.h>
96 #include "ls_types.h"
97 #include "ls_sim_control.h"
98
99
100 extern SCALAR Simtime;
101
102 /* give the  time interval data structure FILE visibility */
103
104 static struct itimerval t, ot;
105
106 static int dbug = 0;
107
108 /*void ls_catch( sig, code, sc) /* signal handler */
109 /*int sig;
110 int code;
111 struct sigcontext *sc;*/
112 void ls_catch()
113 {
114   static DATA lastSimtime = -99.9;
115
116   /* printf("In ls_catch()\n"); */
117
118   /*if (lastSimtime == Simtime) fprintf(stderr, "Overrun.\n"); */
119   if (dbug) printf("ls_catch called\n");
120   sim_control_.overrun = (lastSimtime == Simtime);
121   lastSimtime = Simtime;
122   signal(SIGALRM, ls_catch);
123 }
124
125 void ls_sync(dt)
126 float dt;
127
128 /* this routine syncs up the interval timer for a new dt value */
129 {
130   int terr;
131   int isec;
132   float usec;
133
134   if (sim_control_.debug!=0) return;
135   
136   isec = (int) dt;
137   usec = 1000000* (dt - (float) isec);
138
139   t.it_interval.tv_sec = isec;
140   t.it_interval.tv_usec = usec;
141   t.it_value.tv_sec = isec;
142   t.it_value.tv_usec = usec;
143   if (dbug) printf("ls_sync called\n");
144   ls_catch();   /* set up for SIGALRM signal catch */
145   terr = setitimer( ITIMER_REAL, &t, &ot );
146   if (terr) perror("Error returned from setitimer");
147 }
148
149 void ls_unsync()
150 /* this routine unsyncs the interval timer */
151 {
152   int terr;
153
154   if (sim_control_.debug!=0) return;
155   t.it_interval.tv_sec = 0;
156   t.it_interval.tv_usec = 0;
157   t.it_value.tv_sec = 0;
158   t.it_value.tv_usec = 0;
159 if (dbug) printf("ls_unsync called\n");
160
161   terr = setitimer( ITIMER_REAL, &t, &ot );
162   if (terr) perror("Error returned from setitimer");
163
164 }
165
166 void ls_resync()
167 /* this routine resynchronizes the interval timer to the old
168    interrupt period, stored in struct ot by a previous call
169    to ls_unsync().  */
170 {
171   float dt;
172
173   if (sim_control_.debug!=0) return;
174   if (dbug) printf("ls_resync called\n");
175   dt = ((float) ot.it_interval.tv_usec)/1000000. +
176        ((float) ot.it_interval.tv_sec);
177   ls_sync(dt);
178 }
179
180 void ls_pause()
181 /* this routine waits for the next interrupt */
182 {
183   if (sim_control_.debug!=0) return;
184   if (dbug) printf("ls_pause called\n");
185   pause();
186 }
187