create.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. *
  16. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The original list of contributors to the Pthreads-win32 project
  23. * is contained in the file CONTRIBUTORS.ptw32 included with the
  24. * source code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include "pte_osal.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include "pthread.h"
  47. #include "implement.h"
  48. int
  49. pthread_create (pthread_t * tid,
  50. const pthread_attr_t * attr,
  51. void *(*start) (void *), void *arg)
  52. /*
  53. * ------------------------------------------------------
  54. * DOCPUBLIC
  55. * This function creates a thread running the start function,
  56. * passing it the parameter value, 'arg'. The 'attr'
  57. * argument specifies optional creation attributes.
  58. * The identity of the new thread is returned
  59. * via 'tid', which should not be NULL.
  60. *
  61. * PARAMETERS
  62. * tid
  63. * pointer to an instance of pthread_t
  64. *
  65. * attr
  66. * optional pointer to an instance of pthread_attr_t
  67. *
  68. * start
  69. * pointer to the starting routine for the new thread
  70. *
  71. * arg
  72. * optional parameter passed to 'start'
  73. *
  74. *
  75. * DESCRIPTION
  76. * This function creates a thread running the start function,
  77. * passing it the parameter value, 'arg'. The 'attr'
  78. * argument specifies optional creation attributes.
  79. * The identity of the new thread is returned
  80. * via 'tid', which should not be the NULL pointer.
  81. *
  82. * RESULTS
  83. * 0 successfully created thread,
  84. * EINVAL attr invalid,
  85. * EAGAIN insufficient resources.
  86. *
  87. * ------------------------------------------------------
  88. */
  89. {
  90. pthread_t thread;
  91. pte_thread_t * tp;
  92. register pthread_attr_t a;
  93. int result = EAGAIN;
  94. int run = PTE_TRUE;
  95. ThreadParms *parms = NULL;
  96. long stackSize;
  97. int priority = 0;
  98. pthread_t self;
  99. pte_osResult osResult;
  100. /*
  101. * Before doing anything, check that tid can be stored through
  102. * without invoking a memory protection error (segfault).
  103. * Make sure that the assignment below can't be optimised out by the compiler.
  104. * This is assured by conditionally assigning *tid again at the end.
  105. */
  106. if (tid != NULL)
  107. {
  108. tid->x = 0;
  109. }
  110. if (attr != NULL)
  111. {
  112. a = *attr;
  113. }
  114. else
  115. {
  116. a = NULL;
  117. }
  118. if ((thread = pte_new ()).p == NULL)
  119. {
  120. goto FAIL0;
  121. }
  122. tp = (pte_thread_t *) thread.p;
  123. priority = tp->sched_priority;
  124. if ((parms = (ThreadParms *) malloc (sizeof (*parms))) == NULL)
  125. {
  126. goto FAIL0;
  127. }
  128. parms->tid = thread;
  129. parms->start = start;
  130. parms->arg = arg;
  131. if (a != NULL)
  132. {
  133. stackSize = a->stacksize;
  134. tp->detachState = a->detachstate;
  135. priority = a->param.sched_priority;
  136. if ( (priority > pte_osThreadGetMaxPriority()) ||
  137. (priority < pte_osThreadGetMinPriority()) )
  138. {
  139. result = EINVAL;
  140. goto FAIL0;
  141. }
  142. /* Everything else */
  143. /*
  144. * Thread priority must be set to a valid system level
  145. * without altering the value set by pthread_attr_setschedparam().
  146. */
  147. if (PTHREAD_INHERIT_SCHED == a->inheritsched)
  148. {
  149. /*
  150. * If the thread that called pthread_create() is an OS thread
  151. * then the inherited priority could be the result of a temporary
  152. * system adjustment. This is not the case for POSIX threads.
  153. */
  154. self = pthread_self ();
  155. priority = ((pte_thread_t *) self.p)->sched_priority;
  156. }
  157. }
  158. else
  159. {
  160. /*
  161. * Default stackSize
  162. */
  163. stackSize = PTHREAD_STACK_MIN;
  164. }
  165. tp->state = run ? PThreadStateInitial : PThreadStateSuspended;
  166. tp->keys = NULL;
  167. /*
  168. * Threads must be started in suspended mode and resumed if necessary
  169. * after _beginthreadex returns us the handle. Otherwise we set up a
  170. * race condition between the creating and the created threads.
  171. * Note that we also retain a local copy of the handle for use
  172. * by us in case thread.p->threadH gets NULLed later but before we've
  173. * finished with it here.
  174. */
  175. result = pthread_mutex_lock (&tp->threadLock);
  176. if (0 == result)
  177. {
  178. /*
  179. * Must record the thread's sched_priority as given,
  180. * not as finally adjusted.
  181. */
  182. tp->sched_priority = priority;
  183. (void) pthread_mutex_unlock (&tp->threadLock);
  184. }
  185. osResult = pte_osThreadCreate(pte_threadStart,
  186. stackSize,
  187. priority,
  188. parms,
  189. &(tp->threadId));
  190. if (osResult == PTE_OS_OK)
  191. {
  192. pte_osThreadStart(tp->threadId);
  193. result = 0;
  194. }
  195. else
  196. {
  197. tp->threadId = 0;
  198. result = EAGAIN;
  199. goto FAIL0;
  200. }
  201. /*
  202. * Fall Through Intentionally
  203. */
  204. /*
  205. * ------------
  206. * Failure Code
  207. * ------------
  208. */
  209. FAIL0:
  210. if (result != 0)
  211. {
  212. pte_threadDestroy (thread);
  213. tp = NULL;
  214. if (parms != NULL)
  215. {
  216. free (parms);
  217. }
  218. }
  219. else
  220. {
  221. if (tid != NULL)
  222. {
  223. *tid = thread;
  224. }
  225. }
  226. return (result);
  227. } /* pthread_create */