summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CreateThread/test2/test2.cpp
blob: 1ee410a6c5b4b3b12430c1dd0ab0fa3cf65eaa23 (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
// 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: test2.c 
**
** Purpose: Test that lpThreadId is assigned the correct 
** threadId value and that lpThreadId can be NULL.
**
**
**=========================================================*/

#include <palsuite.h>

#define NUM_TESTS 3

HANDLE hThread[NUM_TESTS];
DWORD dwThreadId[NUM_TESTS];
volatile BOOL bResult[NUM_TESTS];
volatile DWORD dwThreadId1[NUM_TESTS];

DWORD PALAPI Thread( LPVOID lpParameter)
{
    dwThreadId1[(DWORD) lpParameter] = GetCurrentThreadId();
    bResult[(DWORD) lpParameter] = TRUE;
    return (DWORD) lpParameter;
}

struct testCase 
{
    LPSECURITY_ATTRIBUTES lpThreadAttributes;
    DWORD dwStackSize;
    LPTHREAD_START_ROUTINE lpStartAddress;
    DWORD dwCreationFlags;
    LPDWORD lpThreadId;
};

struct testCase testCases[]=
{
    {NULL, 0, &Thread, 0, NULL},
    {NULL, 0, &Thread, CREATE_SUSPENDED, NULL},
    {NULL, 0, &Thread, 0, (LPDWORD) 1}
};

/*
 * close handles 
 */
BOOL cleanup(int index)
{
    int i;
    BOOL bRet = TRUE;

    for (i = 0; i < index; i++)
    {
        if (!CloseHandle(hThread[i]))
        {
            bRet = FALSE;
            Trace("PALSUITE ERROR: CloseHandle(%p) call failed for index %d\n",
                  hThread[i], i);
        }
    }

    return(bRet);
}

int __cdecl main(int argc, char **argv)
{
    SIZE_T i;
    DWORD dwRetWFSO;
    DWORD dwRetRT;
    BOOL bRet = TRUE;

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

    /* set results array to FALSE */
    for (i = 0; i < NUM_TESTS; i++)
    {
        bResult[i]=FALSE;
        dwThreadId[i]=0;
    }

    for (i = 0; i < NUM_TESTS; i++)
    {
        if (NULL != testCases[i].lpThreadId)
        {
            testCases[i].lpThreadId = &dwThreadId[i];
        }
        /* pass the index as the thread argument */
        hThread[i] = CreateThread( testCases[i].lpThreadAttributes,   
                                   testCases[i].dwStackSize,          
                                   testCases[i].lpStartAddress,       
                                   (LPVOID)i,
                                   testCases[i].dwCreationFlags,      
                                   testCases[i].lpThreadId);  
        if (hThread[i] == NULL)
        {
            Trace("PALSUITE ERROR: CreateThread('%p' '%d' '%p' '%p' '%d' "
                  "'%p') call failed.\nGetLastError returned '%u'.\n", 
                  testCases[i].lpThreadAttributes, testCases[i].dwStackSize,
                  testCases[i].lpStartAddress, (LPVOID)i, 
                  testCases[i].dwCreationFlags, 
                  testCases[i].lpThreadId, GetLastError());
            cleanup(i - 1);
            Fail("");
        } 

        /* Resume suspended threads */
        if (testCases[i].dwCreationFlags == CREATE_SUSPENDED)
        {   
            dwRetRT = ResumeThread (hThread[i]);
            if (dwRetRT != 1)
            {
                Trace ("PALSUITE ERROR: ResumeThread(%p) "
                       "call returned %d it should have returned %d.\n"
                       "GetLastError returned %u.\n", hThread[i], dwRetRT,
                       1, GetLastError());
                cleanup(i);
                Fail("");
            }
        }
    }

    /* cleanup */
    for (i = 0; i < NUM_TESTS; i++)
    {
        dwRetWFSO = WaitForSingleObject(hThread[i], 10000);
        if (dwRetWFSO != WAIT_OBJECT_0)
        {
            Trace ("PALSUITE ERROR: WaitForSingleObject('%p' '%d') "
                   "call returned %d instead of WAIT_OBJECT_0 ('%d').\n"
                   "GetLastError returned %u.\n", hThread[i], 10000, 
                   dwRetWFSO, WAIT_OBJECT_0, GetLastError());
            cleanup(i);
            Fail("");
        }
    }
    if(!cleanup(NUM_TESTS))
    {
        Fail("");
    }

    for (i = 0; i < NUM_TESTS; i++)
    {
        /* 
         * check to see that all threads were created and were passed 
         * the array index as an argument. 
         */
        if (FALSE == bResult[i])
        {
            bRet = FALSE;
            Trace("PALSUITE ERROR: result[%d]=%d.  It should be %d\n", i, 
                  FALSE, TRUE);
        }
        /* 
         * check to see that lpThreadId received the correct value. 
         */
        if (0 != dwThreadId[i])
        {
            if (dwThreadId[i] != dwThreadId1[i])
            {
                bRet = FALSE;
                Trace("PALSUITE ERROR: dwThreadId[%d]=%p and dwThreadId1[%d]"
                      "=%p\nThese values should be identical.\n",  i, 
                      dwThreadId[i], i, dwThreadId1[i]);
            }
        }
    }  
    if (!bRet)
    {
        cleanup(NUM_TESTS);
        Fail("");
    }

    PAL_Terminate();
    return (PASS);
}