summaryrefslogtreecommitdiff
path: root/lib/libpthread/thread/thr_kern.c
Commit message (Collapse)AuthorAgeFilesLines
* Replace a comment with more accurated one, memory heap is now protected byDavid Xu2003-12-191-3/+2
| | | | | | | new fork() wrapper. Notes: svn path=/head/; revision=123668
* Rename _thr_enter_cancellation_point to _thr_cancel_enter, renameDavid Xu2003-12-091-18/+11
| | | | | | | | | | | | | | | | | | | | | | | | _thr_leave_cancellation_point to _thr_cancel_leave, add a parameter to _thr_cancel_leave to indicate whether cancellation point should be checked, this gives us an option to not check cancallation point if a syscall successfully returns to avoid any leaks, current I have creat(), open() and fcntl(F_DUPFD) to not check cancellation point after they sucessfully returned. Replace some members in structure kse with bit flags to same some memory. Conditionally compile THR_ASSERT to nothing if _PTHREAD_INVARIANTS is not defined. Inline some small functions in thr_cancel.c. Use __predict_false in thr_kern.c for some executed only once code. Reviewd by: deischen Notes: svn path=/head/; revision=123312
* 1.Macro optimizing KSE_LOCK_ACQUIRE and THR_LOCK_ACQUIRE to use static fallDavid Xu2003-11-291-91/+24
| | | | | | | | | | | | | | | | | through branch predict as suggested in INTEL IA32 optimization guide. 2.Allocate siginfo arrary separately to avoid pthread to be allocated at 2K boundary, which hits L1 address alias problem and causes context switch to be slow down. 3.Simplify context switch code by removing redundant code, code size is reduced, so it is expected to run faster. Reviewed by: deischen Approved by: re (scottl) Notes: svn path=/head/; revision=123048
* If a thread in critical region got a synchronous signal, according currentDavid Xu2003-11-091-0/+2
| | | | | | | | | signal handling mode, there is no chance to handle the signal, something must be wrong in the library, just call kse_thr_interrupt to dump its core. I have the code for a long time, but forgot to commit it. Notes: svn path=/head/; revision=122338
* Add an implementation for pthread_atfork().Daniel Eischen2003-11-041-1/+15
| | | | | | | | | | | Aside from the POSIX requirements for pthread_atfork(), when fork()ing, take the malloc lock to keep malloc state consistent in the child. Reviewed by: davidxu Notes: svn path=/head/; revision=122075
* Complete cancellation support for M:N threads, check cancelling flag whenDavid Xu2003-10-081-69/+156
| | | | | | | | | | thread state is changed from RUNNING to WAIT state and do some cancellation operations for every cancellable state. Reviewed by: deischen Notes: svn path=/head/; revision=120896
* When concurrency level is reduced and a kse is exiting, make sure no otherDavid Xu2003-09-291-0/+13
| | | | | | | | | threads are still referencing the kse by migrating them to initial kse. Reviewed by: deischen Notes: svn path=/head/; revision=120567
* Remove unused variable.David Xu2003-09-281-2/+0
| | | | Notes: svn path=/head/; revision=120554
* Free thread name memory if there is.David Xu2003-09-231-0/+4
| | | | Notes: svn path=/head/; revision=120370
* Make KSE_STACKSIZE machine dependent by moving it from thr_kern.c toMarcel Moolenaar2003-09-191-2/+0
| | | | | | | | | pthread_md.h. This commit only moves the definition; it does not change it for any of the platforms. This more easily allows 64-bit architectures (in particular) to pick a slightly larger stack size. Notes: svn path=/head/; revision=120263
* Fix a typo. Also turn on PTHREAD_SCOPE_SYSTEM after fork().David Xu2003-09-161-1/+2
| | | | Notes: svn path=/head/; revision=120109
* 1. Allocating and freeing lock related resource in _thr_alloc and _thr_freeDavid Xu2003-09-141-73/+44
| | | | | | | | | | | | | | to avoid potential memory leak, also fix a bug in pthread_create, contention scope should be inherited when PTHREAD_INHERIT_SCHED is set, and also check right field for PTHREAD_INHERIT_SCHED, scheduling inherit flag is in sched_inherit. 2. Execute hooks registered by atexit() on thread stack but not on scheduler stack. 3. Simplify some code in _kse_single_thread by calling xxx_destroy functions. Reviewed by: deischen Notes: svn path=/head/; revision=120074
* Add code to support barrier synchronous object and implementDavid Xu2003-09-041-2/+5
| | | | | | | | | pthread_mutex_timedlock(). Reviewed by: deischen Notes: svn path=/head/; revision=119736
* Allow hooks registered by atexit() to run with current thread pointer set,David Xu2003-09-041-1/+4
| | | | | | | without this change, my atexit test dumps core. Notes: svn path=/head/; revision=119732
* Move kse_wakeup_multi call to just before KSE_SCHED_UNLOCK.David Xu2003-09-031-4/+2
| | | | | | | Tested on: SMP Notes: svn path=/head/; revision=119700
* Allow the concurrency level to be reduced.Daniel Eischen2003-08-301-7/+8
| | | | | | | Reviewed by: davidxu Notes: svn path=/head/; revision=119577
* Treat initial thread as scope system thread when KSE mode is not activatedDavid Xu2003-08-181-15/+9
| | | | | | | | | | | | yet, so we can protect some locking code from being interrupted by signal handling. When KSE mode is turned on, reset the thread flag to scope process except we are running in 1:1 mode which we needn't turn it off. Also remove some unused member variables in structure kse. Tested by: deischen Notes: svn path=/head/; revision=119063
* Keep initial kse and kse group just like we keep initial thread,David Xu2003-08-161-9/+11
| | | | | | | | | Don't free them, so some code can still reference them. Reviewed by: deischen Notes: svn path=/head/; revision=118985
* Always set tcb for bound thread, and switch tcb for M:N thread at correctDavid Xu2003-08-131-7/+18
| | | | | | | time. Notes: svn path=/head/; revision=118850
* Correctly set current tcb. This fixes some IA64/KSE problems.David Xu2003-08-121-17/+11
| | | | | | | Reviewed by: deischen, julian Notes: svn path=/head/; revision=118817
* Initialize rtld lock just before turning on thread mode andDavid Xu2003-08-101-19/+33
| | | | | | | uninitialize rtld lock after thread mode shutdown. Notes: svn path=/head/; revision=118747
* o Add code to GC freed KSEs and KSE groupsDavid Xu2003-08-081-25/+116
| | | | | | | | | | o Fix a bug in kse_free_unlocked(), kcb_dtor shouldn't be called because the KSE is cached and will be resued in _kse_alloc(). Reviewed by: deischen Notes: svn path=/head/; revision=118676
* Don't call kse_set_curthread() when scheduling a new boundDaniel Eischen2003-08-061-2/+3
| | | | | | | | | | thread. It should only be called by the current kse and never by a KSE on behalf of another. Submitted by: davidxu Notes: svn path=/head/; revision=118516
* Rethink the MD interfaces for libpthread to account forDaniel Eischen2003-08-051-156/+160
| | | | | | | | | | | | archs that can (or are required to) have per-thread registers. Tested on i386, amd64; marcel is testing on ia64 and will have some follow-up commits. Reviewed by: davidxu Notes: svn path=/head/; revision=118510
* Move idle kse wakeup to outside of regions where locks are held.Daniel Eischen2003-07-231-35/+57
| | | | | | | | | | | This eliminates ping-ponging of locks, where the idle KSE wakes up only to find the lock it needs is being held. This gives little or no gain to M:N mode but greatly speeds up 1:1 mode. Reviewed & Tested by: davidxu Notes: svn path=/head/; revision=117907
* Cleanup thread accounting. Don't reset a threads timesliceDaniel Eischen2003-07-181-36/+21
| | | | | | | | | | | when it blocks; it only gets reset when it yields. Properly set a thread's default stack guardsize. Reviewed by: davidxu Notes: svn path=/head/; revision=117715
* o Eliminate upcall for PTHREAD_SYSTEM_SCOPE thread, now itDavid Xu2003-07-171-104/+217
| | | | | | | | | | | | | | | | is system bound thread and when it is blocked, no upcall is generated. o Add ability to libkse to allow it run in pure 1:1 threading mode, defining SYSTEM_SCOPE_ONLY in Makefile can turn on this option. o Eliminate code for installing dummy signal handler for sigwait call. o Add hash table to find thread. Reviewed by: deischen Notes: svn path=/head/; revision=117706
* Restore signal mask correctly after fork().David Xu2003-07-091-1/+2
| | | | Notes: svn path=/head/; revision=117345
* Save and restore thread's error code around signal handling.David Xu2003-07-091-1/+2
| | | | | | | Reviewed by: deischen Notes: svn path=/head/; revision=117344
* Check if thread is in critical region, only testing check_pendingDavid Xu2003-07-031-1/+2
| | | | | | | is not enough. Notes: svn path=/head/; revision=117193
* Because there are only _SIG_MAXSIG elements in thread siginfo array,David Xu2003-06-301-1/+1
| | | | | | | use [signal number - 1] as subscript to access the array. Notes: svn path=/head/; revision=117066
* Remove surplus unlocking code I accidentally checked in. This won't beDavid Xu2003-06-301-4/+0
| | | | | | | triggered until LDT entry is exhausted. Notes: svn path=/head/; revision=117063
* o Use a daemon thread to monitor signal events in kernel, if pendingDavid Xu2003-06-281-96/+138
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | signals were changed in kernel, it will retrieve the pending set and try to find a thread to dispatch the signal. The dispatching process can be rolled back if the signal is no longer in kernel. o Create two functions _thr_signal_init() and _thr_signal_deinit(), all signal action settings are retrieved from kernel when threading mode is turned on, after a fork(), child process will reset them to user settings by calling _thr_signal_deinit(). when threading mode is not turned on, all signal operations are direct past to kernel. o When a thread generated a synchoronous signals and its context returned from completed list, UTS will retrieve the signal from its mailbox and try to deliver the signal to thread. o Context signal mask is now only used when delivering signals, thread's current signal mask is always the one in pthread structure. o Remove have_signals field in pthread structure, replace it with psf_valid in pthread_signal_frame. when psf_valid is true, in context switch time, thread will backout itself from some mutex/condition internal queues, then begin to process signals. when a thread is not at blocked state and running, check_pending indicates there are signals for the thread, after preempted and then resumed time, UTS will try to deliver signals to the thread. o At signal delivering time, not only pending signals in thread will be scanned, process's pending signals will be scanned too. o Change sigwait code a bit, remove field sigwait in pthread_wait_data, replace it with oldsigmask in pthread structure, when a thread calls sigwait(), its current signal mask is backuped to oldsigmask, and waitset is copied to its signal mask and when the thread gets a signal in the waitset range, its current signal mask is restored from oldsigmask, these are done in atomic fashion. o Two additional POSIX APIs are implemented, sigwaitinfo() and sigtimedwait(). o Signal code locking is better than previous, there is fewer race conditions. o Temporary disable most of code in _kse_single_thread as it is not safe after fork(). Notes: svn path=/head/; revision=116977
* Untangle the inter-dependency of kse types and ksd types/functionsMarcel Moolenaar2003-06-231-1/+1
| | | | | | | | | | | | | | | | | | | | | | by moving the definition of struct ksd to pthread_md.h and removing the inclusion of ksd.h from thr_private.h (which has the definition of struct kse and kse_critical_t). This allows ksd.h to have inline functions that use struct kse and kse_critical_t and generally yields a cleaner implementation at the cost of not having all ksd related types/definitions in one header. Implement the ksd functionality on ia64 by using inline functions and permanently remove ksd.c from the ia64 specific makefile. This change does not clean up the i386 specific version of ksd.h. NOTE: The ksd code on ia64 abuses the tp register in the same way as it is abused in libthr in that it is incompatible with the runtime specification. This will be address when support for TLS hits the tree. Notes: svn path=/head/; revision=116771
* Change the definition of _ksd_curkse, _ksd_curthread andMarcel Moolenaar2003-06-231-3/+3
| | | | | | | | | | | | _ksd_readandclear_tmbx to be function-like. That way we can define them as inline functions or create prototypes for them. This change allows the ksd interface on ia64 to be fully inlined. Notes: svn path=/head/; revision=116719
* Insert threads at the end of the free thread list so thatDaniel Eischen2003-06-081-1/+1
| | | | | | | | | | the chance of getting the same thread id when allocating a new thread is reduced. This won't work if the application creates a new thread for every time a thread exits, but we're still within the allowances of POSIX. Notes: svn path=/head/; revision=116060
* KMF_DONE is now in /sys/sys/kse.h, no longer need to define it here.David Xu2003-06-041-4/+0
| | | | Notes: svn path=/head/; revision=115798
* Change low-level locking a bit so that we can tell ifDaniel Eischen2003-05-241-92/+175
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | a lock is being waitied on. Fix a races in join and cancellation. When trying to wait on a CV and the library is not yet threaded, make it threaded so that waiting actually works. When trying to nanosleep() and we're not threaded, just call the system call nanosleep instead of adding the thread to the wait queue. Clean up adding/removing new threads to the "all threads queue", assigning them unique ids, and tracking how many active threads there are. Do it all when the thread is added to the scheduling queue instead of making pthread_create() know how to do it. Fix a race where a thread could be marked for signal delivery but it could be exited before we actually add the signal to it. Other minor cleanups and bug fixes. Submitted by: davidxu Approved by: re@ (blanket for libpthread) Notes: svn path=/head/; revision=115278
* Eek, staticize a couple of functions that shouldn'tDaniel Eischen2003-05-191-1/+6
| | | | | | | | | | | | | | | be external (initialize()!). Remove cancellation points from _pthread_cond_wait and _pthread_cond_timedwait (single underscore versions are libc private functions). Point the weak reference(!) for these functions to the versions with cancellation points. Approved by: re@(blanket till 5/19) Pointed out by: kan (cancellation point bug) Notes: svn path=/head/; revision=115173
* Add a method of yielding the current thread with the schedulerDaniel Eischen2003-05-161-116/+157
| | | | | | | | | | | | | | | | | | | | | | | | | | lock held (_thr_sched_switch_unlocked()) and use this to avoid dropping the scheduler lock and having the scheduler retake the same lock again. Add a better way of detecting if a low-level lock is in use. When switching out a thread due to blocking in the UTS, don't switch to the KSE's scheduler stack only to switch back to another thread. If possible switch to the new thread directly from the old thread and avoid the overhead of the extra context switch. Check for pending signals on a thread when entering the scheduler and add them to the threads signal frame. This includes some other minor signal fixes. Most of this was a joint effor between davidxu and myself. Reviewed by: davidxu Approved by: re@ (blanket for libpthread) Notes: svn path=/head/; revision=115080
* call dump_queues() only when DEBUG_THREAD_KERN is defined, save someDavid Xu2003-05-051-0/+2
| | | | | | | cpu cycles. Notes: svn path=/head/; revision=114688
* Fix suspend and resume.Daniel Eischen2003-05-041-16/+28
| | | | | | | Submitted (in part) by: Kazuaki Oda <kaakun@highway.ne.jp> Notes: svn path=/head/; revision=114664
* Move the mailbox to the beginning of the thread and align theDaniel Eischen2003-04-301-3/+9
| | | | | | | thread so that the context (SSE FPU state) is also aligned. Notes: svn path=/head/; revision=114295
* Call kse_wakeup_mutli() after remove current thread from RUNQ to avoidDavid Xu2003-04-301-1/+1
| | | | | | | doing unnecessary idle kse wakeup. Notes: svn path=/head/; revision=114267
* Call kse_wakeup_multi() to wakeup idle KSEs when there are threads readyDavid Xu2003-04-301-0/+1
| | | | | | | to run. Notes: svn path=/head/; revision=114266
* o Don't add a scope system thread's KSE to the list of availableDaniel Eischen2003-04-281-40/+121
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | KSEs when it's thread exits; allow the GC handler to do that. o Make spinlock/spinlock critical regions. The following were submitted by davidxu o Alow thr_switch() to take a null mailbox argument. o Better protect cancellation checks. o Don't set KSE specific data when creating new KSEs; rely on the first upcall of the KSE to set it. o Add the ability to set the maximum concurrency level and do this automatically. We should have a way to enable/disable this with some sort of tunable because some applications may not want this to be the default. o Hold the scheduling lock across thread switch calls. o If scheduling of a thread fails, make sure to remove it from the list of active threads. o Better protect accesses to a joining threads when the target thread is exited and detached. o Remove some macro definitions that are now provided by <sys/kse.h>. o Don't leave the library in threaded mode if creation of the initial KSE fails. o Wakeup idle KSEs when there are threads ready to run. o Maintain the number of threads active in the priority queue. Notes: svn path=/head/; revision=114187
* Protect thread errno from being changed while operatingDaniel Eischen2003-04-231-0/+31
| | | | | | | | | | | on behalf of the KSE. Add a kse_reinit function to reinitialize a reused KSE. Submitted by: davidxu Notes: svn path=/head/; revision=113942
* Set the quantum for scope system threads to 0 (no quantum).Daniel Eischen2003-04-221-0/+2
| | | | Notes: svn path=/head/; revision=113881
* Add a couple asserts to pthread_cond_foo to ensure the (low-level)Daniel Eischen2003-04-221-19/+45
| | | | | | | | | | | | | | | | | | lock level is 0. Thus far, the threads implementation doesn't use mutexes or condition variables so the lock level should be 0. Save the return value when trying to schedule a new thread and use this to return an error from pthread_create(). Change the max sleep time for an idle KSE to 1 minute from 2 minutes. Maintain a count of the number of KSEs within a KSEG. With these changes scope system threads seem to work, but heavy use of them crash the kernel (supposedly VM bugs). Notes: svn path=/head/; revision=113870
* Add an i386-specifc hack to always set %gs. There still seemsDaniel Eischen2003-04-211-155/+231
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | to be instances where the kernel doesn't properly save and/or restore it. Use noupcall and nocompleted flags in the KSE mailbox. These require kernel changes to work which will be committed sometime later. Things still work without the changes. Remove the general kse entry function and use two different functions -- one for scope system threads and one for scope process threads. The scope system function is not yet enabled and we use the same function for all threads at the moment. Keep a copy of the KSE stack for the case that a KSE runs a scope system thread and uses the same stack as the thread (no upcalls are generated, so a separate stack isn't needed). This isn't enabled yet. Use a separate field for the KSE waiting flag. It isn't correct to use the mailbox flags field. The following fixes were provided by David Xu: o Initialize condition variable locks with thread versions of the low-level locking functions instead of the kse versions. o Enable threading before creating the first thread instead of after. o Don't enter critical regions when trying to malloc/free or call functions that malloc/free. o Take the scheduling lock when inheriting thread attributes. o Check the attribute's stack pointer instead of the attributes stack size for null when allocating a thread's stack. o Add a kseg reinit function so we don't have to destroy and then recreate the same lock. o Check the return value of kse_create() and return an appropriate error if it fails. o Don't forget to destroy a thread's locks when freeing it. o Examine the correct flags word for checking to see if a thread is in a synchronization queue. Things should now work on an SMP kernel. Notes: svn path=/head/; revision=113786