summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CriticalSectionFunctions/test5/test5.c
blob: 8dfa4f5f3d535c4c6bc6d62e5dccf5480569198b (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
// 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:      CriticalSectionFunctions/test5/test5.c
**
** Purpose:     Attempt to delete a critical section owned by another 
**              thread.
**
**
**===================================================================*/
#include <palsuite.h>

/* 
 * Tokens 0 and 1 are events.  Token 2 is the thread.
 */
#define NUM_TOKENS 3                             

HANDLE hToken[NUM_TOKENS];
CRITICAL_SECTION CriticalSection;

BOOL CleanupHelper (HANDLE *hArray, DWORD dwIndex)
{
    BOOL bCHRet;

    bCHRet = CloseHandle(hArray[dwIndex]);
    if (!bCHRet)
    {
        Trace("PALSUITE ERROR: Unable to execute CloseHandle(%p) during "
              "clean up.\nGetLastError returned '%u'.\n", hArray[dwIndex],
              GetLastError());
    }

    return (bCHRet);
}

BOOL Cleanup(HANDLE *hArray, DWORD dwIndex)
{
    BOOL bCRet;
    BOOL bCHRet = FALSE;

    while (--dwIndex > 0)
    {
        bCHRet = CleanupHelper(&hArray[0], dwIndex); 
    }
     
    bCRet = CloseHandle(hArray[0]);
    if (!bCRet)
    {
        Trace("PALSUITE ERROR: Unable to execute CloseHandle(%p) during "
              "clean up.\nGetLastError returned '%u'.\n", hArray[dwIndex],
              GetLastError());  
    }
    
    return (bCRet&&bCHRet);
}

DWORD PALAPI Thread(LPVOID lpParam)
{
    DWORD dwTRet;

    EnterCriticalSection(&CriticalSection);

    /* signal thread 0 */
    if (0 == SetEvent(hToken[0]))
    {
        Trace("PALSUITE ERROR: Unable to execute SetEvent(%p) during "
              "clean up.\nGetLastError returned '%u'.\n", hToken[0],
              GetLastError());
        LeaveCriticalSection(&CriticalSection);
        Cleanup (&hToken[0], NUM_TOKENS);
        DeleteCriticalSection(&CriticalSection);
        Fail("");
    }
    
    /* wait to be signaled */        
    dwTRet = WaitForSingleObject(hToken[1], 10000);
    if (WAIT_OBJECT_0 != dwTRet)

    {
        Trace("PALSUITE ERROR: WaitForSingleObject(%p,%d) should have "
              "returned\nWAIT_OBJECT_0 ('%d'), instead it returned "
              "('%d').\nGetLastError returned '%u'.\n", 
              hToken[1], 10000, WAIT_OBJECT_0, dwTRet, GetLastError());
        LeaveCriticalSection(&CriticalSection);
        Cleanup (&hToken[0], NUM_TOKENS);
        DeleteCriticalSection(&CriticalSection);
        Fail("");
    }      

    LeaveCriticalSection(&CriticalSection);
    return 0;
}

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

    if ((PAL_Initialize(argc,argv)) != 0)
    {
        return(FAIL);
    }
    
    /* thread 0 event */
    hToken[0] = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (NULL == hToken[0])
    {
        Fail("PALSUITE ERROR: CreateEvent call #0 failed.  GetLastError "
             "returned %u.\n", GetLastError());
    }

    /* thread 1 event */
    hToken[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (NULL == hToken[1])
    {
        Trace("PALSUITE ERROR: CreateEvent call #1 failed.  GetLastError "
              "returned %u.\n", GetLastError());
        Cleanup(&hToken[0], (NUM_TOKENS - 2));
        Fail("");
    }

    InitializeCriticalSection(&CriticalSection);

    hToken[2] = CreateThread(NULL,
                             0,
                             &Thread,
                             (LPVOID) NULL,
                             0,
                             &dwThreadId);
    if (hToken[2] == NULL)
    {
        Trace("PALSUITE ERROR: CreateThread call #0 failed.  GetLastError "
              "returned %u.\n", GetLastError());
        Cleanup(&hToken[0], (NUM_TOKENS - 1));
        DeleteCriticalSection(&CriticalSection);
        Fail("");
    }
    
    /* wait for thread 0 to be signaled */
    dwMRet = WaitForSingleObject(hToken[0], 10000);
    if (WAIT_OBJECT_0 != dwMRet)
    {   
        Trace("PALSUITE ERROR: WaitForSingleObject(%p,%d) should have "
              "returned\nWAIT_OBJECT_0 ('%d'), instead it returned "
              "('%d').\nGetLastError returned '%u'.\n", hToken[0], 10000, 
              WAIT_OBJECT_0, dwMRet, GetLastError());
        Cleanup(&hToken[0], NUM_TOKENS);
        Fail("");
    }

    /* 
     * Attempt to do delete CriticalSection object owned by other thread
     */
    DeleteCriticalSection(&CriticalSection);

    /* signal thread 1 */
    if (0 == SetEvent(hToken[1]))
    {
        Trace("PALSUITE ERROR: Unable to execute SetEvent(%p) call.\n"
              "GetLastError returned '%u'.\n", hToken[1],
              GetLastError());
        Cleanup(&hToken[0], NUM_TOKENS);
        Fail("");
    }

    dwMRet = WaitForSingleObject(hToken[2], 10000);
    if (WAIT_OBJECT_0 != dwMRet)
    {
        Trace("PALSUITE ERROR: WaitForSingleObject(%p, %d) call "
              "returned an unexpected value '%d'.\nGetLastError returned "
              "%u.\n", hToken[2], 10000, dwMRet, GetLastError());
        Cleanup(&hToken[0], NUM_TOKENS);
        Fail("");
    }              

    if (!Cleanup(&hToken[0], NUM_TOKENS)) 
    {
        Fail("");
    }

    PAL_Terminate();

    return (PASS);
}