createthread.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright (C) 2002-2007, 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <sched.h>
  17. #include <setjmp.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <atomic.h>
  21. #include <ldsodefs.h>
  22. #include <tls.h>
  23. #include <bits/kernel-features.h>
  24. #define CLONE_SIGNAL (CLONE_SIGHAND | CLONE_THREAD)
  25. /* Unless otherwise specified, the thread "register" is going to be
  26. initialized with a pointer to the TCB. */
  27. #ifndef TLS_VALUE
  28. # define TLS_VALUE pd
  29. #endif
  30. #ifndef ARCH_CLONE
  31. # define ARCH_CLONE __clone
  32. #endif
  33. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  34. /* Pointer to the corresponding variable in libc. */
  35. int *__libc_multiple_threads_ptr attribute_hidden;
  36. #endif
  37. static int
  38. do_clone (struct pthread *pd, const struct pthread_attr *attr,
  39. int clone_flags, int (*fct) (void *), STACK_VARIABLES_PARMS,
  40. int stopped)
  41. {
  42. #ifdef PREPARE_CREATE
  43. PREPARE_CREATE;
  44. #endif
  45. if (__builtin_expect (stopped != 0, 0))
  46. /* We make sure the thread does not run far by forcing it to get a
  47. lock. We lock it here too so that the new thread cannot continue
  48. until we tell it to. */
  49. lll_lock (pd->lock, LLL_PRIVATE);
  50. /* One more thread. We cannot have the thread do this itself, since it
  51. might exist but not have been scheduled yet by the time we've returned
  52. and need to check the value to behave correctly. We must do it before
  53. creating the thread, in case it does get scheduled first and then
  54. might mistakenly think it was the only thread. In the failure case,
  55. we momentarily store a false value; this doesn't matter because there
  56. is no kosher thing a signal handler interrupting us right here can do
  57. that cares whether the thread count is correct. */
  58. atomic_increment (&__nptl_nthreads);
  59. if (ARCH_CLONE (fct, STACK_VARIABLES_ARGS, clone_flags,
  60. pd, &pd->tid, TLS_VALUE, &pd->tid) == -1)
  61. {
  62. atomic_decrement (&__nptl_nthreads); /* Oops, we lied for a second. */
  63. /* Failed. If the thread is detached, remove the TCB here since
  64. the caller cannot do this. The caller remembered the thread
  65. as detached and cannot reverify that it is not since it must
  66. not access the thread descriptor again. */
  67. if (IS_DETACHED (pd))
  68. __deallocate_stack (pd);
  69. /* We have to translate error codes. */
  70. return errno == ENOMEM ? EAGAIN : errno;
  71. }
  72. /* Now we have the possibility to set scheduling parameters etc. */
  73. if (__builtin_expect (stopped != 0, 0))
  74. {
  75. INTERNAL_SYSCALL_DECL (err);
  76. int res = 0;
  77. /* Set the affinity mask if necessary. */
  78. if (attr->cpuset != NULL)
  79. {
  80. res = INTERNAL_SYSCALL (sched_setaffinity, err, 3, pd->tid,
  81. attr->cpusetsize, attr->cpuset);
  82. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
  83. {
  84. /* The operation failed. We have to kill the thread. First
  85. send it the cancellation signal. */
  86. INTERNAL_SYSCALL_DECL (err2);
  87. err_out:
  88. #if defined (__ASSUME_TGKILL) && __ASSUME_TGKILL
  89. (void) INTERNAL_SYSCALL (tgkill, err2, 3,
  90. THREAD_GETMEM (THREAD_SELF, pid),
  91. pd->tid, SIGCANCEL);
  92. #else
  93. (void) INTERNAL_SYSCALL (tkill, err2, 2, pd->tid, SIGCANCEL);
  94. #endif
  95. return (INTERNAL_SYSCALL_ERROR_P (res, err)
  96. ? INTERNAL_SYSCALL_ERRNO (res, err)
  97. : 0);
  98. }
  99. }
  100. /* Set the scheduling parameters. */
  101. if ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0)
  102. {
  103. res = INTERNAL_SYSCALL (sched_setscheduler, err, 3, pd->tid,
  104. pd->schedpolicy, &pd->schedparam);
  105. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
  106. goto err_out;
  107. }
  108. }
  109. /* We now have for sure more than one thread. The main thread might
  110. not yet have the flag set. No need to set the global variable
  111. again if this is what we use. */
  112. THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
  113. return 0;
  114. }
  115. static int
  116. create_thread (struct pthread *pd, const struct pthread_attr *attr,
  117. STACK_VARIABLES_PARMS)
  118. {
  119. #ifdef TLS_TCB_AT_TP
  120. assert (pd->header.tcb != NULL);
  121. #endif
  122. /* We rely heavily on various flags the CLONE function understands:
  123. CLONE_VM, CLONE_FS, CLONE_FILES
  124. These flags select semantics with shared address space and
  125. file descriptors according to what POSIX requires.
  126. CLONE_SIGNAL
  127. This flag selects the POSIX signal semantics.
  128. CLONE_SETTLS
  129. The sixth parameter to CLONE determines the TLS area for the
  130. new thread.
  131. CLONE_PARENT_SETTID
  132. The kernels writes the thread ID of the newly created thread
  133. into the location pointed to by the fifth parameters to CLONE.
  134. Note that it would be semantically equivalent to use
  135. CLONE_CHILD_SETTID but it is be more expensive in the kernel.
  136. CLONE_CHILD_CLEARTID
  137. The kernels clears the thread ID of a thread that has called
  138. sys_exit() in the location pointed to by the seventh parameter
  139. to CLONE.
  140. CLONE_DETACHED
  141. No signal is generated if the thread exists and it is
  142. automatically reaped.
  143. The termination signal is chosen to be zero which means no signal
  144. is sent. */
  145. int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGNAL
  146. | CLONE_SETTLS | CLONE_PARENT_SETTID
  147. | CLONE_CHILD_CLEARTID | CLONE_SYSVSEM
  148. #if __ASSUME_NO_CLONE_DETACHED == 0
  149. | CLONE_DETACHED
  150. #endif
  151. | 0);
  152. if (__builtin_expect (THREAD_GETMEM (THREAD_SELF, report_events), 0))
  153. {
  154. /* The parent thread is supposed to report events. Check whether
  155. the TD_CREATE event is needed, too. */
  156. const int _idx = __td_eventword (TD_CREATE);
  157. const uint32_t _mask = __td_eventmask (TD_CREATE);
  158. if ((_mask & (__nptl_threads_events.event_bits[_idx]
  159. | pd->eventbuf.eventmask.event_bits[_idx])) != 0)
  160. {
  161. /* We always must have the thread start stopped. */
  162. pd->stopped_start = true;
  163. /* Create the thread. We always create the thread stopped
  164. so that it does not get far before we tell the debugger. */
  165. int res = do_clone (pd, attr, clone_flags, start_thread,
  166. STACK_VARIABLES_ARGS, 1);
  167. if (res == 0)
  168. {
  169. /* Now fill in the information about the new thread in
  170. the newly created thread's data structure. We cannot let
  171. the new thread do this since we don't know whether it was
  172. already scheduled when we send the event. */
  173. pd->eventbuf.eventnum = TD_CREATE;
  174. pd->eventbuf.eventdata = pd;
  175. /* Enqueue the descriptor. */
  176. do
  177. pd->nextevent = __nptl_last_event;
  178. while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
  179. pd, pd->nextevent)
  180. != 0);
  181. /* Now call the function which signals the event. */
  182. __nptl_create_event ();
  183. /* And finally restart the new thread. */
  184. lll_unlock (pd->lock, LLL_PRIVATE);
  185. }
  186. return res;
  187. }
  188. }
  189. #ifdef NEED_DL_SYSINFO
  190. assert (THREAD_SELF_SYSINFO == THREAD_SYSINFO (pd));
  191. #endif
  192. /* Determine whether the newly created threads has to be started
  193. stopped since we have to set the scheduling parameters or set the
  194. affinity. */
  195. bool stopped = false;
  196. if (attr != NULL && (attr->cpuset != NULL
  197. || (attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0))
  198. stopped = true;
  199. pd->stopped_start = stopped;
  200. pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling);
  201. /* Actually create the thread. */
  202. int res = do_clone (pd, attr, clone_flags, start_thread,
  203. STACK_VARIABLES_ARGS, stopped);
  204. if (res == 0 && stopped)
  205. /* And finally restart the new thread. */
  206. lll_unlock (pd->lock, LLL_PRIVATE);
  207. return res;
  208. }