summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CreateThread/test3/test3.c
blob: 0c44d1fdd0bcddc58d2580e0bed5099a8ad70c7c (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
// 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: test3.c 
**
** Purpose: Check to see that the handle CreateThread returns
** can be closed while the thread is still running.
**
**
**=========================================================*/

#include <palsuite.h>

HANDLE hThread;
HANDLE hEvent;

DWORD PALAPI Thread( LPVOID lpParameter)
{
    DWORD dwRet;
    dwRet = WaitForSingleObject(hEvent, INFINITE);
    /* if this thread continues beyond here, fail */
    Fail("");
    
    return 0;
}

int __cdecl main(int argc, char **argv)
{
    DWORD dwThreadId;
    DWORD dwRet;

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

    hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    if (hEvent == NULL)
    {
        Fail("PALSUITE ERROR: CreateEvent call #0 failed.  GetLastError "
             "returned %u.\n", GetLastError());
    }

    /* pass the index as the thread argument */
    hThread = CreateThread( NULL,
                            0,
                            &Thread,
                            (LPVOID) 0,
                            0,
                            &dwThreadId);
    if (hThread == NULL)
    {
        Trace("PALSUITE ERROR: CreateThread('%p' '%d' '%p' '%p' '%d' '%p') "
              "call failed.\nGetLastError returned '%u'.\n", NULL,
              0, &Thread, (LPVOID) 0, 0, &dwThreadId, GetLastError());
        if (0 == CloseHandle(hEvent))
        {
            Trace("PALSUITE ERROR: Unable to execute CloseHandle(%p) during "
                  "clean up.\nGetLastError returned '%u'.\n", hEvent);
        }
        Fail("");
    } 

    dwRet = WaitForSingleObject(hThread, 10000);
    if (dwRet != WAIT_TIMEOUT)
    {
        Trace ("PALSUITE ERROR: WaitForSingleObject('%p' '%d') "
               "call returned %d instead of WAIT_TIMEOUT ('%d').\n"
               "GetLastError returned '%u'.\n", hThread, 10000, 
               dwRet, WAIT_TIMEOUT, GetLastError());
        Fail("");
    }

    if (0 == CloseHandle(hThread))
    {
        Trace("PALSUITE ERROR: Unable to CloseHandle(%p) on a running thread."
              "\nGetLastError returned '%u'.\n", hThread, GetLastError());
        if (0 == CloseHandle(hEvent))
        {
            Trace("PALSUITE ERROR: Unable to execute CloseHandle(%p) during "
                  "cleanup.\nGetLastError returned '%u'.\n", hEvent, 
                  GetLastError());
        }
        Fail("");
    }
    if (0 == CloseHandle(hEvent))
    {
        Trace("PALSUITE ERROR: Unable to execute CloseHandle(%p) during "
              "cleanup.\nGetLastError returned '%u'.\n", hEvent, 
              GetLastError());
        Fail("");
    }
 
    PAL_Terminate();
    return (PASS);
}