summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/TLS/test6_optimizedtls/test.c
blob: 1798844e87bd87cf6c795e44fae4ad183008e752 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*=============================================================================
**
** Source: test.c
**
** Purpose: Test to ensure TlsAlloc, PAL_MakeOptimizedTlsGetter, 
**  PAL_FreeOptimizedTlsGetter and TlsFree are working properly 
**  on supported platforms
**
** Dependencies: PAL_Initialize
**               Fail
**               Sleep
**               LocalAlloc
**               LocalFree
**               WaitForSingleObject
**               CreateThread
**               GetLastError
** 

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


#include <palsuite.h>
#define THREAD_COUNT  5
DWORD dwTlsIndex; /* TLS index */

void PALAPI Run_Thread(LPVOID lpParam);

/**
 * main
 *
 * executable entry point
 */
INT __cdecl main( INT argc, CHAR **argv )
{
    DWORD  dwParam;
    DWORD  dwError;
    HANDLE hThread[THREAD_COUNT];
    DWORD  threadId[THREAD_COUNT];
    
    int i = 0;   
    int returnCode = 0;

    /*PAL initialization */
    if( (PAL_Initialize(argc, argv)) != 0 )
    {
	    return FAIL;
    }

    /*Allocate a TLS index. */
    if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
    {   
        /*ERROR*/
        dwError = GetLastError();
        Fail("TlsAlloc() returned error %d\n",
              dwError);
    }


    for( i = 0; i < THREAD_COUNT; i++ )
    {
        dwParam = (int) i;
        //Create thread
        hThread[i] = CreateThread(
                                    NULL,                   /* no security attributes */
                                    0,                      /* use default stack size */
                                    (LPTHREAD_START_ROUTINE)Run_Thread,/* thread function */
                                    (LPVOID)dwParam,  /* argument to thread function */
                                    0,                      /* use default creation flags  */
                                    &threadId[i]     /* returns the thread identifier*/                                  
                                  );

        if(hThread[i] == NULL)
        {
            Fail("Create Thread failed for iteration %d GetLastError value is %d\n", i, GetLastError());
        }
  
    } 


    returnCode = WaitForMultipleObjects(THREAD_COUNT, hThread, TRUE, INFINITE);
    if( WAIT_OBJECT_0 != returnCode )
    {
        Trace("Wait for Object(s) returned %d, expected value is  %d, and GetLastError value is %d\n", returnCode, WAIT_OBJECT_0, GetLastError());
    }

    /* Release the TLS index */
    if( TlsFree( dwTlsIndex ) == 0 )
    {
        /* ERROR */
        dwError = GetLastError();
        Fail("TlsFree() returned 0 with error %d\n",
             dwError);
    }

    PAL_Terminate();
    return PASS;

}

void  PALAPI Run_Thread (LPVOID lpParam)
{
    unsigned int i = 0;

    LPVOID lpvData;
    DWORD  dwError;
    PAL_POPTIMIZEDTLSGETTER ptrOptimizedTlsGetter;

    int Id=(int)lpParam;


    lpvData = TlsGetValue(dwTlsIndex);
    if ( (lpvData != NULL) &&
        ((dwError = GetLastError()) == NO_ERROR) ) 
    {
        /*ERROR */
        Fail("Error:%d:TlsGetValue(%d) returned data "
            "even if data was not associated with the index, for thread id [%d]\n",
			dwError, dwTlsIndex, Id);
    }


    /* Initialize the TLS index for this thread.*/
    lpvData = (LPVOID) LocalAlloc(0, 256);

    if( lpvData == NULL )
    {
        /*ERROR */
        dwError = GetLastError();
        Fail("Unexpected LocalAlloc(0, 256) failure with error %d\n",
			  dwError);
    }

    if ( TlsSetValue(dwTlsIndex, lpvData) == 0 )
    {
        /*ERROR */
	    dwError = GetLastError();
        Fail("TlsSetValue(%d, %x) returned 0 with error %d\n",
			  dwTlsIndex,
			  lpvData,
			  dwError);
    }
                            
    ptrOptimizedTlsGetter = PAL_MakeOptimizedTlsGetter(dwTlsIndex);
    if( ptrOptimizedTlsGetter == NULL )
    {
         /* Retrieve a data pointer for the current thread. 
        The return value should be NULL since no data has been 
        set in the index */
        lpvData = TlsGetValue(dwTlsIndex);
        Trace("Not Inside the optimizer loop for thread [%d]\n", Id);

        if ( (lpvData == NULL) &&
            ((dwError = GetLastError()) == NO_ERROR) ) 
        {
            /*ERROR */
            Fail("Error:%d:TlsGetValue(%d) returned data "
                "as NULL even if data was associated with the index, for thread id [%d]\n",
			    dwError, dwTlsIndex, Id);
        }
    }
    else
    {
        /* Retrieve a data pointer for the current thread. 
        The return value should be NULL since no data has been 
        set in the index */
        lpvData = ptrOptimizedTlsGetter();

        if ( (lpvData == NULL) &&
            ((dwError = GetLastError()) == NO_ERROR) ) 
        {
            /*ERROR */
            Fail(" Error:%d: MakeOptimizedTlsGetter for dwTlsIndex (%d) returned data "
                "as NULL even if no data was associated with the index, for thread id [%d]\n",
			    dwError, dwTlsIndex, Id);
        }

        Trace("Inside the optimizer loop for thread [%d]\n", Id);
        PAL_FreeOptimizedTlsGetter(ptrOptimizedTlsGetter);
    }




}