createthread.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if defined (__ASSUME_TGKILL) && __ASSUME_TGKILL
  88. (void) INTERNAL_SYSCALL (tgkill, err2, 3,
  89. THREAD_GETMEM (THREAD_SELF, pid),
  90. pd->tid, SIGCANCEL);
  91. #else
  92. (void) INTERNAL_SYSCALL (tkill, err2, 2, pd->tid, SIGCANCEL);
  93. #endif
  94. return (INTERNAL_SYSCALL_ERROR_P (res, err)
  95. ? INTERNAL_SYSCALL_ERRNO (res, err)
  96. : 0);
  97. }
  98. }
  99. /* Set the scheduling parameters. */
  100. if ((attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0)
  101. {
  102. res = INTERNAL_SYSCALL (sched_setscheduler, err, 3, pd->tid,
  103. pd->schedpolicy, &pd->schedparam);
  104. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (res, err), 0))
  105. goto err_out;
  106. }
  107. }
  108. /* We now have for sure more than one thread. The main thread might
  109. not yet have the flag set. No need to set the global variable
  110. again if this is what we use. */
  111. THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
  112. return 0;
  113. }
  114. static int
  115. create_thread (struct pthread *pd, const struct pthread_attr *attr,
  116. STACK_VARIABLES_PARMS)
  117. {
  118. #ifdef TLS_TCB_AT_TP
  119. assert (pd->header.tcb != NULL);
  120. #endif
  121. /* We rely heavily on various flags the CLONE function understands:
  122. CLONE_VM, CLONE_FS, CLONE_FILES
  123. These flags select semantics with shared address space and
  124. file descriptors according to what POSIX requires.
  125. CLONE_SIGNAL
  126. This flag selects the POSIX signal semantics.
  127. CLONE_SETTLS
  128. The sixth parameter to CLONE determines the TLS area for the
  129. new thread.
  130. CLONE_PARENT_SETTID
  131. The kernels writes the thread ID of the newly created thread
  132. into the location pointed to by the fifth parameters to CLONE.
  133. Note that it would be semantically equivalent to use
  134. CLONE_CHILD_SETTID but it is be more expensive in the kernel.
  135. CLONE_CHILD_CLEARTID
  136. The kernels clears the thread ID of a thread that has called
  137. sys_exit() in the location pointed to by the seventh parameter
  138. to CLONE.
  139. CLONE_DETACHED
  140. No signal is generated if the thread exists and it is
  141. automatically reaped.
  142. The termination signal is chosen to be zero which means no signal
  143. is sent. */
  144. int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGNAL
  145. | CLONE_SETTLS | CLONE_PARENT_SETTID
  146. | CLONE_CHILD_CLEARTID | CLONE_SYSVSEM
  147. #if __ASSUME_NO_CLONE_DETACHED == 0
  148. | CLONE_DETACHED
  149. #endif
  150. | 0);
  151. if (__builtin_expect (THREAD_GETMEM (THREAD_SELF, report_events), 0))
  152. {
  153. /* The parent thread is supposed to report events. Check whether
  154. the TD_CREATE event is needed, too. */
  155. const int _idx = __td_eventword (TD_CREATE);
  156. const uint32_t _mask = __td_eventmask (TD_CREATE);
  157. if ((_mask & (__nptl_threads_events.event_bits[_idx]
  158. | pd->eventbuf.eventmask.event_bits[_idx])) != 0)
  159. {
  160. /* We always must have the thread start stopped. */
  161. pd->stopped_start = true;
  162. /* Create the thread. We always create the thread stopped
  163. so that it does not get far before we tell the debugger. */
  164. int res = do_clone (pd, attr, clone_flags, start_thread,
  165. STACK_VARIABLES_ARGS, 1);
  166. if (res == 0)
  167. {
  168. /* Now fill in the information about the new thread in
  169. the newly created thread's data structure. We cannot let
  170. the new thread do this since we don't know whether it was
  171. already scheduled when we send the event. */
  172. pd->eventbuf.eventnum = TD_CREATE;
  173. pd->eventbuf.eventdata = pd;
  174. /* Enqueue the descriptor. */
  175. do
  176. pd->nextevent = __nptl_last_event;
  177. while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
  178. pd, pd->nextevent)
  179. != 0);
  180. /* Now call the function which signals the event. */
  181. __nptl_create_event ();
  182. /* And finally restart the new thread. */
  183. lll_unlock (pd->lock, LLL_PRIVATE);
  184. }
  185. return res;
  186. }
  187. }
  188. #ifdef NEED_DL_SYSINFO
  189. assert (THREAD_SELF_SYSINFO == THREAD_SYSINFO (pd));
  190. #endif
  191. /* Determine whether the newly created threads has to be started
  192. stopped since we have to set the scheduling parameters or set the
  193. affinity. */
  194. bool stopped = false;
  195. if (attr != NULL && (attr->cpuset != NULL
  196. || (attr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0))
  197. stopped = true;
  198. pd->stopped_start = stopped;
  199. pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling);
  200. /* Actually create the thread. */
  201. int res = do_clone (pd, attr, clone_flags, start_thread,
  202. STACK_VARIABLES_ARGS, stopped);
  203. if (res == 0 && stopped)
  204. /* And finally restart the new thread. */
  205. lll_unlock (pd->lock, LLL_PRIVATE);
  206. return res;
  207. }