| 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(16); | 
|---|
| 50 | if (!TlsSetValue( MSVCRT_tls_index, ptr )) MSVCRT__amsg_exit(16); | 
|---|
| 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 MSVCRT_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 CreateThread(NULL, stack_size, _beginthread_trampoline, trampoline, 0, NULL); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /********************************************************************* | 
|---|
| 99 | *              _beginthreadex (MSVCRT.@) | 
|---|
| 100 | */ | 
|---|
| 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 */ | 
|---|
| 108 | { | 
|---|
| 109 | TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr); | 
|---|
| 110 |  | 
|---|
| 111 | /* FIXME */ | 
|---|
| 112 | return CreateThread(security, stack_size, (LPTHREAD_START_ROUTINE) start_address, | 
|---|
| 113 | arglist, initflag, (LPDWORD) thrdaddr); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /********************************************************************* | 
|---|
| 117 | *              MSVCRT_endthread (MSVCRT.@) | 
|---|
| 118 | */ | 
|---|
| 119 | void MSVCRT_endthread(void) | 
|---|
| 120 | { | 
|---|
| 121 | TRACE("(void)\n"); | 
|---|
| 122 |  | 
|---|
| 123 | /* FIXME */ | 
|---|
| 124 | ExitThread(0); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /********************************************************************* | 
|---|
| 128 | *              _endthreadex (MSVCRT.@) | 
|---|
| 129 | */ | 
|---|
| 130 | void _endthreadex( | 
|---|
| 131 | unsigned int retval) /* [in] Thread exit code */ | 
|---|
| 132 | { | 
|---|
| 133 | TRACE("(%d)\n", retval); | 
|---|
| 134 |  | 
|---|
| 135 | /* FIXME */ | 
|---|
| 136 | ExitThread(retval); | 
|---|
| 137 | } | 
|---|