aboutsummaryrefslogtreecommitdiff
path: root/lib/libthr/thread
Commit message (Collapse)AuthorAgeFilesLines
...
* The getcontext() from the __fillcontextx() call in theKonstantin Belousov2013-05-281-2/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | check_deferred_signal() returns twice, since handle_signal() emulates the return from the normal signal handler by sigreturn(2)ing the passed context. Second return is performed on the destroyed stack frame, because __fillcontextx() has already returned. This causes undefined and bad behaviour, usually the victim thread gets SIGSEGV. Avoid nested frame and the need to return from it by doing direct call to getcontext() in the check_deferred_signal() and using a new private libc helper __fillcontextx2() to complement the context with the extended CPU state if the deferred signal is still present. The __fillcontextx() is now unused, but is kept to allow older libthr.so to be used with the new libc. Mark __fillcontextx() as returning twice [1]. Reported by: pgj Pointy hat to: kib Discussed with: dim Tested by: pgj, dim Suggested by: jilles [1] MFC after: 1 week Notes: svn path=/head/; revision=251047
* Partially apply the capitalization of the heading word of the sequenceKonstantin Belousov2013-05-271-3/+3
| | | | | | | | | | and fix typo. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=251040
* Return one-based key so that user can check if the key is ever allocatedDavid Xu2013-05-161-4/+7
| | | | | | | | | in the first place. Initial patch submitted by: phk Notes: svn path=/head/; revision=250691
* Fix return value for setcontext and swapcontext.David Xu2013-05-091-4/+8
| | | | Notes: svn path=/head/; revision=250402
* Add accept4() system call.Jilles Tjoelker2013-05-011-0/+22
| | | | | | | | | | | | | | | | | | The accept4() function, compared to accept(), allows setting the new file descriptor atomically close-on-exec and explicitly controlling the non-blocking status on the new socket. (Note that the latter point means that accept() is not equivalent to any form of accept4().) The linuxulator's accept4 implementation leaves a race window where the new file descriptor is not close-on-exec because it calls sys_accept(). This implementation leaves no such race window (by using falloc() flags). The linuxulator could be fixed and simplified by using the new code. Like accept(), accept4() is async-signal-safe, a cancellation point and permitted in capability mode. Notes: svn path=/head/; revision=250154
* Remove extra code for SA_RESETHAND, it is not needed because kernel hasDavid Xu2013-04-281-7/+0
| | | | | | | already done this. Notes: svn path=/head/; revision=250013
* libthr: Fix a parameter name in an internal header file.Jilles Tjoelker2013-04-271-1/+1
| | | | Notes: svn path=/head/; revision=249985
* Remove debug code.David Xu2013-04-181-1/+0
| | | | Notes: svn path=/head/; revision=249607
* Avoid copying memory if SIGCANCEL is not masked.David Xu2013-04-181-4/+14
| | | | Notes: svn path=/head/; revision=249606
* Revert revision 249323, the PR/177624 is confusing, that bug is causedDavid Xu2013-04-181-1/+10
| | | | | | | | by using buggy getcontext/setcontext on same stack, while swapcontext normally works on different stack, there is no such a problem. Notes: svn path=/head/; revision=249604
* libthr: Remove _thr_rtld_fini(), unused since r245630.Jilles Tjoelker2013-04-122-12/+0
| | | | Notes: svn path=/head/; revision=249425
* swapcontext wrapper can not be implemented in C, the stack pointer saved inDavid Xu2013-04-101-10/+1
| | | | | | | | | the context becomes invalid when the function returns, same as setjmp, it must be implemented in assemble language, see discussions in PR misc/177624. Notes: svn path=/head/; revision=249323
* libthr: Always use the threaded rtld lock implementation.Jilles Tjoelker2013-01-182-5/+6
| | | | | | | | | | | | | | The threaded rtld lock implementation is faster even in the single-threaded case because it postpones signal handlers via THR_CRITICAL_ENTER and THR_CRITICAL_LEAVE instead of calling sigprocmask(2). As a result, exception handling becomes faster in single-threaded applications linked with libthr. Reviewed by: kib Notes: svn path=/head/; revision=245630
* In suspend_common(), don't wait for a thread which is in creation, becauseDavid Xu2012-08-275-4/+64
| | | | | | | | | | pthread_suspend_all_np() may have already suspended its parent thread. Add locking code in pthread_suspend_all_np() to only allow one thread to suspend other threads, this eliminates a deadlock where two or more threads try to suspend each others. Notes: svn path=/head/; revision=239718
* Eliminate redundant code, _thr_spinlock_init() has already been calledDavid Xu2012-08-231-3/+0
| | | | | | | in init_private(), don't call it again in fork() wrapper. Notes: svn path=/head/; revision=239609
* Implement syscall clock_getcpuclockid2, so we can get a clock idDavid Xu2012-08-171-1/+3
| | | | | | | | | | | for process, thread or others we want to support. Use the syscall to implement POSIX API clock_getcpuclock and pthread_getcpuclockid. PR: 168417 Notes: svn path=/head/; revision=239347
* Do defered mutex wakeup once.David Xu2012-08-121-0/+1
| | | | Notes: svn path=/head/; revision=239206
* MFp4:David Xu2012-08-115-24/+38
| | | | | | | | Further decreases unexpected context switches by defering mutex wakeup until internal sleep queue lock is released. Notes: svn path=/head/; revision=239200
* Don't forget to initialize return value.David Xu2012-07-201-1/+1
| | | | Notes: svn path=/head/; revision=238645
* Simplify code by replacing _thr_ref_add() with _thr_find_thread().David Xu2012-07-201-5/+1
| | | | Notes: svn path=/head/; revision=238644
* Eliminate duplicated code.David Xu2012-07-201-19/+10
| | | | Notes: svn path=/head/; revision=238643
* Don't assign same value.David Xu2012-07-202-6/+4
| | | | Notes: svn path=/head/; revision=238642
* Eliminate duplicated code.David Xu2012-07-201-29/+14
| | | | Notes: svn path=/head/; revision=238641
* Eliminate duplicated code.David Xu2012-07-201-30/+16
| | | | Notes: svn path=/head/; revision=238640
* Don't forget to release a thread reference count,David Xu2012-07-201-4/+2
| | | | | | | | | | replace _thr_ref_add() with _thr_find_thread(), so reference count is no longer needed. MFC after: 3 days Notes: svn path=/head/; revision=238637
* Return EBUSY for PTHREAD_MUTEX_ADAPTIVE_NP too when the mutex could notDavid Xu2012-05-271-0/+1
| | | | | | | | | | be acquired. PR: 168317 MFC after: 3 days Notes: svn path=/head/; revision=236135
* Create a common function lookup() to search a chan, this eliminatesDavid Xu2012-05-101-5/+9
| | | | | | | redundant SC_LOOKUP() calling. Notes: svn path=/head/; revision=235218
* Fix mis-merged line, move SC_LOOKUP() call toDavid Xu2012-05-051-1/+1
| | | | | | | upper level. Notes: svn path=/head/; revision=235068
* MFp4:David Xu2012-05-033-1/+10
| | | | | | | | | | Enqueue thread in LIFO, this can cause starvation, but it gives better performance. Use _thr_queuefifo to control the frequency of FIFO vs LIFO, you can use environment string LIBPTHREAD_QUEUE_FIFO to configure the variable. Notes: svn path=/head/; revision=234947
* Set SIGCANCEL to SIGTHR as part of some cleanup of DTrace code.George V. Neville-Neil2012-04-181-1/+1
| | | | | | | | Reviewed by: davidxu@ MFC after: 1 week Notes: svn path=/head/; revision=234424
* umtx operation UMTX_OP_MUTEX_WAKE has a side-effect that it accessesDavid Xu2012-04-052-5/+18
| | | | | | | | | | | | | | | | | | | | | | | | a mutex after a thread has unlocked it, it event writes data to the mutex memory to clear contention bit, there is a race that other threads can lock it and unlock it, then destroy it, so it should not write data to the mutex memory if there isn't any waiter. The new operation UMTX_OP_MUTEX_WAKE2 try to fix the problem. It requires thread library to clear the lock word entirely, then call the WAKE2 operation to check if there is any waiter in kernel, and try to wake up a thread, if necessary, the contention bit is set again by the operation. This also mitgates the chance that other threads find the contention bit and try to enter kernel to compete with each other to wake up sleeping thread, this is unnecessary. With this change, the mutex owner is no longer holding the mutex until it reaches a point where kernel umtx queue is locked, it releases the mutex as soon as possible. Performance is improved when the mutex is contensted heavily. On Intel i3-2310M, the runtime of a benchmark program is reduced from 26.87 seconds to 2.39 seconds, it even is better than UMTX_OP_MUTEX_WAKE which is deprecated now. http://people.freebsd.org/~davidxu/bench/mutex_perf.c Notes: svn path=/head/; revision=233912
* libthr: In the atfork handlers for signals, do not skip the last signal.Jilles Tjoelker2012-03-261-3/+3
| | | | | | | | | | | _SIG_MAXSIG works a bit unexpectedly: signals 1 till _SIG_MAXSIG are valid, both bounds inclusive. Reviewed by: davidxu MFC after: 1 week Notes: svn path=/head/; revision=233516
* Use clockid parameter instead of hard-coded CLOCK_REALTIME.David Xu2012-03-191-1/+1
| | | | | | | Reported by: pjd Notes: svn path=/head/; revision=233134
* Some software think a mutex can be destroyed after it owned it, forDavid Xu2012-03-181-7/+0
| | | | | | | | | | | | | | example, it uses a serialization point like following: pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); pthread_mutex_destroy(&muetx); They think a previous lock holder should have already left the mutex and is no longer referencing it, so they destroy it. To be maximum compatible with such code, we use IA64 version to unlock the mutex in kernel, remove the two steps unlocking code. Notes: svn path=/head/; revision=233103
* When destroying a barrier, waiting all threads exit the barrier,David Xu2012-03-162-4/+31
| | | | | | | | this makes it possible a thread received PTHREAD_BARRIER_SERIAL_THREAD immediately free memory area of the barrier. Notes: svn path=/head/; revision=233022
* Follow changes made in revision 232144, pass absolute timeout to kernel,David Xu2012-02-273-30/+37
| | | | | | | this eliminates a clock_gettime() syscall. Notes: svn path=/head/; revision=232209
* In revision 231989, we pass a 16-bit clock ID into kernel, howeverDavid Xu2012-02-251-17/+32
| | | | | | | | | | | | | | | according to POSIX document, the clock ID may be dynamically allocated, it unlikely will be in 64K forever. To make it future compatible, we pack all timeout information into a new structure called _umtx_time, and use fourth argument as a size indication, a zero means it is old code using timespec as timeout value, but the new structure also includes flags and a clock ID, so the size argument is different than before, and it is non-zero. With this change, it is possible that a thread can sleep on any supported clock, though current kernel code does not have such a POSIX clock driver system. Notes: svn path=/head/; revision=232144
* Use unused fourth argument of umtx_op to pass flags to kernel for operationDavid Xu2012-02-221-13/+3
| | | | | | | | | UMTX_OP_WAIT. Upper 16bits is enough to hold a clock id, and lower 16bits is used to pass flags. The change saves a clock_gettime() syscall from libthr. Notes: svn path=/head/; revision=231989
* Check both seconds and nanoseconds are zero, only checking nanosecondsDavid Xu2012-02-191-1/+1
| | | | | | | is zero may trigger timeout too early. It seems a copy&paste bug. Notes: svn path=/head/; revision=231906
* Make code more stable by checking NULL pointers.David Xu2012-02-111-2/+6
| | | | Notes: svn path=/head/; revision=231503
* Plug a memory leak. When a cached thread is reused, don't clear sleepDavid Xu2012-02-072-12/+19
| | | | | | | | | | queue pointers, just reuse it. PR: 164828 MFC after: 1 week Notes: svn path=/head/; revision=231106
* Use getcontextx(3) internal API instead of getcontext(2) to provideKonstantin Belousov2012-01-211-4/+13
| | | | | | | | | | | | | | | | the signal handlers with the context information in the deferrred case. Only enable the use of getcontextx(3) in the deferred signal delivery code on amd64 and i386. Sparc64 seems to have some undetermined issues with interaction of alloca(3) and signal delivery. Tested by: flo (who also provided sparc64 harware access for me), pho Discussed with: marius MFC after: 1 month Notes: svn path=/head/; revision=230430
* Pass CVWAIT flags to kernel, this should handleDavid Xu2011-11-171-3/+2
| | | | | | | | | | | | timeout correctly for pthread_cond_timedwait when it uses kernel-based condition variable. PR: 162403 Submitted by: jilles MFC after: 3 days Notes: svn path=/head/; revision=227604
* Do not set thread name to less than informative 'initial thread'.Alexander Kabaev2011-06-191-1/+0
| | | | Notes: svn path=/head/; revision=223294
* r179417 introduced a bug into pthread_once(). Previously pthread_once()Ryan Stone2011-04-201-0/+2
| | | | | | | | | | | | | | | | | | | | used a global pthread_mutex_t for synchronization. r179417 replaced that with an implementation that directly used atomic instructions and thr_* syscalls to synchronize callers to pthread_once. However, calling pthread_mutex_lock on the global mutex implicitly ensured that _thr_check_init() had been called but with r179417 this was no longer guaranteed. This meant that if you were unlucky enough to have your first call into libthr be a call to pthread_once(), you would segfault when trying to access the pointer returned by _get_curthread(). The fix is to explicitly call _thr_check_init() from pthread_once(). Reviewed by: davidxu Approved by: emaste (mentor) MFC after: 1 week Notes: svn path=/head/; revision=220888
* Introduce a non-portable function pthread_getthreadid_np(3) to retrieveJung-uk Kim2011-02-072-0/+49
| | | | | | | | | | calling thread's unique integral ID, which is similar to AIX function of the same name. Bump __FreeBSD_version to note its introduction. Reviewed by: kib Notes: svn path=/head/; revision=218414
* Fix a typo.David Xu2011-01-111-1/+1
| | | | | | | Submitted by: avg Notes: svn path=/head/; revision=217253
* For the process that already loaded libthr but still not initializedKonstantin Belousov2011-01-101-0/+26
| | | | | | | | | | threading, fall back to libc method of performing __pthread_map_stacks_exec() job. Reported and tested by: Mykola Dzham <i levsha me> Notes: svn path=/head/; revision=217224
* Implement the __pthread_map_stacks_exec() for libthr.Konstantin Belousov2011-01-094-1/+51
| | | | | | | | | | | | | | | Stack creation code is changed to call _rtld_get_stack_prot() to get the stack protection right. There is a race where thread is created during dlopen() of dso that requires executable stacks. Then, _rtld_get_stack_prot() may return PROT_READ | PROT_WRITE, but thread is still not linked into the thread list. In this case, the callback misses the thread stack, and rechecks the required protection afterward. Reviewed by: davidxu Notes: svn path=/head/; revision=217191
* Return 0 instead of garbage value.David Xu2011-01-061-2/+1
| | | | | | | Found by: clang static analyzer Notes: svn path=/head/; revision=217047