mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
Implement pthread_atfork()
If threads are being used, then fork() will now acquire and release and runtime locks so that fork() may be safely used from threads. This also makes vfork() thread safe, because pthread mutexes will do nothing when the process is a child of vfork(). More torture tests have been written to confirm this all works like a charm. Additionally: - Invent hexpcpy() api - Rename nsync_malloc_() to kmalloc() - Complete posix named semaphore implementation - Make pthread_create() asynchronous signal safe - Add rm, rmdir, and touch to command interpreter builtins - Invent sigisprecious() and modify sigset functions to use it - Add unit tests for posix_spawn() attributes and fix its bugs One unresolved problem is the reclaiming of *NSYNC waiter memory in the forked child processes, within apps which have threads waiting on locks
This commit is contained in:
parent
64c284003d
commit
60cb435cb4
124 changed files with 2169 additions and 718 deletions
|
@ -16,17 +16,36 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/semaphore.h"
|
||||
|
||||
/**
|
||||
* Destroys unnamed semaphore.
|
||||
*
|
||||
* If `sem` was successfully initialized by sem_init() then
|
||||
* sem_destroy() may be called multiple times, before it may be
|
||||
* reinitialized again by calling sem_init().
|
||||
*
|
||||
* Calling sem_destroy() on a semaphore created by sem_open() has
|
||||
* undefined behavior. Using `sem` after calling sem_destroy() is
|
||||
* undefined behavior that will cause semaphore APIs to either crash or
|
||||
* raise `EINVAL` until `sem` is passed to sem_init() again.
|
||||
*
|
||||
* @param sem was created by sem_init()
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EINVAL if `sem` wasn't valid
|
||||
* @raise EBUSY if `sem` has waiters
|
||||
*/
|
||||
int sem_destroy(sem_t *sem) {
|
||||
int waiters;
|
||||
_npassert(sem->sem_magic != SEM_MAGIC_NAMED);
|
||||
if (sem->sem_magic != SEM_MAGIC_UNNAMED) return einval();
|
||||
waiters = atomic_load_explicit(&sem->sem_waiters, memory_order_relaxed);
|
||||
_unassert(waiters >= 0);
|
||||
if (waiters) return ebusy();
|
||||
atomic_store_explicit(&sem->sem_value, INT_MIN, memory_order_relaxed);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue