From aba00ab374621025c49c80b7b6e9a347807ea59a Mon Sep 17 00:00:00 2001 From: jb Date: Mon, 9 Mar 1998 06:54:50 +0000 Subject: When forking a process, only the running thread gets to live. All other threads never see the light of day and if they leave things locked, blame POSIX. --- lib/libpthread/thread/thr_fork.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib/libpthread/thread/thr_fork.c') diff --git a/lib/libpthread/thread/thr_fork.c b/lib/libpthread/thread/thr_fork.c index 19f674d..89d7259 100644 --- a/lib/libpthread/thread/thr_fork.c +++ b/lib/libpthread/thread/thr_fork.c @@ -42,7 +42,13 @@ pid_t fork(void) { int flags; + int status; pid_t ret; + pthread_t pthread; + pthread_t pthread_next; + + /* Block signals to avoid being interrupted at a bad time: */ + _thread_kern_sig_block(&status); /* Fork a new process: */ if ((ret = _thread_sys_fork()) <= 0) { @@ -83,9 +89,42 @@ fork(void) else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); + } else { + /* Point to the first thread in the list: */ + pthread = _thread_link_list; + + /* + * Enter a loop to remove all threads other than + * the running thread from the thread list: + */ + while (pthread != NULL) { + pthread_next = pthread->nxt; + if (pthread == _thread_run) { + _thread_link_list = pthread; + pthread->nxt = NULL; + } else { + if (pthread->attr.stackaddr_attr == + NULL && pthread->stack != NULL) { + /* + * Free the stack of the + * dead thread: + */ + free(pthread->stack); + } + if (pthread->specific_data != NULL) + free(pthread->specific_data); + free(pthread); + } + + /* Point to the next thread: */ + pthread = pthread_next; + } } } + /* Unblock signals: */ + _thread_kern_sig_unblock(&status); + /* Return the process ID: */ return (ret); } -- cgit v1.1