create.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if (attr != NULL)
  101. {
  102. a = *attr;
  103. }
  104. else
  105. {
  106. a = NULL;
  107. }
  108. if ((thread = pte_new ()) == NULL)
  109. {
  110. goto FAIL0;
  111. }
  112. tp = (pte_thread_t *) thread;
  113. priority = tp->sched_priority;
  114. if ((parms = (ThreadParms *) malloc (sizeof (*parms))) == NULL)
  115. {
  116. goto FAIL0;
  117. }
  118. parms->tid = thread;
  119. parms->start = start;
  120. parms->arg = arg;
  121. if (a != NULL)
  122. {
  123. stackSize = a->stacksize;
  124. tp->detachState = a->detachstate;
  125. priority = a->param.sched_priority;
  126. if ( (priority > pte_osThreadGetMaxPriority()) ||
  127. (priority < pte_osThreadGetMinPriority()) )
  128. {
  129. result = EINVAL;
  130. goto FAIL0;
  131. }
  132. /* Everything else */
  133. /*
  134. * Thread priority must be set to a valid system level
  135. * without altering the value set by pthread_attr_setschedparam().
  136. */
  137. if (PTHREAD_INHERIT_SCHED == a->inheritsched)
  138. {
  139. /*
  140. * If the thread that called pthread_create() is an OS thread
  141. * then the inherited priority could be the result of a temporary
  142. * system adjustment. This is not the case for POSIX threads.
  143. */
  144. self = pthread_self ();
  145. priority = ((pte_thread_t *) self)->sched_priority;
  146. }
  147. }
  148. else
  149. {
  150. /*
  151. * Default stackSize
  152. */
  153. stackSize = PTHREAD_STACK_MIN;
  154. }
  155. tp->state = run ? PThreadStateInitial : PThreadStateSuspended;
  156. tp->keys = NULL;
  157. /*
  158. * Threads must be started in suspended mode and resumed if necessary
  159. * after _beginthreadex returns us the handle. Otherwise we set up a
  160. * race condition between the creating and the created threads.
  161. * Note that we also retain a local copy of the handle for use
  162. * by us in case thread.p->threadH gets NULLed later but before we've
  163. * finished with it here.
  164. */
  165. result = pthread_mutex_lock (&tp->threadLock);
  166. if (0 == result)
  167. {
  168. /*
  169. * Must record the thread's sched_priority as given,
  170. * not as finally adjusted.
  171. */
  172. tp->sched_priority = priority;
  173. (void) pthread_mutex_unlock (&tp->threadLock);
  174. }
  175. osResult = pte_osThreadCreate(pte_threadStart,
  176. stackSize,
  177. priority,
  178. parms,
  179. &(tp->threadId));
  180. if (osResult == PTE_OS_OK)
  181. {
  182. pte_osThreadStart(tp->threadId);
  183. result = 0;
  184. }
  185. else
  186. {
  187. tp->threadId = 0;
  188. result = EAGAIN;
  189. goto FAIL0;
  190. }
  191. /*
  192. * Fall Through Intentionally
  193. */
  194. /*
  195. * ------------
  196. * Failure Code
  197. * ------------
  198. */
  199. FAIL0:
  200. if (result != 0)
  201. {
  202. pte_threadDestroy (thread);
  203. tp = NULL;
  204. if (parms != NULL)
  205. {
  206. free (parms);
  207. }
  208. }
  209. else
  210. {
  211. if (tid != NULL)
  212. {
  213. *tid = thread;
  214. }
  215. }
  216. return (result);
  217. } /* pthread_create */