thread.c 7.59 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll thread functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20
#include <process.h>
21
#include "msvcrt.h"
22 23 24
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
25

26 27 28
/********************************************************************/

typedef struct {
29
  HANDLE thread;
30 31 32 33
  union {
      _beginthread_start_routine_t start_address;
      _beginthreadex_start_routine_t start_address_ex;
  };
34
  void *arglist;
35 36 37
#if _MSVCR_VER >= 140
  HMODULE module;
#endif
38 39
} _beginthread_trampoline_t;

40 41 42 43 44
/*********************************************************************
 *		msvcrt_get_thread_data
 *
 * Return the thread local storage structure.
 */
45
thread_data_t *CDECL msvcrt_get_thread_data(void)
46
{
47
    thread_data_t *ptr;
48 49
    DWORD err = GetLastError();  /* need to preserve last error */

50
    if (!(ptr = TlsGetValue( msvcrt_tls_index )))
51 52
    {
        if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) )))
53 54
            _amsg_exit( _RT_THREAD );
        if (!TlsSetValue( msvcrt_tls_index, ptr )) _amsg_exit( _RT_THREAD );
55 56
        ptr->tid = GetCurrentThreadId();
        ptr->handle = INVALID_HANDLE_VALUE;
57
        ptr->random_seed = 1;
58 59
        ptr->locinfo = MSVCRT_locale->locinfo;
        ptr->mbcinfo = MSVCRT_locale->mbcinfo;
60 61 62
#if _MSVCR_VER >= 140
        ptr->module = NULL;
#endif
63 64 65 66 67
    }
    SetLastError( err );
    return ptr;
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*********************************************************************
 *		_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);

85
  _endthreadex(0);
86 87 88 89 90 91 92 93 94 95
}

/*********************************************************************
 *		_endthreadex (MSVCRT.@)
 */
void CDECL _endthreadex(
  unsigned int retval) /* [in] Thread exit code */
{
  TRACE("(%d)\n", retval);

96 97 98 99 100 101 102 103 104 105 106
#if _MSVCR_VER >= 140
  {
      thread_data_t *tls = TlsGetValue(msvcrt_tls_index);

      if (tls && tls->module != NULL)
          FreeLibraryAndExitThread(tls->module, retval);
      else
          WARN("tls=%p tls->module=%p\n", tls, tls ? tls->module : NULL);
  }
#endif

107 108
  ExitThread(retval);
}
109

110 111 112 113 114
/*********************************************************************
 *		_beginthread_trampoline
 */
static DWORD CALLBACK _beginthread_trampoline(LPVOID arg)
{
115
    _beginthread_trampoline_t local_trampoline;
116
    thread_data_t *data = msvcrt_get_thread_data();
117

118
    memcpy(&local_trampoline,arg,sizeof(local_trampoline));
119
    free(arg);
120
    data->handle = local_trampoline.thread;
121
#if _MSVCR_VER >= 140
122
    data->module = local_trampoline.module;
123 124
#endif

125
    local_trampoline.start_address(local_trampoline.arglist);
126
    _endthread();
127 128 129 130 131
}

/*********************************************************************
 *		_beginthread (MSVCRT.@)
 */
132
uintptr_t CDECL _beginthread(
133
  _beginthread_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
134 135 136
  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 */
{
137
  _beginthread_trampoline_t* trampoline;
138
  HANDLE thread;
139 140 141

  TRACE("(%p, %d, %p)\n", start_address, stack_size, arglist);

142 143
  if (!MSVCRT_CHECK_PMT(start_address)) return -1;

144
  trampoline = malloc(sizeof(*trampoline));
145
  if(!trampoline) {
146
      *_errno() = EAGAIN;
147 148 149 150
      return -1;
  }

  thread = CreateThread(NULL, stack_size, _beginthread_trampoline,
151
          trampoline, CREATE_SUSPENDED, NULL);
152
  if(!thread) {
153
      free(trampoline);
154
      msvcrt_set_errno(GetLastError());
155 156 157
      return -1;
  }

158 159 160
  trampoline->thread = thread;
  trampoline->start_address = start_address;
  trampoline->arglist = arglist;
161

162 163 164 165 166
#if _MSVCR_VER >= 140
  if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
              (void*)start_address, &trampoline->module))
  {
      trampoline->module = NULL;
167
      WARN("failed to get module for the start_address: %lu\n", GetLastError());
168 169 170
  }
#endif

171
  if(ResumeThread(thread) == -1) {
172 173 174
#if _MSVCR_VER >= 140
      FreeLibrary(trampoline->module);
#endif
175
      free(trampoline);
176
      *_errno() = EAGAIN;
177 178
      return -1;
  }
179

180
  return (uintptr_t)thread;
181
}
182

183 184 185 186 187 188 189 190 191 192 193
/*********************************************************************
 *		_beginthreadex_trampoline
 */
static DWORD CALLBACK _beginthreadex_trampoline(LPVOID arg)
{
    unsigned int retval;
    _beginthread_trampoline_t local_trampoline;
    thread_data_t *data = msvcrt_get_thread_data();

    memcpy(&local_trampoline, arg, sizeof(local_trampoline));
    free(arg);
194
    data->handle = local_trampoline.thread;
195
#if _MSVCR_VER >= 140
196
    data->module = local_trampoline.module;
197 198
#endif

199 200 201
    retval = local_trampoline.start_address_ex(local_trampoline.arglist);
    _endthreadex(retval);
}
202 203 204
/*********************************************************************
 *		_beginthreadex (MSVCRT.@)
 */
205
uintptr_t CDECL _beginthreadex(
206 207
  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 */
208
  _beginthreadex_start_routine_t start_address, /* [in] Start address of routine that begins execution of new thread */
209 210 211 212
  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 */
{
213 214 215
  _beginthread_trampoline_t* trampoline;
  HANDLE thread;

216 217
  TRACE("(%p, %d, %p, %p, %d, %p)\n", security, stack_size, start_address, arglist, initflag, thrdaddr);

218 219 220 221 222 223 224 225 226 227
  /* FIXME: may use different errno / return values */
  if (!MSVCRT_CHECK_PMT(start_address)) return 0;

  if (!(trampoline = malloc(sizeof(*trampoline))))
      return 0;

  trampoline->thread = INVALID_HANDLE_VALUE;
  trampoline->start_address_ex = start_address;
  trampoline->arglist = arglist;

228 229 230 231 232
#if _MSVCR_VER >= 140
  if(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
              (void*)start_address, &trampoline->module))
  {
     trampoline->module = NULL;
233
     WARN("failed to get module for the start_address: %lu\n", GetLastError());
234 235 236
  }
#endif

237
  thread = CreateThread(security, stack_size, _beginthreadex_trampoline,
238
          trampoline, initflag, (DWORD *)thrdaddr);
239
  if(!thread) {
240 241 242
#if _MSVCR_VER >= 140
      FreeLibrary(trampoline->module);
#endif
243 244 245 246 247 248
      free(trampoline);
      msvcrt_set_errno(GetLastError());
      return 0;
  }

  return (uintptr_t)thread;
249 250
}

251
#if _MSVCR_VER>=80
252
/*********************************************************************
253
 *		_getptd (MSVCR80.@)
254 255 256 257 258 259
 */
thread_data_t* CDECL _getptd(void)
{
    FIXME("returns undocumented/not fully filled data\n");
    return msvcrt_get_thread_data();
}
260
#endif