xref: /reactos/dll/ime/msctfime/tls.cpp (revision 3a49e26f)
1 /*
2  * PROJECT:     ReactOS msctfime.ime
3  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE:     Thread-local storage
5  * COPYRIGHT:   Copyright 2024 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6  */
7 
8 #include "msctfime.h"
9 
10 WINE_DEFAULT_DEBUG_CHANNEL(msctfime);
11 
12 DWORD TLS::s_dwTlsIndex = (DWORD)-1;
13 
14 /// @implemented
15 BOOL TLS::Initialize()
16 {
17     s_dwTlsIndex = ::TlsAlloc();
18     return s_dwTlsIndex != (DWORD)-1;
19 }
20 
21 /// @implemented
22 VOID TLS::Uninitialize()
23 {
24     if (s_dwTlsIndex != (DWORD)-1)
25     {
26         ::TlsFree(s_dwTlsIndex);
27         s_dwTlsIndex = (DWORD)-1;
28     }
29 }
30 
31 /// @implemented
32 TLS* TLS::GetTLS()
33 {
34     if (s_dwTlsIndex == (DWORD)-1)
35         return NULL;
36 
37     return InternalAllocateTLS();
38 }
39 
40 /// @implemented
41 TLS* TLS::PeekTLS()
42 {
43     return (TLS*)::TlsGetValue(TLS::s_dwTlsIndex);
44 }
45 
46 /// @implemented
47 TLS* TLS::InternalAllocateTLS()
48 {
49     TLS *pTLS = TLS::PeekTLS();
50     if (pTLS)
51         return pTLS;
52 
53     if (DllShutdownInProgress())
54         return NULL;
55 
56     pTLS = (TLS *)cicMemAllocClear(sizeof(TLS));
57     if (!pTLS)
58         return NULL;
59 
60     if (!::TlsSetValue(s_dwTlsIndex, pTLS))
61     {
62         cicMemFree(pTLS);
63         return NULL;
64     }
65 
66     pTLS->m_dwFlags1 |= 1;
67     pTLS->m_dwUnknown2 |= 1;
68     return pTLS;
69 }
70 
71 /// @implemented
72 BOOL TLS::InternalDestroyTLS()
73 {
74     TLS *pTLS = TLS::PeekTLS();
75     if (!pTLS)
76         return FALSE;
77 
78     if (pTLS->m_pBridge)
79         pTLS->m_pBridge->Release();
80     if (pTLS->m_pProfile)
81         pTLS->m_pProfile->Release();
82     if (pTLS->m_pThreadMgr)
83         pTLS->m_pThreadMgr->Release();
84 
85     cicMemFree(pTLS);
86     ::TlsSetValue(s_dwTlsIndex, NULL);
87     return TRUE;
88 }
89 
90 /// @implemented
91 BOOL TLS::NonEACompositionEnabled()
92 {
93     if (!m_NonEAComposition)
94     {
95         DWORD dwValue = 1;
96 
97         CicRegKey regKey;
98         LSTATUS error = regKey.Open(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\CTF\\CUAS"));
99         if (error == ERROR_SUCCESS)
100         {
101             error = regKey.QueryDword(TEXT("NonEAComposition"), &dwValue);
102             if (error != ERROR_SUCCESS)
103                 dwValue = 1;
104         }
105 
106         m_NonEAComposition = dwValue;
107     }
108 
109     return (m_NonEAComposition == 2);
110 }
111