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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "msvcrt/malloc.h"
23 #include "msvcrt/process.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
29 /********************************************************************/
32 _beginthread_start_routine_t start_address;
34 } _beginthread_trampoline_t;
36 /*********************************************************************
37 * msvcrt_get_thread_data
39 * Return the thread local storage structure.
41 MSVCRT_thread_data *msvcrt_get_thread_data(void)
43 MSVCRT_thread_data *ptr;
44 DWORD err = GetLastError(); /* need to preserve last error */
46 if (!(ptr = TlsGetValue( MSVCRT_tls_index )))
48 if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) )))
49 MSVCRT__amsg_exit( _RT_THREAD );
50 if (!TlsSetValue( MSVCRT_tls_index, ptr )) MSVCRT__amsg_exit( _RT_THREAD );
57 /*********************************************************************
58 * _beginthread_trampoline
60 static DWORD CALLBACK _beginthread_trampoline(LPVOID arg)
62 _beginthread_trampoline_t local_trampoline;
64 /* Maybe it's just being paranoid, but freeing arg right
67 memcpy(&local_trampoline,arg,sizeof(local_trampoline));
70 local_trampoline.start_address(local_trampoline.arglist);
74 /*********************************************************************
75 * _beginthread (MSVCRT.@)
77 unsigned long _beginthread(
78 _beginthread_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
79 unsigned int stack_size, /* [in] Stack size for new thread or 0 */
80 void *arglist) /* [in] Argument list to be passed to new thread or NULL */
82 _beginthread_trampoline_t* trampoline;
84 TRACE("(%p, %d, %p)\n", start_address, stack_size, arglist);
86 /* Allocate the trampoline here so that it is still valid when the thread
87 * starts... typically after this function has returned.
88 * _beginthread_trampoline is responsible for freeing the trampoline
90 trampoline=MSVCRT_malloc(sizeof(*trampoline));
91 trampoline->start_address = start_address;
92 trampoline->arglist = arglist;
95 return (unsigned long)CreateThread(NULL, stack_size, _beginthread_trampoline,
99 /*********************************************************************
100 * _beginthreadex (MSVCRT.@)
102 unsigned long _beginthreadex(
103 void *security, /* [in] Security descriptor for new thread; must be NULL for Windows 9x applications */
104 unsigned int stack_size, /* [in] Stack size for new thread or 0 */
105 _beginthreadex_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
106 void *arglist, /* [in] Argument list to be passed to new thread or NULL */
107 unsigned int initflag, /* [in] Initial state of new thread (0 for running or CREATE_SUSPEND for suspended) */
108 unsigned int *thrdaddr) /* [out] Points to a 32-bit variable that receives the thread identifier */
110 TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);
113 return (unsigned long)CreateThread(security, stack_size,
114 (LPTHREAD_START_ROUTINE) start_address,
115 arglist, initflag, (LPDWORD) thrdaddr);
118 /*********************************************************************
119 * _endthread (MSVCRT.@)
121 void _endthread(void)
129 /*********************************************************************
130 * _endthreadex (MSVCRT.@)
133 unsigned int retval) /* [in] Thread exit code */
135 TRACE("(%d)\n", retval);