2 * msvcrt.dll thread functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
26 /********************************************************************/
31 _beginthread_start_routine_t start_address
;
32 _beginthreadex_start_routine_t start_address_ex
;
35 } _beginthread_trampoline_t
;
37 /*********************************************************************
38 * msvcrt_get_thread_data
40 * Return the thread local storage structure.
42 thread_data_t
*CDECL
msvcrt_get_thread_data(void)
45 DWORD err
= GetLastError(); /* need to preserve last error */
47 if (!(ptr
= TlsGetValue( msvcrt_tls_index
)))
49 if (!(ptr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptr
) )))
50 _amsg_exit( _RT_THREAD
);
51 if (!TlsSetValue( msvcrt_tls_index
, ptr
)) _amsg_exit( _RT_THREAD
);
52 ptr
->tid
= GetCurrentThreadId();
53 ptr
->handle
= INVALID_HANDLE_VALUE
;
55 ptr
->locinfo
= MSVCRT_locale
->locinfo
;
56 ptr
->mbcinfo
= MSVCRT_locale
->mbcinfo
;
65 /*********************************************************************
66 * _endthread (MSVCRT.@)
68 void CDECL
_endthread(void)
74 tls
= TlsGetValue(msvcrt_tls_index
);
75 if (tls
&& tls
->handle
!= INVALID_HANDLE_VALUE
)
77 CloseHandle(tls
->handle
);
78 tls
->handle
= INVALID_HANDLE_VALUE
;
80 WARN("tls=%p tls->handle=%p\n", tls
, tls
? tls
->handle
: INVALID_HANDLE_VALUE
);
85 /*********************************************************************
86 * _endthreadex (MSVCRT.@)
88 void CDECL
_endthreadex(
89 unsigned int retval
) /* [in] Thread exit code */
91 TRACE("(%d)\n", retval
);
95 thread_data_t
*tls
= TlsGetValue(msvcrt_tls_index
);
97 if (tls
&& tls
->module
!= NULL
)
98 FreeLibraryAndExitThread(tls
->module
, retval
);
100 WARN("tls=%p tls->module=%p\n", tls
, tls
? tls
->module
: NULL
);
107 /*********************************************************************
108 * _beginthread_trampoline
110 static DWORD CALLBACK
_beginthread_trampoline(LPVOID arg
)
112 _beginthread_trampoline_t local_trampoline
;
113 thread_data_t
*data
= msvcrt_get_thread_data();
115 memcpy(&local_trampoline
,arg
,sizeof(local_trampoline
));
116 data
->handle
= local_trampoline
.thread
;
119 #if _MSVCR_VER >= 140
120 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
121 (void*)local_trampoline
.start_address
, &data
->module
))
124 WARN("failed to get module for the start_address: %d\n", GetLastError());
128 local_trampoline
.start_address(local_trampoline
.arglist
);
132 /*********************************************************************
133 * _beginthread (MSVCRT.@)
135 uintptr_t CDECL
_beginthread(
136 _beginthread_start_routine_t start_address
, /* [in] Start address of routine that begins execution of new thread */
137 unsigned int stack_size
, /* [in] Stack size for new thread or 0 */
138 void *arglist
) /* [in] Argument list to be passed to new thread or NULL */
140 _beginthread_trampoline_t
* trampoline
;
143 TRACE("(%p, %d, %p)\n", start_address
, stack_size
, arglist
);
145 if (!MSVCRT_CHECK_PMT(start_address
)) return -1;
147 trampoline
= malloc(sizeof(*trampoline
));
153 thread
= CreateThread(NULL
, stack_size
, _beginthread_trampoline
,
154 trampoline
, CREATE_SUSPENDED
, NULL
);
157 msvcrt_set_errno(GetLastError());
161 trampoline
->thread
= thread
;
162 trampoline
->start_address
= start_address
;
163 trampoline
->arglist
= arglist
;
165 if(ResumeThread(thread
) == -1) {
171 return (uintptr_t)thread
;
174 /*********************************************************************
175 * _beginthreadex_trampoline
177 static DWORD CALLBACK
_beginthreadex_trampoline(LPVOID arg
)
180 _beginthread_trampoline_t local_trampoline
;
181 thread_data_t
*data
= msvcrt_get_thread_data();
183 memcpy(&local_trampoline
, arg
, sizeof(local_trampoline
));
184 data
->handle
= local_trampoline
.thread
;
187 #if _MSVCR_VER >= 140
189 thread_data_t
*data
= msvcrt_get_thread_data();
190 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
191 (void*)local_trampoline
.start_address_ex
, &data
->module
))
194 WARN("failed to get module for the start_address: %d\n", GetLastError());
199 retval
= local_trampoline
.start_address_ex(local_trampoline
.arglist
);
200 _endthreadex(retval
);
202 /*********************************************************************
203 * _beginthreadex (MSVCRT.@)
205 uintptr_t CDECL
_beginthreadex(
206 void *security
, /* [in] Security descriptor for new thread; must be NULL for Windows 9x applications */
207 unsigned int stack_size
, /* [in] Stack size for new thread or 0 */
208 _beginthreadex_start_routine_t start_address
, /* [in] Start address of routine that begins execution of new thread */
209 void *arglist
, /* [in] Argument list to be passed to new thread or NULL */
210 unsigned int initflag
, /* [in] Initial state of new thread (0 for running or CREATE_SUSPEND for suspended) */
211 unsigned int *thrdaddr
) /* [out] Points to a 32-bit variable that receives the thread identifier */
213 _beginthread_trampoline_t
* trampoline
;
216 TRACE("(%p, %d, %p, %p, %d, %p)\n", security
, stack_size
, start_address
, arglist
, initflag
, thrdaddr
);
218 /* FIXME: may use different errno / return values */
219 if (!MSVCRT_CHECK_PMT(start_address
)) return 0;
221 if (!(trampoline
= malloc(sizeof(*trampoline
))))
224 trampoline
->thread
= INVALID_HANDLE_VALUE
;
225 trampoline
->start_address_ex
= start_address
;
226 trampoline
->arglist
= arglist
;
228 thread
= CreateThread(security
, stack_size
, _beginthreadex_trampoline
,
229 trampoline
, initflag
, thrdaddr
);
232 msvcrt_set_errno(GetLastError());
236 return (uintptr_t)thread
;
240 /*********************************************************************
241 * _getptd (MSVCR80.@)
243 thread_data_t
* CDECL
_getptd(void)
245 FIXME("returns undocumented/not fully filled data\n");
246 return msvcrt_get_thread_data();