mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-21 03:48:29 +00:00
Make threads faster and more reliable
This change doubles the performance of thread spawning. That's thanks to our new stack manager, which allows us to avoid zeroing stacks. It gives us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is faster now, since it doesn't need to acquire the pthread GIL. On NetBSD, that helps us avoid allocating too many semaphores. Even if that happens we're now able to survive semaphores running out and even memory running out, when allocating *NSYNC waiter objects. I found a lot more rare bugs in the POSIX threads runtime that could cause things to crash, if you've got dozens of threads all spawning and joining dozens of threads. I want cosmo to be world class production worthy for 2025 so happy holidays all
This commit is contained in:
parent
906bd06a5a
commit
624573207e
51 changed files with 1006 additions and 321 deletions
|
@ -16,22 +16,32 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/intrin/stack.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
/**
|
||||
* Garbage collects POSIX threads runtime.
|
||||
*
|
||||
* Let's say you want to run a memory leak detector. You can say:
|
||||
* This function frees unreferenced zombie threads and empties cache
|
||||
* memory associated with the Cosmopolitan POSIX threads runtime.
|
||||
*
|
||||
* Here's an example use case for this function. Let's say you want to
|
||||
* create a malloc() memory leak detector. If your program was running
|
||||
* threads earlier, then there might still be allocations lingering
|
||||
* around, that'll give you false positives. To fix this, what you would
|
||||
* do is call the following, right before running your leak detector:
|
||||
*
|
||||
* while (!pthread_orphan_np())
|
||||
* pthread_decimate_np();
|
||||
*
|
||||
* To wait until all threads have exited.
|
||||
* Which will wait until all threads have exited and their memory freed.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int pthread_decimate_np(void) {
|
||||
_pthread_decimate(false);
|
||||
_pthread_decimate();
|
||||
cosmo_stack_clear();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue