ReactOS  0.4.13-dev-99-g7e18b6d
tlsthrd.c
Go to the documentation of this file.
1 
14 /*#ifndef WIN32_LEAN_AND_MEAN
15 #define WIN32_LEAN_AND_MEAN
16 #endif
17 #include <windows.h>*/
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <windef.h>
21 #include <winbase.h>
22 
25 int ___w64_mingwthr_add_key_dtor (DWORD key, void (*dtor)(void *));
26 
27 /* To protect the thread/key association data structure modifications. */
29 static volatile int __mingwthr_cs_init = 0;
30 
32 
33 /* The list of threads active with key/dtor pairs. */
36  void (*dtor)(void *);
38 };
39 
40 
42 
43 int
44 ___w64_mingwthr_add_key_dtor (DWORD key, void (*dtor)(void *))
45 {
47 
48  if (__mingwthr_cs_init == 0)
49  return 0;
50  new_key = (__mingwthr_key_t *) calloc (1, sizeof (__mingwthr_key_t));
51  if (new_key == NULL)
52  return -1;
53 
54  new_key->key = key;
55  new_key->dtor = dtor;
56 
58 
59  new_key->next = key_dtor_list;
61 
63  return 0;
64 }
65 
66 int
68 {
69  __mingwthr_key_t volatile *prev_key;
70  __mingwthr_key_t volatile *cur_key;
71 
72  if (__mingwthr_cs_init == 0)
73  return 0;
74 
76 
77  prev_key = NULL;
78  cur_key = key_dtor_list;
79 
80  while (cur_key != NULL)
81  {
82  if ( cur_key->key == key)
83  {
84  if (prev_key == NULL)
85  key_dtor_list = cur_key->next;
86  else
87  prev_key->next = cur_key->next;
88 
89  free ((void*)cur_key);
90  break;
91  }
92  prev_key = cur_key;
93  cur_key = cur_key->next;
94  }
95 
97  return 0;
98 }
99 
100 static void
102 {
103  __mingwthr_key_t volatile *keyp;
104 
105  if (__mingwthr_cs_init == 0)
106  return;
108 
109  for (keyp = key_dtor_list; keyp; )
110  {
111  LPVOID value = TlsGetValue (keyp->key);
112  if (GetLastError () == ERROR_SUCCESS)
113  {
114  if (value)
115  (*keyp->dtor) (value);
116  }
117  keyp = keyp->next;
118  }
119 
121 }
122 
123 WINBOOL
125  DWORD reason,
127 {
128  switch (reason)
129  {
130  case DLL_PROCESS_ATTACH:
131  if (__mingwthr_cs_init == 0)
133  __mingwthr_cs_init = 1;
134  break;
135  case DLL_PROCESS_DETACH:
137  if (__mingwthr_cs_init == 1)
138  {
139  __mingwthr_cs_init = 0;
141  }
142  break;
143  case DLL_THREAD_ATTACH:
144  break;
145  case DLL_THREAD_DETACH:
147  break;
148  }
149  return TRUE;
150 }
151 
#define TRUE
Definition: types.h:120
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
int ___w64_mingwthr_add_key_dtor(DWORD key, void(*dtor)(void *))
Definition: tlsthrd.c:44
#define ERROR_SUCCESS
Definition: deptool.c:10
__mingwthr_key_t volatile * next
Definition: tlsthrd.c:37
#define DLL_THREAD_ATTACH
Definition: compat.h:121
#define free
Definition: debug_ros.c:5
#define __UNUSED_PARAM(x)
Definition: _mingw.h:72
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define DLL_THREAD_DETACH
Definition: compat.h:122
r reserved
Definition: btrfs.c:2655
WINBOOL __mingw_TLScallback(HANDLE hDllHandle, DWORD reason, LPVOID reserved)
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:697
smooth NULL
Definition: ftsmooth.c:416
static CRITICAL_SECTION __mingwthr_cs
Definition: tlsthrd.c:28
static volatile int __mingwthr_cs_init
Definition: tlsthrd.c:29
void(* dtor)(void *)
Definition: tlsthrd.c:36
#define DLL_PROCESS_DETACH
Definition: compat.h:119
int32_t WINBOOL
Definition: typedefs.h:56
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
static void __mingwthr_run_key_dtors(void)
Definition: tlsthrd.c:101
unsigned long DWORD
Definition: ntddk_ex.h:95
static WCHAR reason[MAX_STRING_RESOURCE_LEN]
Definition: object.c:1904
DWORD key
Definition: tlsthrd.c:35
int ___w64_mingwthr_remove_key_dtor(DWORD key)
Definition: tlsthrd.c:67
HKEY key
Definition: reg.c:42
static __mingwthr_key_t volatile * key_dtor_list
Definition: tlsthrd.c:41
LPVOID WINAPI TlsGetValue(IN DWORD Index)
Definition: thread.c:1163
#define calloc
Definition: rosglue.h:14
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
Definition: path.c:42
static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, CRYPTKEY **ppCryptKey)
Definition: rsaenh.c:793