2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
6 * Written by Kai Tietz <kai.tietz@onevision.com>
8 * This file is used by if gcc is built with --enable-threads=win32.
10 * Based on version created by Mumit Khan <khan@nanotech.wisc.edu>
14 /*#ifndef WIN32_LEAN_AND_MEAN
15 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>*/
23 WINBOOL
__mingw_TLScallback (HANDLE hDllHandle
, DWORD reason
, LPVOID reserved
);
24 int ___w64_mingwthr_remove_key_dtor (DWORD key
);
25 int ___w64_mingwthr_add_key_dtor (DWORD key
, void (*dtor
)(void *));
27 /* To protect the thread/key association data structure modifications. */
28 static CRITICAL_SECTION __mingwthr_cs
;
29 static volatile int __mingwthr_cs_init
= 0;
31 typedef struct __mingwthr_key __mingwthr_key_t
;
33 /* The list of threads active with key/dtor pairs. */
34 struct __mingwthr_key
{
37 __mingwthr_key_t
volatile *next
;
41 static __mingwthr_key_t
volatile *key_dtor_list
;
44 ___w64_mingwthr_add_key_dtor (DWORD key
, void (*dtor
)(void *))
46 __mingwthr_key_t
*new_key
;
48 if (__mingwthr_cs_init
== 0)
50 new_key
= (__mingwthr_key_t
*) calloc (1, sizeof (__mingwthr_key_t
));
57 EnterCriticalSection (&__mingwthr_cs
);
59 new_key
->next
= key_dtor_list
;
60 key_dtor_list
= new_key
;
62 LeaveCriticalSection (&__mingwthr_cs
);
67 ___w64_mingwthr_remove_key_dtor (DWORD key
)
69 __mingwthr_key_t
volatile *prev_key
;
70 __mingwthr_key_t
volatile *cur_key
;
72 if (__mingwthr_cs_init
== 0)
75 EnterCriticalSection (&__mingwthr_cs
);
78 cur_key
= key_dtor_list
;
80 while (cur_key
!= NULL
)
82 if ( cur_key
->key
== key
)
85 key_dtor_list
= cur_key
->next
;
87 prev_key
->next
= cur_key
->next
;
89 free ((void*)cur_key
);
93 cur_key
= cur_key
->next
;
96 LeaveCriticalSection (&__mingwthr_cs
);
101 __mingwthr_run_key_dtors (void)
103 __mingwthr_key_t
volatile *keyp
;
105 if (__mingwthr_cs_init
== 0)
107 EnterCriticalSection (&__mingwthr_cs
);
109 for (keyp
= key_dtor_list
; keyp
; )
111 LPVOID value
= TlsGetValue (keyp
->key
);
112 if (GetLastError () == ERROR_SUCCESS
)
115 (*keyp
->dtor
) (value
);
120 LeaveCriticalSection (&__mingwthr_cs
);
124 __mingw_TLScallback (HANDLE
__UNUSED_PARAM(hDllHandle
),
126 LPVOID
__UNUSED_PARAM(reserved
))
130 case DLL_PROCESS_ATTACH
:
131 if (__mingwthr_cs_init
== 0)
132 InitializeCriticalSection (&__mingwthr_cs
);
133 __mingwthr_cs_init
= 1;
135 case DLL_PROCESS_DETACH
:
136 __mingwthr_run_key_dtors();
137 if (__mingwthr_cs_init
== 1)
139 __mingwthr_cs_init
= 0;
140 DeleteCriticalSection (&__mingwthr_cs
);
143 case DLL_THREAD_ATTACH
:
145 case DLL_THREAD_DETACH
:
146 __mingwthr_run_key_dtors();