summaryrefslogtreecommitdiff
path: root/tests/src/baseservices/threading/waithandle/waitany/waitanyex10a.cs
blob: c3157d7e8ad8be2a86ab8f92bd0513bea95b5211 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;

class WaitAnyEx
{
    WaitHandle[] wh;
    ManualResetEvent myMRE;

    public WaitAnyEx()
    {
        myMRE = new ManualResetEvent(false);
    }

    public static int Main(string[] args)
    {
        // Check number of args
        if(args.Length != 2)
        {
            Console.WriteLine("USAGE:  WaitAnyEx10a /size:<int> /pos:<int>");
            return -1;
        }

        // Get the args
        int iPos=-1, iSize = -1;;
        
        for(int i=0;i<args.Length;i++)
        {
            if(args[i].ToLower().StartsWith("/size:"))
            {
                iSize = Convert.ToInt32(args[i].Substring(6));
                continue;
            }

            if(args[i].ToLower().StartsWith("/pos:"))
            {
                iPos = Convert.ToInt32(args[i].Substring(5));
            }
        }

        WaitAnyEx wae = new WaitAnyEx();
        return wae.Run(iSize, iPos);
    }

    private int Run(int iNumElements, int iPos)
    {
        int iRet = -1;
        Console.WriteLine("Abandon during a wait, signaling others - " + 
            iNumElements + ", " + iPos);
        CreateOneMutexArray(iNumElements, iPos);

        Thread t = new Thread(new ThreadStart(this.AbandonAllMutexesWait));
        t.Start();
        myMRE.WaitOne();
        try
        {
            Console.WriteLine("Waiting...");
            int i = WaitHandle.WaitAny(wh);
            iRet = 100;
        }
        catch(AbandonedMutexException)
        {
            // Should never throw
            Console.WriteLine("AbandonedMutexException thrown and " +
                "shouldn't have");
        }
        catch(Exception e)
        {
            Console.WriteLine("Unexpected exception thrown: " + 
                e.ToString());
        }
        Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed");
        return iRet;
    }

    private void AbandonAllMutexesWait()
    {
        foreach(WaitHandle w in wh)
        {
            if(w is Mutex)
                w.WaitOne();
            else
                ((ManualResetEvent)w).Set();
        }
        myMRE.Set();
        Thread.Sleep(1000);
    }

    private void CreateOneMutexArray(int numElements, int iPos)
    {
        wh = new WaitHandle[numElements];
        for(int i=0;i<numElements;i++)
        {
            if(i == iPos)
                wh[i] = new Mutex();
            else
                wh[i] = new ManualResetEvent(false);
        }
    }
}