summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/errno/test2/test2.cpp
blob: f418d2f199947c014fcdbf7f42b8540365155ad2 (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
// 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 errno is 'per-thread' as noted in the documentation. 
**
**
**==========================================================================*/

#include <palsuite.h>

/* 
   This thread function just checks that errno is initially 0 and then sets
   it to a new value before returning.
*/
DWORD PALAPI ThreadFunc( LPVOID lpParam ) 
{ 
       
    if(errno != 0) 
    {
        *((DWORD*)lpParam) = 1;
    }

    errno = 20;

    return 0; 
} 


int __cdecl main(int argc, char *argv[])
{
    DWORD dwThreadId, dwThrdParam = 0; 
    HANDLE hThread; 
    
    
    if (PAL_Initialize(argc, argv))
    {
        return FAIL;
    }
    
    /* Set errno to a value within this thread */

    errno = 50;
    
    hThread = CreateThread(NULL, 0, ThreadFunc, &dwThrdParam, 0, &dwThreadId);
    
    if (hThread == NULL) 
    {
        Fail("ERROR: CreateThread failed to create a thread.  "
             "GetLastError() returned %d.\n",GetLastError());
    }
    
    WaitForSingleObject(hThread, INFINITE);
 
    /* This checks the result of calling the thread */
    if(dwThrdParam)
    {
        Fail("ERROR: errno was not set to 0 in the new thread.  Each "
             "thread should have its own value for errno.\n");
    }
    
    /* Check to make sure errno is still set to 50 */
    if(errno != 50)
    {
        Fail("ERROR: errno should be 50 in the main thread, even though "
             "it was set to 20 in another thread.  Currently it is %d.\n",
             errno);
    }
    
    PAL_Terminate();
    return PASS;
}