Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / dlls / msvcrt / thread.c
blobf6203187ac0503c91afd1ba180d2ee282558360c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "msvcrt.h"
22 #include "msvcrt/malloc.h"
23 #include "msvcrt/process.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
29 /********************************************************************/
31 typedef struct {
32 _beginthread_start_routine_t start_address;
33 void *arglist;
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 );
52 SetLastError( err );
53 return ptr;
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
65 * away seems safer.
67 memcpy(&local_trampoline,arg,sizeof(local_trampoline));
68 MSVCRT_free(arg);
70 local_trampoline.start_address(local_trampoline.arglist);
71 return 0;
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;
94 /* FIXME */
95 return CreateThread(NULL, stack_size, _beginthread_trampoline, trampoline, 0, NULL);
98 /*********************************************************************
99 * _beginthreadex (MSVCRT.@)
101 unsigned long _beginthreadex(
102 void *security, /* [in] Security descriptor for new thread; must be NULL for Windows 9x applications */
103 unsigned int stack_size, /* [in] Stack size for new thread or 0 */
104 _beginthreadex_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
105 void *arglist, /* [in] Argument list to be passed to new thread or NULL */
106 unsigned int initflag, /* [in] Initial state of new thread (0 for running or CREATE_SUSPEND for suspended) */
107 unsigned int *thrdaddr) /* [out] Points to a 32-bit variable that receives the thread identifier */
109 TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);
111 /* FIXME */
112 return CreateThread(security, stack_size, (LPTHREAD_START_ROUTINE) start_address,
113 arglist, initflag, (LPDWORD) thrdaddr);
116 /*********************************************************************
117 * _endthread (MSVCRT.@)
119 void _endthread(void)
121 TRACE("(void)\n");
123 /* FIXME */
124 ExitThread(0);
127 /*********************************************************************
128 * _endthreadex (MSVCRT.@)
130 void _endthreadex(
131 unsigned int retval) /* [in] Thread exit code */
133 TRACE("(%d)\n", retval);
135 /* FIXME */
136 ExitThread(retval);