winewayland.drv: Implement vkGetPhysicalDeviceSurfaceSupportKHR.
[wine.git] / dlls / msvcrt / thread.c
blob6aad9a22d4781bd1d359ca3feadb26620858574b
1 /*
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
20 #include <process.h>
21 #include "msvcrt.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
26 /********************************************************************/
28 typedef struct {
29 HANDLE thread;
30 union {
31 _beginthread_start_routine_t start_address;
32 _beginthreadex_start_routine_t start_address_ex;
34 void *arglist;
35 #if _MSVCR_VER >= 140
36 HMODULE module;
37 #endif
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)
47 thread_data_t *ptr;
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;
57 ptr->random_seed = 1;
58 ptr->locinfo = MSVCRT_locale->locinfo;
59 ptr->mbcinfo = MSVCRT_locale->mbcinfo;
60 #if _MSVCR_VER >= 140
61 ptr->module = NULL;
62 #endif
64 SetLastError( err );
65 return ptr;
68 /*********************************************************************
69 * _endthread (MSVCRT.@)
71 void CDECL _endthread(void)
73 thread_data_t *tls;
75 TRACE("(void)\n");
77 tls = TlsGetValue(msvcrt_tls_index);
78 if (tls && tls->handle != INVALID_HANDLE_VALUE)
80 CloseHandle(tls->handle);
81 tls->handle = INVALID_HANDLE_VALUE;
82 } else
83 WARN("tls=%p tls->handle=%p\n", tls, tls ? tls->handle : INVALID_HANDLE_VALUE);
85 _endthreadex(0);
88 /*********************************************************************
89 * _endthreadex (MSVCRT.@)
91 void CDECL _endthreadex(
92 unsigned int retval) /* [in] Thread exit code */
94 TRACE("(%d)\n", retval);
96 #if _MSVCR_VER >= 140
98 thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
100 if (tls && tls->module != NULL)
101 FreeLibraryAndExitThread(tls->module, retval);
102 else
103 WARN("tls=%p tls->module=%p\n", tls, tls ? tls->module : NULL);
105 #endif
107 ExitThread(retval);
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));
119 free(arg);
120 data->handle = local_trampoline.thread;
121 #if _MSVCR_VER >= 140
122 data->module = local_trampoline.module;
123 #endif
125 local_trampoline.start_address(local_trampoline.arglist);
126 _endthread();
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;
138 HANDLE thread;
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));
145 if(!trampoline) {
146 *_errno() = EAGAIN;
147 return -1;
150 thread = CreateThread(NULL, stack_size, _beginthread_trampoline,
151 trampoline, CREATE_SUSPENDED, NULL);
152 if(!thread) {
153 free(trampoline);
154 msvcrt_set_errno(GetLastError());
155 return -1;
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());
169 #endif
171 if(ResumeThread(thread) == -1) {
172 #if _MSVCR_VER >= 140
173 FreeLibrary(trampoline->module);
174 #endif
175 free(trampoline);
176 *_errno() = EAGAIN;
177 return -1;
180 return (uintptr_t)thread;
183 /*********************************************************************
184 * _beginthreadex_trampoline
186 static DWORD CALLBACK _beginthreadex_trampoline(LPVOID arg)
188 unsigned int retval;
189 _beginthread_trampoline_t local_trampoline;
190 thread_data_t *data = msvcrt_get_thread_data();
192 memcpy(&local_trampoline, arg, sizeof(local_trampoline));
193 free(arg);
194 data->handle = local_trampoline.thread;
195 #if _MSVCR_VER >= 140
196 data->module = local_trampoline.module;
197 #endif
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;
214 HANDLE thread;
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))))
222 return 0;
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());
235 #endif
237 thread = CreateThread(security, stack_size, _beginthreadex_trampoline,
238 trampoline, initflag, (DWORD *)thrdaddr);
239 if(!thread) {
240 #if _MSVCR_VER >= 140
241 FreeLibrary(trampoline->module);
242 #endif
243 free(trampoline);
244 msvcrt_set_errno(GetLastError());
245 return 0;
248 return (uintptr_t)thread;
251 #if _MSVCR_VER>=80
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();
260 #endif