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
;
38 } _beginthread_trampoline_t
;
40 /*********************************************************************
41 * msvcrt_get_thread_data
43 * Return the thread local storage structure.
45 thread_data_t
*CDECL
msvcrt_get_thread_data(void)
48 DWORD err
= GetLastError(); /* need to preserve last error */
50 if (!(ptr
= TlsGetValue( msvcrt_tls_index
)))
52 if (!(ptr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptr
) )))
53 _amsg_exit( _RT_THREAD
);
54 if (!TlsSetValue( msvcrt_tls_index
, ptr
)) _amsg_exit( _RT_THREAD
);
55 ptr
->tid
= GetCurrentThreadId();
56 ptr
->handle
= INVALID_HANDLE_VALUE
;
58 ptr
->locinfo
= MSVCRT_locale
->locinfo
;
59 ptr
->mbcinfo
= MSVCRT_locale
->mbcinfo
;
68 /*********************************************************************
69 * _endthread (MSVCRT.@)
71 void CDECL
_endthread(void)
77 tls
= TlsGetValue(msvcrt_tls_index
);
78 if (tls
&& tls
->handle
!= INVALID_HANDLE_VALUE
)
80 CloseHandle(tls
->handle
);
81 tls
->handle
= INVALID_HANDLE_VALUE
;
83 WARN("tls=%p tls->handle=%p\n", tls
, tls
? tls
->handle
: INVALID_HANDLE_VALUE
);
88 /*********************************************************************
89 * _endthreadex (MSVCRT.@)
91 void CDECL
_endthreadex(
92 unsigned int retval
) /* [in] Thread exit code */
94 TRACE("(%d)\n", retval
);
98 thread_data_t
*tls
= TlsGetValue(msvcrt_tls_index
);
100 if (tls
&& tls
->module
!= NULL
)
101 FreeLibraryAndExitThread(tls
->module
, retval
);
103 WARN("tls=%p tls->module=%p\n", tls
, tls
? tls
->module
: NULL
);
110 /*********************************************************************
111 * _beginthread_trampoline
113 static DWORD CALLBACK
_beginthread_trampoline(LPVOID arg
)
115 _beginthread_trampoline_t local_trampoline
;
116 thread_data_t
*data
= msvcrt_get_thread_data();
118 memcpy(&local_trampoline
,arg
,sizeof(local_trampoline
));
120 data
->handle
= local_trampoline
.thread
;
121 #if _MSVCR_VER >= 140
122 data
->module
= local_trampoline
.module
;
125 local_trampoline
.start_address(local_trampoline
.arglist
);
129 /*********************************************************************
130 * _beginthread (MSVCRT.@)
132 uintptr_t CDECL
_beginthread(
133 _beginthread_start_routine_t start_address
, /* [in] Start address of routine that begins execution of new thread */
134 unsigned int stack_size
, /* [in] Stack size for new thread or 0 */
135 void *arglist
) /* [in] Argument list to be passed to new thread or NULL */
137 _beginthread_trampoline_t
* trampoline
;
140 TRACE("(%p, %d, %p)\n", start_address
, stack_size
, arglist
);
142 if (!MSVCRT_CHECK_PMT(start_address
)) return -1;
144 trampoline
= malloc(sizeof(*trampoline
));
150 thread
= CreateThread(NULL
, stack_size
, _beginthread_trampoline
,
151 trampoline
, CREATE_SUSPENDED
, NULL
);
154 msvcrt_set_errno(GetLastError());
158 trampoline
->thread
= thread
;
159 trampoline
->start_address
= start_address
;
160 trampoline
->arglist
= arglist
;
162 #if _MSVCR_VER >= 140
163 if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
164 (void*)start_address
, &trampoline
->module
))
166 trampoline
->module
= NULL
;
167 WARN("failed to get module for the start_address: %lu\n", GetLastError());
171 if(ResumeThread(thread
) == -1) {
172 #if _MSVCR_VER >= 140
173 FreeLibrary(trampoline
->module
);
180 return (uintptr_t)thread
;
183 /*********************************************************************
184 * _beginthreadex_trampoline
186 static DWORD CALLBACK
_beginthreadex_trampoline(LPVOID arg
)
189 _beginthread_trampoline_t local_trampoline
;
190 thread_data_t
*data
= msvcrt_get_thread_data();
192 memcpy(&local_trampoline
, arg
, sizeof(local_trampoline
));
194 data
->handle
= local_trampoline
.thread
;
195 #if _MSVCR_VER >= 140
196 data
->module
= local_trampoline
.module
;
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 #if _MSVCR_VER >= 140
229 if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
230 (void*)start_address
, &trampoline
->module
))
232 trampoline
->module
= NULL
;
233 WARN("failed to get module for the start_address: %lu\n", GetLastError());
237 thread
= CreateThread(security
, stack_size
, _beginthreadex_trampoline
,
238 trampoline
, initflag
, (DWORD
*)thrdaddr
);
240 #if _MSVCR_VER >= 140
241 FreeLibrary(trampoline
->module
);
244 msvcrt_set_errno(GetLastError());
248 return (uintptr_t)thread
;
252 /*********************************************************************
253 * _getptd (MSVCR80.@)
255 thread_data_t
* CDECL
_getptd(void)
257 FIXME("returns undocumented/not fully filled data\n");
258 return msvcrt_get_thread_data();