aboutsummaryrefslogtreecommitdiff
path: root/lib/libthr/thread/thr_exit.c
Commit message (Collapse)AuthorAgeFilesLines
* libthr: remove explicit sys/cdefs.h includesKonstantin Belousov2024-03-061-1/+0
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-161-2/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Use uintptr_t for return type of _Unwind_GetCFA.John Baldwin2022-01-271-2/+2
| | | | | | | This matches the type in other unwind headers. Reviewed by: dim, emaste Differential Revision: https://reviews.freebsd.org/D34050
* Use an unsigned 64-bit integer for exception class.John Baldwin2022-01-271-2/+2
| | | | | | | | | | This matches the type in other unwind headers (LLVM libunwind, libcxxrt, glibc). NB: include/unwind.h is not installed but is only used by libthr Reviewed by: imp, dim, emaste Differential Revision: https://reviews.freebsd.org/D34049
* Avoid conflicts with libc symbols in libthr jump table.Konstantin Belousov2019-07-311-2/+3
| | | | | | | | | | | | | | | | | | | In some corner cases of static linking and unexpected libraries order on the linker command line, libc symbol might preempt the same libthr symbol, in which case libthr jump table points back to libc causing either infinite recursion or loop. Handle all of such symbols by using private libthr names for them, ensuring that the right pointers are installed into the table. In collaboration with: arichardson PR: 239475 Tested by: pho MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D21088 Notes: svn path=/head/; revision=350481
* Add pthread_get_name_np(3).Konstantin Belousov2018-08-171-0/+3
| | | | | | | | | | | | | | | | The function retrieves the thread name previously set by pthread_set_name_np(3). The name is cached in the process memory. Requested by: Willem Jan Withagen <wjw@digiware.nl> Man page update: Yuri Pankov <yuripv@yuripv.net> Reviewed by: ian (previous version) Discussed with: arichardson, bjk (man page) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D16702 Notes: svn path=/head/; revision=337983
* General further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-201-1/+3
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
* libthr: fix warnings from GCC when WARNS=6Eric van Gyzen2017-05-231-5/+3
| | | | | | | | | | | | | | | | | | | 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 "make tinderbox" passes this time, except for a few unrelated kernel failures. Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D10870 Notes: svn path=/head/; revision=318749
* libthr: fix warnings at WARNS=6Eric van Gyzen2017-05-191-5/+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 The last is the only functional change. Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D10808 Notes: svn path=/head/; revision=318539
* libthr: coalesce repeated #if blocksEd Maste2017-01-251-3/+0
| | | | Notes: svn path=/head/; revision=312774
* Add __cxa_thread_atexit(3) API implementation.Konstantin Belousov2016-08-061-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the backing feature to implement C++11 thread storage duration specified by the thread_local keyword. A destructor for given thread-local object is registered to be executed at the thread termination time using __cxa_thread_atexit(). Libc calls the __cxa_thread_calls_dtors() during exit(3), before finalizers and atexit functions, and libthr calls the function at the thread termination time, after the stack unwinding and thread-specific key destruction. There are several uncertainties in the API which lacks a formal specification. Among them: - is it allowed to register destructors during destructing; we allow, but limiting the nesting level. If too many iterations detected, a diagnostic is issued to stderr and thread forcibly terminates for now. - how to handle destructors which belong to an unloading dso; for now, we ignore destructor calls for such entries, and issue a diagnostic. Linux does prevent dso unload until all threads with destructors from the dso terminated. It is supposed that the diagnostics allow to detect real-world applications relying on the above details and possibly adjust our implementation. Right now the choices were to provide the slim API (but that rarely stands the practice test). Tests are added to check generic functionality and to specify some of the above implementation choices. Submitted by: Mahdi Mokhtari <mokhi64_gmail.com> Reviewed by: theraven Discussed with: dim (detection of -std=c++11 supoort for tests) Sponsored by: The FreeBSD Foundation (my involvement) MFC after: 2 weeks Differential revisions: https://reviews.freebsd.org/D7224, https://reviews.freebsd.org/D7427 Notes: svn path=/head/; revision=303795
* libthr: Use formatted PANIC()Conrad Meyer2016-06-011-6/+3
| | | | | | | | | | | | 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 Notes: svn path=/head/; revision=301136
* libthr: Add vprintf variant of _thread_printf, formatted PANIC()Conrad Meyer2016-06-011-4/+18
| | | | | | | | | | | No ABI change. Reviewed by: kib Sponsored by: EMC / Isilon Storage Division Differential Revision: https://reviews.freebsd.org/D6672 Notes: svn path=/head/; revision=301134
* Use __FBSDID() for .c files from lib/libthr/thread.Konstantin Belousov2016-04-081-2/+3
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297706
* Revert r274772: it is not valid on MIPSEd Maste2014-11-251-5/+5
| | | | | | | Reported by: sbruno Notes: svn path=/head/; revision=275004
* Use canonical __PIC__ flagEd Maste2014-11-211-5/+5
| | | | | | | | | | | It is automatically set when -fPIC is passed to the compiler. Reviewed by: dim, kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D1179 Notes: svn path=/head/; revision=274772
* change code to use unwind.h.David Xu2010-09-301-4/+4
| | | | Notes: svn path=/head/; revision=213297
* Report death event to debugger before moving to gc list, otherwiseDavid Xu2010-09-261-3/+2
| | | | | | | debugger may can not find it on thread list. Notes: svn path=/head/; revision=213182
* Because old _pthread_cleanup_push/pop do not have frame address,David Xu2010-09-251-5/+16
| | | | | | | | | it is incompatible with stack unwinding code, if they are invoked, disable stack unwinding for current thread, and when thread is exiting, print a warning message. Notes: svn path=/head/; revision=213159
* Simplify code, and in while loop, fix operator to match the unwindingDavid Xu2010-09-251-7/+4
| | | | | | | direction. Notes: svn path=/head/; revision=213154
* Because atfork lock is held while forking, a thread cancellation triggeredDavid Xu2010-09-191-1/+1
| | | | | | | by atfork handler is unsafe, use intenal flag no_cancel to disable it. Notes: svn path=/head/; revision=212841
* - _Unwind_Resume function is not used, remove it.David Xu2010-09-191-14/+8
| | | | | | | | | - Use a store barrier to make sure uwl_forcedunwind is lastest thing other threads can see. - Add some comments. Notes: svn path=/head/; revision=212838
* Fix a race condition when finding stack unwinding functions.David Xu2010-09-191-7/+20
| | | | Notes: svn path=/head/; revision=212837
* add code to support stack unwinding when thread exits. note that onlyDavid Xu2010-09-151-1/+150
| | | | | | | | | 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. Notes: svn path=/head/; revision=212630
* Convert thread list lock from mutex to rwlock.David Xu2010-09-131-10/+7
| | | | Notes: svn path=/head/; revision=212536
* Add signal handler wrapper, the reason to add it becauses there areDavid Xu2010-09-011-0/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | 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. Notes: svn path=/head/; revision=212076
* eliminate unused code.David Xu2010-08-261-12/+0
| | | | Notes: svn path=/head/; revision=211836
* Tweak code a bit to be POSIX compatible, when a cancellation requestDavid Xu2010-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. Notes: svn path=/head/; revision=211409
* Move call to _malloc_thread_cleanup() so that if this is the last thread,Jason Evans2008-09-091-3/+6
| | | | | | | | | | the call never happens. This is necessary because malloc may be used during exit handler processing. Submitted by: davidxu Notes: svn path=/head/; revision=182894
* Add thread-specific caching for small size classes, based on magazines.Jason Evans2008-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. Notes: svn path=/head/; revision=182225
* Remove libc_r's remnant code.David Xu2008-05-061-16/+0
| | | | Notes: svn path=/head/; revision=178806
* Use UMTX_OP_WAIT_UINT_PRIVATE and UMTX_OP_WAKE_PRIVATE to saveDavid Xu2008-04-291-1/+1
| | | | | | | time in kernel(avoid VM lookup). Notes: svn path=/head/; revision=178647
* Compile libthr with warnings.Ruslan Ermilov2008-03-251-0/+2
| | | | Notes: svn path=/head/; revision=177605
* - Copy signal mask out before THR_UNLOCK(), because THR_UNLOCK() may callDavid Xu2008-03-181-0/+4
| | | | | | | | | | | _thr_suspend_check() which messes sigmask saved in thread structure. - Don't suspend a thread has force_exit set. - In pthread_exit(), if there is a suspension flag set, wake up waiting- thread after setting PS_DEAD, this causes waiting-thread to break loop in suspend_common(). Notes: svn path=/head/; revision=177337
* Don't report death event to debugger if it is a forced exit.David Xu2008-03-061-1/+1
| | | | Notes: svn path=/head/; revision=176861
* call underscore version of pthread_cleanup_pop instead.David Xu2007-12-201-1/+1
| | | | Notes: svn path=/head/; revision=174806
* Remove 3rd clause, renumber, ok per emailWarner Losh2007-01-121-4/+1
| | | | Notes: svn path=/head/; revision=165967
* Eliminate atomic operations in thread cancellation functions, it shouldDavid Xu2006-11-241-2/+2
| | | | | | | reduce overheads of cancellation points. Notes: svn path=/head/; revision=164583
* WARNS level 4 cleanup.David Xu2006-04-041-4/+2
| | | | Notes: svn path=/head/; revision=157457
* Refine thread suspension code, now thread suspension is a blockableDavid Xu2006-01-051-1/+8
| | | | | | | | | | operation, the caller is blocked util target threads are really suspended, also avoid suspending a thread when it is holding a critical lock. Fix a bug in _thr_ref_delete which tests a never set flag. Notes: svn path=/head/; revision=154055
* Follow the change in kernel, joiner thread just waits at thread idDavid Xu2005-10-261-2/+5
| | | | | | | address, let kernel wake it up. Notes: svn path=/head/; revision=151694
* Add debugger event reporting support, current only TD_CREATE and TD_DEATHDavid Xu2005-04-121-1/+3
| | | | | | | events are reported. Notes: svn path=/head/; revision=144921
* Import my recent 1:1 threading working. some features improved includes:David Xu2005-04-021-118/+36
| | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=144518
* 1. Now that it's a thread's state is changed from within the kernel, whereMike Makonnen2004-10-131-1/+2
| | | | | | | | | | | | | | | | | no userland locks are heald, the dead thread lock can no longer protect access to it. Therefore, instead of using an if (!dead)...else clause after walking the active threads list test the thread pointer before deciding not to walk the dead threads list. If the thread pointer is null it means it was not found in the active threads list and the dead threads list should be checked. 2. Do not free the stack of a thread that is not marked dead. This is the 2nd and final part of eliminating the race to free a thread's stack. MFC after: 3 days Notes: svn path=/head/; revision=136483
* Remove a reference to a non-existent syscall: _thr_exit(). TheMike Makonnen2004-10-081-4/+1
| | | | | | | actual name is thr_exit(). How this ever worked is beyond me. Notes: svn path=/head/; revision=136262
* Close a race between a thread exiting and the freeing of it's stack.Mike Makonnen2004-10-061-3/+2
| | | | | | | | | | | | After some discussion the best option seems to be to signal the thread's death from within the kernel. This requires that thr_exit() take an argument. Discussed with: davidxu, deischen, marcel MFC after: 3 days Notes: svn path=/head/; revision=136192
* Make libthr async-signal-safe without costly signal masking. The guidlines IMike Makonnen2004-05-201-29/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | followed are: Only 3 functions (pthread_cancel, pthread_setcancelstate, pthread_setcanceltype) are required to be async-signal-safe by POSIX. None of the rest of the pthread api is required to be async-signal-safe. This means that only the three mentioned functions are safe to use from inside signal handlers. However, there are certain system/libc calls that are cancellation points that a caller may call from within a signal handler, and since they are cancellation points calls have to be made into libthr to test for cancellation and exit the thread if necessary. So, the cancellation test and thread exit code paths must be async-signal-safe as well. A summary of the changes follows: o Almost all of the code paths that masked signals, as well as locking the pthread structure now lock only the pthread structure. o Signals are masked (and left that way) as soon as a thread enters pthread_exit(). o The active and dead threads locks now explicitly require that signals are masked. o Access to the isdead field of the pthread structure is protected by both the active and dead list locks for writing. Either one is sufficient for reading. o The thread state and type fields have been combined into one three-state switch to make it easier to read without requiring a lock. It doesn't need a lock for writing (and therefore for reading either) because only the current thread can write to it and it is an integer value. o The thread state field of the pthread structure has been eliminated. It was an unnecessary field that mostly duplicated the flags field, but required additional locking that would make a lot more code paths require signal masking. Any truly unique values (such as PS_DEAD) have been reborn as separate members of the pthread structure. o Since the mutex and condvar pthread functions are not async-signal-safe there is no need to muck about with the wait queues when handling a signal ... o ... which also removes the need for wrapping signal handlers and sigaction(2). o The condvar and mutex async-cancellation code had to be revised as a result of some of these changes, which resulted in semi-unrelated changes which would have been difficult to work on as a separate commit, so they are included as well. The only part of the changes I am worried about is related to locking for the pthread joining fields. But, I will take a closer look at them once this mega-patch is committed. Notes: svn path=/head/; revision=129484
* Remove the garbage collector thread. All resources are freedMike Makonnen2004-03-281-10/+38
| | | | | | | | in-line. If the exiting thread cannot release a resource, then the next thread to exit will release it. Notes: svn path=/head/; revision=127523
* Implement reference counting of read-write locks. This usesMike Makonnen2004-01-191-0/+6
| | | | | | | | | | | | | | | | | | | | a list in the thread structure to keep track of the locks and how many times they have been locked. This list is checked on every lock and unlock. The traversal through the list is O(n). Most applications don't hold so many locks at once that this will become a problem. However, if it does become a problem it might be a good idea to review this once libthr is off probation and in the optimization cycle. This fixes: o deadlock when a thread tries to recursively acquire a read lock when a writer is waiting on the lock. o a thread could previously successfully unlock a lock it did not own o deadlock when a thread tries to acquire a write lock on a lock it already owns for reading or writing [ this is admittedly not required by POSIX, but is nice to have ] Notes: svn path=/head/; revision=124718
* Change all instances of THR_LOCK/UNLOCK, etc to UMTX_*.Mike Makonnen2003-07-061-2/+2
| | | | | | | | It is a more acurate description of the locks they operate on. Notes: svn path=/head/; revision=117277