wgl: Use the right pixel format.
[mesa.git] / src / gallium / state_trackers / wgl / shared / stw_tls.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <windows.h>
29
30 #include "pipe/p_compiler.h"
31 #include "util/u_memory.h"
32 #include "stw_tls.h"
33
34 static DWORD tlsIndex = TLS_OUT_OF_INDEXES;
35
36 boolean
37 stw_tls_init(void)
38 {
39 tlsIndex = TlsAlloc();
40 if (tlsIndex == TLS_OUT_OF_INDEXES) {
41 return FALSE;
42 }
43
44 return TRUE;
45 }
46
47 static INLINE struct stw_tls_data *
48 stw_tls_data_create()
49 {
50 struct stw_tls_data *data;
51
52 data = CALLOC_STRUCT(stw_tls_data);
53 if (!data)
54 return NULL;
55
56 return data;
57 }
58
59 boolean
60 stw_tls_init_thread(void)
61 {
62 struct stw_tls_data *data;
63
64 if (tlsIndex == TLS_OUT_OF_INDEXES) {
65 return FALSE;
66 }
67
68 data = stw_tls_data_create();
69 if(!data)
70 return FALSE;
71
72 TlsSetValue(tlsIndex, data);
73
74 return TRUE;
75 }
76
77 void
78 stw_tls_cleanup_thread(void)
79 {
80 struct stw_tls_data *data;
81
82 if (tlsIndex == TLS_OUT_OF_INDEXES) {
83 return;
84 }
85
86 data = (struct stw_tls_data *) TlsGetValue(tlsIndex);
87 TlsSetValue(tlsIndex, NULL);
88 FREE(data);
89 }
90
91 void
92 stw_tls_cleanup(void)
93 {
94 if (tlsIndex != TLS_OUT_OF_INDEXES) {
95 TlsFree(tlsIndex);
96 tlsIndex = TLS_OUT_OF_INDEXES;
97 }
98 }
99
100 struct stw_tls_data *
101 stw_tls_get_data(void)
102 {
103 struct stw_tls_data *data;
104
105 if (tlsIndex == TLS_OUT_OF_INDEXES) {
106 return NULL;
107 }
108
109 data = (struct stw_tls_data *) TlsGetValue(tlsIndex);
110 if(!data) {
111 /* DllMain is called with DLL_THREAD_ATTACH only by threads created after
112 * the DLL is loaded by the process */
113
114 data = stw_tls_data_create();
115 if(!data)
116 return NULL;
117
118 TlsSetValue(tlsIndex, data);
119 }
120
121 return data;
122 }