summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread/thr_init.c
Commit message (Collapse)AuthorAgeFilesLines
* Fix known issues which blow up the process after dlopen("libthr.so")kib2015-01-031-2/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (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
* Switch the defaults to not split the RLIMIT_STACK-sized initial threadkib2014-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
* Add a knob LIBPTHREAD_BIGSTACK_MAIN, which instructs libthr to leavekib2014-08-131-0/+8
| | | | | | | | | | | | | | | | | 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
* libthr: Always use the threaded rtld lock implementation.jilles2013-01-181-0/+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
* In suspend_common(), don't wait for a thread which is in creation, becausedavidxu2012-08-271-0/+7
| | | | | | | 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.
* MFp4:davidxu2012-05-031-0/+4
| | | | | | | 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.
* Do not set thread name to less than informative 'initial thread'.kan2011-06-191-1/+0
|
* MFp4:davidxu2010-12-221-0/+2
| | | | | | | | | | | | | | | - 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.
* In current code, statically initialized and destroyed object havedavidxu2010-09-281-0/+6
| | | | | | | | 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.
* To support stack unwinding for cancellation points, add -fexceptions flagdavidxu2010-09-251-1/+3
| | | | | | | for them, two functions _pthread_cancel_enter and _pthread_cancel_leave are added to let thread enter and leave a cancellation point, it also makes it possible that other functions can be cancellation points in libraries without having to be rewritten in libthr.
* add code to support stack unwinding when thread exits. note that onlydavidxu2010-09-151-0/+4
| | | | | | defer-mode cancellation works, asynchrnous mode does not work because it lacks of libuwind's support. stack unwinding is not enabled unless LIBTHR_UNWIND_STACK is defined in Makefile.
* Convert thread list lock from mutex to rwlock.davidxu2010-09-131-1/+1
|
* Change atfork lock from mutex to rwlock, also make mutexes used by malloc()davidxu2010-09-011-2/+2
| | | | | | | | 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-011-7/+1
| | | | | | | | | | | | | | | | | | | | | | | 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.
* Use umtx to implement process sharable semaphore, to make this work,davidxu2010-01-051-1/+3
| | | | | | | | | | | | | | | | | | | | | | 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@
* - Reduce function call overhead for uncontended case.davidxu2008-05-291-2/+1
| | | | | - Remove unused flags MUTEX_FLAGS_* and their code. - Check validity of the timeout parameter in mutex_self_lock().
* _vfork is not in libthr, remove the reference.davidxu2008-04-161-1/+0
|
* Use cpuset defined in pthread_attr for newly created thread, for now,davidxu2008-03-051-1/+1
| | | | | | | we set scheduling parameters and cpu binding fully in userland, and because default scheduling policy is SCHED_RR (time-sharing), we set default sched_inherit to PTHREAD_SCHED_INHERIT, this saves a system call.
* implement pthread_attr_getaffinity_np and pthread_attr_setaffinity_np.davidxu2008-03-041-1/+3
|
* Add my recent work of adaptive spin mutex code. Use two environments variabledavidxu2007-10-301-1/+9
| | | | | | | | | | | | | | | | | | | | | | | | | 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.
* backout experimental adaptive spinning mutex for product use.davidxu2007-05-091-7/+0
|
* get LIBPTHREAD_ADAPTIVE_SPIN early, so it can be used for some globaldavidxu2006-12-201-2/+5
| | | | mutexes.
* Check environment variable PTHREAD_ADAPTIVE_SPIN, if it is set, usedavidxu2006-12-201-0/+4
| | | | it as a default spin cycle count.
* - Remove variable _thr_scope_system, all threads are system scope.davidxu2006-12-151-12/+4
| | | | | - Rename _thr_smp_cpus to boolean variable _thr_is_smp. - Define CPU_SPINWAIT macro for each arch, only X86 supports it.
* Eliminate atomic operations in thread cancellation functions, it shoulddavidxu2006-11-241-1/+2
| | | | reduce overheads of cancellation points.
* use rtprio_thread system call to get or set thread priority.davidxu2006-09-211-2/+2
|
* Replace internal usage of struct umtx with umutex which can supportsdavidxu2006-09-061-13/+13
| | | | real-time if we want, no functionality is changed.
* Use umutex APIs to implement pthread_mutex, member pp_mutexq is addeddavidxu2006-08-281-0/+1
| | | | | | into pthread structure to keep track of locked PTHREAD_PRIO_PROTECT mutex, no real mutex code is changed, the mutex locking and unlocking code should has same performance as before.
* Get number of CPUs and ignore spin count on single processor machine.davidxu2006-08-081-0/+3
|
* 1. Don't override underscore version of aio_suspend(), system(),davidxu2006-07-251-17/+17
| | | | | | | | | | wait(), waitpid() and usleep(), they are internal versions and should not be cancellation points. 2. Make wait3() as a cancellation point. 3. Move raise() and pause() into file thr_sig.c. 4. Add functions _sigsuspend, _sigwait, _sigtimedwait and _sigwaitinfo, remove SIGCANCEL bit in wait-set for those functions, the signal is used internally to implement thread cancellation.
* Caching scheduling policy and priority in userland, a critical but baddlydavidxu2006-07-131-0/+6
| | | | | written application is frequently changing thread priority for SCHED_OTHER policy.
* Use kernel facilities to support real-time scheduling.davidxu2006-07-121-14/+4
|
* - Use same priority range returned by kernel's sched_get_priority_min()davidxu2006-04-271-5/+13
| | | | | and sched_get_priority_max() syscalls. - Remove unused fields from structure pthread_attr.
* WARNS level 4 cleanup.davidxu2006-04-041-25/+7
|
* Remove priority mutex code because it does not work correctly,davidxu2006-03-271-2/+1
| | | | | | | | | to make it work, turnstile like mechanism to support priority propagating and other realtime scheduling options in kernel should be available to userland mutex, for the moment, I just want to make libthr be simple and efficient thread library. Discussed with: deischen, julian
* Set default contention scope to system.davidxu2006-03-201-1/+1
|
* Add some more pthread stubs so that librt can use them.deischen2006-03-051-4/+35
| | | | | | | The thread jump table has been resorted, so you need to keep libc, libpthread, and libthr in sync. Submitted by: xu
* Rework last change of pthread_once, create a function _thr_once_init todavidxu2006-02-151-2/+1
| | | | reinitialize its internal locks.
* After fork(), reinitialize internal locks for pthread_once().davidxu2006-02-151-0/+2
|
* Now, thread name is stored in kernel, userland no longer has to keep it.davidxu2006-02-051-2/+1
|
* Use macro STATIC_LIB_REQUIRE to declare a symbol should be linked intodavidxu2006-01-101-95/+68
| | | | static binary.
* 1. Retire macro SCLASS, instead simply use language keyword anddavidxu2005-12-211-0/+52
| | | | | put variables in thr_init.c. 2. Hide all global symbols which won't be exported.
* Add code to handle timer_delete(). The timer wrapper code is completelydavidxu2005-11-011-0/+1
| | | | | | rewritten, now timers created with same sigev_notify_attributes will run in same thread, this allows user to organize which timers can run in same thread to save some thread resource.
* Conditionally report initial thread event.davidxu2005-04-121-1/+2
|
* Add debugger event reporting support, current only TD_CREATE and TD_DEATHdavidxu2005-04-121-0/+2
| | | | events are reported.
* Remove unique id field which is no longer used by debugger.davidxu2005-04-061-1/+0
|
* Import my recent 1:1 threading working. some features improved includes:davidxu2005-04-021-161/+214
| | | | | | | | | | | | | | | | 1. fast simple type mutex. 2. __thread tls works. 3. asynchronous cancellation works ( using signal ). 4. thread synchronization is fully based on umtx, mainly, condition variable and other synchronization objects were rewritten by using umtx directly. those objects can be shared between processes via shared memory, it has to change ABI which does not happen yet. 5. default stack size is increased to 1M on 32 bits platform, 2M for 64 bits platform. As the result, some mysql super-smack benchmarks show performance is improved massivly. Okayed by: jeff, mtm, rwatson, scottl
* Increase the default stacksizes:marcus2005-03-061-6/+16
| | | | | | | | | 32-bit 64-bit main thread 2 MB 4 MB other threads 1 MB 2 MB Approved by: mtm Adapted from: libpthread
* Don't include sys/user.h merely for its side-effect of recursivelydas2004-11-271-1/+0
| | | | including other headers.
* Implement pthread_atfork in libthr. This is mostly from deichen'smtm2004-06-271-0/+4
| | | | | | work in libpthread. Submitted by: Dan Nelson <dnelson@allantgroup.com>
OpenPOWER on IntegriCloud