create.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * create.c
  3. *
  4. * Description:
  5. * This translation unit implements routines associated with spawning a new
  6. * thread.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. #if ! defined(_UWIN) && ! defined(WINCE)
  40. #include <process.h>
  41. #endif
  42. int
  43. pthread_create (pthread_t * tid,
  44. const pthread_attr_t * attr,
  45. void *(PTW32_CDECL *start) (void *), void *arg)
  46. /*
  47. * ------------------------------------------------------
  48. * DOCPUBLIC
  49. * This function creates a thread running the start function,
  50. * passing it the parameter value, 'arg'. The 'attr'
  51. * argument specifies optional creation attributes.
  52. * The identity of the new thread is returned
  53. * via 'tid', which should not be NULL.
  54. *
  55. * PARAMETERS
  56. * tid
  57. * pointer to an instance of pthread_t
  58. *
  59. * attr
  60. * optional pointer to an instance of pthread_attr_t
  61. *
  62. * start
  63. * pointer to the starting routine for the new thread
  64. *
  65. * arg
  66. * optional parameter passed to 'start'
  67. *
  68. *
  69. * DESCRIPTION
  70. * This function creates a thread running the start function,
  71. * passing it the parameter value, 'arg'. The 'attr'
  72. * argument specifies optional creation attributes.
  73. * The identity of the new thread is returned
  74. * via 'tid', which should not be the NULL pointer.
  75. *
  76. * RESULTS
  77. * 0 successfully created thread,
  78. * EINVAL attr invalid,
  79. * EAGAIN insufficient resources.
  80. *
  81. * ------------------------------------------------------
  82. */
  83. {
  84. pthread_t thread;
  85. ptw32_thread_t * tp;
  86. register pthread_attr_t a;
  87. HANDLE threadH = 0;
  88. int result = EAGAIN;
  89. int run = PTW32_TRUE;
  90. ThreadParms *parms = NULL;
  91. unsigned int stackSize;
  92. int priority;
  93. pthread_t self;
  94. /*
  95. * Before doing anything, check that tid can be stored through
  96. * without invoking a memory protection error (segfault).
  97. * Make sure that the assignment below can't be optimised out by the compiler.
  98. * This is assured by conditionally assigning *tid again at the end.
  99. */
  100. tid->x = 0;
  101. if (attr != NULL)
  102. {
  103. a = *attr;
  104. }
  105. else
  106. {
  107. a = NULL;
  108. }
  109. if ((thread = ptw32_new ()).p == NULL)
  110. {
  111. goto FAIL0;
  112. }
  113. tp = (ptw32_thread_t *) thread.p;
  114. priority = tp->sched_priority;
  115. if ((parms = (ThreadParms *) malloc (sizeof (*parms))) == NULL)
  116. {
  117. goto FAIL0;
  118. }
  119. parms->tid = thread;
  120. parms->start = start;
  121. parms->arg = arg;
  122. #if defined(HAVE_SIGSET_T)
  123. /*
  124. * Threads inherit their initial sigmask from their creator thread.
  125. */
  126. self = pthread_self();
  127. tp->sigmask = ((ptw32_thread_t *)self.p)->sigmask;
  128. #endif /* HAVE_SIGSET_T */
  129. if (a != NULL)
  130. {
  131. stackSize = (unsigned int)a->stacksize;
  132. tp->detachState = a->detachstate;
  133. priority = a->param.sched_priority;
  134. #if (THREAD_PRIORITY_LOWEST > THREAD_PRIORITY_NORMAL)
  135. /* WinCE */
  136. #else
  137. /* Everything else */
  138. /*
  139. * Thread priority must be set to a valid system level
  140. * without altering the value set by pthread_attr_setschedparam().
  141. */
  142. /*
  143. * PTHREAD_EXPLICIT_SCHED is the default because Win32 threads
  144. * don't inherit their creator's priority. They are started with
  145. * THREAD_PRIORITY_NORMAL (win32 value). The result of not supplying
  146. * an 'attr' arg to pthread_create() is equivalent to defaulting to
  147. * PTHREAD_EXPLICIT_SCHED and priority THREAD_PRIORITY_NORMAL.
  148. */
  149. if (PTHREAD_INHERIT_SCHED == a->inheritsched)
  150. {
  151. /*
  152. * If the thread that called pthread_create() is a Win32 thread
  153. * then the inherited priority could be the result of a temporary
  154. * system adjustment. This is not the case for POSIX threads.
  155. */
  156. #if ! defined(HAVE_SIGSET_T)
  157. self = pthread_self ();
  158. #endif
  159. priority = ((ptw32_thread_t *) self.p)->sched_priority;
  160. }
  161. #endif
  162. }
  163. else
  164. {
  165. /*
  166. * Default stackSize
  167. */
  168. stackSize = PTHREAD_STACK_MIN;
  169. }
  170. tp->state = run ? PThreadStateInitial : PThreadStateSuspended;
  171. tp->keys = NULL;
  172. /*
  173. * Threads must be started in suspended mode and resumed if necessary
  174. * after _beginthreadex returns us the handle. Otherwise we set up a
  175. * race condition between the creating and the created threads.
  176. * Note that we also retain a local copy of the handle for use
  177. * by us in case thread.p->threadH gets NULLed later but before we've
  178. * finished with it here.
  179. */
  180. #if ! (defined (__MINGW64__) || defined(__MINGW32__)) || defined (__MSVCRT__) || defined (__DMC__)
  181. tp->threadH =
  182. threadH =
  183. (HANDLE) _beginthreadex ((void *) NULL, /* No security info */
  184. stackSize, /* default stack size */
  185. ptw32_threadStart,
  186. parms,
  187. (unsigned)
  188. CREATE_SUSPENDED,
  189. (unsigned *) &(tp->thread));
  190. if (threadH != 0)
  191. {
  192. if (a != NULL)
  193. {
  194. (void) ptw32_setthreadpriority (thread, SCHED_OTHER, priority);
  195. }
  196. if (run)
  197. {
  198. ResumeThread (threadH);
  199. }
  200. }
  201. #else
  202. {
  203. ptw32_mcs_local_node_t stateLock;
  204. /*
  205. * This lock will force pthread_threadStart() to wait until we have
  206. * the thread handle and have set the priority.
  207. */
  208. ptw32_mcs_lock_acquire(&tp->stateLock, &stateLock);
  209. tp->threadH =
  210. threadH =
  211. (HANDLE) _beginthread (ptw32_threadStart, stackSize, /* default stack size */
  212. parms);
  213. /*
  214. * Make the return code match _beginthreadex's.
  215. */
  216. if (threadH == (HANDLE) - 1L)
  217. {
  218. tp->threadH = threadH = 0;
  219. }
  220. else
  221. {
  222. if (!run)
  223. {
  224. /*
  225. * beginthread does not allow for create flags, so we do it now.
  226. * Note that beginthread itself creates the thread in SUSPENDED
  227. * mode, and then calls ResumeThread to start it.
  228. */
  229. SuspendThread (threadH);
  230. }
  231. if (a != NULL)
  232. {
  233. (void) ptw32_setthreadpriority (thread, SCHED_OTHER, priority);
  234. }
  235. }
  236. ptw32_mcs_lock_release (&stateLock);
  237. }
  238. #endif
  239. result = (threadH != 0) ? 0 : EAGAIN;
  240. /*
  241. * Fall Through Intentionally
  242. */
  243. /*
  244. * ------------
  245. * Failure Code
  246. * ------------
  247. */
  248. FAIL0:
  249. if (result != 0)
  250. {
  251. ptw32_threadDestroy (thread);
  252. tp = NULL;
  253. if (parms != NULL)
  254. {
  255. free (parms);
  256. }
  257. }
  258. else
  259. {
  260. *tid = thread;
  261. }
  262. #if defined(_UWIN)
  263. if (result == 0)
  264. pthread_count++;
  265. #endif
  266. return (result);
  267. } /* pthread_create */