]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Autopilot: save loads of CPU cycles
[flightgear.git] / src / Main / bootstrap.cxx
1 // bootstrap.cxx -- bootstrap routines: main()
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997 - 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #include <windows.h>
30 #endif
31
32 #if defined(HAVE_FEENABLEEXCEPT)
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36 #include <fenv.h>
37 #elif defined(__linux__) && defined(__i386__)
38 #  include <fpu_control.h>
39 #endif
40
41 #ifndef _WIN32
42 #  include <unistd.h> // for gethostname()
43 #endif
44
45 #include <errno.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49
50 #include <simgear/compiler.h>
51 #include <simgear/structure/exception.hxx>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/math/SGMath.hxx>
54
55 #include <cstring>
56 #include <iostream>
57 using std::cerr;
58 using std::endl;
59
60 #include "main.hxx"
61 #include "globals.hxx"
62 #include "fg_props.hxx"
63 #include "fgviewer.hxx"
64
65
66 #include "fg_os.hxx"
67
68 char *homedir = NULL;
69 char *hostname = NULL;
70 bool free_hostname = false;
71
72 // foreward declaration.
73 void fgExitCleanup();
74
75 static bool fpeAbort = false;
76 static void initFPE();
77
78 #if defined(HAVE_FEENABLEEXCEPT)
79 static void handleFPE(int);
80 static void
81 initFPE ()
82 {
83     if (fpeAbort) {
84         int except = fegetexcept();
85         feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
86     } else {
87         signal(SIGFPE, handleFPE);
88     }
89 }
90
91 static void handleFPE(int)
92 {
93     feclearexcept(FE_ALL_EXCEPT);
94     signal(SIGFPE, handleFPE);
95 }
96 #elif defined(__linux__) && defined(__i386__)
97
98 static void handleFPE(int);
99 static void
100 initFPE ()
101 {
102     fpu_control_t fpe_flags = 0;
103     _FPU_GETCW(fpe_flags);
104 //     fpe_flags &= ~_FPU_MASK_IM;      // invalid operation
105 //     fpe_flags &= ~_FPU_MASK_DM;      // denormalized operand
106 //     fpe_flags &= ~_FPU_MASK_ZM;      // zero-divide
107 //     fpe_flags &= ~_FPU_MASK_OM;      // overflow
108 //     fpe_flags &= ~_FPU_MASK_UM;      // underflow
109 //     fpe_flags &= ~_FPU_MASK_PM;      // precision (inexact result)
110     _FPU_SETCW(fpe_flags);
111     signal(SIGFPE, handleFPE);
112 }
113
114 static void
115 handleFPE (int num)
116 {
117   initFPE();
118   SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
119 }
120 #else
121 static void initFPE()
122 {
123 }
124 #endif
125
126 #ifdef _MSC_VER
127 int main ( int argc, char **argv );
128 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
129                              LPSTR lpCmdLine, int nCmdShow) {
130
131   logbuf::has_no_console();
132   main( __argc, __argv );
133 }
134 #endif
135
136 #if defined( sgi )
137 #include <sys/fpu.h>
138 #include <sys/sysmp.h>
139 #include <unistd.h>
140
141 /*
142  *  set the special "flush zero" bit (FS, bit 24) in the Control Status
143  *  Register of the FPU of R4k and beyond so that the result of any
144  *  underflowing operation will be clamped to zero, and no exception of
145  *  any kind will be generated on the CPU.  This has no effect on an
146  *  R3000.
147  */
148 void flush_fpe(void)
149 {
150     union fpc_csr f;
151     f.fc_word = get_fpc_csr();
152     f.fc_struct.flush = 1;
153     set_fpc_csr(f.fc_word);
154 }
155 #endif
156
157 static void fg_terminate() {
158     cerr << endl <<
159             "Uncaught Exception: you should see a meaningful error message\n"
160             "here, but your GLUT (or SDL) library was apparently compiled\n"
161             "and/or linked without exception support. Please complain to\n"
162             "its provider!"
163             << endl << endl;
164     abort();
165 }
166
167 int _bootstrap_OSInit;
168
169 // Main entry point; catch any exceptions that have made it this far.
170 int main ( int argc, char **argv ) {
171 #if _MSC_VER
172   // Don't show blocking "no disk in drive" error messages on Windows 7,
173   // silently return errors to application instead.
174   // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX"
175   SetErrorMode(SEM_NOOPENFILEERRORBOX);
176
177   // Windows has no $HOME aka %HOME%, so we have to construct the full path.
178   // make sure it fits into the buffer. Max. path length is 255, but who knows
179   // what's in these environment variables?
180   char homepath[256] = "";
181   homepath[sizeof(homepath)-1] = 0;
182   strncpy( homepath, ::getenv("APPDATA"), sizeof(homepath)-1 );
183   strncat( homepath, "\\flightgear.org", sizeof(homepath)-strlen(homepath)-1 );
184   
185   homedir = strdup(homepath);
186   hostname = ::getenv( "COMPUTERNAME" );
187 #else
188   // Unix(alike) systems
189   char _hostname[256];
190   gethostname(_hostname, 256);
191   hostname = strdup(_hostname);
192   free_hostname = true;
193   
194   homedir = ::getenv( "HOME" );
195   
196   signal(SIGPIPE, SIG_IGN);
197 #endif
198
199 #ifdef PTW32_STATIC_LIB
200     // Initialise static pthread win32 lib
201     pthread_win32_process_attach_np ();
202 #endif
203     _bootstrap_OSInit = 0;
204
205 #if defined(__FreeBSD__)
206     // Ignore floating-point exceptions on FreeBSD
207     signal(SIGFPE, SIG_IGN); 
208 #else
209     // Maybe Enable floating-point exceptions on Linux
210     for (int i = 0; i < argc; ++i) {
211         if (!strcmp("--enable-fpe", argv[i])) {
212             fpeAbort = true;
213             break;
214         }
215     }
216     initFPE();
217 #endif
218
219 #if defined(sgi)
220     flush_fpe();
221
222     // Bind all non-rendering threads to CPU1
223     // This will reduce the jitter caused by them to an absolute minimum,
224     // but it will only work with superuser authority.
225     if ( geteuid() == 0 )
226     {
227        sysmp(MP_CLOCK, 0);              // bind the timer only to CPU0
228        sysmp(MP_ISOLATE, 1 );           // Isolate CPU1
229        sysmp(MP_NONPREEMPTIVE, 1 );     // disable process time slicing on CPU1
230     }
231 #endif
232
233     // Enable floating-point exceptions for Windows
234 #if defined( _MSC_VER ) && defined( DEBUG )
235     // Christian, we should document what this does
236     _control87( _EM_INEXACT, _MCW_EM );
237 #endif
238
239 #if defined( HAVE_BC5PLUS )
240     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
241 #endif
242   
243     bool fgviewer = false;
244     for (int i = 0; i < argc; ++i) {
245         if (!strcmp("--fgviewer", argv[i])) {
246             fgviewer = true;
247             break;
248         }
249     }
250
251     // FIXME: add other, more specific
252     // exceptions.
253     try {
254         std::set_terminate(fg_terminate);
255         atexit(fgExitCleanup);
256         if (fgviewer)
257             fgviewerMain(argc, argv);
258         else
259             fgMainInit(argc, argv);
260             
261         
262     } catch (const sg_throwable &t) {
263                             // We must use cerr rather than
264                             // logging, since logging may be
265                             // disabled.
266         cerr << "Fatal error: " << t.getFormattedMessage() << endl;
267         if (std::strlen(t.getOrigin()) != 0)
268             cerr << " (received from " << t.getOrigin() << ')' << endl;
269
270     } catch (const string &s) {
271         cerr << "Fatal error: " << s << endl;
272
273     } catch (const char *s) {
274         cerr << "Fatal error: " << s << endl;
275
276     } catch (...) {
277         cerr << "Unknown exception in the main loop. Aborting..." << endl;
278         if (errno)
279             perror("Possible cause");
280     }
281
282     return 0;
283 }
284
285 // do some clean up on exit.  Specifically we want to call alutExit()
286 // which happens in the sound manager destructor.
287 void fgExitCleanup() {
288
289     if (_bootstrap_OSInit != 0)
290         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
291
292     delete globals;
293
294     if (free_hostname && hostname != NULL)
295         free(hostname);
296 }
297