1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================================
6**
7** Source: test5.c (threading/tls)
8**
9** Purpose: Test that creates a key, sets its value, deletes the key,
10** creates a new key, and gets its value to make sure its NULL.
11**
12** Dependencies: PAL_Initialize
13** PAL_Terminate
14** LocalAlloc
15** LocalFree
16**
17
18**
19**===========================================================================*/
20#include <palsuite.h>
21
22DWORD dwTlsIndex; /* TLS index */
23
24/**
25 * main
26 *
27 * executable entry point
28 */
29INT __cdecl main( INT argc, CHAR **argv )
30{
31 LPVOID lpvData;
32 DWORD dwError;
33
34 /*PAL initialization */
35 if( (PAL_Initialize(argc, argv)) != 0 )
36 {
37 return FAIL;
38 }
39
40 /**
41 * create a key, set its value, delete the key
42 */
43
44 /*Allocate a TLS index. */
45 if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
46 {/*ERROR*/
47 DWORD dwError = GetLastError();
48 Fail("TlsAlloc() returned error %d\n",
49 dwError);
50 }
51
52 /* Initialize the TLS index for this thread.*/
53 lpvData = (LPVOID) LocalAlloc(0, 256);
54
55 if( lpvData == NULL )
56 {/*ERROR */
57 dwError = GetLastError();
58 Fail("Unexpected LocalAlloc(0, 256) failure with error %d\n",
59 dwError);
60 }
61
62 if ( TlsSetValue(dwTlsIndex, lpvData) == 0 )
63 {/*ERROR */
64 dwError = GetLastError();
65 Fail("TlsSetValue(%d, %x) returned 0 with error %d\n",
66 dwTlsIndex,
67 lpvData,
68 dwError);
69 }
70
71 /* Release the TLS index */
72 if( TlsFree( dwTlsIndex ) == 0 )
73 {/* ERROR */
74 DWORD dwError = GetLastError();
75 Fail("TlsFree() returned 0 with error %d\n",
76 dwError);
77 }
78
79
80 /**
81 * create a new key, and get its value
82 */
83
84 /*Allocate a TLS index. */
85 if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
86 {/*ERROR*/
87 DWORD dwError = GetLastError();
88 Fail("TlsAlloc() returned error %d\n",
89 dwError);
90 }
91
92 /* Retrieve a data pointer for the current thread.
93 The return value should be NULL since no data has been
94 set in the index */
95 lpvData = TlsGetValue(dwTlsIndex);
96
97 if ( (lpvData != NULL) &&
98 ((dwError = GetLastError()) == NO_ERROR) )
99 {/*ERROR */
100 Fail("TlsGetValue(%d) returned data "
101 "even if no data was associated with the index\n",
102 dwTlsIndex);
103 }
104
105 PAL_Terminate();
106 return PASS;
107}
108
109