singrdk/base/Applications/Tests/SmpTest/ForkTest.sg

63 lines
1.9 KiB
Plaintext
Raw Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Note: Allocate strings and encourage GCs
//
using System;
using System.Collections;
using System.Threading;
using Microsoft.Singularity.UnitTest;
namespace Microsoft.Singularity.Applications
{
public class ForkTest
{
2008-11-17 18:29:00 -05:00
static readonly string []! ProcessArgs = new string[] { "testpe" };
2008-03-05 09:52:00 -05:00
static readonly TimeSpan ProcessTimeout = TimeSpan.FromSeconds(10);
const int IterationsPerThread = 64;
static object! monitor = new object();
static AutoResetEvent! exitEvent = new AutoResetEvent(false);
static int threadCount;
private static void ThreadMain()
{
lock (ForkTest.monitor) {
ForkTest.threadCount++;
}
for (int i = 0; i < IterationsPerThread; i++) {
Process process = new Process(ForkTest.ProcessArgs);
process.Start();
Assert.True(process.Join(ProcessTimeout),
"Failed to join process.");
}
lock (ForkTest.monitor) {
ForkTest.threadCount--;
if (ForkTest.threadCount == 0) {
ForkTest.exitEvent.Set();
}
}
}
public static void Run()
{
ForkTest.monitor = new object();
ForkTest.exitEvent = new AutoResetEvent(false);
ForkTest.threadCount = 0;
for (int i = 0; i < Settings.MaxThreads; i++) {
Thread t = new Thread(new ThreadStart(ThreadMain));
t.Start();
}
ForkTest.exitEvent.WaitOne();
}
}
}