]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/bootstrap.cxx
Fix line endings
[flightgear.git] / src / Main / bootstrap.cxx
index 39bfcbc899f75fe3eaf545e40b04a63c3c4c7ae1..78f34101128a1c50e7e1d1b59560ba4f75e9efaa 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started May 1997.
 //
-// Copyright (C) 1997 - 2002  Curtis L. Olson  - curt@flightgear.org
+// Copyright (C) 1997 - 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -30,7 +30,9 @@
 #  include <signal.h>
 #endif
 
+#include <errno.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 #include <simgear/compiler.h>
 #include <simgear/structure/exception.hxx>
@@ -41,6 +43,7 @@ SG_USING_STD(cerr);
 SG_USING_STD(endl);
 
 #include "main.hxx"
+#include "globals.hxx"
 
 
 #ifdef HAVE_WINDOWS_H
@@ -54,6 +57,12 @@ SG_USING_STD(endl);
 #  include <console.h>         // -dw- for command line dialog
 #endif
 
+char *homedir = ::getenv( "HOME" );
+char *hostname = ::getenv( "HOSTNAME" );
+bool free_hostname = false;
+
+// foreward declaration.
+void fgExitCleanup();
 
 #if defined(__linux__) && defined(__i386__)
 
@@ -110,14 +119,53 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 }
 #endif
 
+#if defined( sgi )
+#include <sys/fpu.h>
+#include <sys/sysmp.h>
+#include <unistd.h>
+
+/*
+ *  set the special "flush zero" bit (FS, bit 24) in the Control Status
+ *  Register of the FPU of R4k and beyond so that the result of any
+ *  underflowing operation will be clamped to zero, and no exception of
+ *  any kind will be generated on the CPU.  This has no effect on an
+ *  R3000.
+ */
+void flush_fpe(void)
+{
+    union fpc_csr f;
+    f.fc_word = get_fpc_csr();
+    f.fc_struct.flush = 1;
+    set_fpc_csr(f.fc_word);
+}
+#endif
+
+int _bootstrap_OSInit;
+
 // Main entry point; catch any exceptions that have made it this far.
 int main ( int argc, char **argv ) {
 
+    _bootstrap_OSInit = 0;
+
     // Enable floating-point exceptions for Linux/x86
 #if defined(__linux__) && defined(__i386__)
     initFPE();
 #endif
 
+#if defined(sgi)
+    flush_fpe();
+
+    // Bind all non-rendering threads to CPU1
+    // This will reduce the jitter caused by them to an absolute minimum,
+    // but it will only work with superuser authority.
+    if ( geteuid() == 0 )
+    {
+       sysmp(MP_CLOCK, 0);             // bind the timer only to CPU0
+       sysmp(MP_ISOLATE, 1 );          // Isolate CPU1
+       sysmp(MP_NONPREEMPTIVE, 1 );    // disable process time slicing on CPU1
+    }
+#endif
+
     // Enable floating-point exceptions for Windows
 #if defined( _MSC_VER ) && defined( DEBUG )
     // Christian, we should document what this does
@@ -134,6 +182,7 @@ int main ( int argc, char **argv ) {
       PSN psn;
 
       fgOSInit (&argc, argv);
+      _bootstrap_OSInit++;
 
       CPSGetCurrentProcess(&psn);
       CPSSetProcessName(&psn, "FlightGear");
@@ -145,17 +194,35 @@ int main ( int argc, char **argv ) {
     // FIXME: add other, more specific
     // exceptions.
     try {
+        atexit(fgExitCleanup);
         fgMainInit(argc, argv);
     } catch (sg_throwable &t) {
                             // We must use cerr rather than
                             // logging, since logging may be
                             // disabled.
-        cerr << "Fatal error: " << t.getFormattedMessage()
-             << "\n (received from " << t.getOrigin() << ')' << endl;
-        exit(1);
+        cerr << "Fatal error: " << t.getFormattedMessage() << endl;
+        if (!t.getOrigin().empty())
+            cerr << " (received from " << t.getOrigin() << ')' << endl;
+
+    } catch (...) {
+        cerr << "Unknown exception in the main loop. Aborting..." << endl;
+        if (errno)
+            perror("Possible cause");
     }
 
     return 0;
 }
 
+// do some clean up on exit.  Specifically we want to call alutExit()
+// which happens in the sound manager destructor.
+void fgExitCleanup() {
+
+    if (_bootstrap_OSInit != 0)
+        fgSetMouseCursor(MOUSE_CURSOR_POINTER);
+
+    delete globals;
+
+    if (free_hostname && hostname != NULL)
+        free(hostname);
+}