Removed the A/W constants for builtin cursors, icons and resource
[wine] / dlls / msvcrt / thread.c
1 /*
2  * msvcrt.dll thread functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20 #include "msvcrt.h"
21
22 #include "msvcrt/malloc.h"
23 #include "msvcrt/process.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28
29 /********************************************************************/
30
31 typedef struct {
32   _beginthread_start_routine_t start_address;
33   void *arglist;
34 } _beginthread_trampoline_t;
35
36 /*********************************************************************
37  *              msvcrt_get_thread_data
38  *
39  * Return the thread local storage structure.
40  */
41 MSVCRT_thread_data *msvcrt_get_thread_data(void)
42 {
43     MSVCRT_thread_data *ptr;
44     DWORD err = GetLastError();  /* need to preserve last error */
45
46     if (!(ptr = TlsGetValue( MSVCRT_tls_index )))
47     {
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 );
51     }
52     SetLastError( err );
53     return ptr;
54 }
55
56
57 /*********************************************************************
58  *              _beginthread_trampoline
59  */
60 static DWORD CALLBACK _beginthread_trampoline(LPVOID arg)
61 {
62     _beginthread_trampoline_t local_trampoline;
63
64     /* Maybe it's just being paranoid, but freeing arg right
65      * away seems safer.
66      */
67     memcpy(&local_trampoline,arg,sizeof(local_trampoline));
68     MSVCRT_free(arg);
69
70     local_trampoline.start_address(local_trampoline.arglist);
71     return 0;
72 }
73
74 /*********************************************************************
75  *              _beginthread (MSVCRT.@)
76  */
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 */
81 {
82   _beginthread_trampoline_t* trampoline;
83
84   TRACE("(%p, %d, %p)\n", start_address, stack_size, arglist);
85
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
89    */
90   trampoline=MSVCRT_malloc(sizeof(*trampoline));
91   trampoline->start_address = start_address;
92   trampoline->arglist = arglist;
93
94   /* FIXME */
95   return (unsigned long)CreateThread(NULL, stack_size, _beginthread_trampoline,
96                                      trampoline, 0, NULL);
97 }
98
99 /*********************************************************************
100  *              _beginthreadex (MSVCRT.@)
101  */
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 */
109 {
110   TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);
111
112   /* FIXME */
113   return (unsigned long)CreateThread(security, stack_size,
114                                      (LPTHREAD_START_ROUTINE) start_address,
115                                      arglist, initflag, (LPDWORD) thrdaddr);
116 }
117
118 /*********************************************************************
119  *              _endthread (MSVCRT.@)
120  */
121 void _endthread(void)
122 {
123   TRACE("(void)\n");
124
125   /* FIXME */
126   ExitThread(0);
127 }
128
129 /*********************************************************************
130  *              _endthreadex (MSVCRT.@)
131  */
132 void _endthreadex(
133   unsigned int retval) /* [in] Thread exit code */
134 {
135   TRACE("(%d)\n", retval);
136
137   /* FIXME */
138   ExitThread(retval);
139 }