aboutsummaryrefslogtreecommitdiff
path: root/libc/bionic/pthread_create.cpp
Commit message (Collapse)AuthorAgeFilesLines
...
* Use bionic lock in pthread_internal_t.Yabin Cui2015-10-221-8/+7
| | | | | | | It removes calling to pthread_mutex_lock() at the beginning of new thread, which helps to support thread sanitizer. Change-Id: Ia3601c476de7976a9177b792bd74bb200cee0e13
* Name the thread stack and signal stack guard pages.Elliott Hughes2015-07-011-0/+2
| | | | | Bug: http://b/22228722 Change-Id: I1dae672e386e404fb304a34496a29fe21134c784
* Revert "Revert "add guard pages to the internal signal stacks""Yabin Cui2015-03-301-5/+12
| | | | | | | This reverts commit a3125fd1396a09a7fc4872dc4653f342150a3deb. And Fix the prctl() problem that cause system crash. Change-Id: Icc8d12d848cfba881a7984ca2827fd81be41f9fd
* Revert "add guard pages to the internal signal stacks"Elliott Hughes2015-03-311-12/+6
| | | | | | This reverts commit 595752f623ae88f7e4193a6e531a0805f1c6c4dc. Change-Id: Iefa66e9049ca0424e53cd5fc320d161b93556dcb
* add guard pages to the internal signal stacksDaniel Micay2015-03-301-6/+12
| | | | | | | Signal handlers tend to be lean, but can still overflow the (tiny) stack. Change-Id: Ia21c6453d92a9f8d1536ad01ff26a1a84c05f8fb
* Fix in error handling in pthread_create.cpp.Yabin Cui2015-03-251-0/+1
| | | | | | | | It is due to a previous change "Let g_thread_list_lock only protect g_thread_list". We need to add the newly created thread to thread_list even if __init_thread fails, so the thread can exit successfully. Change-Id: I0332df11acfdd181350bcc092b12d90d679057a4
* Merge "Let g_thread_list_lock only protect g_thread_list."Yabin Cui2015-03-241-7/+3
|\
| * Let g_thread_list_lock only protect g_thread_list.Yabin Cui2015-03-231-7/+3
| | | | | | | | | | | | | | | | | | As glibc/netbsd don't protect access to thread struct members by a global lock, we don't want to do it either. This change reduces the responsibility of g_thread_list_lock to only protect g_thread_list. Bug: 19636317 Change-Id: I897890710653dac165d8fa4452c7ecf74abdbf2b
* | Fix alignment error for pthread_internal_t/pthread stack.Yabin Cui2015-03-201-5/+6
|/ | | | | | | | aligned attribute can only control compiler's behavior, but we are manually allocating pthread_internal_t. So we need to make sure of alignment manually. Change-Id: Iea4c46eadf10dfd15dc955c5f41cf6063cfd8536
* Make pthread join_state not protected by g_thread_list_lock.Yabin Cui2015-03-121-1/+7
| | | | | | | | | | | 1. Move the representation of thread join_state from pthread.attr.flag to pthread.join_state. This clarifies thread state change. 2. Use atomic operations for pthread.join_state. So we don't need to protect it by g_thread_list_lock. g_thread_list_lock will be reduced to only protect g_thread_list or even removed in further changes. Bug: 19636317 Change-Id: I31fb143a7c69508c7287307dd3b0776993ec0f43
* Refactor pthread_key.cpp to be lock-free.Yabin Cui2015-03-031-2/+3
| | | | Change-Id: I20dfb9d3cdc40eed10ea12ac34f03caaa94f7a49
* Add missing includes.Elliott Hughes2015-01-281-0/+1
| | | | Change-Id: Ibf549266a19a67eb9158d341a69dddfb654be669
* Turn on -Wold-style-cast and fix the errors.Elliott Hughes2015-01-211-1/+1
| | | | | | A couple of dodgy cases where we cast away const, but otherwise pretty boring. Change-Id: Ibc39ebd525377792b5911464be842121c20f03b9
* Make pthread stack size match real range.Yabin Cui2015-01-081-0/+1
| | | | | Bug: 18908062 Change-Id: I7037ac8273ebe54dd19b1561c7a376819049124c
* Remove PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK.Yabin Cui2015-01-061-26/+27
| | | | | | Patch for https://android-review.googlesource.com/#/c/120844/. Change-Id: Idca5ccd7b28e8f07f1d2d1b6e3bba6781b62f0e0
* Reserve enough user request stack space in pthread_create.Yabin Cui2015-01-021-10/+16
| | | | | Bug: 18830897 Change-Id: I1ba4aaeaf66a7ff99c5d82ad45469011171b0a3b
* Use mmap to create the pthread_internal_tYabin Cui2014-12-191-38/+53
| | | | | | | | | Add name to mmaped regions. Add pthread benchmark code. Allocate pthread_internal_t on regular stack. Bug: 16847284 Change-Id: Id60835163bb0d68092241f1a118015b5a8f85069
* Fix pthread key num calculation.Yabin Cui2014-12-121-1/+1
| | | | | Bug: 18723085 Change-Id: Iba2c834b350e4cdba0b2d771b221560a3e5df952
* Use mmap to create the pthread_internal_t.Yabin Cui2014-12-021-4/+3
| | | | | Bug: 16847284 Change-Id: I488fa236f57aa3acb29b4ffbbab2fab51b0653be
* Revert "Use mmap to create the pthread_internal_t."Nicolas Geoffray2014-11-261-3/+4
| | | | | | | | | | | | Unfortunately, this change provokes random crashes for ART, and I have seen libc crashes on the device that might be related to it. Reverting it fixes the ART crashes. there is unfortunately no stack trace for the crashes, but just a "Segmentation fault" message. This reverts commit cc5f6543e3f91385b9a912438965b7e8265df54a. Change-Id: I68dca8e1e9b9edcce7eb84596e8db619e40e8052
* Use mmap to create the pthread_internal_t.Yabin Cui2014-11-251-4/+3
| | | | | Bug: 16847284 Change-Id: Ic8c85f95afac1d8422ecb69674c688d1fecb6a44
* Only wipe TLS for user-supplied stacks.Elliott Hughes2014-07-301-5/+5
| | | | | Bug: 16667988 Change-Id: Id180ab2bc6713e1612386120a306db5bbf1d6046
* Remove the global lock around thread stack creation.Elliott Hughes2014-07-101-4/+0
| | | | | | | | This lock has been here since the original commits, but as far as I can tell it never served any purpose. We've never had a free list of cached stacks or anything like that. Change-Id: I9d665c7eaa9c699ce0659ffb111402a0239fe1f5
* Remove the obsolete _thread_created_hook.Elliott Hughes2014-07-091-15/+0
| | | | | | | | gdb won't even try to use this on Android because it knows we don't support old enough kernels to need it. Bug: 15470251 Change-Id: Ia6d54585d888bbab8ee0490a148a1586b25437b9
* Cache getpid.Elliott Hughes2014-06-201-0/+3
| | | | | | | | | | | | | | | | | In practice, with this implementation we never need to make a system call. We get the main thread's tid (which is the same as our pid) back from the set_tid_address system call we have to make during initialization. A new pthread will have the same pid as its parent, and a fork child's main (and only) thread will have a pid equal to its tid, which we get for free from the kernel before clone returns. The only time we'd actually have to make a getpid system call now is if we take a signal during fork and the signal handler calls getpid. (That, or we call getpid in the dynamic linker while it's still dealing with its own relocations and hasn't even set up the main thread yet.) Bug: 15387103 Change-Id: I6d4718ed0a5c912fc75b5f738c49a023dbed5189
* Move common macros into bionic_macros.h.Christopher Ferris2014-06-181-2/+3
| | | | | Bug: 15590152 Change-Id: I730636613ef3653f68c5ab1d43b53beaf8e0dc25
* Revert "Revert "Lose the hand-written futex assembler.""Elliott Hughes2014-05-281-11/+9
| | | | | | | | | | | | | | | | | | | | | | The problem with the original patch was that using syscall(3) means that errno can be set, but pthread_create(3) was abusing the TLS errno slot as a pthread_mutex_t for the thread startup handshake. There was also a mistake in the check for syscall failures --- it should have checked against -1 instead of 0 (not just because that's the default idiom, but also here because futex(2) can legitimately return values > 0). This patch stops abusing the TLS errno slot and adds a pthread_mutex_t to pthread_internal_t instead. (Note that for LP64 sizeof(pthread_mutex_t) > sizeof(uintptr_t), so we could potentially clobber other TLS slots too.) I've also rewritten the LP32 compatibility stubs to directly reuse the code from the .h file. This reverts commit 75c55ff84ebfa686c7ae2cc8ee431c6a33bd46b4. Bug: 15195455 Change-Id: I6ffb13e5cf6a35d8f59f692d94192aae9ab4593d
* Fix a typo in the big g_ search/replace.Elliott Hughes2014-05-141-2/+2
| | | | Change-Id: I79261de70d225236d0eadff288220258d697437f
* Switch to g_ for globals.Elliott Hughes2014-05-141-4/+4
| | | | | | | That's what the Google style guide recommends, and we're starting to get a mix. Change-Id: Ib0c53a890bb5deed5c679e887541a715faea91fc
* Align the child stack in clone(2).Elliott Hughes2014-05-091-15/+14
| | | | | | | | Also let clone(2) set the TLS for x86. Also ensure we initialize the TLS before we clone(2) for all architectures. Change-Id: Ie5fa4466e1c9ee116a281dfedef574c5ba60c0b5
* Remove unnecessary #includes.Elliott Hughes2014-04-101-1/+0
| | | | Change-Id: Ie7e0c9ea03f35517c7dcf09fc808c12e55262bc1
* Clean up <stdio.h> macros.Elliott Hughes2014-03-131-5/+3
| | | | | | | | | | Also neuter __isthreaded. We should come back to try to hide struct FILE's internals for LP64. Bug: 3453512 Bug: 3453550 Change-Id: I7e115329fb4579246a72fea367b9fc8cb6055d18
* Move _thread_created_hook to where it belongs.Elliott Hughes2014-01-141-5/+4
| | | | Change-Id: I643d761c78ccaae25270aeffa2afb811c4e2fcd7
* Work around CLONE_SETTLS being weird on x86.Elliott Hughes2013-11-261-0/+11
| | | | | | | | | | | | Unlike other architectures, on x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc instead of a pointer to the TLS itself. Rather than have to deal with this here, let's just use the old __set_tls mechanism we used to use (and still use for the main thread on all architectures, so it's not going away any time soon). Bug: 11826724 Change-Id: I02a27939a73ae6cea1134a3f4c1dd7eafea479da
* Clean up pthread_internal_t.Elliott Hughes2013-11-191-10/+13
| | | | | Bug: 11755300 Change-Id: Ib509e8c5ec6b23513aa78b5ac5141d7c34ce2dc8
* Fix pthread_join.Elliott Hughes2013-11-181-11/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | Let the kernel keep pthread_internal_t::tid updated, including across forks and for the main thread. This then lets us fix pthread_join to only return after the thread has really exited. Also fix the thread attributes of the main thread so we don't unmap the main thread's stack (which is really owned by the dynamic linker and contains things like environment variables), which fixes crashes when joining with an exited main thread and also fixes problems reported publicly with accessing environment variables after the main thread exits (for which I've added a new unit test). In passing I also fixed a bug where if the clone(2) inside pthread_create(3) fails, we'd unmap the child's stack and TLS (which contains the mutex) and then try to unlock the mutex. Boom! It wasn't until after I'd uploaded the fix for this that I came across a new public bug reporting this exact failure. Bug: 8206355 Bug: 11693195 Bug: https://code.google.com/p/android/issues/detail?id=57421 Bug: https://code.google.com/p/android/issues/detail?id=62392 Change-Id: I2af9cf6e8ae510a67256ad93cad891794ed0580b
* Clean up the pthread_create trampoline.Elliott Hughes2013-11-151-2/+27
| | | | | | Bug: 8206355 Bug: 11693195 Change-Id: I35cc024d5b6ebd19d1d2e45610db185addaf45df
* Switch pthread_create over to __bionic_clone.Elliott Hughes2013-11-151-15/+15
| | | | | | Bug: 8206355 Bug: 11693195 Change-Id: I04aadbc36c87e1b7e33324b9a930a1e441fbfed6
* <pthread.h> fixes and pthread cleanup.Elliott Hughes2013-10-311-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | <pthread.h> was missing nonnull attributes, noreturn on pthread_exit, and had incorrect cv qualifiers for several standard functions. I've also marked the non-standard stuff (where I count glibc rather than POSIX as "standard") so we can revisit this cruft for LP64 and try to ensure we're compatible with glibc. I've also broken out the pthread_cond* functions into a new file. I've made the remaining pthread files (plus ptrace) part of the bionic code and fixed all the warnings. I've added a few more smoke tests for chunks of untested pthread functionality. We no longer need the libc_static_common_src_files hack for any of the pthread implementation because we long since stripped out the rest of the armv5 support, and this hack was just to ensure that __get_tls in libc.a went via the kernel if necessary. This patch also finishes the job of breaking up the pthread.c monolith, and adds a handful of new tests. Change-Id: Idc0ae7f5d8aa65989598acd4c01a874fe21582c7
* Explain the sigprocmask in pthread_exit.Elliott Hughes2013-10-291-2/+2
| | | | | | | | | | | | | Also remove the SIGSEGV special case, which was probably because hand-written __exit_with_stack_teardown stubs used to try to cause SIGSEGV if the exit system call returned (which it never does, so that dead code disappeared). Also move the sigprocmask into the only case where it's necessary --- the one where we unmap the stack that would be used by a signal handler. Change-Id: Ie40d20c1ae2f5e7125131b6b492cba7a2c6d08e9
* Make pthread_create report sched_setscheduler failures on LP64.Elliott Hughes2013-10-151-2/+4
| | | | | | | | | We couldn't fix this for 32-bit because there's too much broken code out there. (Pretty much everyone asks for real-time scheduling for all their threads, and the kernel says "don't be stupid".) Change-Id: I43c5271e6b6bb91278b9a19eec08cbf05391e3c4
* Fix x86_64 build, clean up intermediate libraries.Elliott Hughes2013-10-091-27/+1
| | | | | | | | | | | | | | | | | | | | | | The x86_64 build was failing because clone.S had a call to __thread_entry which was being added to a different intermediate .a on the way to making libc.so, and the linker couldn't guarantee statically that such a relocation would be possible. ld: error: out/target/product/generic_x86_64/obj/STATIC_LIBRARIES/libc_common_intermediates/libc_common.a(clone.o): requires dynamic R_X86_64_PC32 reloc against '__thread_entry' which may overflow at runtime; recompile with -fPIC This patch addresses that by ensuring that the caller and callee end up in the same intermediate .a. While I'm here, I've tried to clean up some of the mess that led to this situation too. In particular, this removes libc/private/ from the default include path (except for the DNS code), and splits out the DNS code into its own library (since it's a weird special case of upstream NetBSD code that's diverged so heavily it's unlikely ever to get back in sync). There's more cleanup of the DNS situation possible, but this is definitely a step in the right direction, and it's more than enough to get x86_64 building cleanly. Change-Id: I00425a7245b7a2573df16cc38798187d0729e7c4
* Fix __errno for LP64 and clean up __get_tls.Elliott Hughes2013-10-091-0/+2
| | | | | | | | | | | If __get_tls has the right type, a lot of confusing casting can disappear. It was probably a mistake that __get_tls was exposed as a function for mips and x86 (but not arm), so let's (a) ensure that the __get_tls function always matches the macro, (b) that we have the function for arm too, and (c) that we don't have the function for any 64-bit architecture. Change-Id: Ie9cb989b66e2006524ad7733eb6e1a65055463be
* Improve stack overflow diagnostics (take 2).Elliott Hughes2013-07-171-3/+20
| | | | | | | | | | This reverts commits eb1b07469f2b5a392dc1bfd8adc211aea8c72bc5 and d14dc3b87fbf80553f1cafa453816b7f11366627, and fixes the bug where we were calling mmap (which might cause errno to be set) before __set_tls (which is required to implement errno). Bug: 8557703 Change-Id: I2c36d00240c56e156e1bb430d8c22a73a068b70c
* Revert "Improve stack overflow diagnostics."Guang Zhu2013-07-171-18/+3
| | | | | | This reverts commit aa754dca90487356cabf07ade0e8d88c2630b784. Change-Id: Ifa76eee31f7f44075eb3a48554315b2693062f44
* Improve stack overflow diagnostics.Elliott Hughes2013-07-161-3/+18
| | | | | | | | | We notify debuggerd of problems by installing signal handlers. That's fine except for when the signal is caused by us running off the end of a thread's stack and into the guard page. Bug: 8557703 Change-Id: I1ef65b4bb3bbca7e9a9743056177094921e60ed3
* Fix pthread_getattr_np, pthread_attr_setguardsize, and ↵Elliott Hughes2013-07-151-12/+19
| | | | | | | | | | | | | | | pthread_attr_setstacksize. pthread_getattr_np was reporting the values supplied to us, not the values we actually used, which is kinda the whole point of pthread_getattr_np. pthread_attr_setguardsize and pthread_attr_setstacksize were reporting EINVAL for any size that wasn't a multiple of the system page size. This is unnecessary. We can just round like POSIX suggests and glibc already does. Also improve the error reporting for pthread_create failures. Change-Id: I7ebc518628a8a1161ec72e111def911d500bba71
* Handles spurious wake-ups in pthread_join()msg5552013-06-121-1/+0
| | | | | | | | | | | | | | | | | | | Removed 'join_count' from pthread_internal_t and switched to using the flag PTHREAD_ATTR_FLAG_JOINED to indicate if a thread is being joined. Combined with a switch to a while loop in pthread_join, this fixes spurious wake-ups but prevents a thread from being joined multiple times. This is fine for two reasons: 1) The pthread_join specification allows for undefined behavior when multiple threads try to join a single thread. 2) There is no thread safe way to allow multiple threads to join a single thread with the pthread interface. The second thread calling pthread_join could be pre-empted until the thread is destroyed and its handle reused for a different thread. Therefore multi-join is always an error. Bug: https://code.google.com/p/android/issues/detail?id=52255 Change-Id: I8b6784d47620ffdcdbfb14524e7402e21d46c5f7
* Extra logging in pthread_create.Elliott Hughes2013-03-291-0/+3
| | | | | | | | | | pthread_create returns EAGAIN when it can't allocate a pthread_internal_t, when it can't allocate a stack for the new thread, or when clone(2) fails because there are too many threads. It's useful to be able to know why your pthread_create just failed, so add some logging. Bug: 8470684 Change-Id: I1bb4497d4f7528eacce0db35c2014771cba64569
* Clean up internal libc logging.Elliott Hughes2013-03-151-2/+1
| | | | | | | | | | | We only need one logging API, and I prefer the one that does no allocation and is thus safe to use in any context. Also use O_CLOEXEC when opening the /dev/log files. Move everything logging-related into one header file. Change-Id: Ic1e3ea8e9b910dc29df351bff6c0aa4db26fbb58