createthread.c 8.0 KB

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