summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForMultipleObjectsEx/test5/test5.c
blob: d19978c103a25fd0e7a0806e6a436518dcc96057 (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// 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.

/*=============================================================
**
** Source: test5.c
**
** Purpose: Test the functionality of simultaneously waiting
** on multiple processes. Create the same number of helper
** processes and helper threads.
** Helper threads wait on helper processes to finish. 
** Helper processes wait on the event signal from test
** thread before exit.
** The test thread can wake up one helper
** thread at a time by signaling the corresponding helper
** process to finish.
** The test thread can also wake up all helper threads at once
** by signaling help process 0 to exit.
**
**
**============================================================*/

#define UNICODE

#include "commonconsts.h"

#include <palsuite.h>

/* The maximum number of objects a thread can wait is MAXIMUM_WAIT_OBJECTS.
   The last helper thread in this test case will wait on all helper processes
   plus a thread finish event so the maximum number of helper processes
   can be created in this test case is (MAXIMUM_WAIT_OBJECTS-1). */
#define MAX_HELPER_PROCESS (MAXIMUM_WAIT_OBJECTS-1)

int MaxNumHelperProcess = MAX_HELPER_PROCESS;

/* indicate how the test thread wake up helper thread. */
typedef enum _TestCaseType {
    WakeUpOneThread, /* wake up one helper thread at a time. */
    WakeUpAllThread  /* wake up all helper threads at once */
} TestCaseType;

TestCaseType TestCase = WakeUpOneThread;

/* When the test thread wakes up one thread at a time,
   ThreadIndexOfThreadFinishEvent specifies the index of the thread that
   should be waked up using hThreadFinishEvent instead of helper process. */
DWORD ThreadIndexOfThreadFinishEvent = 0;

struct helper_process_t 
{
    PROCESS_INFORMATION pi;
    HANDLE hProcessReadyEvent;
    HANDLE hProcessFinishEvent;
} helper_process[MAX_HELPER_PROCESS];

HANDLE hProcessStartEvent;

struct helper_thread_t 
{
    HANDLE hThread;
    DWORD dwThreadId;
    HANDLE hThreadReadyEvent;
    HANDLE hThreadFinishEvent;
} helper_thread[MAX_HELPER_PROCESS];

/* 
 * Entry Point for helper thread.
 */
DWORD PALAPI WaitForProcess(LPVOID lpParameter)
{
    DWORD index, i;
    DWORD dwRet;
    HANDLE handles[MAX_HELPER_PROCESS+1];

    index = (DWORD) lpParameter;

    /* The helper thread 0 will wait for helper process 0, helper thread 1 will
       wait for helper process 0 and 1, helper thread 2 will wait for helper
       process 0, 1, and 2, and so on ..., and the last helper thread will wait
       on all helper processes.
       Each helper thread also waits on hThreadFinishEvent so that
       it can exit without waiting on any process to finish. */

    for (i = 0; i <= index; i++)
    {
        handles[i] = helper_process[i].pi.hProcess;
    }

    handles[index+1] = helper_thread[index].hThreadFinishEvent;

    if(!SetEvent(helper_thread[index].hThreadReadyEvent))
    {
        Fail("test5.WaitProcess: SetEvent of hThreadReadyEvent failed for thread %d. "
            "GetLastError() returned %d.\n", index, 
            GetLastError());
    }

    dwRet = WaitForMultipleObjectsEx(index+2, &handles[0], FALSE, TIMEOUT, TRUE);
    if (WakeUpAllThread == TestCase)
    {
        /* If the test thread signals helper process 0 to exit, all threads will be waked up,
           and the return value must be (WAIT_OBJECT_0+0) because the handle of helper process 0
           is in handle[0]. */
        if (dwRet != (WAIT_OBJECT_0+0))
        {
            Fail("test5.WaitForProcess: invalid return value %d for WakupAllThread from WaitForMultipleObjectsEx for thread %d\n"
                    "LastError:(%u)\n", 
                    dwRet, index,
                    GetLastError());
        }
    }
    else if (WakeUpOneThread == TestCase)
    {
        /* If the test thread wakes up one helper thread at a time,
           the return value must be either (WAIT_OBJECT_0+index) if the helper thread
           wakes up because the corresponding help process exits,
           or (index+1) if the helper thread wakes up because of hThreadReadyEvent. */
        if ((index != ThreadIndexOfThreadFinishEvent && dwRet != (WAIT_OBJECT_0+index)) || 
            (index == ThreadIndexOfThreadFinishEvent && dwRet != (index+1)))
        {
            Fail("test5.WaitForProcess: invalid return value %d for WakupOneThread from WaitForMultipleObjectsEx for thread %d\n"
                    "LastError:(%u)\n", 
                    dwRet, index,
                    GetLastError());
        }
    }
    else
    {
        Fail("Unknown TestCase %d\n", TestCase);
    }
    return 0;
}

/* 
 * Setup the helper processes and helper threads.
 */
void
Setup()
{

    STARTUPINFO si;
    DWORD dwRet;
    int i;
    
    char szEventName[MAX_PATH];
    PWCHAR uniStringHelper;
    PWCHAR uniString;

    /* Create the event to start helper process after it was created. */
    uniString = convert(szcHelperProcessStartEvName);
    hProcessStartEvent = CreateEvent(NULL, TRUE, FALSE, uniString);
    free(uniString);
    if (!hProcessStartEvent) 
    {
        Fail("test5.Setup: CreateEvent of '%s' failed. "
             "GetLastError() returned %d.\n", szcHelperProcessStartEvName, 
             GetLastError());
    }

    /* Create the helper processes. */
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    uniStringHelper = convert("helper");
    for (i = 0; i < MaxNumHelperProcess; i++)
    {
        ZeroMemory( &helper_process[i].pi, sizeof(PROCESS_INFORMATION));
        
        if(!CreateProcess( NULL, uniStringHelper, NULL, NULL, 
                            FALSE, 0, NULL, NULL, &si, &helper_process[i].pi)) 
        {
            Fail("test5.Setup: CreateProcess failed to load executable for helper process %d.  "
                "GetLastError() returned %u.\n",
                i, GetLastError());
        }

        /* Create the event to let helper process tell us it is ready. */
        if (_snprintf(szEventName, MAX_PATH-1, "%s%d",
            szcHelperProcessReadyEvName, helper_process[i].pi.dwProcessId) < 0)
        {
            Fail ("test5.Setup: Insufficient event name string length for %s\n", szcHelperProcessReadyEvName);
        }

        uniString = convert(szEventName);

        helper_process[i].hProcessReadyEvent = CreateEvent(NULL, FALSE, FALSE, uniString);
        free(uniString);
        if (!helper_process[i].hProcessReadyEvent) 
        {
            Fail("test5.Setup: CreateEvent of '%s' failed. "
                "GetLastError() returned %d.\n", szEventName, 
                GetLastError());
        }

        /* Create the event to tell helper process to exit. */
        if (_snprintf(szEventName, MAX_PATH-1, "%s%d",
            szcHelperProcessFinishEvName, helper_process[i].pi.dwProcessId) < 0)
        {
            Fail ("test5.Setup: Insufficient event name string length for %s\n", szcHelperProcessFinishEvName);
        }

        uniString = convert(szEventName);

        helper_process[i].hProcessFinishEvent = CreateEvent(NULL, TRUE, FALSE, uniString);
        free(uniString);
        if (!helper_process[i].hProcessFinishEvent) 
        {
            Fail("test5.Setup: CreateEvent of '%s' failed. "
                "GetLastError() returned %d.\n", szEventName, 
                GetLastError());
        }

    }
    free(uniStringHelper);

    /* Signal all helper processes to start. */
    if (!SetEvent(hProcessStartEvent))
    {
        Fail("test5.Setup: SetEvent '%s' failed\n",
            "LastError:(%u)\n",
            szcHelperProcessStartEvName, GetLastError());
    }

    /* Wait for ready signals from all helper processes. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {  
        dwRet = WaitForSingleObject(helper_process[i].hProcessReadyEvent, TIMEOUT);
        if (dwRet != WAIT_OBJECT_0)
        {
            Fail("test5.Setup: WaitForSingleObject %s failed for helper process %d\n"
                    "LastError:(%u)\n", 
                    szcHelperProcessReadyEvName, i, GetLastError());
        }
    }

    /* Create the same number of helper threads as helper processes. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {
        /* Create the event to let helper thread tell us it is ready. */
        helper_thread[i].hThreadReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (!helper_thread[i].hThreadReadyEvent) 
        {
            Fail("test5.Setup: CreateEvent of hThreadReadyEvent failed for thread %d\n"
                "LastError:(%u)\n", i, GetLastError());
        }

        /* Create the event to tell helper thread to exit without waiting for helper process. */
        helper_thread[i].hThreadFinishEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (!helper_thread[i].hThreadFinishEvent) 
        {
            Fail("test5.Setup: CreateEvent of hThreadFinishEvent failed for thread %d\n"
                "LastError:(%u)\n", i, GetLastError());
        }

        /* Create the helper thread. */
        helper_thread[i].hThread = CreateThread( NULL, 
                                0, 
                                (LPTHREAD_START_ROUTINE)WaitForProcess,
                                (LPVOID)i,
                                0,
                                &helper_thread[i].dwThreadId);
        if (NULL == helper_thread[i].hThread)
        {
            Fail("test5.Setup: Unable to create the helper thread %d\n"
                "LastError:(%u)\n", i, GetLastError());
        }
    }

    /* Wait for ready signals from all helper threads. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {  
        dwRet = WaitForSingleObject(helper_thread[i].hThreadReadyEvent, TIMEOUT);
        if (dwRet != WAIT_OBJECT_0)
        {
            Fail("test5.Setup: WaitForSingleObject hThreadReadyEvent for thread %d\n"
                    "LastError:(%u)\n", i, GetLastError());
        }
    }
}

/* 
 * Cleanup the helper processes and helper threads.
 */
DWORD
Cleanup()
{
    DWORD dwExitCode;
    DWORD dwRet;
    int i;

    /* Wait for all helper process to finish and close their handles
       and associated events. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {

        /* wait for the child process to complete */
        dwRet = WaitForSingleObject ( helper_process[i].pi.hProcess, TIMEOUT );
        if (WAIT_OBJECT_0 != dwRet)
        {
            Fail("test5.Cleanup: WaitForSingleObject hThreadReadyEvent failed for thread %d\n"
                    "LastError:(%u)\n", i, GetLastError());
        }

        /* check the exit code from the process */       
        if (!GetExitCodeProcess(helper_process[i].pi.hProcess, &dwExitCode))
        {
            Trace( "test5.Cleanup: GetExitCodeProcess %d call failed LastError:(%u)\n", 
                i, GetLastError()); 
            dwExitCode = FAIL;
        }
        PEDANTIC(CloseHandle, (helper_process[i].pi.hThread));
        PEDANTIC(CloseHandle, (helper_process[i].pi.hProcess));
        PEDANTIC(CloseHandle, (helper_process[i].hProcessReadyEvent)); 
        PEDANTIC(CloseHandle, (helper_process[i].hProcessFinishEvent));
    }

    /* Close all helper threads' handles */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {
        PEDANTIC(CloseHandle, (helper_thread[i].hThread));
        PEDANTIC(CloseHandle, (helper_thread[i].hThreadReadyEvent));
        PEDANTIC(CloseHandle, (helper_thread[i].hThreadFinishEvent));
    }

    /* Close all process start event. */
    PEDANTIC(CloseHandle, (hProcessStartEvent));

    return dwExitCode;
}

/* 
 * In this test case, the test thread will signal one helper
 * process to exit at a time starting from the last helper
 * process and then wait for the corresponding helper thread to exit.
 * The ThreadIndexOfThreadFinishEvent specifies the index of the thread that
 * should be waked up using hThreadFinishEvent instead of helper process.
 */
void
TestWakeupOneThread()
{
    DWORD dwRet;
    int i;

    TestCase = WakeUpOneThread;

    if (((LONG)ThreadIndexOfThreadFinishEvent) < 0 ||
        ThreadIndexOfThreadFinishEvent >= MAX_HELPER_PROCESS)
        Fail("test5.TestWaitOnOneThread: Invalid ThreadIndexOfThreadFinishEvent %d\n", ThreadIndexOfThreadFinishEvent);

    /* Since helper thread 0 waits on helper process 0,
       thread 1 waits on on process 0, and 1,
       thread 2 waits on process 0, 1, and 2, and so on ...,
       and the last helper thread will wait on all helper processes,
       the helper thread can be waked up one at a time by
       waking up the help process one at a time starting from the
       last helper process. */
    for (i = MaxNumHelperProcess-1; i >= 0; i--)
    {
        /* make sure the helper thread has not exited yet. */
        dwRet = WaitForSingleObject(helper_thread[i].hThread, 0);
        if (WAIT_TIMEOUT != dwRet)
        {
            Fail("test5.TestWaitOnOneThread: helper thread %d already exited %d\n", i);
        }

        /* Decide how to wakeup the helper thread:
           using event or using helper process. */
        if (i == ThreadIndexOfThreadFinishEvent)
        {
           if (!SetEvent(helper_thread[i].hThreadFinishEvent))
            {
                Fail("test5.TestWaitOnOneThread: SetEvent hThreadFinishEvent failed for thread %d\n",
                    "LastError:(%u)\n", i, GetLastError());
            }
        }
        else
        {
            if (!SetEvent(helper_process[i].hProcessFinishEvent))
            {
                Fail("test5.TestWaitOnOneThread: SetEvent %s%d failed for helper process %d\n",
                    "LastError:(%u)\n",
                    szcHelperProcessFinishEvName, helper_process[i].pi.dwProcessId, i,
                    GetLastError());
            }
        }
    
        dwRet = WaitForSingleObject(helper_thread[i].hThread, TIMEOUT);
        if (WAIT_OBJECT_0 != dwRet)
        {
            Fail("test5.TestWaitOnOneThread: WaitForSingleObject helper thread %d"
                    "LastError:(%u)\n", 
                    i, GetLastError());
        }
    }
    
    /* Finally, need to wake up the helper process which the test thread
       skips waking up in the last loop. */
    if (!SetEvent(helper_process[ThreadIndexOfThreadFinishEvent].hProcessFinishEvent))
    {
        Fail("test5.TestWaitOnOneThread: SetEvent %s%d failed\n",
            "LastError:(%u)\n",
            szcHelperProcessFinishEvName, helper_process[ThreadIndexOfThreadFinishEvent].pi.dwProcessId,
            GetLastError());
    }
}

/* 
 * In this test case, the test thread will signal the helper
 * process 0 to exit. Since all helper threads wait on process 0,
 * all helper threads will wake up and exit, and the test thread
 * will wait for all of them to exit.
 */
void
TestWakeupAllThread()
{
    DWORD dwRet;
    int i;

    TestCase = WakeUpAllThread;

    /* make sure none of the helper thread exits. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {
        dwRet = WaitForSingleObject(helper_thread[i].hThread, 0);
        if (WAIT_TIMEOUT != dwRet)
        {
            Fail("test5.TestWaitOnAllThread: helper thread %d already exited %d\n", i);
        }
    }

    /* Signal helper process 0 to exit. */
    if (!SetEvent(helper_process[0].hProcessFinishEvent))
    {
        Fail("test5.TestWaitOnAllThread: SetEvent %s%d failed\n",
            "LastError:(%u)\n",
            szcHelperProcessFinishEvName, helper_process[0].pi.dwProcessId,
            GetLastError());
    }

    /* Wait for all helper threads to exit. */
    for (i = 0; i < MaxNumHelperProcess; i++)
    {
    
        dwRet = WaitForSingleObject(helper_thread[i].hThread, TIMEOUT);
        if (WAIT_OBJECT_0 != dwRet)
        {
            Fail("test5.TestWaitOnAllThread: WaitForSingleObject failed for helper thread %d\n"
                    "LastError:(%u)\n", 
                    i, GetLastError());
        }
    }

    /* Signal the rest of helper processes to exit. */    
    for (i = 1; i < MaxNumHelperProcess; i++)
    {
        if (!SetEvent(helper_process[i].hProcessFinishEvent))
        {
            Fail("test5.TestWaitOnAllThread: SetEvent %s%d failed\n",
                "LastError:(%u)\n",
                szcHelperProcessFinishEvName, helper_process[i].pi.dwProcessId,
                GetLastError());
        }
    }
}

int __cdecl main(int argc, char *argv[])
{
    DWORD dwExitCode;

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    switch (argc)
    {
        case 1:
            MaxNumHelperProcess = MAX_HELPER_PROCESS;
            break;
        case 2:
            MaxNumHelperProcess = atol(argv[1]);
            break;
        default:
            Fail("Invalid number of arguments\n");
    }

    if (MaxNumHelperProcess < 1 ||
        MaxNumHelperProcess > MAX_HELPER_PROCESS)
        Fail("test5.main: Invalid MaxNumHelperProcess %d\n", MaxNumHelperProcess);

    Setup();
    ThreadIndexOfThreadFinishEvent = 3;
    TestWakeupOneThread();
    dwExitCode = Cleanup();

    if (PASS == dwExitCode)
    {
        Setup();
        TestWakeupAllThread();
        dwExitCode = Cleanup();
    }

    PAL_TerminateEx(dwExitCode);
    return dwExitCode;
}