2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll thread functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
|
|
|
#include "msvcrt.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2001-06-19 05:46:27 +02:00
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
typedef struct {
|
2011-06-19 21:43:32 +02:00
|
|
|
HANDLE thread;
|
2004-06-25 03:19:15 +02:00
|
|
|
MSVCRT__beginthread_start_routine_t start_address;
|
2001-06-19 05:46:27 +02:00
|
|
|
void *arglist;
|
|
|
|
} _beginthread_trampoline_t;
|
|
|
|
|
2002-07-19 05:24:50 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* msvcrt_get_thread_data
|
|
|
|
*
|
|
|
|
* Return the thread local storage structure.
|
|
|
|
*/
|
2004-06-25 03:19:15 +02:00
|
|
|
thread_data_t *msvcrt_get_thread_data(void)
|
2002-07-19 05:24:50 +02:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
thread_data_t *ptr;
|
2002-07-19 05:24:50 +02:00
|
|
|
DWORD err = GetLastError(); /* need to preserve last error */
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if (!(ptr = TlsGetValue( msvcrt_tls_index )))
|
2002-07-19 05:24:50 +02:00
|
|
|
{
|
|
|
|
if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) )))
|
2004-06-25 03:19:15 +02:00
|
|
|
_amsg_exit( _RT_THREAD );
|
|
|
|
if (!TlsSetValue( msvcrt_tls_index, ptr )) _amsg_exit( _RT_THREAD );
|
2011-05-24 17:22:02 +02:00
|
|
|
ptr->tid = GetCurrentThreadId();
|
|
|
|
ptr->handle = INVALID_HANDLE_VALUE;
|
2006-01-14 17:27:28 +01:00
|
|
|
ptr->random_seed = 1;
|
2011-05-24 17:22:42 +02:00
|
|
|
ptr->locinfo = MSVCRT_locale->locinfo;
|
|
|
|
ptr->mbcinfo = MSVCRT_locale->mbcinfo;
|
2002-07-19 05:24:50 +02:00
|
|
|
}
|
|
|
|
SetLastError( err );
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-19 05:46:27 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _beginthread_trampoline
|
|
|
|
*/
|
|
|
|
static DWORD CALLBACK _beginthread_trampoline(LPVOID arg)
|
|
|
|
{
|
2011-06-19 21:43:32 +02:00
|
|
|
_beginthread_trampoline_t local_trampoline;
|
2011-05-24 17:22:02 +02:00
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
2001-10-08 22:32:41 +02:00
|
|
|
|
2011-06-19 21:43:32 +02:00
|
|
|
memcpy(&local_trampoline,arg,sizeof(local_trampoline));
|
2011-05-24 17:22:02 +02:00
|
|
|
data->handle = local_trampoline.thread;
|
2011-06-19 21:43:32 +02:00
|
|
|
MSVCRT_free(arg);
|
2001-10-08 22:32:41 +02:00
|
|
|
|
|
|
|
local_trampoline.start_address(local_trampoline.arglist);
|
|
|
|
return 0;
|
2001-06-19 05:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _beginthread (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_uintptr_t CDECL _beginthread(
|
2004-06-25 03:19:15 +02:00
|
|
|
MSVCRT__beginthread_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
|
2001-06-19 05:46:27 +02:00
|
|
|
unsigned int stack_size, /* [in] Stack size for new thread or 0 */
|
|
|
|
void *arglist) /* [in] Argument list to be passed to new thread or NULL */
|
|
|
|
{
|
2011-06-19 21:43:32 +02:00
|
|
|
_beginthread_trampoline_t* trampoline;
|
2011-05-24 17:22:02 +02:00
|
|
|
HANDLE thread;
|
2001-06-19 05:46:27 +02:00
|
|
|
|
|
|
|
TRACE("(%p, %d, %p)\n", start_address, stack_size, arglist);
|
|
|
|
|
2011-06-19 21:43:32 +02:00
|
|
|
trampoline = MSVCRT_malloc(sizeof(*trampoline));
|
|
|
|
if(!trampoline) {
|
2011-05-24 17:22:02 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread = CreateThread(NULL, stack_size, _beginthread_trampoline,
|
2011-06-19 21:43:32 +02:00
|
|
|
trampoline, CREATE_SUSPENDED, NULL);
|
2011-05-24 17:22:02 +02:00
|
|
|
if(!thread) {
|
2011-06-19 21:43:32 +02:00
|
|
|
MSVCRT_free(trampoline);
|
2011-05-24 17:22:02 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-19 21:43:32 +02:00
|
|
|
trampoline->thread = thread;
|
|
|
|
trampoline->start_address = start_address;
|
|
|
|
trampoline->arglist = arglist;
|
2011-05-24 17:22:02 +02:00
|
|
|
|
2011-06-19 21:43:32 +02:00
|
|
|
if(ResumeThread(thread) == -1) {
|
|
|
|
MSVCRT_free(trampoline);
|
2011-05-24 17:22:02 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-19 21:43:32 +02:00
|
|
|
|
|
|
|
return (MSVCRT_uintptr_t)thread;
|
2001-06-19 05:46:27 +02:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _beginthreadex (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_uintptr_t CDECL _beginthreadex(
|
2001-06-19 05:46:27 +02:00
|
|
|
void *security, /* [in] Security descriptor for new thread; must be NULL for Windows 9x applications */
|
|
|
|
unsigned int stack_size, /* [in] Stack size for new thread or 0 */
|
2004-06-25 03:19:15 +02:00
|
|
|
MSVCRT__beginthreadex_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
|
2001-06-19 05:46:27 +02:00
|
|
|
void *arglist, /* [in] Argument list to be passed to new thread or NULL */
|
|
|
|
unsigned int initflag, /* [in] Initial state of new thread (0 for running or CREATE_SUSPEND for suspended) */
|
|
|
|
unsigned int *thrdaddr) /* [out] Points to a 32-bit variable that receives the thread identifier */
|
|
|
|
{
|
|
|
|
TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);
|
|
|
|
|
|
|
|
/* FIXME */
|
2005-11-03 14:17:51 +01:00
|
|
|
return (MSVCRT_uintptr_t)CreateThread(security, stack_size,
|
2008-01-12 17:05:11 +01:00
|
|
|
start_address, arglist,
|
|
|
|
initflag, thrdaddr);
|
2001-06-19 05:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _endthread (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL _endthread(void)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-06-19 05:46:27 +02:00
|
|
|
TRACE("(void)\n");
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* FIXME */
|
2001-06-19 05:46:27 +02:00
|
|
|
ExitThread(0);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _endthreadex (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void CDECL _endthreadex(
|
2001-06-19 05:46:27 +02:00
|
|
|
unsigned int retval) /* [in] Thread exit code */
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-06-19 05:46:27 +02:00
|
|
|
TRACE("(%d)\n", retval);
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/* FIXME */
|
|
|
|
ExitThread(retval);
|
|
|
|
}
|
2011-05-24 17:23:24 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getptd - not exported in native msvcrt
|
|
|
|
*/
|
|
|
|
thread_data_t* CDECL _getptd(void)
|
|
|
|
{
|
|
|
|
FIXME("returns undocumented/not fully filled data\n");
|
|
|
|
return msvcrt_get_thread_data();
|
|
|
|
}
|