wow64: Add thunks for the I/O completion syscalls.
[wine.git] / dlls / msvcrt / thread.c
blob01500d93d910fe6fc520289e4af158882ccdc483
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 } _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)
44 thread_data_t *ptr;
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;
54 ptr->random_seed = 1;
55 ptr->locinfo = MSVCRT_locale->locinfo;
56 ptr->mbcinfo = MSVCRT_locale->mbcinfo;
57 #if _MSVCR_VER >= 140
58 ptr->module = NULL;
59 #endif
61 SetLastError( err );
62 return ptr;
65 /*********************************************************************
66 * _endthread (MSVCRT.@)
68 void CDECL _endthread(void)
70 thread_data_t *tls;
72 TRACE("(void)\n");
74 tls = TlsGetValue(msvcrt_tls_index);
75 if (tls && tls->handle != INVALID_HANDLE_VALUE)
77 CloseHandle(tls->handle);
78 tls->handle = INVALID_HANDLE_VALUE;
79 } else
80 WARN("tls=%p tls->handle=%p\n", tls, tls ? tls->handle : INVALID_HANDLE_VALUE);
82 _endthreadex(0);
85 /*********************************************************************
86 * _endthreadex (MSVCRT.@)
88 void CDECL _endthreadex(
89 unsigned int retval) /* [in] Thread exit code */
91 TRACE("(%d)\n", retval);
93 #if _MSVCR_VER >= 140
95 thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
97 if (tls && tls->module != NULL)
98 FreeLibraryAndExitThread(tls->module, retval);
99 else
100 WARN("tls=%p tls->module=%p\n", tls, tls ? tls->module : NULL);
102 #endif
104 ExitThread(retval);
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;
117 free(arg);
119 #if _MSVCR_VER >= 140
120 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
121 (void*)local_trampoline.start_address, &data->module))
123 data->module = NULL;
124 WARN("failed to get module for the start_address: %d\n", GetLastError());
126 #endif
128 local_trampoline.start_address(local_trampoline.arglist);
129 _endthread();
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;
141 HANDLE thread;
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));
148 if(!trampoline) {
149 *_errno() = EAGAIN;
150 return -1;
153 thread = CreateThread(NULL, stack_size, _beginthread_trampoline,
154 trampoline, CREATE_SUSPENDED, NULL);
155 if(!thread) {
156 free(trampoline);
157 msvcrt_set_errno(GetLastError());
158 return -1;
161 trampoline->thread = thread;
162 trampoline->start_address = start_address;
163 trampoline->arglist = arglist;
165 if(ResumeThread(thread) == -1) {
166 free(trampoline);
167 *_errno() = EAGAIN;
168 return -1;
171 return (uintptr_t)thread;
174 /*********************************************************************
175 * _beginthreadex_trampoline
177 static DWORD CALLBACK _beginthreadex_trampoline(LPVOID arg)
179 unsigned int retval;
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;
185 free(arg);
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))
193 data->module = NULL;
194 WARN("failed to get module for the start_address: %d\n", GetLastError());
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 thread = CreateThread(security, stack_size, _beginthreadex_trampoline,
229 trampoline, initflag, thrdaddr);
230 if(!thread) {
231 free(trampoline);
232 msvcrt_set_errno(GetLastError());
233 return 0;
236 return (uintptr_t)thread;
239 #if _MSVCR_VER>=80
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();
248 #endif