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
|
|
|
*/
|
2020-11-30 14:37:24 +01:00
|
|
|
#include <process.h>
|
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;
|
2020-11-30 14:37:24 +01:00
|
|
|
_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.
|
|
|
|
*/
|
2020-11-10 16:58:37 +01:00
|
|
|
thread_data_t *CDECL 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;
|
|
|
|
}
|
|
|
|
|
2016-02-01 13:16:01 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _endthread (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void CDECL _endthread(void)
|
|
|
|
{
|
|
|
|
thread_data_t *tls;
|
|
|
|
|
|
|
|
TRACE("(void)\n");
|
|
|
|
|
|
|
|
tls = TlsGetValue(msvcrt_tls_index);
|
|
|
|
if (tls && tls->handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(tls->handle);
|
|
|
|
tls->handle = INVALID_HANDLE_VALUE;
|
|
|
|
} else
|
|
|
|
WARN("tls=%p tls->handle=%p\n", tls, tls ? tls->handle : INVALID_HANDLE_VALUE);
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
ExitThread(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _endthreadex (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
void CDECL _endthreadex(
|
|
|
|
unsigned int retval) /* [in] Thread exit code */
|
|
|
|
{
|
|
|
|
TRACE("(%d)\n", retval);
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
ExitThread(retval);
|
|
|
|
}
|
2002-07-19 05:24:50 +02:00
|
|
|
|
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;
|
2020-12-01 13:51:32 +01:00
|
|
|
free(arg);
|
2001-10-08 22:32:41 +02:00
|
|
|
|
|
|
|
local_trampoline.start_address(local_trampoline.arglist);
|
2016-02-01 13:16:01 +01:00
|
|
|
_endthread();
|
2001-10-08 22:32:41 +02:00
|
|
|
return 0;
|
2001-06-19 05:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _beginthread (MSVCRT.@)
|
|
|
|
*/
|
2020-11-20 12:35:52 +01:00
|
|
|
uintptr_t CDECL _beginthread(
|
2020-11-30 14:37:24 +01:00
|
|
|
_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);
|
|
|
|
|
2020-12-01 13:51:32 +01:00
|
|
|
trampoline = malloc(sizeof(*trampoline));
|
2011-06-19 21:43:32 +02:00
|
|
|
if(!trampoline) {
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EAGAIN;
|
2011-05-24 17:22:02 +02:00
|
|
|
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) {
|
2020-12-01 13:51:32 +01:00
|
|
|
free(trampoline);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EAGAIN;
|
2011-05-24 17:22:02 +02:00
|
|
|
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) {
|
2020-12-01 13:51:32 +01:00
|
|
|
free(trampoline);
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EAGAIN;
|
2011-05-24 17:22:02 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-06-19 21:43:32 +02:00
|
|
|
|
2020-11-20 12:35:52 +01:00
|
|
|
return (uintptr_t)thread;
|
2001-06-19 05:46:27 +02:00
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _beginthreadex (MSVCRT.@)
|
|
|
|
*/
|
2020-11-20 12:35:52 +01:00
|
|
|
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 */
|
2020-11-30 14:37:24 +01:00
|
|
|
_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 */
|
2020-11-20 12:35:52 +01:00
|
|
|
return (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
|
|
|
}
|
|
|
|
|
2018-01-07 06:05:31 +01:00
|
|
|
#if _MSVCR_VER>=80
|
2011-05-24 17:23:24 +02:00
|
|
|
/*********************************************************************
|
2014-04-21 14:32:40 +02:00
|
|
|
* _getptd (MSVCR80.@)
|
2011-05-24 17:23:24 +02:00
|
|
|
*/
|
|
|
|
thread_data_t* CDECL _getptd(void)
|
|
|
|
{
|
|
|
|
FIXME("returns undocumented/not fully filled data\n");
|
|
|
|
return msvcrt_get_thread_data();
|
|
|
|
}
|
2018-01-07 06:05:31 +01:00
|
|
|
#endif
|