*
* @APPLE_LICENSE_HEADER_START@
*
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
- *
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* fixed kernel cache set up of cproc info
*
*/
+#include "pthread_internals.h"
#include <stdlib.h>
+#include <sys/queue.h>
#include "cthreads.h"
#include "cthread_internals.h"
-#include "pthread_internals.h"
/*
* C Threads imports:
*/
extern void mig_init();
extern void _pthread_set_self(pthread_t);
+extern void pthread_workqueue_atfork_prepare(void);
+extern void pthread_workqueue_atfork_parent(void);
+extern void pthread_workqueue_atfork_child(void);
/*
* Mach imports:
*/
extern void fork_mach_init();
extern void _cproc_fork_child(), _stack_fork_child();
extern void _lu_fork_child(void);
+extern void _asl_fork_child(void);
extern void _pthread_fork_child(pthread_t);
extern void _notify_fork_child(void);
static pthread_t psaved_self = 0;
static pthread_lock_t psaved_self_global_lock = LOCK_INITIALIZER;
+static pthread_lock_t pthread_atfork_lock = LOCK_INITIALIZER;
+struct pthread_atfork_entry {
+ TAILQ_ENTRY(pthread_atfork_entry) qentry;
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+};
+static TAILQ_HEAD(pthread_atfork_queue_head, pthread_atfork_entry) pthread_atfork_queue = TAILQ_HEAD_INITIALIZER(pthread_atfork_queue);
+
+int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
+{
+ struct pthread_atfork_entry *e;
+
+ e = malloc(sizeof(struct pthread_atfork_entry));
+ if (e == NULL)
+ return (ENOMEM);
+
+ e->prepare = prepare;
+ e->parent = parent;
+ e->child = child;
+
+ _spin_lock(&pthread_atfork_lock);
+ TAILQ_INSERT_TAIL(&pthread_atfork_queue, e, qentry);
+ _spin_unlock(&pthread_atfork_lock);
+
+ return 0;
+}
void _cthread_fork_prepare()
/*
* state in the child, which comes up with only the forking thread running.
*/
{
- _spin_lock(&psaved_self_global_lock);
- psaved_self = pthread_self();
- _spin_lock(&psaved_self->lock);
+ struct pthread_atfork_entry *e;
+
+ _spin_lock(&pthread_atfork_lock);
+#ifdef TAILQ_FOREACH_REVERSE_LEGACY_ORDER
+ TAILQ_FOREACH_REVERSE(e, &pthread_atfork_queue, qentry, pthread_atfork_queue_head)
+#else /* !TAILQ_FOREACH_REVERSE_LEGACY_ORDER */
+ TAILQ_FOREACH_REVERSE(e, &pthread_atfork_queue, pthread_atfork_queue_head, qentry)
+#endif /* TAILQ_FOREACH_REVERSE_LEGACY_ORDER */
+ {
+ if (e->prepare != NULL)
+ e->prepare();
+ }
+
+ _spin_lock(&psaved_self_global_lock);
+ psaved_self = pthread_self();
+ _spin_lock(&psaved_self->lock);
_malloc_fork_prepare();
+
+ pthread_workqueue_atfork_prepare();
}
void _cthread_fork_parent()
* Releases locks acquired by cthread_fork_prepare().
*/
{
+ struct pthread_atfork_entry *e;
+
_malloc_fork_parent();
_spin_unlock(&psaved_self->lock);
_spin_unlock(&psaved_self_global_lock);
+ TAILQ_FOREACH(e, &pthread_atfork_queue, qentry) {
+ if (e->parent != NULL)
+ e->parent();
+ }
+ _spin_unlock(&pthread_atfork_lock);
+
+ pthread_workqueue_atfork_parent();
}
void _cthread_fork_child()
*/
{
pthread_t p = psaved_self;
+ struct pthread_atfork_entry *e;
_pthread_set_self(p);
_spin_unlock(&psaved_self_global_lock);
p->kernel_thread = mach_thread_self();
p->reply_port = mach_reply_port();
p->mutexes = NULL;
- p->cleanup_stack = NULL;
+ p->__cleanup_stack = NULL;
p->death = MACH_PORT_NULL;
p->joiner = NULL;
p->detached |= _PTHREAD_CREATE_PARENT;
_cproc_fork_child();
_lu_fork_child();
-
+ _asl_fork_child();
_notify_fork_child();
__is_threaded = 0;
mig_init(1); /* enable multi-threaded mig interfaces */
+
+ pthread_workqueue_atfork_child();
+
+ TAILQ_FOREACH(e, &pthread_atfork_queue, qentry) {
+ if (e->child != NULL)
+ e->child();
+ }
+ LOCK_INIT(pthread_atfork_lock);
}