xref: /reactos/dll/ime/msctfime/tls.cpp (revision 14d3b53c)
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 TLS* TLS::InternalAllocateTLS()
16 {
17     TLS *pTLS = TLS::PeekTLS();
18     if (pTLS)
19         return pTLS;
20 
21     if (DllShutdownInProgress())
22         return NULL;
23 
24     pTLS = (TLS *)cicMemAllocClear(sizeof(TLS));
25     if (!pTLS)
26         return NULL;
27 
28     if (!::TlsSetValue(s_dwTlsIndex, pTLS))
29     {
30         cicMemFree(pTLS);
31         return NULL;
32     }
33 
34     pTLS->m_dwFlags1 |= 1;
35     pTLS->m_dwUnknown2 |= 1;
36     return pTLS;
37 }
38 
39 /// @implemented
40 BOOL TLS::InternalDestroyTLS()
41 {
42     TLS *pTLS = TLS::PeekTLS();
43     if (!pTLS)
44         return FALSE;
45 
46     if (pTLS->m_pBridge)
47         pTLS->m_pBridge->Release();
48     if (pTLS->m_pProfile)
49         pTLS->m_pProfile->Release();
50     if (pTLS->m_pThreadMgr)
51         pTLS->m_pThreadMgr->Release();
52 
53     cicMemFree(pTLS);
54     ::TlsSetValue(s_dwTlsIndex, NULL);
55     return TRUE;
56 }
57