summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CriticalSectionFunctions/test4/test4.c
blob: 8a245a47765c1bc83a70987d5d7429023b0b2163 (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
// 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/test4/test4.c
** 
** Purpose: Test to see if threads blocked on a CRITICAL_SECTION object will 
** be released in an orderly manner.  This case looks at the following
** scenario.  If one thread owns a CRITICAL_SECTION object and two threads 
** block in EnterCriticalSection, trying to hold the already owned 
** CRITICAL_SECTION object, when the first thread releases the CRITICAL_SECTION
** object, will one and only one of the waiters get unblocked?
**
** Dependencies: CreateThread
**               InitializeCriticalSection
**               EnterCriticalSection
**               LeaveCriticalSection
**               DeleteCriticalSection
**               Sleep
**               WaitForSingleObject
** 

**
**=========================================================*/

#include <palsuite.h>

#define NUM_BLOCKING_THREADS 2
                         
BOOL bTestResult;
CRITICAL_SECTION CriticalSection;
HANDLE hThread[NUM_BLOCKING_THREADS];
HANDLE hEvent;
DWORD dwThreadId[NUM_BLOCKING_THREADS];
volatile int flags[NUM_BLOCKING_THREADS] = {0,0};

DWORD PALAPI ThreadTest1(LPVOID lpParam)
{

    EnterCriticalSection ( &CriticalSection );

    flags[0] = 1;
    
    return 0;

}

DWORD PALAPI ThreadTest2(LPVOID lpParam)
{

    EnterCriticalSection ( &CriticalSection );

    flags[1] = 1;

    return 0;

}

int __cdecl main(int argc, char **argv)
{

    DWORD dwRet;
    DWORD dwRet1;
    bTestResult = FAIL;
    
    if ((PAL_Initialize(argc,argv)) != 0)
    {
        return(bTestResult);
    }

    /*
     * Create Critical Section Object
     */
    InitializeCriticalSection ( &CriticalSection );

    EnterCriticalSection ( &CriticalSection );

    hThread[0] = CreateThread(NULL,
                              0,
                              &ThreadTest1,
                              (LPVOID) 0,
                              CREATE_SUSPENDED,
                              &dwThreadId[0]);
    if (hThread[0] == NULL)
    {
        Trace("PALSUITE ERROR: CreateThread(%p, %d, %p, %p, %d, %p) call "
             "failed.\nGetLastError returned %d.\n", NULL, 0, &ThreadTest1,
             (LPVOID) 0, CREATE_SUSPENDED, &dwThreadId[0], GetLastError());
        LeaveCriticalSection(&CriticalSection);
        DeleteCriticalSection ( &CriticalSection );
        Fail("");
    }
    
    hThread[1] = CreateThread(NULL,
                              0,
                              &ThreadTest2,
                              (LPVOID) 0,
                              CREATE_SUSPENDED,
                              &dwThreadId[1]);
    if (hThread[1] == NULL)
    {
        Trace("PALSUITE ERROR: CreateThread(%p, %d, %p, %p, %d, %p) call "
             "failed.\nGetLastError returned %d.\n", NULL, 0, &ThreadTest2,
             (LPVOID) 0, CREATE_SUSPENDED, &dwThreadId[1], GetLastError());
        LeaveCriticalSection(&CriticalSection);

        dwRet = ResumeThread(hThread[0]);
        if (-1 == dwRet)
        {
            Trace("PALSUITE ERROR: ResumeThread(%p) call failed.\n"
                  "GetLastError returned '%d'.\n", hThread[0],
             GetLastError());
    }

        dwRet = WaitForSingleObject(hThread[0], 10000);
        if (WAIT_OBJECT_0 == dwRet)
        {
            Trace("PALSUITE ERROR: WaitForSingleObject(%p, %d) call "
                  "failed.  '%d' was returned instead of the expected '%d'.\n"
                  "GetLastError returned '%d'.\n", hThread[0], 10000, dwRet, 
                  WAIT_OBJECT_0, GetLastError());
        }

        if (0 == CloseHandle(hThread[0]))
        {
            Trace("PALSUITE NOTIFICATION: CloseHandle(%p) call failed.\n"
                  "GetLastError returned %d.  Not failing tests.\n", 
                  hThread[0], GetLastError());
        }

        DeleteCriticalSection(&CriticalSection);
        Fail("");
    }

    /* 
     * Set other thread priorities to be higher than ours & Sleep to ensure 
     * we give up the processor. 
     */
    dwRet = (DWORD) SetThreadPriority(hThread[0], 
                                      THREAD_PRIORITY_ABOVE_NORMAL);
    if (0 == dwRet)
    {
        Trace("PALSUITE ERROR: SetThreadPriority(%p, %d) call failed.\n"
              "GetLastError returned %d", hThread[0], 
              THREAD_PRIORITY_ABOVE_NORMAL, GetLastError());
    }
    
    dwRet = (DWORD) SetThreadPriority(hThread[1], 
                                      THREAD_PRIORITY_ABOVE_NORMAL);
    if (0 == dwRet)
    {
        Trace("PALSUITE ERROR: SetThreadPriority(%p, %d) call failed.\n"
              "GetLastError returned %d", hThread[1], 
              THREAD_PRIORITY_ABOVE_NORMAL, GetLastError());
    }

    dwRet = ResumeThread(hThread[0]);
    if (-1 == dwRet)
    {
        Trace("PALSUITE ERROR: ResumeThread(%p, %d) call failed.\n"
              "GetLastError returned %d", hThread[0], 
              GetLastError() );
    }
   
    dwRet = ResumeThread(hThread[1]); 
    if (-1 == dwRet)
    {
        Trace("PALSUITE ERROR: ResumeThread(%p, %d) call failed.\n"
              "GetLastError returned %d", hThread[0], 
              GetLastError());              
    }

    Sleep (0);

    LeaveCriticalSection (&CriticalSection);
    
    dwRet = WaitForSingleObject(hThread[0], 10000);
    dwRet1 = WaitForSingleObject(hThread[1], 10000);

    if ((WAIT_OBJECT_0 == dwRet) || 
        (WAIT_OBJECT_0 == dwRet1))
    {
        if ((1 == flags[0] && 0 == flags[1]) ||
            (0 == flags[0] && 1 == flags[1]))
        {
            bTestResult = PASS;
        }
        else 
        {
            bTestResult = FAIL;
            Trace ("PALSUITE ERROR: flags[%d] = {%d,%d}.  These values are"
                   "inconsistent.\nCriticalSection test failed.\n",
                   NUM_BLOCKING_THREADS, flags[0], flags[1]);
        }

        /* Fail the test if both threads returned WAIT_OBJECT_0 */
        if ((WAIT_OBJECT_0 == dwRet) && (WAIT_OBJECT_0 == dwRet1))
        {
            bTestResult = FAIL;
            Trace ("PALSUITE ERROR: WaitForSingleObject(%p, %d) and "
                   "WaitForSingleObject(%p, %d)\nboth returned dwRet = '%d'\n"
                   "One should have returned WAIT_TIMEOUT ('%d').\n", 
                   hThread[0], 10000, hThread[1], 10000, dwRet, WAIT_TIMEOUT);
        }        
    }
    else 
    {
        bTestResult = FAIL;
        Trace ("PALSUITE ERROR: WaitForSingleObject(%p, %d) and "
               "WaitForSingleObject(%p, %d)\nReturned dwRet = '%d' and\n"
               "dwRet1 = '%d' respectively.\n", hThread[0], 10000, hThread[1],
               10000, dwRet, dwRet1);
    }    

    if (WAIT_OBJECT_0 == dwRet)
    {
        if (0 == CloseHandle(hThread[0]))
        {
            Trace("PALSUITE NOTIFICATION: CloseHandle(%p) call failed.\n"
                  "GetLastError returned %d.  Not failing tests.\n", 
                  hThread[0], GetLastError());
        }
    }
    if (WAIT_OBJECT_0 == dwRet1)
    {
        if (0 == CloseHandle(hThread[1]))
        {
            Trace("PALSUITE NOTIFICATION: CloseHandle(%p) call failed.\n"
                  "GetLastError returned %d.  Not failing tests.\n", 
                  hThread[1], GetLastError());
        }
    }

    /* Leaking the CS on purpose, since there is still a thread 
       waiting on it */

    PAL_TerminateEx(bTestResult);
    return (bTestResult);
}