aboutsummaryrefslogtreecommitdiff
path: root/lib/libthr/thread
Commit message (Collapse)AuthorAgeFilesLines
...
* libthr: Add vprintf variant of _thread_printf, formatted PANIC()Conrad Meyer2016-06-013-11/+34
| | | | | | | | | | | No ABI change. Reviewed by: kib Sponsored by: EMC / Isilon Storage Division Differential Revision: https://reviews.freebsd.org/D6672 Notes: svn path=/head/; revision=301134
* Stop inlining the struct ucond definition into struct pthread_cond.Konstantin Belousov2016-05-292-15/+11
| | | | | | | | | | | | | | | | | | | | This avoids unneccessary casts and make the calls to _thr_ucond_*() functions less questionable. The c_spare field was not included into struct pthread_cond, so the change modifies libthr ABI for shared condvars. But since an off-page does not legitimately contains any other data past the struct pthread_cond, the change keeps shared condvars from pre- and post- changed libthr compatible. Also note that the whole struct ucond was never copied in or out by kernel. For private condvars, the privately allocated memory was never exposed outside libthr. Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=300971
* Add implementation of robust mutexes, hopefully close enough to theKonstantin Belousov2016-05-177-298/+612
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | intention of the POSIX IEEE Std 1003.1TM-2008/Cor 1-2013. A robust mutex is guaranteed to be cleared by the system upon either thread or process owner termination while the mutex is held. The next mutex locker is then notified about inconsistent mutex state and can execute (or abandon) corrective actions. The patch mostly consists of small changes here and there, adding neccessary checks for the inconsistent and abandoned conditions into existing paths. Additionally, the thread exit handler was extended to iterate over the userspace-maintained list of owned robust mutexes, unlocking and marking as terminated each of them. The list of owned robust mutexes cannot be maintained atomically synchronous with the mutex lock state (it is possible in kernel, but is too expensive). Instead, for the duration of lock or unlock operation, the current mutex is remembered in a special slot that is also checked by the kernel at thread termination. Kernel must be aware about the per-thread location of the heads of robust mutex lists and the current active mutex slot. When a thread touches a robust mutex for the first time, a new umtx op syscall is issued which informs about location of lists heads. The umtx sleep queues for PP and PI mutexes are split between non-robust and robust. Somewhat unrelated changes in the patch: 1. Style. 2. The fix for proper tdfind() call use in umtxq_sleep_pi() for shared pi mutexes. 3. Removal of the userspace struct pthread_mutex m_owner field. 4. The sysctl kern.ipc.umtx_vnode_persistent is added, which controls the lifetime of the shared mutex associated with a vnode' page. Reviewed by: jilles (previous version, supposedly the objection was fixed) Discussed with: brooks, Martin Simmons <martin@lispworks.com> (some aspects) Tested by: pho Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=300043
* Do not leak THR_FLAGS_SUSPENDED from the previous suspend/resumeKonstantin Belousov2016-05-052-4/+2
| | | | | | | | | | | | | | | | | cycle. The flag currently is cleared by the resumed thread. If next suspend request comes before the thread was able to clean the flag, in which case suspender skip the thread. Instead, clear the THR_FLAGS_SUSPEND flag in resume_common(), we do not care how much code was executed in the resumed thread when the pthread_resume_*np(s) functions returned. PR: 209233 Reported by: Lawrence Esswood <le277@cam.ac.uk> MFC after: 1 week Notes: svn path=/head/; revision=299114
* If off-page lookup failed, there is no memory to performKonstantin Belousov2016-04-121-1/+2
| | | | | | | | | shared_mutex_init() upon. Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297853
* Use __FBSDID() for .c files from lib/libthr/thread.Konstantin Belousov2016-04-0852-112/+158
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297706
* Use ANSI C function definitions, fix spelling in a comment.Konstantin Belousov2016-04-083-4/+5
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297704
* Assert that the lock objects put into the off-page, fit into the page.Konstantin Belousov2016-04-085-0/+15
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297701
* Remove unused variable. It was write-only before r297139.Konstantin Belousov2016-04-042-2/+0
| | | | | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=297535
* Apparently there are some popular programs around which assume that itKonstantin Belousov2016-03-222-2/+61
| | | | | | | | | | | | | | | | | | | | is safe to call pthread_mutex_init() on the same shared mutex several times. POSIX claims that the behaviour in this case is undefined. Make this working by only allowing one caller to initialize the mutex. Other callers either see already completed initialization and do nothing, or busy-loop yielding while designated initializer finishes. Also make the API requirements loose by initializing mutexes on other pthread_mutex*() calls if they see uninitialized shared mutex. Only mutexes provide the hack for now, but it could be also implemented for other process shared primitives from libthr. Reported and tested by: "Oleg V. Nauman" <oleg@opentransfer.com> Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297185
* Lock pshared_lock shared around fork, to ensure that the COW snapshotKonstantin Belousov2016-03-214-2/+22
| | | | | | | | | | of the pshared hash in child is consistent and can be safely used. Reported and tested by: "Oleg V. Nauman" <oleg@opentransfer.com> Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297141
* Provide more information on failing checks in mutex_assert_is_owned()Konstantin Belousov2016-03-211-4/+16
| | | | | | | | | | and mutex_assert_not_owned(). snprintf() use in this context should be safe. Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297140
* From libthr, remove special and strange code to set up session andKonstantin Belousov2016-03-212-22/+2
| | | | | | | | | | | | | | | control terminal, activated when running with pid 1. It is application duty to handle this, and unsuspecting init replacements which are linked with libthr would be broken by this. The pre-resolving of getpid() is restored, just in case. Reviewed by: jilles Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=297139
* Implement process-shared spinlocks.Konstantin Belousov2016-03-211-47/+58
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297138
* Add two comments explaining the fine points of the hashKonstantin Belousov2016-03-011-0/+31
| | | | | | | | | | | implementation. Reviewed by: emaste Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D5490 Notes: svn path=/head/; revision=296268
* Implement process-shared locks support for libthr.so.3, withoutKonstantin Belousov2016-02-2813-304/+785
| | | | | | | | | | | | | | | breaking the ABI. Special value is stored in the lock pointer to indicate shared lock, and offline page in the shared memory is allocated to store the actual lock. Reviewed by: vangyzen (previous version) Discussed with: deischen, emaste, jhb, rwatson, Martin Simmons <martin@lispworks.com> Tested by: pho Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=296162
* If libthr.so is dlopened without RTLD_GLOBAL flag, the libthr symbolsKonstantin Belousov2016-02-083-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | do not participate in the global symbols namespace, but rtld locks are still replaced and functions are interposed. In particular, __pthread_map_stacks_exec is resolved to the libc version. If a library is loaded later, which requires adjustment of the stack protection mode, rtld calls into libc __pthread_map_stacks_exec due to the symbols scope. The libc version might recurse into binder and recursively acquire rtld bind lock, causing the hang. Make libc __pthread_map_stacks_exec() interposed, which synchronizes rtld locks and version of the stack exec hook when libthr loaded, regardless of the symbol scope control or symbol resolution order. The __pthread_map_stacks_exec() symbol is removed from the private version in libthr since libc symbol now operates correctly in presence of libthr. Reported and tested by: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=295407
* libthr: const-ify two variablesEric van Gyzen2016-01-131-2/+3
| | | | | | | | | | | Make the default umutex and urwlock initializers const, because they can be, and as a microoptimization. MFC after: 5 days Sponsored by: Dell Inc. Notes: svn path=/head/; revision=293858
* Typo.Konstantin Belousov2015-12-271-1/+1
| | | | Notes: svn path=/head/; revision=292763
* libthr: Don't use both __sys_open() and __sys_openat().Jilles Tjoelker2015-12-202-2/+1
| | | | Notes: svn path=/head/; revision=292516
* Style. Use ANSI definition, wrap long lines, no initialization inKonstantin Belousov2015-09-081-8/+15
| | | | | | | | | | declaration for locals. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=287557
* In the pthread_once(), if the initializer has already run, then theKonstantin Belousov2015-09-081-3/+5
| | | | | | | | | | | | | | | | | | | | calling thread is supposed to see accesses issued by the initializer. This means that the read of the once_control->state variable should have an acquire semantic. Use atomic_thread_fence_acq() when the value read is ONCE_DONE, instead of straightforward atomic_load_acq(), to only put a barrier when needed (*). On the other hand, the updates of the once_control->state with the intermediate progress state do not need to synchronize with other state accesses, remove _acq suffix. Reviewed by: alc (previous version) Suggested by: alc (*) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=287556
* Pre-resolve symbols required for the deferred signal processing. ThisKonstantin Belousov2015-08-102-1/+9
| | | | | | | | | | | | | | | | avoids recursion into rtld when leaving libthr critical section for the deferred signal delivery. For the same reason, use syscall(2) instead of referencing __sys_sigreturn(2). Syscall() is already pre-resolved for fork() interceptor. Tested by: Andre Meiser <ortadur@web.de> Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=286582
* cosmetic: whitespaces-tab before EOLPedro F. Giffuni2015-07-081-1/+1
| | | | | | | Obtained from: cpi-llvm project Notes: svn path=/head/; revision=285278
* Fix typo in comment.Konstantin Belousov2015-06-141-2/+2
| | | | | | | MFC after: 3 days Notes: svn path=/head/; revision=284385
* Some third-party malloc(3) implementations use pthread_setspecific(3)Konstantin Belousov2015-05-151-110/+91
| | | | | | | | | | | | | | | | | | | | | | | | | | | | to handle per-thread information. Since our pthread_setspecific() implementation calls calloc(3) to allocate per-thread specific data storage, things get complicated. Switch the allocator to use bare mmap(2). There is some loss of the allocated page, since e.g. on amd64, PTHREAD_KEYS_MAX * sizeof(struct pthread_specific_elem) is 3K (it actually spans whole page due to padding), but I believe it is more acceptable than additional code for specialized allocator(). The alternatives would either to make the specific data array be part of the struct thread, or use internal bindings to call the libc malloc, avoiding interposing. Also do the style pass over the thr_spec.c, esp. simplify the conditionals nesting by returning early when an error detected. Remove trivial comments. Found by: yuri@rawbw.com PR: 200138 Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=282948
* _pthread_cleanup_push: fix allocator sizeof operand mismatchPedro F. Giffuni2015-04-221-1/+1
| | | | | | | | | | Same fix appears to be in DragonFly's libthread_xu. Found by: Clang Static Analyzer MFC after: 1 week Notes: svn path=/head/; revision=281857
* The lseek(2), mmap(2), truncate(2), ftruncate(2), pread(2), andKonstantin Belousov2015-04-181-12/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | pwrite(2) syscalls are wrapped to provide compatibility with pre-7.x kernels which required padding before the off_t parameter. The fcntl(2) contains compatibility code to handle kernels before the struct flock was changed during the 8.x CURRENT development. The shims were reasonable to allow easier revert to the older kernel at that time. Now, two or three major releases later, shims do not serve any purpose. Such old kernels cannot handle current libc, so revert the compatibility code. Make padded syscalls support conditional under the COMPAT6 config option. For COMPAT32, the syscalls were under COMPAT6 already. Remove WITHOUT_SYSCALL_COMPAT build option, which only purpose was to (partially) disable the removed shims. Reviewed by: jhb, imp (previous versions) Discussed with: peter Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=281714
* Make wait6(2), waitid(3) and ppoll(2) cancellation points. TheKonstantin Belousov2015-04-181-0/+41
| | | | | | | | | | | | waitid() function is required to be cancellable by the standard. The wait6() and ppoll() follow the other syscalls in their groups. Reviewed by: jhb, jilles (previous versions) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=281712
* Make kevent(2) a cancellation point.Konstantin Belousov2015-03-291-0/+24
| | | | | | | | | | | | | | | | | | | | | | | | Note that to cancel blocked kevent(2) call, changelist must be empty, since we cannot cancel a call which already made changes to the process state. And in reverse, call which only makes changes to the kqueue state, without waiting for an event, is not cancellable. This makes a natural usage model to migrate kqueue loop to support cancellation, where existing single kevent(2) call must be split into two: first uncancellable update of kqueue, then cancellable wait for events. Note that this is ABI-incompatible change, but it is believed that there is no cancel-safe code that relies on kevent(2) not being a cancellation point. Option to preserve the ABI would be to keep kevent(2) as is, but add new call with flags to specify cancellation behaviour, which only value seems to add complications. Suggested and reviewed by: jilles Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=280818
* Fix a typo in comment and explain the reason.Jung-uk Kim2015-03-091-1/+1
| | | | Notes: svn path=/head/; revision=279820
* Propagate errors from _thr_umutex_unlock2 through mutex_unlock_common.Konstantin Belousov2015-02-251-3/+4
| | | | | | | | | | | | | | | Errors from _thr_umutex_unlock2 should "never happen" in normal circumstances. If they do, however, return them to the application so it can fail early and loudly. Hiding the errors will only delay the inevitable failure, making it harder to find and diagnose. Submitted by: Eric van Gyzen <eric_van_gyzen@dell.com> Obtained from: Dell Inc. PR: 198914 MFC after: 1 week Notes: svn path=/head/; revision=279284
* Properly interpose libc spinlocks, was missed in r276630. InKonstantin Belousov2015-02-143-8/+8
| | | | | | | | | | | particular, stdio locking was affected. Reported and tested by: "Matthew D. Fuller" <fullermd@over-yonder.net> Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=278751
* Fully initialize allocated memory for the new barrier. TheKonstantin Belousov2015-02-061-4/+1
| | | | | | | | | | | | | b_destroying member was left uninitialized, which caused spurious EBUSY. PR: 197365 Noted by: Florent Guiliani <fguiliani@verisign.com> Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=278313
* Merge all the copies of _tcb_ctor and _tcb_dtor.Andrew Turner2015-01-213-0/+60
| | | | | | | | | | | | | | | | | | | | | | | | | | | | The amd64, i386, and sparc64 versions were identical, with the one difference where the former two used inline asm instead of _tcb_get. I have compared the function before and after replacing the asm with _tcb_get and found the object files to be identical. The arm, mips, and powerpc versions were almost identical. The only difference was the powerpc version used an alignment of 1 where arm and mips used 16. As this is an increase in alignment is will be safe. Along with this arm, mips, and powerpc all passed, when initial was true, the value returned from _tcb_get as the first argument to _rtld_allocate_tls. This would then return this pointer back to the caller. We can remove these extra calls by checking if initial is set and setting the thread control block directly. As this is what the sparc64 code does we can use it directly. As after these observations all the architectures can now have identical code we can merge them into a common file. Differential Revision: https://reviews.freebsd.org/D1556 Reviewed by: kib Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=277490
* Fix bug in r276630. Do not allow pthread_sigmask() to block SIGCANCEL.Konstantin Belousov2015-01-211-1/+2
| | | | | | | | | Reported and tested by: royger Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=277488
* Reduce the size of the interposing table and amount ofKonstantin Belousov2015-01-113-160/+0
| | | | | | | | | | | | | | | | | | | | cancellation-handling code in the libthr. Translate some syscalls into their more generic counterpart, and remove translated syscalls from the table. List of the affected syscalls: creat, open -> openat raise -> thr_kill sleep, usleep -> nanosleep pause -> sigsuspend wait, wait3, waitpid -> wait4 Suggested and reviewed by: jilles (previous version) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=277032
* Avoid calling internal libc function through PLT or accessing dataKonstantin Belousov2015-01-052-2/+2
| | | | | | | | | | | | though GOT, by staticizing and hiding. Add setter for __error_selector to hide it as well. Suggested and reviewed by: jilles Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=276681
* Fix known issues which blow up the process after dlopen("libthr.so")Konstantin Belousov2015-01-037-351/+358
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (or loading a dso linked to libthr.so into process which was not linked against threading library). - Remove libthr interposers of the libc functions, including __error(). Instead, functions calls are indirected through the interposing table, similar to how pthread stubs in libc are already done. Libc by default points either to syscall trampolines or to existing libc implementations. On libthr load, libthr rewrites the pointers to the cancellable implementations already in libthr. The interposition table is separate from pthreads stubs indirection table to not pull pthreads stubs into static binaries. - Postpone the malloc(3) internal mutexes initialization until libthr is loaded. This avoids recursion between calloc(3) and static pthread_mutex_t initialization. - Reinstall signal handlers with wrapper on libthr load. The _rtld_is_dlopened(3) is used to avoid useless calls to sigaction(2) when libthr is statically referenced from the main binary. In the process, fix openat(2), swapcontext(2) and setcontext(2) interposing. The libc symbols were exported at different versions than libthr interposers. Export both libc and libthr versions from libc now, with default set to the higher version from libthr. Remove unused and disconnected swapcontext(3) userspace implementation from libc/gen. No objections from: deischen Tested by: pho, antoine (exp-run) (previous versions) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=276630
* Revert r274772: it is not valid on MIPSEd Maste2014-11-252-6/+6
| | | | | | | Reported by: sbruno Notes: svn path=/head/; revision=275004
* Use canonical __PIC__ flagEd Maste2014-11-212-6/+6
| | | | | | | | | | | It is automatically set when -fPIC is passed to the compiler. Reviewed by: dim, kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D1179 Notes: svn path=/head/; revision=274772
* Switch the defaults to not split the RLIMIT_STACK-sized initial threadKonstantin Belousov2014-09-241-3/+4
| | | | | | | | | | | stack into the stacks of the created threads. Add knob LIBPTHREAD_SPLITSTACK_MAIN to restore the older behaviour. Sponsored by: The FreeBSD Foundation MFC after: 3 weeks Notes: svn path=/head/; revision=272069
* Fix typo in a comment.Rui Paulo2014-09-021-1/+1
| | | | Notes: svn path=/head/; revision=270972
* Add a knob LIBPTHREAD_BIGSTACK_MAIN, which instructs libthr to leaveKonstantin Belousov2014-08-132-1/+12
| | | | | | | | | | | | | | | | | | | | the whole RLIMIT_STACK-sized region of the kernel-allocated stack as the stack of main thread. By default, the main thread stack is clamped at 2MB (4MB on 64bit ABIs) and the rest is used for other threads stack allocation. Since there is no programmatic way to adjust the size of the main thread stack, pthread_attr_setstacksize() is too late, the knob allows user to manage the main stack size both for single-threaded and multi-threaded processes with the rlimit. Reported by: "Ivan A. Kosarev" <ivan@ivan-labs.com> Tested by: dim Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=269909
* Style.Konstantin Belousov2014-08-131-1/+1
| | | | | | | | Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=269908
* Right now, the rtld prefork hook locks the rtld bind lock in the readKonstantin Belousov2014-05-242-4/+30
| | | | | | | | | | | | | | | | | | | | | mode. This allows the binder to be functional in the child after the fork (assuming no lazy loading of a filter is needed), but other rtld services which require write lock on rtld_bind_lock cause deadlock, if called by child. Change the _rtld_atfork() to lock the bind lock in write mode, making the rtld fully functional after the fork. Pre-resolve the symbols which are called by the libthr' fork() interposer, since dynamic resolution causes deadlock due to the rtld_bind_lock already owned in the write mode. Reported and tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=266609
* In _pthread_kill(), if passed pthread is current thread, do not sendKonstantin Belousov2014-02-011-5/+8
| | | | | | | | | | | | | | the signal second time, by adding the missed else before if statement. While there, postpone initializing local curthread variable until passed signal number is checked for validity. Submitted by: John Wolfe <jlw@xinuos.com> PR: threads/186309 MFC after: 1 week Notes: svn path=/head/; revision=261354
* If check_deferred_signal() execution needs binding of PLT symbol,Konstantin Belousov2013-11-232-2/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | unlocking the rtld bind lock results in the processing of ast and recursing into the check_deferred_signal(). Nested execution of check_deferred_signal() delivers the signal to user code and clears si_signo. On return, top-level check_deferred_signal() frame continues delivering the same signal one more time, but now with zero si_signo. Fix this by adding a flag to indicate that deferred delivery is running, so check_deferred_signal() should avoid doing anything. Since user signal handler is allowed to modify the passed machine context to make return from the signal handler to cause arbitrary jump, or do longjmp(). For this case, also clear the flag in thr_sighandler(), since kernel signal delivery means that nested delivery code should not run right now. Reported by: Vitaly Magerya <vmagerya@gmail.com> Reviewed by: davidxu, jilles Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=258499
* The SUSv4tc1 requires that pthread_setcancelstate() shall be not aKonstantin Belousov2013-06-191-1/+2
| | | | | | | | | | | | cancellation point. When enabling the cancellation, only process the pending cancellation for asynchronous mode. Reported and reviewed by: Kohji Okuno <okuno.kohji@jp.panasonic.com> Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=251985
* Since the cause of the problems with the __fillcontextx() wasKonstantin Belousov2013-06-031-15/+7
| | | | | | | | | | | | identified, unify the code of check_deferred_signal() for all architectures, making the variant under #ifdef x86 common. Tested by: marius (sparc64) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=251284