aboutsummaryrefslogtreecommitdiff
path: root/sys/sys/umtx.h
Commit message (Collapse)AuthorAgeFilesLines
* Implement process-shared locks support for libthr.so.3, withoutKonstantin Belousov2016-02-281-1/+9
| | | | | | | | | | | | | | | 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
* Use C99 array initialization, which also makes the codeKonstantin Belousov2015-10-301-1/+0
| | | | | | | | | | | | | self-documented, and eases addition of new ops. For the similar reasons, eliminate UMTX_OP_MAX. nitems() handles the only use of the symbol. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=290202
* Add missing const keyword to function parameter.Ed Schouten2015-08-031-1/+1
| | | | | | | | The umtx_key_get() function does not dereference the address off the userspace object. The pointer can safely be const. Notes: svn path=/head/; revision=286257
* The current POSIX semaphore implementation stores the _has_waiters flagJohn Baldwin2014-10-241-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | in a separate word from the _count. This does not permit both items to be updated atomically in a portable manner. As a result, sem_post() must always perform a system call to safely clear _has_waiters. This change removes the _has_waiters field and instead uses the high bit of _count as the _has_waiters flag. A new umtx object type (_usem2) and two new umtx operations are added (SEM_WAIT2 and SEM_WAKE2) to implement these semantics. The older operations are still supported under the COMPAT_FREEBSD9/10 options. The POSIX semaphore API in libc has been updated to use the new implementation. Note that the new implementation is not compatible with the previous implementation. However, this only affects static binaries (which cannot be helped by symbol versioning). Binaries using a dynamic libc will continue to work fine. SEM_MAGIC has been bumped so that mismatched binaries will error rather than corrupting a shared semaphore. In addition, a padding field has been added to sem_t so that it remains the same size. Differential Revision: https://reviews.freebsd.org/D961 Reported by: adrian Reviewed by: kib, jilles (earlier version) Sponsored by: Norse Notes: svn path=/head/; revision=273604
* Remove dead code from umtx support:Attilio Rao2014-03-181-83/+2
| | | | | | | | | | | | | | | | | | - Retire long time unused (basically always unused) sys__umtx_lock() and sys__umtx_unlock() syscalls - struct umtx and their supporting definitions - UMUTEX_ERROR_CHECK flag - Retire UMTX_OP_LOCK/UMTX_OP_UNLOCK from _umtx_op() syscall __FreeBSD_version is not bumped yet because it is expected that further breakages to the umtx interface will follow up in the next days. However there will be a final bump when necessary. Sponsored by: EMC / Isilon storage division Reviewed by: jhb Notes: svn path=/head/; revision=263318
* umtx operation UMTX_OP_MUTEX_WAKE has a side-effect that it accessesDavid Xu2012-04-051-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | 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
* In revision 231989, we pass a 16-bit clock ID into kernel, howeverDavid Xu2012-02-251-1/+1
| | | | | | | | | | | | | | | 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-0/+2
| | | | | | | | | 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
* Add declaration of umtx_copyin_timeout()Peter Holm2011-12-031-0/+1
| | | | | | | | In collaboration with: kib MFC after: 1 week Notes: svn path=/head/; revision=228220
* Use umtx_key objects to uniquely identify futexes. Private futexes inJohn Baldwin2011-02-231-0/+1
| | | | | | | | | | | | | | | different processes that happen to use the same user address in the separate processes will now be treated as distinct futexes rather than the same futex. We can now honor shared futexes properly by mapping them to a PROCESS_SHARED umtx_key. Private futexes use THREAD_SHARED umtx_key objects. In conjunction with: dchagin Reviewed by: kib MFC after: 1 week Notes: svn path=/head/; revision=218970
* Expose the umtx_key structure and API to the rest of the kernel.John Baldwin2011-02-231-0/+51
| | | | | | | MFC after: 3 days Notes: svn path=/head/; revision=218969
* MFp4:David Xu2010-12-221-3/+8
| | | | | | | | | | | | | | | | | | - 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. Notes: svn path=/head/; revision=216641
* Add user-level semaphore synchronous type, this change allows multipleDavid Xu2010-01-041-36/+12
| | | | | | | | | | | | | processes to share semaphore by using shared memory area, in simplest case, only one atomic operation is needed in userland, waiter flag is maintained by kernel and userland only checks the flag, if the flag is set, user code enters kernel and does a wakeup() call. Move type definitions into file _umtx.h to minimize compiling time. Also type names need to be prefixed with underline character, this would reduce name conflict (still in progress). Notes: svn path=/head/; revision=201472
* Add two commands to _umtx_op system call to allow a simple mutex to beDavid Xu2008-06-241-1/+3
| | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=179970
* Introduce command UMTX_OP_WAIT_UINT_PRIVATE and UMTX_OP_WAKE_PRIVATEDavid Xu2008-04-291-7/+9
| | | | | | | | to allow userland to specify that an address is not shared by multiple processes. Notes: svn path=/head/; revision=178646
* Introduce kernel based userland rwlock. Each umtx chain now has two lists,David Xu2008-04-021-1/+21
| | | | | | | | | | one for readers and one for writers, other types of synchronization object just use first list. Asked by: jeff Notes: svn path=/head/; revision=177848
* Add function UMTX_OP_WAIT_UINT, the function causes thread to wait forDavid Xu2007-11-211-1/+2
| | | | | | | an integer to be changed. Notes: svn path=/head/; revision=173800
* Backout experimental adaptive-spin umtx code.David Xu2007-06-061-2/+1
| | | | Notes: svn path=/head/; revision=170368
* Add a lwpid field into per-cpu structure, the lwpid represents currentDavid Xu2006-12-201-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | running thread's id on each cpu. This allow us to add in-kernel adaptive spin for user level mutex. While spinning in user space is possible, without correct thread running state exported from kernel, it hardly can be implemented efficiently without wasting cpu cycles, however exporting thread running state unlikely will be implemented soon as it has to design and stablize interfaces. This implementation is transparent to user space, it can be disabled dynamically. With this change, mutex ping-pong program's performance is improved massively on SMP machine. performance of mysql super-smack select benchmark is increased about 7% on Intel dual dual-core2 Xeon machine, it indicates on systems which have bunch of cpus and system-call overhead is low (athlon64, opteron, and core-2 are known to be fast), the adaptive spin does help performance. Added sysctls: kern.threads.umtx_dflt_spins if the sysctl value is non-zero, a zero umutex.m_spincount will cause the sysctl value to be used a spin cycle count. kern.threads.umtx_max_spins the sysctl sets upper limit of spin cycle count. Tested on: Athlon64 X2 3800+, Dual Xeon 5130 Notes: svn path=/head/; revision=165369
* if a thread blocked on userland condition variable isDavid Xu2006-12-041-0/+3
| | | | | | | | | | | | | pthread_cancel()ed, it is expected that the thread will not consume a pthread_cond_signal(), therefor, we use thr_wake() to mark a flag, the flag tells a thread calling do_cv_wait() in umtx code to not block on a condition variable. Thread library is expected that once a thread detected itself is in pthread_cond_wait, it will call the thr_wake() for itself in its SIGCANCEL handler. Notes: svn path=/head/; revision=164876
* Introduce userspace condition variable, since we have already POSIXDavid Xu2006-12-031-15/+25
| | | | | | | | | priority mutex implemented, it is the time to introduce this stuff, now we can use umutex and ucond together to implement pthread's condition wait/signal. Notes: svn path=/head/; revision=164839
* define UMUTEX_CONTESTED as an unsigned integer.David Xu2006-11-111-1/+1
| | | | Notes: svn path=/head/; revision=164179
* o Add keyword volatile for user mutex owner field.David Xu2006-10-171-18/+14
| | | | | | | | | o Fix type consistent problem by using type long for old umtx and wait channel. o Rename casuptr to casuword. Notes: svn path=/head/; revision=163449
* Add umtx support for 32bit process on AMD64 machine.David Xu2006-09-221-0/+1
| | | | Notes: svn path=/head/; revision=162536
* This is initial version of POSIX priority mutex support, a new userlandDavid Xu2006-08-281-10/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | mutex structure is added as following: struct umutex { __lwpid_t m_owner; uint32_t m_flags; uint32_t m_ceilings[2]; uint32_t m_spare[4]; }; The m_owner represents owner thread, it is a thread id, in non-contested case, userland can simply use atomic_cmpset_int to lock the mutex, if the mutex is contested, high order bit will be set, and userland should do locking and unlocking via kernel syscall. Flag UMUTEX_PRIO_INHERIT represents pthread's PTHREAD_PRIO_INHERIT mutex, which when contention happens, kernel should do priority propagating. Flag UMUTEX_PRIO_PROTECT indicates it is pthread's PTHREAD_PRIO_PROTECT mutex, userland should initialize m_owner to contested state UMUTEX_CONTESTED, then atomic_cmpset_int will be failure and kernel syscall should be invoked to do locking, this becauses for such a mutex, kernel should always boost the thread's priority before it can lock the mutex, m_ceilings is used by PTHREAD_PRIO_PROTECT mutex, the first element is used to boost thread's priority when it locked the mutex, second element is used when the mutex is unlocked, the PTHREAD_PRIO_PROTECT mutex's link list is kept in userland, the m_ceiling[1] is managed by thread library so kernel needn't allocate memory to keep the link list, when such a mutex is unlocked, kernel reset m_owner to UMUTEX_CONTESTED. Flag USYNC_PROCESS_SHARED indicate if the synchronization object is process shared, if the flag is not set, it saves a vm_map_lookup() call. The umtx chain is still used as a sleep queue, when a thread is blocked on PTHREAD_PRIO_INHERIT mutex, a umtx_pi is allocated to support priority propagating, it is dynamically allocated and reference count is used, it is not optimized but works well in my tests, while the umtx chain has its own locking protocol, the priority propagating protocol are all protected by sched_lock because priority propagating function is called with sched_lock held from scheduler. No visible performance degradation is found which these changes. Some parameter names in _umtx_op syscall are renamed. Notes: svn path=/head/; revision=161678
* Add user priority loaning code to support priority propagation forDavid Xu2006-08-251-1/+1
| | | | | | | | 1:1 threading's POSIX priority mutexes, the code is no-op unless priority-aware umtx code is committed. Notes: svn path=/head/; revision=161599
* WARNS level 4 cleanup, still has work to do.David Xu2006-04-041-16/+17
| | | | Notes: svn path=/head/; revision=157456
* Revert previous commit at davidxu's insistance. Instead, use __DECONSTDag-Erling Smørgrav2006-03-281-6/+10
| | | | | | | | (argh!) and rearrange the prototypes to make it clear that _umtx_op() is not deprecated. Notes: svn path=/head/; revision=157211
* The undocumented and deprecated system call _umtx_op() takes two pointerDag-Erling Smørgrav2006-03-281-3/+3
| | | | | | | | | | arguments. The first one is never used (all callers pass in 0); the second is sometimes used to pass in a struct timespec * which is used as a timeout and never modified. Constify that argument so callers can pass a const struct timespec * without jumping through hoops. Notes: svn path=/head/; revision=157206
* do umtx_wake at userland thread exit address, so that others userlandDavid Xu2005-10-261-0/+2
| | | | | | | | | | | | | threads can wait for a thread to exit, and safely assume that the thread has left userland and is no longer using its userland stack, this is necessary for pthread_join when a thread is waiting for another thread to exit which has user customized stack, after pthread_join returns, the userland stack can be reused for other purposes, without this change, the joiner thread has to spin at the address to ensure the thread is really exited. Notes: svn path=/head/; revision=151692
* Allocate umtx_q from heap instead of stack, this avoidsDavid Xu2005-03-051-0/+4
| | | | | | | page fault panic in kernel under heavy swapping. Notes: svn path=/head/; revision=143149
* unbreak libthr binary compatibility.David Xu2005-01-261-2/+2
| | | | | | | Reported by: kris Notes: svn path=/head/; revision=140858
* Revert my previous errno hack, that is certainly an issue,David Xu2005-01-181-5/+12
| | | | | | | | | | | and always has been, but the system call itself returns errno in a register so the problem is really a function of libc, not the system call. Discussed with : Matthew Dillion <dillon@apollo.backplane.com> Notes: svn path=/head/; revision=140421
* Add a cast to fix a warning.Scott Long2005-01-151-1/+1
| | | | Notes: svn path=/head/; revision=140273
* make umtx timeout relative so userland can select different clock type,David Xu2005-01-141-10/+4
| | | | | | | | e.g, CLOCK_REALTIME or CLOCK_MONOTONIC. merge umtx_wait and umtx_timedwait into single function. Notes: svn path=/head/; revision=140245
* Let _umtx_op directly return error code rather than from errno becauseDavid Xu2005-01-121-16/+6
| | | | | | | | | errno can be tampered potentially by nested signal handle. Now all error codes are returned in negative value, positive value are reserved for future expansion. Notes: svn path=/head/; revision=140102
* /* -> /*- for license, minor formatting changesWarner Losh2005-01-071-1/+1
| | | | Notes: svn path=/head/; revision=139825
* Make umtx_wait and umtx_wake more like linux futex does, it isDavid Xu2004-12-301-9/+7
| | | | | | | | | | more general than previous. It also lets me implement cancelable point in thread library. Also in theory, umtx_lock and umtx_unlock can be implemented by using umtx_wait and umtx_wake, all atomic operations can be done in userland without kernel's casuptr() function. Notes: svn path=/head/; revision=139427
* Make _umtx_op() as more general interface, the final parameter needn't beDavid Xu2004-12-251-2/+1
| | | | | | | timespec pointer, every parameter will be interpreted by its opcode. Notes: svn path=/head/; revision=139292
* 1. introduce umtx_owner to get an owner of a umtx.David Xu2004-12-251-4/+20
| | | | | | | | 2. add const qualifier to umtx_timedlock and umtx_timedwait. 3. add missing blackets in umtx do_unlock_and_wait. Notes: svn path=/head/; revision=139291
* 1. Fix race condition between umtx lock and unlock, heavy testingDavid Xu2004-12-241-4/+3
| | | | | | | | on SMP can explore the bug. 2. Let umtx_wake returns number of threads have been woken. Notes: svn path=/head/; revision=139257
* 1. make umtx sharable between processes, the way is two or more processesDavid Xu2004-12-181-3/+51
| | | | | | | | | | | | | call mmap() to create a shared space, and then initialize umtx on it, after that, each thread in different processes can use the umtx same as threads in same process. 2. introduce a new syscall _umtx_op to support timed lock and condition variable semantics. also, orignal umtx_lock and umtx_unlock inline functions now are reimplemented by using _umtx_op, the _umtx_op can use arbitrary id not just a thread id. Notes: svn path=/head/; revision=139013
* Change the thread ID (thr_id_t) used for 1:1 threading from being aMarcel Moolenaar2004-07-021-11/+10
| | | | | | | | | | | | | | | | | | | | | | | pointer to the corresponding struct thread to the thread ID (lwpid_t) assigned to that thread. The primary reason for this change is that libthr now internally uses the same ID as the debugger and the kernel when referencing to a kernel thread. This allows us to implement the support for debugging without additional translations and/or mappings. To preserve the ABI, the 1:1 threading syscalls, including the umtx locking API have not been changed to work on a lwpid_t. Instead the 1:1 threading syscalls operate on long and the umtx locking API has not been changed except for the contested bit. Previously this was the least significant bit. Now it's the most significant bit. Since the contested bit should not be tested by userland, this change is not expected to be visible. Just to be sure, UMTX_CONTESTED has been removed from <sys/umtx.h>. Reviewed by: mtm@ ABI preservation tested on: i386, ia64 Notes: svn path=/head/; revision=131431
* Catch a few places where NULL (pointer) was used where 0 (integer) wasPeter Wemm2003-12-231-1/+1
| | | | | | | expected. Notes: svn path=/head/; revision=123740
* - Remove the blocked pointer from the umtx structure.Jeff Roberson2003-06-031-1/+0
| | | | | | | | | | | - Use a hash of umtx queues to queue blocked threads. We hash on pid and the virtual address of the umtx structure. This eliminates cases where we previously held a lock across a casuptr call. Reviwed by: jhb (quickly) Notes: svn path=/head/; revision=115765
* - Add an api for doing smp safe locks in userland.Jeff Roberson2003-04-011-0/+87
- umtx_lock() is defined as an inline in umtx.h. It tries to do an uncontested acquire of a lock which falls back to the _umtx_lock() system-call if that fails. - umtx_unlock() is also an inline which falls back to _umtx_unlock() if the uncontested unlock fails. - Locks are keyed off of the thr_id_t of the currently running thread which is currently just the pointer to the 'struct thread' in kernel. - _umtx_lock() uses the proc pointer to synchronize access to blocked thread queues which are stored in the first blocked thread. Notes: svn path=/head/; revision=112904