createthread.c 8.0 KB

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