1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================================
6**
7** Source: test1.c
8**
9** Purpose: Test to ensure SwitchToThread works, without
10** causing test to hang
11**
12** Dependencies: PAL_Initialize
13** Fail
14** SwitchToThread
15** WaitForMultipleObject
16** CreateThread
17** GetLastError
18**
19
20**
21**===========================================================================*/
22
23
24#include <palsuite.h>
25#define THREAD_COUNT 10
26#define REPEAT_COUNT 1000
27#define TIMEOUT 60000
28void PALAPI Run_Thread(LPVOID lpParam);
29
30/**
31 * main
32 *
33 * executable entry point
34 */
35INT __cdecl main( INT argc, CHAR **argv )
36{
37 DWORD dwParam;
38 HANDLE hThread[THREAD_COUNT];
39 DWORD threadId[THREAD_COUNT];
40
41 int i = 0;
42 int returnCode = 0;
43
44 /*PAL initialization */
45 if( (PAL_Initialize(argc, argv)) != 0 )
46 {
47 return FAIL;
48 }
49
50
51 for( i = 0; i < THREAD_COUNT; i++ )
52 {
53 dwParam = (int) i;
54 //Create thread
55 hThread[i] = CreateThread(
56 NULL, /* no security attributes */
57 0, /* use default stack size */
58 (LPTHREAD_START_ROUTINE)Run_Thread,/* thread function */
59 (LPVOID)dwParam, /* argument to thread function */
60 0, /* use default creation flags */
61 &threadId[i] /* returns the thread identifier*/
62 );
63
64 if(hThread[i] == NULL)
65 {
66 Fail("Create Thread failed for iteration %d GetLastError value is %d\n", i, GetLastError());
67 }
68
69 }
70
71
72 returnCode = WaitForMultipleObjects(THREAD_COUNT, hThread, TRUE, TIMEOUT);
73 if( WAIT_OBJECT_0 != returnCode )
74 {
75 Trace("Wait for Object(s) returned %d, expected value is %d, and GetLastError value is %d\n", returnCode, WAIT_OBJECT_0, GetLastError());
76 }
77
78 PAL_Terminate();
79 return PASS;
80
81}
82
83void PALAPI Run_Thread (LPVOID lpParam)
84{
85 int i = 0;
86 int Id=(int)lpParam;
87
88 for(i=0; i < REPEAT_COUNT; i++ )
89 {
90 // No Last Error is set..
91 if(!SwitchToThread())
92 {
93 Trace( "The operating system did not switch execution to another thread,"
94 "for thread id[%d], iteration [%d]\n", Id, i );
95 }
96 }
97}
98