summaryrefslogtreecommitdiff
path: root/lib/libpthread/uthread
diff options
context:
space:
mode:
authorDavid Leonard <d@cvs.openbsd.org>1999-01-17 23:46:27 +0000
committerDavid Leonard <d@cvs.openbsd.org>1999-01-17 23:46:27 +0000
commit9ebd7c855719a914d8220cf8b7ab845bd006e6fc (patch)
tree5c1bbd0e96b6b2cff8e39b834dbc918a988a3c9b /lib/libpthread/uthread
parent899c831eae6d973eed37aecb6bef358e00083afb (diff)
pthread_atfork()
Diffstat (limited to 'lib/libpthread/uthread')
-rw-r--r--lib/libpthread/uthread/uthread_atfork.c93
-rw-r--r--lib/libpthread/uthread/uthread_fork.c8
2 files changed, 100 insertions, 1 deletions
diff --git a/lib/libpthread/uthread/uthread_atfork.c b/lib/libpthread/uthread/uthread_atfork.c
new file mode 100644
index 00000000000..a3ea65e8288
--- /dev/null
+++ b/lib/libpthread/uthread/uthread_atfork.c
@@ -0,0 +1,93 @@
+/*
+ * David Leonard <d@openbsd.org>, 1999. Public domain.
+ * $OpenBSD: uthread_atfork.c,v 1.1 1999/01/17 23:46:26 d Exp $
+ */
+
+#include <stdlib.h>
+#include <sys/queue.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+
+struct atfork_entry {
+ void (*handler)(void);
+ TAILQ_ENTRY(atfork_entry) entries;
+};
+
+static TAILQ_HEAD(atfork_list, atfork_entry) atfork_head[3] =
+ { TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PREPARE]),
+ TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PARENT]),
+ TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_CHILD]) };
+
+void
+_thread_atfork(which)
+{
+ struct atfork_list *head;
+ struct atfork_entry *ae;
+
+ head = &atfork_head[which];
+
+ /* Call the fork handlers in order: */
+ for (ae = head->tqh_first; ae != NULL; ae = ae->entries.tqe_next)
+ (*ae->handler)();
+}
+
+int
+pthread_atfork(prepare, parent, child)
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+{
+ int ret = 0;
+ struct atfork_entry *prepare_entry = NULL;
+ struct atfork_entry *parent_entry = NULL;
+ struct atfork_entry *child_entry = NULL;
+
+ if (ret == 0 && prepare != NULL) {
+ /* Allocate space for the prepare handler: */
+ if ((prepare_entry = malloc(sizeof *prepare_entry)) != NULL)
+ prepare_entry->handler = prepare;
+ else
+ ret = -1;
+ }
+
+ if (ret == 0 && parent != NULL) {
+ /* Allocate space for the parent handler: */
+ if ((parent_entry = malloc(sizeof *parent_entry)) != NULL)
+ parent_entry->handler = parent;
+ else
+ ret = -1;
+ }
+
+ if (ret == 0 && child != NULL) {
+ /* Allocate space for the child handler: */
+ if ((child_entry = malloc(sizeof *child_entry)) != NULL)
+ child_entry->handler = child;
+ else
+ ret = -1;
+ }
+
+ if (ret == 0) {
+ /* Insert the handlers into the handler lists: */
+ if (prepare_entry != NULL)
+ TAILQ_INSERT_HEAD(&atfork_head[PTHREAD_ATFORK_PREPARE],
+ prepare_entry, entries);
+ if (parent_entry != NULL)
+ TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_PARENT],
+ parent_entry, entries);
+ if (child_entry != NULL)
+ TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_CHILD],
+ child_entry, entries);
+ } else {
+ /* Release unused resources: */
+ if (prepare_entry)
+ free(prepare_entry);
+ if (child_entry)
+ free(child_entry);
+ if (parent_entry)
+ free(parent_entry);
+ }
+
+ return (ret);
+}
+#endif
diff --git a/lib/libpthread/uthread/uthread_fork.c b/lib/libpthread/uthread/uthread_fork.c
index 7f532f38adb..913e79e0bdd 100644
--- a/lib/libpthread/uthread/uthread_fork.c
+++ b/lib/libpthread/uthread/uthread_fork.c
@@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $OpenBSD: uthread_fork.c,v 1.3 1999/01/06 05:29:23 d Exp $
+ * $OpenBSD: uthread_fork.c,v 1.4 1999/01/17 23:46:26 d Exp $
*/
#include <errno.h>
#include <string.h>
@@ -48,12 +48,17 @@ fork(void)
pthread_t pthread;
pthread_t pthread_next;
+ /* Call atfork handlers: */
+ _thread_atfork(PTHREAD_ATFORK_PREPARE);
+
/* Lock the thread list: */
_lock_thread_list();
/* Fork a new process: */
if ((ret = _thread_sys_fork()) != 0) {
/* Parent process or error. Nothing to do here. */
+ if (ret > 0)
+ _thread_atfork(PTHREAD_ATFORK_PARENT);
} else {
/* Close the pthread kernel pipe: */
_thread_sys_close(_thread_kern_pipe[0]);
@@ -121,6 +126,7 @@ fork(void)
/* Point to the next thread: */
pthread = pthread_next;
}
+ _thread_atfork(PTHREAD_ATFORK_CHILD);
}
}