PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/base/Applications/Tests/WaitTest/WaitTest.cs

#
C# | 124 lines | 99 code | 16 blank | 9 comment | 4 complexity | 179f6e808e26545f6a45fc5fe0f1efbf MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using System;
  10. using System.Threading;
  11. using Microsoft.Singularity.V1.Services;
  12. using Microsoft.Singularity.Channels;
  13. using Microsoft.Contracts;
  14. using Microsoft.SingSharp.Reflection;
  15. using Microsoft.Singularity.Applications;
  16. using Microsoft.Singularity.Io;
  17. using Microsoft.Singularity.Configuration;
  18. [assembly: Transform(typeof(ApplicationResourceTransform))]
  19. namespace Microsoft.Singularity.Applications
  20. {
  21. [ConsoleCategory(DefaultAction=true)]
  22. internal class Parameters {
  23. [InputEndpoint("data")]
  24. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  25. [OutputEndpoint("data")]
  26. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  27. reflective internal Parameters();
  28. internal int AppMain() {
  29. return WaitTest.AppMain(this);
  30. }
  31. }
  32. public class WaitTest
  33. {
  34. //[ShellCommand("waittest", "Execute demo for non-infinite waiting")]
  35. internal static int AppMain(Parameters! config) {
  36. Console.WriteLine("Testing float and threads.");
  37. Console.Write("Still testing float and threads.");
  38. Console.WriteLine();
  39. TestFloat();
  40. Thread t1 = new Thread(new ThreadStart(WaitThreadOne));
  41. Thread t2 = new Thread(new ThreadStart(WaitThreadTwo));
  42. t1.Start();
  43. t2.Start();
  44. Console.WriteLine("Main Thread sleeping 6 seconds for test completion");
  45. #if DEBUG_SLEEP
  46. ulong u1 = Processor.CycleCount;
  47. #endif
  48. Thread.Sleep(TimeSpan.FromMilliseconds(6000));
  49. #if DEBUG_SLEEP
  50. ulong u2 = Processor.CycleCount;
  51. #endif
  52. Console.WriteLine("Main Thread waking and exiting");
  53. #if DEBUG_SLEEP
  54. Console.WriteLine(" u1 = {0,14}", u1);
  55. Console.WriteLine(" u2 = {0,14}", u2);
  56. Console.WriteLine(" u2-u1 = {0,14}", u2 - u1);
  57. Console.WriteLine(" u2-u1 = {0,14} ms", ((u2 - u1) * 1000) / Processor.CyclesPerSecond);
  58. #endif
  59. return 0;
  60. }
  61. public static void TestFloat()
  62. {
  63. ulong v;
  64. float f = (float)2.0;
  65. v = BitConverter.SingleToUInt32Bits((float)1.0);
  66. Console.WriteLine(" 1.0 {0:x16}", v);
  67. v = BitConverter.SingleToUInt32Bits(f);
  68. Console.WriteLine(" 2.0 {0:x16}", v);
  69. v = BitConverter.SingleToUInt32Bits((float)10.0);
  70. Console.WriteLine(" 10.0 {0:x16}", v);
  71. v = BitConverter.DoubleToUInt64Bits(1.0);
  72. Console.WriteLine(" 1.0 {0:x16}", v);
  73. v = BitConverter.DoubleToUInt64Bits(f);
  74. Console.WriteLine(" 2.0 {0:x16}", v);
  75. v = BitConverter.DoubleToUInt64Bits(10.0);
  76. Console.WriteLine(" 10.0 {0:x16}", v);
  77. if (f >= 1.0) {
  78. Console.WriteLine(" f >= 1.0");
  79. }
  80. if (f <= 10.0) {
  81. Console.WriteLine(" f <= 10.0");
  82. }
  83. }
  84. public static void WaitThreadOne() {
  85. Console.WriteLine("Starting Wait Thread One");
  86. TestFloat();
  87. #if DONT
  88. Queue q = new Queue();
  89. Console.WriteLine("Queue: {0}\n", q);
  90. #endif
  91. for (int i = 0; i < 5; i++) {
  92. Console.WriteLine("Thread One About To Sleep " + ProcessService.GetUpTime().Ticks);
  93. Thread.Sleep(TimeSpan.FromMilliseconds(1000));
  94. Console.WriteLine("Thread One Waking Up " + ProcessService.GetUpTime().Ticks);
  95. }
  96. Console.WriteLine("Wait Thread One Exiting");
  97. }
  98. public static void WaitThreadTwo() {
  99. Console.WriteLine("Starting Wait Thread Two");
  100. for (int i = 0; i < 10; i++) {
  101. Console.WriteLine("Thread Two About To Sleep " + ProcessService.GetUpTime().Ticks);
  102. Thread.Sleep(TimeSpan.FromMilliseconds(30));
  103. Console.WriteLine("Thread Two Waking Up " + ProcessService.GetUpTime().Ticks);
  104. }
  105. Console.WriteLine("Wait Thread Two Exiting");
  106. }
  107. }
  108. }