Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
glibc
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security
Insights
Files
0f6699e
abilist
aout
argp
assert
bits
catgets
conf
conform
crypt
csu
ctype
debug
dirent
dlfcn
elf
gmon
gnulib
grp
hesiod
hurd
iconv
iconvdata
include
inet
intl
io
libidn
libio
locale
localedata
login
mach
malloc
manual
math
misc
nis
nptl
sysdeps
ANNOUNCE
Banner
ChangeLog
DESIGN-barrier.txt
DESIGN-condvar.txt
DESIGN-rwlock.txt
DESIGN-sem.txt
Makeconfig
Makefile
TODO
TODO-kernel
TODO-testing
Versions
alloca_cutoff.c
allocatestack.c
cancellation.c
cleanup.c
cleanup_compat.c
cleanup_defer.c
cleanup_defer_compat.c
cleanup_routine.c
cond-perf.c
configure
descr.h
eintr.c
errno-loc.c
events.c
forward.c
herrno.c
init.c
libc-cancellation.c
lowlevellock.h
old_pthread_atfork.c
old_pthread_cond_broadcast.c
old_pthread_cond_destroy.c
old_pthread_cond_init.c
old_pthread_cond_signal.c
old_pthread_cond_timedwait.c
old_pthread_cond_wait.c
perf.c
pt-allocrtsig.c
pt-cleanup.c
pt-raise.c
pt-system.c
pthread-errnos.sym
pthreadP.h
pthread_atfork.c
pthread_attr_destroy.c
pthread_attr_getdetachstate.c
pthread_attr_getguardsize.c
pthread_attr_getinheritsched.c
pthread_attr_getschedparam.c
pthread_attr_getschedpolicy.c
pthread_attr_getscope.c
pthread_attr_getstack.c
pthread_attr_getstackaddr.c
pthread_attr_getstacksize.c
pthread_attr_init.c
pthread_attr_setdetachstate.c
pthread_attr_setguardsize.c
pthread_attr_setinheritsched.c
pthread_attr_setschedparam.c
pthread_attr_setschedpolicy.c
pthread_attr_setscope.c
pthread_attr_setstack.c
pthread_attr_setstackaddr.c
pthread_attr_setstacksize.c
pthread_barrier_destroy.c
pthread_barrier_init.c
pthread_barrierattr_destroy.c
pthread_barrierattr_getpshared.c
pthread_barrierattr_init.c
pthread_barrierattr_setpshared.c
pthread_cancel.c
pthread_clock_gettime.c
pthread_clock_settime.c
pthread_cond_destroy.c
pthread_cond_init.c
pthread_condattr_destroy.c
pthread_condattr_getclock.c
pthread_condattr_getpshared.c
pthread_condattr_init.c
pthread_condattr_setclock.c
pthread_condattr_setpshared.c
pthread_create.c
pthread_detach.c
pthread_equal.c
pthread_exit.c
pthread_getattr_np.c
pthread_getconcurrency.c
pthread_getschedparam.c
pthread_getspecific.c
pthread_join.c
pthread_key_create.c
pthread_key_delete.c
pthread_kill_other_threads.c
pthread_mutex_consistent.c
pthread_mutex_destroy.c
pthread_mutex_getprioceiling.c
pthread_mutex_init.c
pthread_mutex_lock.c
pthread_mutex_setprioceiling.c
pthread_mutex_timedlock.c
pthread_mutex_trylock.c
pthread_mutex_unlock.c
pthread_mutexattr_destroy.c
pthread_mutexattr_getprioceiling.c
pthread_mutexattr_getprotocol.c
pthread_mutexattr_getpshared.c
pthread_mutexattr_getrobust.c
pthread_mutexattr_gettype.c
pthread_mutexattr_init.c
pthread_mutexattr_setprioceiling.c
pthread_mutexattr_setprotocol.c
pthread_mutexattr_setpshared.c
pthread_mutexattr_setrobust.c
pthread_mutexattr_settype.c
pthread_rwlock_destroy.c
pthread_rwlock_init.c
pthread_rwlock_tryrdlock.c
pthread_rwlock_trywrlock.c
pthread_rwlockattr_destroy.c
pthread_rwlockattr_getkind_np.c
pthread_rwlockattr_getpshared.c
pthread_rwlockattr_init.c
pthread_rwlockattr_setkind_np.c
pthread_rwlockattr_setpshared.c
pthread_self.c
pthread_setcancelstate.c
pthread_setcanceltype.c
pthread_setconcurrency.c
pthread_setegid.c
pthread_seteuid.c
pthread_setgid.c
pthread_setregid.c
pthread_setresgid.c
pthread_setresuid.c
pthread_setreuid.c
pthread_setschedparam.c
pthread_setschedprio.c
pthread_setspecific.c
pthread_setuid.c
pthread_testcancel.c
pthread_timedjoin.c
pthread_tryjoin.c
res.c
sem_close.c
sem_destroy.c
sem_getvalue.c
sem_init.c
sem_open.c
sem_unlink.c
semaphore.h
semaphoreP.h
shlib-versions
sigaction.c
sockperf.c
tst-_res1.c
tst-_res1mod1.c
tst-_res1mod2.c
tst-align.c
tst-align2.c
tst-align3.c
tst-atfork1.c
tst-atfork2.c
tst-atfork2mod.c
tst-attr1.c
tst-attr2.c
tst-attr3.c
tst-backtrace1.c
tst-barrier1.c
tst-barrier2.c
tst-barrier3.c
tst-barrier4.c
tst-basic1.c
tst-basic2.c
tst-basic3.c
tst-basic4.c
tst-basic5.c
tst-basic6.c
tst-cancel-wrappers.sh
tst-cancel1.c
tst-cancel10.c
tst-cancel11.c
tst-cancel12.c
tst-cancel13.c
tst-cancel14.c
tst-cancel15.c
tst-cancel16.c
tst-cancel17.c
tst-cancel18.c
tst-cancel19.c
tst-cancel2.c
tst-cancel20.c
tst-cancel21.c
tst-cancel22.c
tst-cancel23.c
tst-cancel24.cc
tst-cancel3.c
tst-cancel4.c
tst-cancel5.c
tst-cancel6.c
tst-cancel7.c
tst-cancel8.c
tst-cancel9.c
tst-cancelx1.c
tst-cancelx10.c
tst-cancelx11.c
tst-cancelx12.c
tst-cancelx13.c
tst-cancelx14.c
tst-cancelx15.c
tst-cancelx16.c
tst-cancelx17.c
tst-cancelx18.c
tst-cancelx2.c
tst-cancelx20.c
tst-cancelx21.c
tst-cancelx3.c
tst-cancelx4.c
tst-cancelx5.c
tst-cancelx6.c
tst-cancelx7.c
tst-cancelx8.c
tst-cancelx9.c
tst-cleanup0.c
tst-cleanup0.expect
tst-cleanup1.c
tst-cleanup2.c
tst-cleanup3.c
tst-cleanup4.c
tst-cleanup4aux.c
tst-cleanupx0.c
tst-cleanupx0.expect
tst-cleanupx1.c
tst-cleanupx2.c
tst-cleanupx3.c
tst-cleanupx4.c
tst-clock1.c
tst-clock2.c
tst-cond1.c
tst-cond10.c
tst-cond11.c
tst-cond12.c
tst-cond13.c
tst-cond14.c
tst-cond15.c
tst-cond16.c
tst-cond17.c
tst-cond18.c
tst-cond19.c
tst-cond2.c
tst-cond20.c
tst-cond21.c
tst-cond3.c
tst-cond4.c
tst-cond5.c
tst-cond6.c
tst-cond7.c
tst-cond8.c
tst-cond9.c
tst-context1.c
tst-detach1.c
tst-dlsym1.c
tst-eintr1.c
tst-eintr2.c
tst-eintr3.c
tst-eintr4.c
tst-eintr5.c
tst-exec1.c
tst-exec2.c
tst-exec3.c
tst-exec4.c
tst-execstack-mod.c
tst-execstack.c
tst-exit1.c
tst-exit2.c
tst-exit3.c
tst-fini1.c
tst-fini1mod.c
tst-flock1.c
tst-flock2.c
tst-fork1.c
tst-fork2.c
tst-fork3.c
tst-fork4.c
tst-getpid1.c
tst-getpid2.c
tst-initializers1-c89.c
tst-initializers1-c99.c
tst-initializers1-gnu89.c
tst-initializers1-gnu99.c
tst-initializers1.c
tst-join1.c
tst-join2.c
tst-join3.c
tst-join4.c
tst-join5.c
tst-key1.c
tst-key2.c
tst-key3.c
tst-key4.c
tst-kill1.c
tst-kill2.c
tst-kill3.c
tst-kill4.c
tst-kill5.c
tst-kill6.c
tst-locale1.c
tst-locale2.c
tst-mutex1.c
tst-mutex2.c
tst-mutex3.c
tst-mutex4.c
tst-mutex5.c
tst-mutex5a.c
tst-mutex6.c
tst-mutex7.c
tst-mutex7a.c
tst-mutex8.c
tst-mutex9.c
tst-oddstacklimit.c
tst-once1.c
tst-once2.c
tst-once3.c
tst-once4.c
tst-oncex3.c
tst-oncex4.c
tst-popen1.c
tst-raise1.c
tst-robust1.c
tst-robust2.c
tst-robust3.c
tst-robust4.c
tst-robust5.c
tst-robust6.c
tst-robust7.c
tst-robust8.c
tst-rwlock1.c
tst-rwlock10.c
tst-rwlock11.c
tst-rwlock12.c
tst-rwlock13.c
tst-rwlock14.c
tst-rwlock2.c
tst-rwlock3.c
tst-rwlock4.c
tst-rwlock5.c
tst-rwlock6.c
tst-rwlock7.c
tst-rwlock8.c
tst-rwlock9.c
tst-sched1.c
tst-sem1.c
tst-sem2.c
tst-sem3.c
tst-sem4.c
tst-sem5.c
tst-sem6.c
tst-sem7.c
tst-sem8.c
tst-sem9.c
tst-setuid1-static.c
tst-setuid1.c
tst-signal1.c
tst-signal2.c
tst-signal3.c
tst-signal4.c
tst-signal5.c
tst-signal6.c
tst-signal7.c
tst-spin1.c
tst-spin2.c
tst-spin3.c
tst-stack1.c
tst-stack2.c
tst-stack3.c
tst-stackguard1-static.c
tst-stackguard1.c
tst-stdio1.c
tst-stdio2.c
tst-sysconf.c
tst-tls1.c
tst-tls2.c
tst-tls3.c
tst-tls3mod.c
tst-tls4.c
tst-tls4moda.c
tst-tls4modb.c
tst-tls5.c
tst-tls5.h
tst-tls5mod.c
tst-tls5moda.c
tst-tls5modb.c
tst-tls5modc.c
tst-tls5modd.c
tst-tls5mode.c
tst-tls5modf.c
tst-tls6.sh
tst-tsd1.c
tst-tsd2.c
tst-tsd3.c
tst-tsd4.c
tst-tsd5.c
tst-typesizes.c
tst-umask1.c
tst-unload.c
tst-vfork1.c
tst-vfork1x.c
tst-vfork2.c
tst-vfork2x.c
unwind.c
vars.c
version.c
nptl_db
nscd
nss
po
posix
pwd
resolv
resource
rt
scripts
setjmp
shadow
signal
socket
soft-fp
stdio-common
stdlib
streams
string
sunrpc
sysdeps
sysvipc
termios
time
timezone
wcsmbs
wctype
.cvsignore
BUGS
CANCEL-FCT-WAIVE
CANCEL-FILE-WAIVE
CONFORMANCE
COPYING
COPYING.LIB
ChangeLog
ChangeLog.1
ChangeLog.10
ChangeLog.11
ChangeLog.12
ChangeLog.13
ChangeLog.14
ChangeLog.15
ChangeLog.2
ChangeLog.3
ChangeLog.4
ChangeLog.5
ChangeLog.6
ChangeLog.7
ChangeLog.8
ChangeLog.9
FAQ
FAQ.in
INSTALL
INTERFACE
LICENSES
Makeconfig
Makefile
Makefile.in
Makerules
NAMESPACE
NEWS
NOTES
PROJECTS
README
README.libm
README.template
Rules
Versions.def
WUR-REPORT
abi-tags
aclocal.m4
config-name.in
config.h.in
config.make.in
configure
configure.in
cppflags-iterator.mk
extra-lib.mk
extra-modules.mk
o-iterator.mk
shlib-versions
test-skeleton.c
tls.make.c
version.h
Breadcrumbs
glibc
/
nptl
/
pthread_create.c
Blame
Blame
Latest commit
History
History
583 lines (473 loc) · 16.3 KB
Breadcrumbs
glibc
/
nptl
/
pthread_create.c
Top
File metadata and controls
Code
Blame
583 lines (473 loc) · 16.3 KB
Raw
/* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include <errno.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include "pthreadP.h" #include <hp-timing.h> #include <ldsodefs.h> #include <atomic.h> #include <libc-internal.h> #include <resolv.h> #include <shlib-compat.h> /* Local function to start thread and handle cleanup. */ static int start_thread (void *arg); /* Nozero if debugging mode is enabled. */ int __pthread_debug; /* Globally enabled events. */ static td_thr_events_t __nptl_threads_events; /* Pointer to descriptor with the last event. */ static struct pthread *__nptl_last_event; /* Number of threads running. */ unsigned int __nptl_nthreads = 1; /* Code to allocate and deallocate a stack. */ #include "allocatestack.c" /* Code to create the thread. */ #include <createthread.c> struct pthread * internal_function __find_in_stack_list (pd) struct pthread *pd; { list_t *entry; struct pthread *result = NULL; lll_lock (stack_cache_lock); list_for_each (entry, &stack_used) { struct pthread *curp; curp = list_entry (entry, struct pthread, list); if (curp == pd) { result = curp; break; } } if (result == NULL) list_for_each (entry, &__stack_user) { struct pthread *curp; curp = list_entry (entry, struct pthread, list); if (curp == pd) { result = curp; break; } } lll_unlock (stack_cache_lock); return result; } /* Deallocate POSIX thread-local-storage. */ void attribute_hidden __nptl_deallocate_tsd (void) { struct pthread *self = THREAD_SELF; /* Maybe no data was ever allocated. This happens often so we have a flag for this. */ if (THREAD_GETMEM (self, specific_used)) { size_t round; size_t cnt; round = 0; do { size_t idx; /* So far no new nonzero data entry. */ THREAD_SETMEM (self, specific_used, false); for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) { struct pthread_key_data *level2; level2 = THREAD_GETMEM_NC (self, specific, cnt); if (level2 != NULL) { size_t inner; for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE; ++inner, ++idx) { void *data = level2[inner].data; if (data != NULL) { /* Always clear the data. */ level2[inner].data = NULL; /* Make sure the data corresponds to a valid key. This test fails if the key was deallocated and also if it was re-allocated. It is the user's responsibility to free the memory in this case. */ if (level2[inner].seq == __pthread_keys[idx].seq /* It is not necessary to register a destructor function. */ && __pthread_keys[idx].destr != NULL) /* Call the user-provided destructor. */ __pthread_keys[idx].destr (data); } } } else idx += PTHREAD_KEY_1STLEVEL_SIZE; } if (THREAD_GETMEM (self, specific_used) == 0) /* No data has been modified. */ goto just_free; } /* We only repeat the process a fixed number of times. */ while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0)); /* Just clear the memory of the first block for reuse. */ memset (&THREAD_SELF->specific_1stblock, '\0', sizeof (self->specific_1stblock)); just_free: /* Free the memory for the other blocks. */ for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) { struct pthread_key_data *level2; level2 = THREAD_GETMEM_NC (self, specific, cnt); if (level2 != NULL) { /* The first block is allocated as part of the thread descriptor. */ free (level2); THREAD_SETMEM_NC (self, specific, cnt, NULL); } } THREAD_SETMEM (self, specific_used, false); } } /* Deallocate a thread's stack after optionally making sure the thread descriptor is still valid. */ void internal_function __free_tcb (struct pthread *pd) { /* The thread is exiting now. */ if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling, TERMINATED_BIT) == 0, 1)) { /* Remove the descriptor from the list. */ if (DEBUGGING_P && __find_in_stack_list (pd) == NULL) /* Something is really wrong. The descriptor for a still running thread is gone. */ abort (); /* Queue the stack memory block for reuse and exit the process. The kernel will signal via writing to the address returned by QUEUE-STACK when the stack is available. */ __deallocate_stack (pd); } } static int start_thread (void *arg) { struct pthread *pd = (struct pthread *) arg; #if HP_TIMING_AVAIL /* Remember the time when the thread was started. */ hp_timing_t now; HP_TIMING_NOW (now); THREAD_SETMEM (pd, cpuclock_offset, now); #endif /* Initialize resolver state pointer. */ __resp = &pd->res; #ifdef __NR_set_robust_list # ifndef __ASSUME_SET_ROBUST_LIST if (__set_robust_list_avail >= 0) # endif { INTERNAL_SYSCALL_DECL (err); /* This call should never fail because the initial call in init.c succeeded. */ INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head, sizeof (struct robust_list_head)); } #endif /* This is where the try/finally block should be created. For compilers without that support we do use setjmp. */ struct pthread_unwind_buf unwind_buf; /* No previous handlers. */ unwind_buf.priv.data.prev = NULL; unwind_buf.priv.data.cleanup = NULL; int not_first_call; not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf); if (__builtin_expect (! not_first_call, 1)) { /* Store the new cleanup handler info. */ THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf); if (__builtin_expect (pd->stopped_start, 0)) { int oldtype = CANCEL_ASYNC (); /* Get the lock the parent locked to force synchronization. */ lll_lock (pd->lock); /* And give it up right away. */ lll_unlock (pd->lock); CANCEL_RESET (oldtype); } /* Run the code the user provided. */ #ifdef CALL_THREAD_FCT THREAD_SETMEM (pd, result, CALL_THREAD_FCT (pd)); #else THREAD_SETMEM (pd, result, pd->start_routine (pd->arg)); #endif } /* Run the destructor for the thread-local data. */ __nptl_deallocate_tsd (); /* Clean up any state libc stored in thread-local variables. */ __libc_thread_freeres (); /* If this is the last thread we terminate the process now. We do not notify the debugger, it might just irritate it if there is no thread left. */ if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads), 0)) /* This was the last thread. */ exit (0); /* Report the death of the thread if this is wanted. */ if (__builtin_expect (pd->report_events, 0)) { /* See whether TD_DEATH is in any of the mask. */ const int idx = __td_eventword (TD_DEATH); const uint32_t mask = __td_eventmask (TD_DEATH); if ((mask & (__nptl_threads_events.event_bits[idx] | pd->eventbuf.eventmask.event_bits[idx])) != 0) { /* Yep, we have to signal the death. Add the descriptor to the list but only if it is not already on it. */ if (pd->nextevent == NULL) { pd->eventbuf.eventnum = TD_DEATH; pd->eventbuf.eventdata = pd; do pd->nextevent = __nptl_last_event; while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event, pd, pd->nextevent)); } /* Now call the function to signal the event. */ __nptl_death_event (); } } /* The thread is exiting now. Don't set this bit until after we've hit the event-reporting breakpoint, so that td_thr_get_info on us while at the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */ atomic_bit_set (&pd->cancelhandling, EXITING_BIT); #ifndef __ASSUME_SET_ROBUST_LIST /* If this thread has any robust mutexes locked, handle them now. */ # if __WORDSIZE == 64 void *robust = pd->robust_head.list; # else __pthread_slist_t *robust = pd->robust_list.__next; # endif /* We let the kernel do the notification if it is able to do so. */ if (__set_robust_list_avail < 0 && __builtin_expect (robust != &pd->robust_head, 0)) { do { struct __pthread_mutex_s *this = (struct __pthread_mutex_s *) ((char *) robust - offsetof (struct __pthread_mutex_s, __list.__next)); robust = *((void **) robust); # ifdef __PTHREAD_MUTEX_HAVE_PREV this->__list.__prev = NULL; # endif this->__list.__next = NULL; lll_robust_mutex_dead (this->__lock); } while (robust != &pd->robust_head); } #endif /* If the thread is detached free the TCB. */ if (IS_DETACHED (pd)) /* Free the TCB. */ __free_tcb (pd); else if (__builtin_expect (pd->cancelhandling & SETXID_BITMASK, 0)) { /* Some other thread might call any of the setXid functions and expect us to reply. In this case wait until we did that. */ do lll_futex_wait (&pd->setxid_futex, 0); while (pd->cancelhandling & SETXID_BITMASK); /* Reset the value so that the stack can be reused. */ pd->setxid_futex = 0; } /* We cannot call '_exit' here. '_exit' will terminate the process. The 'exit' implementation in the kernel will signal when the process is really dead since 'clone' got passed the CLONE_CLEARTID flag. The 'tid' field in the TCB will be set to zero. The exit code is zero since in case all threads exit by calling 'pthread_exit' the exit status must be 0 (zero). */ __exit_thread_inline (0); /* NOTREACHED */ return 0; } /* Default thread attributes for the case when the user does not provide any. */ static const struct pthread_attr default_attr = { /* Just some value > 0 which gets rounded to the nearest page size. */ .guardsize = 1, }; int __pthread_create_2_1 (newthread, attr, start_routine, arg) pthread_t *newthread; const pthread_attr_t *attr; void *(*start_routine) (void *); void *arg; { STACK_VARIABLES; const struct pthread_attr *iattr = (struct pthread_attr *) attr; if (iattr == NULL) /* Is this the best idea? On NUMA machines this could mean accessing far-away memory. */ iattr = &default_attr; struct pthread *pd = NULL; int err = ALLOCATE_STACK (iattr, &pd); if (__builtin_expect (err != 0, 0)) /* Something went wrong. Maybe a parameter of the attributes is invalid or we could not allocate memory. */ return err; /* Initialize the TCB. All initializations with zero should be performed in 'get_cached_stack'. This way we avoid doing this if the stack freshly allocated with 'mmap'. */ #ifdef TLS_TCB_AT_TP /* Reference to the TCB itself. */ pd->header.self = pd; /* Self-reference for TLS. */ pd->header.tcb = pd; #endif /* Store the address of the start routine and the parameter. Since we do not start the function directly the stillborn thread will get the information from its thread descriptor. */ pd->start_routine = start_routine; pd->arg = arg; /* Copy the thread attribute flags. */ struct pthread *self = THREAD_SELF; pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))); /* Initialize the field for the ID of the thread which is waiting for us. This is a self-reference in case the thread is created detached. */ pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL; /* The debug events are inherited from the parent. */ pd->eventbuf = self->eventbuf; /* Copy the parent's scheduling parameters. The flags will say what is valid and what is not. */ pd->schedpolicy = self->schedpolicy; pd->schedparam = self->schedparam; /* Copy the stack guard canary. */ #ifdef THREAD_COPY_STACK_GUARD THREAD_COPY_STACK_GUARD (pd); #endif /* Copy the pointer guard value. */ #ifdef THREAD_COPY_POINTER_GUARD THREAD_COPY_POINTER_GUARD (pd); #endif /* Determine scheduling parameters for the thread. */ if (attr != NULL && __builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0) && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0) { INTERNAL_SYSCALL_DECL (scerr); /* Use the scheduling parameters the user provided. */ if (iattr->flags & ATTR_FLAG_POLICY_SET) pd->schedpolicy = iattr->schedpolicy; else if ((pd->flags & ATTR_FLAG_POLICY_SET) == 0) { pd->schedpolicy = INTERNAL_SYSCALL (sched_getscheduler, scerr, 1, 0); pd->flags |= ATTR_FLAG_POLICY_SET; } if (iattr->flags & ATTR_FLAG_SCHED_SET) memcpy (&pd->schedparam, &iattr->schedparam, sizeof (struct sched_param)); else if ((pd->flags & ATTR_FLAG_SCHED_SET) == 0) { INTERNAL_SYSCALL (sched_getparam, scerr, 2, 0, &pd->schedparam); pd->flags |= ATTR_FLAG_SCHED_SET; } /* Check for valid priorities. */ int minprio = INTERNAL_SYSCALL (sched_get_priority_min, scerr, 1, iattr->schedpolicy); int maxprio = INTERNAL_SYSCALL (sched_get_priority_max, scerr, 1, iattr->schedpolicy); if (pd->schedparam.sched_priority < minprio || pd->schedparam.sched_priority > maxprio) { err = EINVAL; goto errout; } } /* Pass the descriptor to the caller. */ *newthread = (pthread_t) pd; /* Remember whether the thread is detached or not. In case of an error we have to free the stacks of non-detached stillborn threads. */ bool is_detached = IS_DETACHED (pd); /* Start the thread. */ err = create_thread (pd, iattr, STACK_VARIABLES_ARGS); if (err != 0) { /* Something went wrong. Free the resources. */ if (!is_detached) { errout: __deallocate_stack (pd); } return err; } return 0; } versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1); #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1) int __pthread_create_2_0 (newthread, attr, start_routine, arg) pthread_t *newthread; const pthread_attr_t *attr; void *(*start_routine) (void *); void *arg; { /* The ATTR attribute is not really of type `pthread_attr_t *'. It has the old size and access to the new members might crash the program. We convert the struct now. */ struct pthread_attr new_attr; if (attr != NULL) { struct pthread_attr *iattr = (struct pthread_attr *) attr; size_t ps = __getpagesize (); /* Copy values from the user-provided attributes. */ new_attr.schedparam = iattr->schedparam; new_attr.schedpolicy = iattr->schedpolicy; new_attr.flags = iattr->flags; /* Fill in default values for the fields not present in the old implementation. */ new_attr.guardsize = ps; new_attr.stackaddr = NULL; new_attr.stacksize = 0; new_attr.cpuset = NULL; /* We will pass this value on to the real implementation. */ attr = (pthread_attr_t *) &new_attr; } return __pthread_create_2_1 (newthread, attr, start_routine, arg); } compat_symbol (libpthread, __pthread_create_2_0, pthread_create, GLIBC_2_0); #endif /* Information for libthread_db. */ #include "../nptl_db/db_info.c" /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread functions to be present as well. */ PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_lock) PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_unlock) PTHREAD_STATIC_FN_REQUIRE (pthread_once) PTHREAD_STATIC_FN_REQUIRE (pthread_cancel) PTHREAD_STATIC_FN_REQUIRE (pthread_key_create) PTHREAD_STATIC_FN_REQUIRE (pthread_setspecific) PTHREAD_STATIC_FN_REQUIRE (pthread_getspecific)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
You can’t perform that action at this time.