summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread/thr_mutex.c
Commit message (Collapse)AuthorAgeFilesLines
* MFC r318539 r318580 r318581 r318584 r318749 r318952 r318953 r318955vangyzen2017-06-011-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | libthr: fix warnings at WARNS=6 Fix warnings about the following when WARNS=6 (which I will commit soon): - casting away const - no previous 'extern' declaration for non-static variable - others as explained by #pragmas and comments - unused parameters libthr: disable thread-safety warnings These warnings don't make sense for code that implements the locking primitives. libthr: change CHECK_AND_INIT_RWLOCK to an inline function This was prompted by a compiler warning about 'ret' shadowing a local variable in the callers of the macro. libthr: Use CLI flags instead of pragmas to disable warnings People tweaking the build system or compilers tend to look into the Makefile and not into the source. Having some warning controls in the Makefile and some in the source code is surprising. Pragmas have the advantage that they leave the warnings enabled for more code, but that advantage isn't very relevant in these cases. libthr: fix warnings from GCC when WARNS=6 Fix warnings about: - redundant declarations - a local variable shadowing a global function (dlinfo) - an old-style function definition (with an empty parameter list) - a variable that is possibly used uninitialized libthr: prevent setcontext() from masking SIGTHR __thr_setcontext() mistakenly tested for the presence of SIGCANCEL in its local ucontext_t instead of the parameter. Therefore, if a thread calls setcontext() with a context whose signal mask contains SIGTHR (a.k.a. SIGCANCEL), that signal will be blocked, preventing the thread from being cancelled or suspended. Sponsored by: Dell EMC
* MFC r318254:jasone2017-05-141-1/+1
| | | | | | Fix __pthread_mutex_trylock() to call THR_CRITICAL_LEAVE() on failure rather than on success. This regression was introduced by r300043 (Add implementation of robust mutexes...).
* Revert r302194, there are issues with some applications after changingkib2016-06-251-4/+1
| | | | | | | | | the return value, in particular console-kit-daemon. Reported by: Ivan Klymenko <fidaj@ukr.net> Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Approved by: re (gjb)
* For pthread_mutex_trylock() call on owned error-check or non-portablekib2016-06-251-2/+5
| | | | | | | | | | adaptive mutex, return EDEADLK as required by POSIX. The pthread_mutex_lock() is already compliant. Tested by: Guy Yur <guyyur@gmail.com> Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Approved by: re (gjb)
* libthr: Use formatted PANIC()cem2016-06-011-17/+6
| | | | | | | | | No functional change, although _thread_printf() may be slightly less functional or render some values differently from libc snprintf(3). No ABI change. Reviewed by: kib Sponsored by: EMC / Isilon Storage Division Differential Revision: https://reviews.freebsd.org/D6672
* Add implementation of robust mutexes, hopefully close enough to thekib2016-05-171-103/+277
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* If off-page lookup failed, there is no memory to performkib2016-04-121-1/+2
| | | | | | shared_mutex_init() upon. Sponsored by: The FreeBSD Foundation
* Use __FBSDID() for .c files from lib/libthr/thread.kib2016-04-081-2/+3
| | | | Sponsored by: The FreeBSD Foundation
* Assert that the lock objects put into the off-page, fit into the page.kib2016-04-081-0/+3
| | | | Sponsored by: The FreeBSD Foundation
* Apparently there are some popular programs around which assume that itkib2016-03-221-2/+53
| | | | | | | | | | | | | | | | | 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
* Provide more information on failing checks in mutex_assert_is_owned()kib2016-03-211-4/+16
| | | | | | | and mutex_assert_not_owned(). snprintf() use in this context should be safe. Sponsored by: The FreeBSD Foundation
* Implement process-shared locks support for libthr.so.3, withoutkib2016-02-281-178/+338
| | | | | | | | | | | | 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
* Propagate errors from _thr_umutex_unlock2 through mutex_unlock_common.kib2015-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
* MFp4:davidxu2012-08-111-8/+8
| | | | | Further decreases unexpected context switches by defering mutex wakeup until internal sleep queue lock is released.
* Return EBUSY for PTHREAD_MUTEX_ADAPTIVE_NP too when the mutex could notdavidxu2012-05-271-0/+1
| | | | | | | be acquired. PR: 168317 MFC after: 3 days
* Return 0 instead of garbage value.davidxu2011-01-061-2/+1
| | | | Found by: clang static analyzer
* Always clear flag PMUTEX_FLAG_DEFERED when unlocking, as it is onlydavidxu2010-12-241-2/+2
| | | | significant for lock owner.
* MFp4:davidxu2010-12-221-82/+120
| | | | | | | | | | | | | | | - Add flags CVWAIT_ABSTIME and CVWAIT_CLOCKID for umtx kernel based condition variable, this should eliminate an extra system call to get current time. - Add sub-function UMTX_OP_NWAKE_PRIVATE to wake up N channels in single system call. Create userland sleep queue for condition variable, in most cases, thread will wait in the queue, the pthread_cond_signal will defer thread wakeup until the mutex is unlocked, it tries to avoid an extra system call and a extra context switch in time window of pthread_cond_signal and pthread_mutex_unlock. The changes are part of process-shared mutex project.
* Remove locking and unlock in pthread_mutex_destroy, becausedavidxu2010-10-271-25/+2
| | | | | it can not fix race condition in application code, as a result, the problem described in PR threads/151767 is avoided.
* Check invalid mutex in _mutex_cv_unlock.davidxu2010-09-291-0/+6
|
* In current code, statically initialized and destroyed object havedavidxu2010-09-281-119/+98
| | | | | | | | same null value, the code can not distinguish between them, to fix the problem, now a destroyed object is assigned to a non-null value, and it will be rejected by some pthread functions. PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP is changed to number 1, so that adaptive mutex can be statically initialized correctly.
* Change atfork lock from mutex to rwlock, also make mutexes used by malloc()davidxu2010-09-011-5/+23
| | | | | | | | module private type, when private type mutex is locked/unlocked, thread critical region is entered or leaved. These changes makes fork() async-signal safe which required by POSIX. Note that user's atfork handler still needs to be async-signal safe, but it is not problem of libthr, it is user's responsiblity.
* Add two commands to _umtx_op system call to allow a simple mutex to bedavidxu2008-06-241-35/+33
| | | | | | | | | | | | | | locked and unlocked completely in userland. by locking and unlocking mutex in userland, it reduces the total time a mutex is locked by a thread, in some application code, a mutex only protects a small piece of code, the code's execution time is less than a simple system call, if a lock contention happens, however in current implemenation, the lock holder has to extend its locking time and enter kernel to unlock it, the change avoids this disadvantage, it first sets mutex to free state and then enters kernel and wake one waiter up. This improves performance dramatically in some sysbench mutex tests. Tested by: kris Sounds great: jeff
* - Reduce function call overhead for uncontended case.davidxu2008-05-291-205/+103
| | | | | - Remove unused flags MUTEX_FLAGS_* and their code. - Check validity of the timeout parameter in mutex_self_lock().
* Increase the default MUTEX_ADAPTIVE_SPINS to 2000, after furtherkris2008-04-261-1/+1
| | | | | | | | testing it turns out 200 was too short to give good adaptive performance. Reviewed by: jeff MFC after: 1 week
* Fixed mis-implementation of pthread_mutex_get{spin,yield}loops_np().ru2008-03-251-4/+7
| | | | Reviewed by: davidxu
* _pthread_mutex_isowned_np(): use a more reliable method; the current codedes2008-02-141-1/+1
| | | | | | will work in simple cases, but may fail in more complicated ones. Reviewed by: davidxu
* Remove unnecessary prototype.des2008-02-061-1/+0
|
* Per discussion on -threads, rename _islocked_np() to _isowned_np().des2008-02-061-3/+3
|
* After careful consideration (and a brief discussion with attilio@), changedes2008-02-041-1/+1
| | | | | | | | | the semantics of pthread_mutex_islocked_np() to return true if and only if the mutex is held by the current thread. Obviously, change the regression test to match. MFC after: 2 weeks
* Add pthread_mutex_islocked_np(), a cheap way to verify that a mutex isdes2008-02-031-0/+16
| | | | | | | locked. This is intended primarily to support the userland equivalent of the various *_ASSERT_LOCKED() macros we have in the kernel. MFC after: 2 weeks
* Add function prototypes.davidxu2007-12-171-1/+7
|
* 1. Add function pthread_mutex_setspinloops_np to turn a mutex's spindavidxu2007-12-141-29/+104
| | | | | | | | loop count. 2. Add function pthread_mutex_setyieldloops_np to turn a mutex's yield loop count. 3. Make environment variables PTHREAD_SPINLOOPS and PTHREAD_YIELDLOOPS to be only used for turnning PTHREAD_MUTEX_ADAPTIVE_NP mutex.
* Enclose all code for macro ENQUEUE_MUTEX in do while statement, anddavidxu2007-12-111-5/+7
| | | | | | add missing brackets. MFC: after 1 day
* Fix pointer dereferencing problems in _pthread_mutex_init_calloc_cb() thatjasone2007-11-281-7/+3
| | | | were obscured by pseudo-opaque pthreads API pointer casting.
* Add _pthread_mutex_init_calloc_cb() to libthr and libkse, so that malloc(3)jasone2007-11-271-6/+27
| | | | | (part of libc) can use pthreads mutexes without causing infinite recursion during initialization.
* Convert ceiling type to unsigned integer before comparing, fix compilerdavidxu2007-11-211-3/+3
| | | | warnings.
* Avoid doing adaptive spinning for priority protected mutex, currentdavidxu2007-10-311-2/+5
| | | | implementation always does lock in kernel.
* Don't do adaptive spinning if it is running on UP kernel.davidxu2007-10-311-3/+5
|
* Restore revision 1.55, the kris's adaptive mutex type.davidxu2007-10-311-14/+36
|
* Adaptive mutexes should have the same deadlock detection properties thatkris2007-10-301-0/+1
| | | | | | default (errorcheck) mutexes do. Noticed by: davidxu
* Add my recent work of adaptive spin mutex code. Use two environments variabledavidxu2007-10-301-45/+37
| | | | | | | | | | | | | | | | | | | | | | | | | to tune pthread mutex performance: 1. LIBPTHREAD_SPINLOOPS If a pthread mutex is being locked by another thread, this environment variable sets total number of spin loops before the current thread sleeps in kernel, this saves a syscall overhead if the mutex will be unlocked very soon (well written application code). 2. LIBPTHREAD_YIELDLOOPS If a pthread mutex is being locked by other threads, this environment variable sets total number of sched_yield() loops before the currrent thread sleeps in kernel. if a pthread mutex is locked, the current thread gives up cpu, but will not sleep in kernel, this means, current thread does not set contention bit in mutex, but let lock owner to run again if the owner is on kernel's run queue, and when lock owner unlocks the mutex, it does not need to enter kernel and do lots of work to resume mutex waiters, in some cases, this saves lots of syscall overheads for mutex owner. In my practice, sometimes LIBPTHREAD_YIELDLOOPS can massively improve performance than LIBPTHREAD_SPINLOOPS, this depends on application. These two environments are global to all pthread mutex, there is no interface to set them for each pthread mutex, the default values are zero, this means spinning is turned off by default.
* Add a new "non-portable" mutex type, PTHREAD_MUTEX_ADAPTIVE_NP. Thiskris2007-10-291-0/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | is also implemented in glibc and is used by a number of existing applications (mysql, firefox, etc). This mutex type is a default mutex with the additional property that it spins briefly when attempting to acquire a contested lock, doing trylock operations in userland before entering the kernel to block if eventually unsuccessful. The expectation is that applications requesting this mutex type know that the mutex is likely to be only held for very brief periods, so it is faster to spin in userland and probably succeed in acquiring the mutex, than to enter the kernel and sleep, only to be woken up almost immediately. This can help significantly in certain cases when pthread mutexes are heavily contended and held for brief durations (such as mysql). Spin up to 200 times before entering the kernel, which represents only a few us on modern CPUs. No performance degradation was observed with this value and it is sufficient to avoid a large performance drop in mysql performance in the heavily contended pthread mutex case. The libkse implementation is a NOP. Reviewed by: jeff MFC after: 3 days
* backout experimental adaptive spinning mutex for product use.davidxu2007-05-091-1/+0
|
* Insert mutex at tail if it has highest ceiling.davidxu2007-01-051-1/+1
|
* Oops, don't corrupt the list.davidxu2007-01-051-1/+1
|
* Check if the PP mutex is recursive, if we have already locked it, place thedavidxu2007-01-051-9/+28
| | | | mutex in right order sorted by priority ceiling.
* Check environment variable PTHREAD_ADAPTIVE_SPIN, if it is set, usedavidxu2006-12-201-0/+1
| | | | it as a default spin cycle count.
* Create inline function _thr_umutex_trylock2 to only try one atomicdavidxu2006-12-141-3/+3
| | | | | operation, if it is failed, we call syscall directly, this saves one atomic operation per lock contention.
* Move code calculating new inherited priority into single function.davidxu2006-11-111-30/+21
|
OpenPOWER on IntegriCloud