123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sysdep.h>
- #include <tls.h>
- #include "fork.h"
- #include <hp-timing.h>
- #include <ldsodefs.h>
- #include <atomic.h>
- #include <errno.h>
- unsigned long int *__fork_generation_pointer;
- struct fork_handler *__fork_handlers;
- static void
- fresetlockfiles (void)
- {
- FILE *fp;
- #ifdef __USE_STDIO_FUTEXES__
- for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
- STDIO_INIT_MUTEX(fp->__lock);
- #else
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
- for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen)
- pthread_mutex_init(&fp->__lock, &attr);
- pthread_mutexattr_destroy(&attr);
- #endif
- }
- pid_t
- fork (void)
- {
- pid_t pid;
- struct used_handler
- {
- struct fork_handler *handler;
- struct used_handler *next;
- } *allp = NULL;
-
- struct fork_handler *runp;
- while ((runp = __fork_handlers) != NULL)
- {
-
- atomic_full_barrier ();
- unsigned int oldval = runp->refcntr;
- if (oldval == 0)
-
- continue;
-
- if (atomic_compare_and_exchange_bool_acq (&__fork_handlers->refcntr,
- oldval + 1, oldval))
-
- continue;
-
- while (1)
- {
-
- if (runp->prepare_handler != NULL)
- runp->prepare_handler ();
-
- struct used_handler *newp
- = (struct used_handler *) alloca (sizeof (*newp));
- newp->handler = runp;
- newp->next = allp;
- allp = newp;
-
- runp = runp->next;
- if (runp == NULL)
- break;
-
- atomic_increment (&runp->refcntr);
- }
-
- break;
- }
- __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
- #ifndef NDEBUG
- pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
- #endif
-
- pid_t parentpid = THREAD_GETMEM (THREAD_SELF, pid);
- THREAD_SETMEM (THREAD_SELF, pid, -parentpid);
- #ifdef ARCH_FORK
- pid = ARCH_FORK ();
- #else
- # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
- pid = INLINE_SYSCALL (fork, 0);
- #endif
- if (pid == 0)
- {
- struct pthread *self = THREAD_SELF;
- assert (THREAD_GETMEM (self, tid) != ppid);
- if (__fork_generation_pointer != NULL)
- *__fork_generation_pointer += 4;
-
- THREAD_SETMEM (self, pid, THREAD_GETMEM (self, tid));
- #if HP_TIMING_AVAIL
-
- hp_timing_t now;
- HP_TIMING_NOW (now);
- THREAD_SETMEM (self, cpuclock_offset, now);
- GL(dl_cpuclock_offset) = now;
- #endif
-
- fresetlockfiles ();
-
- STDIO_INIT_MUTEX(_stdio_openlist_add_lock);
-
-
- while (allp != NULL)
- {
- if (allp->handler->child_handler != NULL)
- allp->handler->child_handler ();
-
- allp->handler->refcntr = 1;
-
- allp = allp->next;
- }
-
- __fork_lock = LLL_LOCK_INITIALIZER;
- }
- else
- {
- assert (THREAD_GETMEM (THREAD_SELF, tid) == ppid);
-
- THREAD_SETMEM (THREAD_SELF, pid, parentpid);
-
- __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(_stdio_openlist_add_lock);
-
- while (allp != NULL)
- {
- if (allp->handler->parent_handler != NULL)
- allp->handler->parent_handler ();
- if (atomic_decrement_and_test (&allp->handler->refcntr)
- && allp->handler->need_signal)
- lll_futex_wake (allp->handler->refcntr, 1, LLL_PRIVATE);
- allp = allp->next;
- }
- }
- return pid;
- }
- libc_hidden_def(fork)
|