Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
8.8 KiB

  1. /***
  2. *thread.c - Begin and end a thread
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This source contains the _beginthread() and _endthread()
  8. * routines which are used to start and terminate a thread.
  9. *
  10. *Revision History:
  11. * 05-09-90 JCR Translated from ASM to C
  12. * 07-25-90 SBM Removed '32' from API names
  13. * 10-08-90 GJF New-style function declarators.
  14. * 10-09-90 GJF Thread ids are of type unsigned long.
  15. * 10-19-90 GJF Added code to set _stkhqq properly in stub().
  16. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  17. * 12-06-90 SRW Added _CRUISER_ and _WIN32 conditionals.
  18. * 06-03-91 GJF Win32 version [_WIN32_].
  19. * 07-18-91 GJF Fixed many silly errors [_WIN32_].
  20. * 08-19-91 GJF Allow for newly created thread terminating before
  21. * _beginthread returns
  22. * 09-30-91 GJF Add per-thread initialization and termination calls
  23. * for floating point.
  24. * 01-18-92 GJF Revised try - except statement.
  25. * 02-25-92 GJF Initialize _holdrand field to 1.
  26. * 09-30-92 SRW Add WINAPI keyword to _threadstart routine
  27. * 10-30-92 GJF Error ret for CreateThread is 0 (NULL), not -1.
  28. * 02-13-93 GJF Revised to use TLS API. Also, purged Cruiser support.
  29. * 03-26-93 GJF Fixed horribly embarrassing bug: ptd->pxcptacttab
  30. * must be initialized to _XcptActTab!
  31. * 04-01-93 CFW Change try-except to __try-__except
  32. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  33. * 04-27-93 GJF Removed support for _RT_STACK, _RT_INTDIV,
  34. * _RT_INVALDISP and _RT_NONCONT.
  35. * 10-26-93 GJF Replaced PF with _PVFV (defined in internal.h).
  36. * 12-13-93 SKS Free up per-thread data using a call to _freeptd()
  37. * 01-06-94 GJF Free up _tiddata struct upon failure in _beginthread.
  38. * Also, set errno on failure.
  39. * 01-10-95 CFW Debug CRT allocs.
  40. * 04-18-95 SKS Add 5 MIPS per-thread variables.
  41. * 05-02-95 SKS Call _initptd for initialization of per-thread data.
  42. * 02-03-98 GJF Changes for Win64: use uintptr_t type for anything with
  43. * a HANDLE value.
  44. * 02-02-00 GB Modified threadstart() to prevent leaking of ptd
  45. * allocated during call to getptd while ATTACHing THREAD
  46. * in dlls.
  47. * 08-04-00 PML Set EINVAL error if thread start address null in
  48. * _beginthread (VS7#118688).
  49. *
  50. *******************************************************************************/
  51. #ifdef _MT
  52. #include <cruntime.h>
  53. #include <oscalls.h>
  54. #include <internal.h>
  55. #include <mtdll.h>
  56. #include <msdos.h>
  57. #include <malloc.h>
  58. #include <process.h>
  59. #include <stddef.h>
  60. #include <rterr.h>
  61. #include <dbgint.h>
  62. #include <errno.h>
  63. /*
  64. * Startup code for new thread.
  65. */
  66. static unsigned long WINAPI _threadstart(void *);
  67. /*
  68. * declare pointers to per-thread FP initialization and termination routines
  69. */
  70. _PVFV _FPmtinit;
  71. _PVFV _FPmtterm;
  72. /***
  73. *_beginthread() - Create a child thread
  74. *
  75. *Purpose:
  76. * Create a child thread.
  77. *
  78. *Entry:
  79. * initialcode = pointer to thread's startup code address
  80. * stacksize = size of stack
  81. * argument = argument to be passed to new thread
  82. *
  83. *Exit:
  84. * success = handle for new thread if successful
  85. *
  86. * failure = (unsigned long) -1L in case of error, errno and _doserrno
  87. * are set
  88. *
  89. *Exceptions:
  90. *
  91. *******************************************************************************/
  92. uintptr_t __cdecl _beginthread (
  93. void (__cdecl * initialcode) (void *),
  94. unsigned stacksize,
  95. void * argument
  96. )
  97. {
  98. _ptiddata ptd; /* pointer to per-thread data */
  99. uintptr_t thdl; /* thread handle */
  100. unsigned long errcode = 0L; /* Return from GetLastError() */
  101. if ( initialcode == NULL ) {
  102. errno = EINVAL;
  103. return( (uintptr_t)(-1) );
  104. }
  105. /*
  106. * Allocate and initialize a per-thread data structure for the to-
  107. * be-created thread.
  108. */
  109. if ( (ptd = _calloc_crt(1, sizeof(struct _tiddata))) == NULL )
  110. goto error_return;
  111. /*
  112. * Initialize the per-thread data
  113. */
  114. _initptd(ptd);
  115. ptd->_initaddr = (void *) initialcode;
  116. ptd->_initarg = argument;
  117. /*
  118. * Create the new thread. Bring it up in a suspended state so that
  119. * the _thandle and _tid fields are filled in before execution
  120. * starts.
  121. */
  122. if ( (ptd->_thandle = thdl = (uintptr_t)
  123. CreateThread( NULL,
  124. stacksize,
  125. _threadstart,
  126. (LPVOID)ptd,
  127. CREATE_SUSPENDED,
  128. (LPDWORD)&(ptd->_tid) ))
  129. == (uintptr_t)0 )
  130. {
  131. errcode = GetLastError();
  132. goto error_return;
  133. }
  134. /*
  135. * Start the new thread executing
  136. */
  137. if ( ResumeThread( (HANDLE)thdl ) == (DWORD)(-1) ) {
  138. errcode = GetLastError();
  139. goto error_return;
  140. }
  141. /*
  142. * Good return
  143. */
  144. return(thdl);
  145. /*
  146. * Error return
  147. */
  148. error_return:
  149. /*
  150. * Either ptd is NULL, or it points to the no-longer-necessary block
  151. * calloc-ed for the _tiddata struct which should now be freed up.
  152. */
  153. _free_crt(ptd);
  154. /*
  155. * Map the error, if necessary.
  156. */
  157. if ( errcode != 0L )
  158. _dosmaperr(errcode);
  159. return( (uintptr_t)(-1) );
  160. }
  161. /***
  162. *_threadstart() - New thread begins here
  163. *
  164. *Purpose:
  165. * The new thread begins execution here. This routine, in turn,
  166. * passes control to the user's code.
  167. *
  168. *Entry:
  169. * void *ptd = pointer to _tiddata structure for this thread
  170. *
  171. *Exit:
  172. * Never returns - terminates thread!
  173. *
  174. *Exceptions:
  175. *
  176. *******************************************************************************/
  177. static unsigned long WINAPI _threadstart (
  178. void * ptd
  179. )
  180. {
  181. _ptiddata _ptd; /* pointer to per-thread data */
  182. /*
  183. * Check if ptd is initialised during THREAD_ATTACH call to dll mains
  184. */
  185. if ( (_ptd = TlsGetValue(__tlsindex)) == NULL)
  186. {
  187. /*
  188. * Stash the pointer to the per-thread data stucture in TLS
  189. */
  190. if ( !TlsSetValue(__tlsindex, ptd) )
  191. _amsg_exit(_RT_THREAD);
  192. }
  193. else
  194. {
  195. _ptd->_initaddr = ((_ptiddata) ptd)->_initaddr;
  196. _ptd->_initarg = ((_ptiddata) ptd)->_initarg;
  197. _ptd->_thandle = ((_ptiddata) ptd)->_thandle;
  198. _free_crt(ptd);
  199. ptd = _ptd;
  200. }
  201. /*
  202. * Call fp initialization, if necessary
  203. */
  204. if ( _FPmtinit != NULL )
  205. (*_FPmtinit)();
  206. /*
  207. * Guard call to user code with a _try - _except statement to
  208. * implement runtime errors and signal support
  209. */
  210. __try {
  211. ( (void(__cdecl *)(void *))(((_ptiddata)ptd)->_initaddr) )
  212. ( ((_ptiddata)ptd)->_initarg );
  213. _endthread();
  214. }
  215. __except ( _XcptFilter(GetExceptionCode(), GetExceptionInformation()) )
  216. {
  217. /*
  218. * Should never reach here
  219. */
  220. _exit( GetExceptionCode() );
  221. } /* end of _try - _except */
  222. /*
  223. * Never executed!
  224. */
  225. return(0L);
  226. }
  227. /***
  228. *_endthread() - Terminate the calling thread
  229. *
  230. *Purpose:
  231. *
  232. *Entry:
  233. * void
  234. *
  235. *Exit:
  236. * Never returns!
  237. *
  238. *Exceptions:
  239. *
  240. *******************************************************************************/
  241. void __cdecl _endthread (
  242. void
  243. )
  244. {
  245. _ptiddata ptd; /* pointer to thread's _tiddata struct */
  246. /*
  247. * Call fp termination, if necessary
  248. */
  249. if ( _FPmtterm != NULL )
  250. (*_FPmtterm)();
  251. if ( (ptd = _getptd()) == NULL )
  252. _amsg_exit(_RT_THREAD);
  253. /*
  254. * Close the thread handle (if there was one)
  255. */
  256. if ( ptd->_thandle != (uintptr_t)(-1) )
  257. (void) CloseHandle( (HANDLE)(ptd->_thandle) );
  258. /*
  259. * Free up the _tiddata structure & its subordinate buffers
  260. * _freeptd() will also clear the value for this thread
  261. * of the TLS variable __tlsindex.
  262. */
  263. _freeptd(ptd);
  264. /*
  265. * Terminate the thread
  266. */
  267. ExitThread(0);
  268. }
  269. #endif /* _MT */