ReactOS 0.4.16-dev-2104-gb84fa49
thread.c File Reference
#include <process.h>
#include "msvcrt.h"
#include "wine/debug.h"
Include dependency graph for thread.c:

Go to the source code of this file.

Classes

struct  _beginthread_trampoline_t
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (msvcrt)
 
thread_data_t *CDECL msvcrt_get_thread_data (void)
 
void CDECL _endthread (void)
 
void CDECL _endthreadex (unsigned int retval)
 
static DWORD CALLBACK _beginthread_trampoline (LPVOID arg)
 
uintptr_t CDECL _beginthread (_beginthread_start_routine_t start_address, unsigned int stack_size, void *arglist)
 
static DWORD CALLBACK _beginthreadex_trampoline (LPVOID arg)
 
uintptr_t CDECL _beginthreadex (void *security, unsigned int stack_size, _beginthreadex_start_routine_t start_address, void *arglist, unsigned int initflag, unsigned int *thrdaddr)
 

Function Documentation

◆ _beginthread()

uintptr_t CDECL _beginthread ( _beginthread_start_routine_t  start_address,
unsigned int  stack_size,
void arglist 
)

Definition at line 134 of file thread.c.

138{
139 _beginthread_trampoline_t* trampoline;
141
142 TRACE("(%p, %d, %p)\n", start_address, stack_size, arglist);
143
144 if (!MSVCRT_CHECK_PMT(start_address)) return -1;
145
146 trampoline = malloc(sizeof(*trampoline));
147 if(!trampoline) {
148 *_errno() = EAGAIN;
149 return -1;
150 }
151
153 trampoline, CREATE_SUSPENDED, NULL);
154 if(!thread) {
155 free(trampoline);
157 return -1;
158 }
159
160 trampoline->thread = thread;
161 trampoline->start_address = start_address;
162 trampoline->arglist = arglist;
163
164#if _MSVCR_VER >= 140
165 if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
166 (void*)start_address, &trampoline->module))
167 {
168 trampoline->module = NULL;
169 WARN("failed to get module for the start_address: %lu\n", GetLastError());
170 }
171#endif
172
173 if(ResumeThread(thread) == -1) {
174#if _MSVCR_VER >= 140
175 FreeLibrary(trampoline->module);
176#endif
177 free(trampoline);
178 *_errno() = EAGAIN;
179 return -1;
180 }
181
182 return (uintptr_t)thread;
183}
#define WARN(fmt,...)
Definition: precomp.h:61
static HANDLE thread
Definition: service.c:33
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define FreeLibrary(x)
Definition: compat.h:748
BOOL WINAPI GetModuleHandleExW(IN DWORD dwFlags, IN LPCWSTR lpwModuleName OPTIONAL, OUT HMODULE *phModule)
Definition: loader.c:866
DWORD WINAPI ResumeThread(IN HANDLE hThread)
Definition: thread.c:567
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
int *CDECL _errno(void)
Definition: errno.c:215
unsigned int uintptr_t
Definition: corecrt.h:185
#define EAGAIN
Definition: errno.h:34
#define MSVCRT_CHECK_PMT(x)
Definition: msvcrt.h:378
static DWORD CALLBACK _beginthread_trampoline(LPVOID arg)
Definition: thread.c:115
#define msvcrt_set_errno
Definition: heap.c:50
va_lists_t arglist[FMT_ARGMAX+1]
Definition: format.c:284
#define TRACE(s)
Definition: solgame.cpp:4
_beginthread_start_routine_t start_address
Definition: thread.c:31
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CREATE_SUSPENDED
Definition: winbase.h:182

Referenced by test_thread_invalid_params(), and test_thread_library_reference().

◆ _beginthread_trampoline()

static DWORD CALLBACK _beginthread_trampoline ( LPVOID  arg)
static

Definition at line 115 of file thread.c.

116{
117 _beginthread_trampoline_t local_trampoline;
119
120 memcpy(&local_trampoline,arg,sizeof(local_trampoline));
121 free(arg);
122 data->handle = local_trampoline.thread;
123#if _MSVCR_VER >= 140
124 data->module = local_trampoline.module;
125#endif
126
127 local_trampoline.start_address(local_trampoline.arglist);
128 _endthread();
129}
thread_data_t *CDECL msvcrt_get_thread_data(void)
Definition: thread.c:45
void CDECL _endthread(void)
Definition: thread.c:73
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878

Referenced by _beginthread().

◆ _beginthreadex()

uintptr_t CDECL _beginthreadex ( void security,
unsigned int  stack_size,
_beginthreadex_start_routine_t  start_address,
void arglist,
unsigned int  initflag,
unsigned int thrdaddr 
)

Definition at line 207 of file thread.c.

214{
215 _beginthread_trampoline_t* trampoline;
217
218 TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);
219
220 /* FIXME: may use different errno / return values */
221 if (!MSVCRT_CHECK_PMT(start_address)) return 0;
222
223 if (!(trampoline = malloc(sizeof(*trampoline))))
224 return 0;
225
226 trampoline->thread = INVALID_HANDLE_VALUE;
227 trampoline->start_address_ex = start_address;
228 trampoline->arglist = arglist;
229
230#if _MSVCR_VER >= 140
231 if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
232 (void*)start_address, &trampoline->module))
233 {
234 trampoline->module = NULL;
235 WARN("failed to get module for the start_address: %lu\n", GetLastError());
236 }
237#endif
238
240 trampoline, initflag, (DWORD *)thrdaddr);
241 if(!thread) {
242#if _MSVCR_VER >= 140
243 FreeLibrary(trampoline->module);
244#endif
245 free(trampoline);
247 return 0;
248 }
249
250 return (uintptr_t)thread;
251}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
static DWORD CALLBACK _beginthreadex_trampoline(LPVOID arg)
Definition: thread.c:188
unsigned long DWORD
Definition: ntddk_ex.h:95
_beginthreadex_start_routine_t start_address_ex
Definition: thread.c:32

Referenced by clnt_vc_create(), CDirectoryWatcher::CreateAPCThread(), DialogProc_0(), DialogProc_1(), CIconWatcher::Initialize(), InitProgressDialog(), nfs41_delegation_recall(), nfs41_session_set_lease(), CAutoComplete::OnAutoCompStart(), OpenPropSheet(), pattern_fork(), ProgressDlg::ProcessWindowMessage(), CDeviceView::Refresh(), CZipCreator::runThread(), ServiceStart(), START_TEST(), StartWatchGUI(), CDownloadManager::StartWorkerThread(), test_file_inherit(), test_thread_handle_close(), test_thread_invalid_params(), test_thread_library_reference(), test_thread_setlocale(), and test_thread_suspended().

◆ _beginthreadex_trampoline()

static DWORD CALLBACK _beginthreadex_trampoline ( LPVOID  arg)
static

Definition at line 188 of file thread.c.

189{
190 unsigned int retval;
191 _beginthread_trampoline_t local_trampoline;
193
194 memcpy(&local_trampoline, arg, sizeof(local_trampoline));
195 free(arg);
196 data->handle = local_trampoline.thread;
197#if _MSVCR_VER >= 140
198 data->module = local_trampoline.module;
199#endif
200
201 retval = local_trampoline.start_address_ex(local_trampoline.arglist);
203}
void CDECL _endthreadex(unsigned int retval)
Definition: thread.c:93
int retval
Definition: wcstombs.cpp:91

Referenced by _beginthreadex().

◆ _endthread()

void CDECL _endthread ( void  )

Definition at line 73 of file thread.c.

74{
76
77 TRACE("(void)\n");
78
80 if (tls && tls->handle != INVALID_HANDLE_VALUE)
81 {
82 CloseHandle(tls->handle);
83 tls->handle = INVALID_HANDLE_VALUE;
84 } else
85 WARN("tls=%p tls->handle=%p\n", tls, tls ? tls->handle : INVALID_HANDLE_VALUE);
86
87 _endthreadex(0);
88}
#define CloseHandle
Definition: compat.h:739
LPVOID WINAPI TlsGetValue(IN DWORD Index)
Definition: thread.c:1240
DWORD msvcrt_tls_index
Definition: main.c:29
static DWORD tls
Definition: sock.c:229

Referenced by _beginthread_trampoline().

◆ _endthreadex()

void CDECL _endthreadex ( unsigned int  retval)

Definition at line 93 of file thread.c.

95{
96 TRACE("(%d)\n", retval);
97
98#if _MSVCR_VER >= 140
99 {
101
102 if (tls && tls->module != NULL)
104 else
105 WARN("tls=%p tls->module=%p\n", tls, tls ? tls->module : NULL);
106 }
107#endif
108
110}
VOID WINAPI FreeLibraryAndExitThread(HMODULE hLibModule, DWORD dwExitCode)
Definition: loader.c:507
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:365

Referenced by _beginthreadex_trampoline(), and _endthread().

◆ msvcrt_get_thread_data()

thread_data_t *CDECL msvcrt_get_thread_data ( void  )

Definition at line 45 of file thread.c.

46{
48 DWORD err = GetLastError(); /* need to preserve last error */
49
51 {
52 if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) )))
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 ptr->cached_locale[0] = 'C';
61 ptr->cached_locale[1] = 0;
62#if _MSVCR_VER >= 140
63 ptr->module = NULL;
64#endif
65 }
67 return ptr;
68}
#define GetProcessHeap()
Definition: compat.h:736
#define SetLastError(x)
Definition: compat.h:752
#define HeapAlloc
Definition: compat.h:733
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
BOOL WINAPI TlsSetValue(IN DWORD Index, IN LPVOID Value)
Definition: thread.c:1276
void CDECL _amsg_exit(int errnum)
Definition: exit.c:233
#define _RT_THREAD
Definition: msvcrt.h:308
static PVOID ptr
Definition: dispmode.c:27
#define err(...)
#define MSVCRT_locale
Definition: locale.h:80
DWORD WINAPI GetCurrentThreadId(void)
Definition: thread.c:459

Referenced by _beginthread_trampoline(), and _beginthreadex_trampoline().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( msvcrt  )