summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread
Commit message (Collapse)AuthorAgeFilesLines
...
* Convert thread list lock from mutex to rwlock.davidxu2010-09-1315-129/+153
|
* Because POSIX does not allow EINTR to be returned from sigwait(),davidxu2010-09-101-6/+8
| | | | | | | | add a wrapper for it in libc and rework the code in libthr, the system call still can return EINTR, we keep this feature. Discussed on: thread Reviewed by: jilles
* To avoid possible race condition, SIGCANCEL is always sent except thedavidxu2010-09-081-1/+1
| | | | thread is dead.
* Fix off-by-one error in function _thr_sigact_unload, also disable thedavidxu2010-09-061-3/+5
| | | | | function, it seems some gnome application tends to crash if we unregister sigaction automatically.
* Remove incorrect comments, also make sure signal isdavidxu2010-09-011-5/+4
| | | | disabled when unregistering sigaction.
* In function __pthread_cxa_finalize(), also make code for removingdavidxu2010-09-011-1/+8
| | | | atfork handler be async-signal safe.
* pthread_atfork should acquire writer lock and protect the codedavidxu2010-09-011-1/+3
| | | | with critical region.
* Change atfork lock from mutex to rwlock, also make mutexes used by malloc()davidxu2010-09-016-28/+60
| | | | | | | | 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 signal handler wrapper, the reason to add it becauses there aredavidxu2010-09-0114-273/+591
| | | | | | | | | | | | | | | | | | | | | | | some cases we want to improve: 1) if a thread signal got a signal while in cancellation point, it is possible the TDP_WAKEUP may be eaten by signal handler if the handler called some interruptibly system calls. 2) In signal handler, we want to disable cancellation. 3) When thread holding some low level locks, it is better to disable signal, those code need not to worry reentrancy, sigprocmask system call is avoided because it is a bit expensive. The signal handler wrapper works in this way: 1) libthr installs its signal handler if user code invokes sigaction to install its handler, the user handler is recorded in internal array. 2) when a signal is delivered, libthr's signal handler is invoke, libthr checks if thread holds some low level lock or is in critical region, if it is true, the signal is buffered, and all signals are masked, once the thread leaves critical region, correct signal mask is restored and buffered signal is processed. 3) before user signal handler is invoked, cancellation is temporarily disabled, after user signal handler is returned, cancellation state is restored, and pending cancellation is rescheduled.
* Unregister thread specific data destructor when a corresponding dsodavidxu2010-08-273-0/+23
| | | | is unloaded.
* clear lock to zero state if it is destroyed.davidxu2010-08-271-0/+3
|
* eliminate unused code.davidxu2010-08-262-17/+0
|
* Decrease rdlock count only when thread unlocked a reader lock.davidxu2010-08-261-1/+1
| | | | MFC after: 3 days
* Remove unused source.kib2010-08-241-57/+0
| | | | MFC after: 2 weeks
* The __hidden definition is provided by sys/cdefs.h.kib2010-08-241-4/+0
| | | | MFC after: 2 weeks
* Add wrapper for setcontext() and swapcontext(), the wrappersdavidxu2010-08-242-14/+51
| | | | unblock SIGCANCEL which is needed by thread cancellation.
* On shared object unload, in __cxa_finalize, call and clear all installedkib2010-08-232-0/+25
| | | | | | | | | | | | | | | | | | | | | | atexit and __cxa_atexit handlers that are either installed by unloaded dso, or points to the functions provided by the dso. Use _rtld_addr_phdr to locate segment information from the address of private variable belonging to the dso, supplied by crtstuff.c. Provide utility function __elf_phdr_match_addr to do the match of address against dso executable segment. Call back into libthr from __cxa_finalize using weak __pthread_cxa_finalize symbol to remove any atfork handler which function points into unloaded object. The rtld needs private __pthread_cxa_finalize symbol to not require resolution of the weak undefined symbol at initialization time. This cannot work, since rtld is relocated before sym_zero is set up. Idea by: kan Reviewed by: kan (previous version) MFC after: 3 weeks
* Reduce redundant code.davidxu2010-08-201-76/+21
| | | | Submitted by: kib
* In current implementation, thread cancellation is done in signal handler,davidxu2010-08-206-99/+284
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | which does not know what is the state of interrupted system call, for example, open() system call opened a file and the thread is still cancelled, result is descriptor leak, there are other problems which can cause resource leak or undeterminable side effect when a thread is cancelled. However, this is no longer true in new implementation. In defering mode, a thread is canceled if cancellation request is pending and later the thread enters a cancellation point, otherwise, a later pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and causes target thread to abort system call, userland code in libthr then checks cancellation state, and cancels the thread if needed. For example, the cancellation point open(), the thread may be canceled at start, but later, if it opened a file descriptor, it is not canceled, this avoids file handle leak. Another example is read(), a thread may be canceled at start of the function, but later, if it read some bytes from a socket, the thread is not canceled, the caller then can decide if it should still enable cancelling or disable it and continue reading data until it thinks it has read all bytes of a packet, and keeps a protocol stream in health state, if user ignores partly reading of a packet without disabling cancellation, then second iteration of read loop cause the thread to be cancelled. An exception is that the close() cancellation point always closes a file handle despite whether the thread is cancelled or not. The old mechanism is still kept, for a functions which is not so easily to fix a cancellation problem, the rough mechanism is used. Reviewed by: kib@
* According to specification, function fcntl() is a cancellation point onlydavidxu2010-08-201-4/+11
| | | | when cmd argument is F_SETLKW.
* Tweak code a bit to be POSIX compatible, when a cancellation requestdavidxu2010-08-171-0/+2
| | | | | | | | | | | | is acted upon, or when a thread calls pthread_exit(), the thread first disables cancellation by setting its cancelability state to PTHREAD_CANCEL_DISABLE and its cancelability type to PTHREAD_CANCEL_DEFERRED. The cancelability state remains set to PTHREAD_CANCEL_DISABLE until the thread has terminated. It has no effect if a cancellation cleanup handler or thread-specific data destructor routine changes the cancelability state to PTHREAD_CANCEL_ENABLE.
* Use _SIG_VALID instead of expanded form of the macro.kib2010-07-121-1/+1
| | | | | Submitted by: Garrett Cooper <yanegomi gmail com> MFC after: 1 week
* Coalesce one more broken line.deischen2010-05-241-2/+1
|
* Coalesce a couple of broken lines since they can fit within 80deischen2010-05-241-4/+2
| | | | characters. Little nit found while looking at a bug report.
* remove file thr_sem_new.c.davidxu2010-01-051-1/+0
|
* Remove extra new semaphore stubs, because libc already has them, anddavidxu2010-01-051-103/+0
| | | | | | ld can find the newest version which is default. Poked by: kan@
* Use umtx to implement process sharable semaphore, to make this work,davidxu2010-01-055-218/+165
| | | | | | | | | | | | | | | | | | | | | | now type sema_t is a structure which can be put in a shared memory area, and multiple processes can operate it concurrently. User can either use mmap(MAP_SHARED) + sem_init(pshared=1) or use sem_open() to initialize a shared semaphore. Named semaphore uses file system and is located in /tmp directory, and its file name is prefixed with 'SEMD', so now it is chroot or jail friendly. In simplist cases, both for named and un-named semaphore, userland code does not have to enter kernel to reduce/increase semaphore's count. The semaphore is designed to be crash-safe, it means even if an application is crashed in the middle of operating semaphore, the semaphore state is still safely recovered by later use, there is no waiter counter maintained by userland code. The main semaphore code is in libc and libthr only has some necessary stubs, this makes it possible that a non-threaded application can use semaphore without linking to thread library. Old semaphore implementation is kept libc to maintain binary compatibility. The kernel ksem API is no longer used in the new implemenation. Discussed on: threads@
* Work-around a race condition on ia64 while unlocking a contested lock.marcel2009-12-141-0/+3
| | | | | | | | The race condition is believed to be in UMTX_OP_MUTEX_WAKE. On ia64, we simply go to the kernel to unlock. The big question is why this is only a race condition on ia64... MFC after: 3 days
* Current pselect(3) is implemented in usermode and thus vulnerable tokib2009-10-271-1/+3
| | | | | | | | | | | | | | | | | well-known race condition, which elimination was the reason for the function appearance in first place. If sigmask supplied as argument to pselect() enables a signal, the signal might be delivered before thread called select(2), causing lost wakeup. Reimplement pselect() in kernel, making change of sigmask and sleep atomic. Since signal shall be delivered to the usermode, but sigmask restored, set TDP_OLDMASK and save old mask in td_oldsigmask. The TDP_OLDMASK should be cleared by ast() in case signal was not gelivered during syscall execution. Reviewed by: davidxu Tested by: pho MFC after: 1 month
* Make openat(2) a cancellation point.jilles2009-10-112-0/+29
| | | | | | | This is required by POSIX and matches open(2). Reviewed by: kib, jhb MFC after: 1 month
* don't report error if key was deleted.davidxu2009-09-251-1/+11
| | | | PR: threads/135462
* rwlock implemented from libthr need to fall through the 'hard path' andattilio2009-09-231-2/+5
| | | | | | | | | | | | | | | | | | | | query umtx also if the shared waiters bit is set on a shared lock. The writer starvation avoidance technique, infact, can lead to shared waiters on a shared lock which can bring to a missed wakeup and thus to a deadlock if the right bit is not checked (a notable case is the writers counterpart to be handled through expired timeouts). Fix that by checking for the shared waiters bit also when unlocking the shared locks. That bug was causing a reported MySQL deadlock. Many thanks go to Nick Esborn and his employer DesertNet which provided time and machines to identify and fix this issue. PR: thread/135673 Reported by: Nick Esborn <nick at desert dot net> Tested by: Nick Esborn <nick at desert dot net> Reviewed by: jeff
* In the current code, rdlock_count is not correctly handled for some cases.attilio2009-07-062-1/+4
| | | | | | | | | | | | | | | The most notable is that it is not bumped in rwlock_rdlock_common() when the hard path (__thr_rwlock_rdlock()) returns successfully. This can lead to deadlocks in libthr when rwlocks recursion in read mode happens. Fix the interested parts by correctly handling rdlock_count. PR: threads/136345 Reported by: rink Tested by: rink Reviewed by: jeff Approved by: re (kib) MFC: 2 weeks
* These are some cosmetic changes to improve the clarity of libthr's fork ↵green2009-05-111-9/+9
| | | | implementation.
* Now that the kernel defines CACHE_LINE_SIZE in machine/param.h, userwatson2009-04-191-2/+0
| | | | | | | | that definition in the custom locking code for the run-time linker rather than local definitions. Pointed out by: tinderbox MFC after: 2 weeks
* Forcibly unlock the malloc() locks in the child process after fork(),kib2009-03-191-1/+4
| | | | | | | by temporary pretending that the process is still multithreaded. Current malloc lock primitives do nothing for singlethreaded process. Reviewed by: davidxu, deischen
* Don't ignore other fcntl functions, directly call __sys_fcntl ifdavidxu2009-03-091-1/+1
| | | | | | WITHOUT_SYSCALL_COMPAT is not defined. Reviewed by: deischen
* Don't reference non-existent __fcntl_compat if WITHOUT_SYSCALL_COMPAT is ↵davidxu2009-03-091-0/+6
| | | | | | defined. Submitted by: Pawel Worach "pawel dot worach at gmail dot com"
* When libthr and rtld start up, there are a number of magic spells castpeter2008-12-071-1/+1
| | | | | | | | | | | | | | in order to get the symbol binding state "just so". This is to allow locking to be activated and not run into recursion problems later. However, one of the magic bits involves an explicit call to _umtx_op() to force symbol resolution. It does a wakeup operation on a fake, uninitialized (ie: random contents) umtx. Since libthr isn't active, this is harmless. Nothing can match the random wakeup. However, valgrind finds this and is not amused. Normally I'd just write a suppression record for it, but the idea of passing random args to syscalls (on purpose) just doesn't feel right.
* Provide custom simple allocator for rtld locks in libthr. The allocatorkib2008-12-022-24/+24
| | | | | | | | | does not use any external symbols, thus avoiding possible recursion into rtld to resolve symbols, when called. Reviewed by: kan, davidxu Tested by: rink MFC after: 1 month
* Invoke _rtld_atfork_post earlier, before we reinitialize rtld lockskan2008-12-011-3/+4
| | | | | | | | | | | | by switching into single-thread mode. libthr ignores broken use of lock bitmaps used by default rtld locking implementation, this in turn turns lock handoff in _rtld_thread_init into NOP. This in turn makes child processes of forked multi-threaded programs to run with _thr_signal_block still in effect, with most signals blocked. Reported by: phk, kib
* Unlock the malloc() locks in the child process after fork(). This giveskib2008-11-291-0/+1
| | | | | | | | | | | | | us working malloc in the fork child of the multithreaded process. Although POSIX requires that only async-signal safe functions shall be operable after fork in multithreaded process, not having malloc lower the quality of our implementation. Tested by: rink Discussed with: kan, davidxu Reviewed by: kan MFC after: 1 month
* Add two rtld exported symbols, _rtld_atfork_pre and _rtld_atfork_post.kib2008-11-271-1/+10
| | | | | | | | | | | | | | Threading library calls _pre before the fork, allowing the rtld to lock itself to ensure that other threads of the process are out of dynamic linker. _post releases the locks. This allows the rtld to have consistent state in the child. Although child may legitimately call only async-safe functions, the call may need plt relocation resolution, and this requires working rtld. Reported and debugging help by: rink Reviewed by: kan, davidxu MFC after: 1 month (anyway, not before 7.1 is out)
* Allow psaddr_t to be widened by using thr_pread_{int,long,ptr},marcel2008-09-141-2/+2
| | | | | | | where critical. Some places still use ps_pread/ps_pwrite directly, but only need changed when byte-order comes into the picture. Also, change th_p in td_event_msg_t from a pointer type to psaddr_t, so that events also work when psaddr_t is widened.
* Move call to _malloc_thread_cleanup() so that if this is the last thread,jasone2008-09-091-3/+6
| | | | | | | the call never happens. This is necessary because malloc may be used during exit handler processing. Submitted by: davidxu
* Add thread-specific caching for small size classes, based on magazines.jasone2008-08-271-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | This caching allows for completely lock-free allocation/deallocation in the steady state, at the expense of likely increased memory use and fragmentation. Reduce the default number of arenas to 2*ncpus, since thread-specific caching typically reduces arena contention. Modify size class spacing to include ranges of 2^n-spaced, quantum-spaced, cacheline-spaced, and subpage-spaced size classes. The advantages are: fewer size classes, reduced false cacheline sharing, and reduced internal fragmentation for allocations that are slightly over 512, 1024, etc. Increase RUN_MAX_SMALL, in order to limit fragmentation for the subpage-spaced size classes. Add a size-->bin lookup table for small sizes to simplify translating sizes to size classes. Include a hard-coded constant table that is used unless custom size class spacing is specified at run time. Add the ability to disable tiny size classes at compile time via MALLOC_TINY.
* In function pthread_condattr_getpshared, store result correctly.davidxu2008-08-011-1/+1
| | | | PR: kern/126128
* Add two commands to _umtx_op system call to allow a simple mutex to bedavidxu2008-06-243-54/+106
| | | | | | | | | | | | | | 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
* Make pthread_cleanup_push() and pthread_cleanup_pop() as a pair of macros,davidxu2008-06-092-22/+48
| | | | | | | use stack space to keep cleanup information, this eliminates overhead of calling malloc() and free() in thread library. Discussed on: thread@
* Call the fcntl compatiblity wrapper from the thread library fcntl wrappersdfr2008-05-301-1/+2
| | | | | | so that they get the benefit of the (limited) forward ABI compatibility. MFC after: 1 week
OpenPOWER on IntegriCloud