xref: /reactos/dll/opengl/opengl32/dllmain.c (revision 64d10228)
1 /*
2  * COPYRIGHT:            See COPYING in the top level directory
3  * PROJECT:              ReactOS
4  * FILE:                 dll/opengl/opengl32/dllmain.c
5  * PURPOSE:              OpenGL32 DLL
6  */
7 
8 #include "opengl32.h"
9 
10 #ifdef OPENGL32_USE_TLS
11 DWORD OglTlsIndex = 0xFFFFFFFF;
12 
13 BOOL init_tls_data(void)
14 {
15     struct Opengl32_ThreadData* ThreadData;
16 
17     ThreadData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ThreadData));
18     if(!ThreadData)
19         return FALSE;
20     TlsSetValue(OglTlsIndex, ThreadData);
21     ThreadData->glDispatchTable = &StubTable.glDispatchTable;
22     ThreadData->hglrc = NULL;
23     ThreadData->hdc = NULL;
24     ThreadData->dc_data = NULL;
25     return TRUE;
26 }
27 
28 #endif
29 
30 BOOL WINAPI
31 DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
32 {
33 #ifdef OPENGL32_USE_TLS
34     struct Opengl32_ThreadData* ThreadData;
35 #endif
36     switch ( Reason )
37     {
38         case DLL_PROCESS_ATTACH:
39 #ifdef OPENGL32_USE_TLS
40             OglTlsIndex = TlsAlloc();
41             if(OglTlsIndex == TLS_OUT_OF_INDEXES)
42                 return FALSE;
43 #endif
44             /* Initialize Context list */
45             InitializeListHead(&ContextListHead);
46             /* no break */
47         case DLL_THREAD_ATTACH:
48 #ifdef OPENGL32_USE_TLS
49             if (!init_tls_data())
50                 return FALSE;
51 #else
52             NtCurrentTeb()->glTable = &StubTable.glDispatchTable;
53 #endif // defined(OPENGL32_USE_TLS)
54             break;
55 
56         case DLL_THREAD_DETACH:
57             /* Set NULL context for this thread */
58             wglMakeCurrent(NULL, NULL);
59             /* Clean up */
60 #ifdef OPENGL32_USE_TLS
61             ThreadData = TlsGetValue(OglTlsIndex);
62             if(ThreadData)
63                 HeapFree(GetProcessHeap(), 0, ThreadData);
64 #else
65             NtCurrentTeb()->glTable = NULL;
66 #endif // defined(OPENGL32_USE_TLS)
67         break;
68         case DLL_PROCESS_DETACH:
69             /* Clean up */
70             if (!Reserved)
71             {
72                 /* The process is not shutting down: release everything */
73                 wglMakeCurrent(NULL, NULL);
74                 IntDeleteAllContexts();
75                 IntDeleteAllICDs();
76 #ifdef OPENGL32_USE_TLS
77                 ThreadData = TlsGetValue(OglTlsIndex);
78                 if(ThreadData)
79                     HeapFree(GetProcessHeap(), 0, ThreadData);
80 #endif
81             }
82 #ifdef OPENGL32_USE_TLS
83             TlsFree(OglTlsIndex);
84 #endif
85             break;
86     }
87 
88     return TRUE;
89 }
90