singularity /base/Applications/Runtime/Full/Singularity/AppRuntime.cs

Language C# Lines 304
MD5 Hash 8ea8a4b0b07b896dee44fa8cde55f72e Estimated Cost $5,221 (why?)
Repository https://singularity.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
////////////////////////////////////////////////////////////////////////////////
//
//  Microsoft Research Singularity
//
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//
//  Note:
//

using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using Microsoft.Bartok.Runtime;
using Microsoft.Singularity.Io;
using Microsoft.Singularity.V1.Services;

using Microsoft.Singularity.Eventing;

[assembly: AssemblyTitle("Microsoft.Singularity")]
[assembly: AssemblyProduct("Microsoft Research Singularity Runtime")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyKeyFile("public.snk")]
[assembly: AssemblyDelaySign(true)]

namespace Microsoft.Singularity
{

    [NoCCtor]
    [CLSCompliant(false)]
    [AccessedByRuntime("referenced from hal.cpp")]
    public class AppRuntime
    {
        // Note: This function is called by Hal.cpp.
        [AccessedByRuntime("referenced from hal.cpp")]
        public static unsafe int AppStart(Type userClass)
        {
            System.GCs.Transitions.ThreadStart();

            int result = 0;

            string arg0 = "(unknown)";

            try {
                Tracing.Log(Tracing.Audit, "Runtime.Main()");

                // Initialize the primitive runtime, which calls the
                // class constructor for Runtime().
                VTable.Initialize((RuntimeType)typeof(AppRuntime));
                //VTable.ParseArgs(args);  

                Tracing.Log(Tracing.Audit, "Enabling GC Heap");
                GC.EnableHeap();
                Controller.InitializeSystem();
                GCProfilerLogger.StartProfiling();

                InitializeConsole();

                SetDebuggerPresence(DebugService.IsDebuggerPresent());

                int argCount = 0;
                int argMaxLen = 0;
                for (;; argCount++) {
                    int len = ProcessService.GetStartupArg(argCount, null, 0);
                    if (len == 0) {
                        break;
                    }
                    if (argMaxLen < len) {
                        argMaxLen = len;
                    }
                }
                char[] argArray = new char [argMaxLen];
                string[] args = new string[argCount];
                for (int arg = 0; arg < argCount; arg++) {
                    fixed (char *argptr = &argArray[0]) {
                        int len = ProcessService.GetStartupArg(arg,
                                                               argptr,
                                                               argArray.Length);
                        args[arg] = String.StringCTOR(argptr, 0, len);
                        if (arg == 0)
                            arg0 = args[arg];
                    }
                }

#if DEBUG || true
                // Record the first argument passed to this program, under the assumption that
                // this is the application name.  We use this string in DebugStub.WriteLine.
                // Also, if the name has an extension, such as ".x86", chop it off.
                // Also chop off any path prefix, such as "/init/".
                appName = arg0;
                int index = appName.LastIndexOf('.');
                if (index != -1)
                    appName = appName.Substring(0, index);
                index = appName.LastIndexOf('/');
                if (index != -1)
                    appName = appName.Substring(index + 1);

                // The default DebugName value for the main thread is "main";
                // apps can override this by setting Thread.CurrentThread.DebugName.
                Thread mainThread = Thread.CurrentThread;
                if (mainThread != null) {
                    mainThread.DebugName = "main";
                }
#endif

                if (userClass != null) {
                    VTable.initType((RuntimeType)userClass);
                }

                result = CallMain(args);
                if (!MainReturnsInt()) result = 0;

                Tracing.Log(Tracing.Audit, "Main thread exited [{0}]",
                            (UIntPtr)unchecked((uint)result));
            }
            catch (Exception e) {
                Tracing.Log(Tracing.Fatal, "Failed with exception {0}.{1}",
                            e.GetType().Namespace, e.GetType().Name);
                Tracing.Log(Tracing.Trace, "Exception message was {0}",
                            e.ToString());
                TopLevelException(e);
                result = -1;
            }

            Thread.RemoveThread(Thread.CurrentThread.threadIndex);
            Thread.JoinAll();

            try {
                FinalizeConsole();
            }
            catch (Exception e) {
                Tracing.Log(Tracing.Fatal, "An exception occurred while shutting down the console: {0}",
                    e.ToString());
            }

            Controller.Finalize();
            Tracing.Log(Tracing.Audit, "Runtime shutdown started.");
            VTable.Shutdown(result);
            Tracing.Log(Tracing.Audit, "Runtime exiting [{0}]",
                        (UIntPtr)unchecked((uint)result));
            return result;
        }


        /// <summary>
        /// This method runs when the "main" thread terminates by throwing an exception.
        /// It is a separate method, rather than simply code within the AppStart method,
        /// in order to make life easier during debugging.
        /// </summary>
        /// <param name="ex"></param>
        private static void TopLevelException(Exception/*!*/ ex)
        {
            DebugStub.WriteLine("AppRuntime: Main app thread terminated with exception:");
            for (Exception focus = ex; focus != null; focus = focus.InnerException) {
                DebugStub.WriteLine("{0}: {1}", __arglist(focus.GetType().FullName, focus.Message));
            }
        }

#if DEBUG || true
        /// <summary>
        /// Returns the name of the application, as determined by the first parameter
        /// passed in the argv block.  This property is only present on DEBUG builds 
        /// (Debug and Prototype).  It is provided for DebugStub.WriteLine, to provide 
        /// useful debugging spew.
        /// </summary>
        public static string AppName
        {
            [NoHeapAllocation]
            get
            {
                return appName;
            }
        }

        private static string appName;
#endif

        [AccessedByRuntime("output to header : defined in hal.cpp")]
        [MethodImpl(MethodImplOptions.InternalCall)]
        [StackBound(256)]
        private static extern int CallMain(String[] args);

        [AccessedByRuntime("output to header: defined in hal.cpp")]
        [MethodImpl(MethodImplOptions.InternalCall)]
        [StackBound(256)]
        private static extern bool MainReturnsInt();

        [AccessedByRuntime("output to header: defined in hal.cpp")]
        [MethodImpl(MethodImplOptions.InternalCall)]
        [StackBound(256)]
        private static extern void SetDebuggerPresence(bool debuggerPresent);

        internal static void Shutdown(int exitCode)
        {
            //
            // Gracefully close down the process.
            //
            Tracing.Log(Tracing.Audit, "Runtime.Shutdown({0})",
                        (UIntPtr)unchecked((uint)exitCode));

            DebugStub.WriteLine("Runtime.Shutdown({0})", __arglist(exitCode));

            VTable.Shutdown(exitCode);
            Tracing.Log(Tracing.Audit, "Runtime.Shutdown({0}) terminating",
                        (UIntPtr)unchecked((uint)exitCode));
            ProcessService.Stop(exitCode);
        }

        internal static void Stop(int exitCode)
        {
            //
            // Halt the process immediately.
            //
            Tracing.Log(Tracing.Audit, "Runtime.Stop({0})",
                        (UIntPtr)unchecked((uint)exitCode));

            DebugStub.WriteLine("Runtime.Stop({0})", __arglist(exitCode));

            ProcessService.Stop(exitCode);
        }

        //////////////////////////////////////////////////////////////////////
        //
        [NoHeapAllocation]
        public static UIntPtr AddressOf(Object o)
        {
            return Magic.addressOf(o);
        }

        [NoHeapAllocation]
        public static UIntPtr SizeOf(Object o)
        {
            return System.GCs.ObjectLayout.Sizeof(o);
        }

        public static void InitType(Type ty)
        {
            VTable.initType((RuntimeType) ty);
        }

        public static void DumpPageTable()
        {
            System.GCs.PageTable.Dump("PageTable");
        }

        //////////////////////////////////////////////////////////////////////
        //
        public static bool EnableGCVerify
        {
            get {
                return VTable.enableGCVerify;
            }
            set {
                VTable.enableGCVerify = value;
            }
        }

        public static bool EnableGCAccounting
        {
            get {
                return VTable.enableGCAccounting;
            }
            set {
                VTable.enableGCAccounting = value;
                if (value == true) {
                    System.GCs.MemoryAccounting.Initialize(GC.gcType);
                }
            }
        }

        public static uint GCPerfCounter
        {
            get {
                return System.GC.perfCounter;
            }
            set {
                System.GC.perfCounter = value;
            }
        }

        /// <summary>
        /// This stub method is provided for so that System.Console.dll can override it for those
        /// apps that want a console.  This is necessary, in order to initialize and shutdown the
        /// console at the right time.
        /// </summary>
        private static void InitializeConsole()
        {
        }

        /// <summary>
        /// This stub method is provided for so that System.Console.dll can override it for those
        /// apps that want a console.  This is necessary, in order to initialize and shutdown the
        /// console at the right time.
        /// </summary>
        private static void FinalizeConsole()
        {
        }
    }
}
Back to Top