summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libpthread/pthread_atfork.c
diff options
context:
space:
mode:
authorGuillaume Cottenceau <gc@mandriva.com>2001-05-14 14:17:54 +0000
committerGuillaume Cottenceau <gc@mandriva.com>2001-05-14 14:17:54 +0000
commit9097327dc1c667fc51b8e05cc7c0626fac96665d (patch)
tree1a67c4a8c6116747dfe0177e96928805dbba94b0 /mdk-stage1/dietlibc/libpthread/pthread_atfork.c
parentf15aa3a552022743398a652165d76bf912c715e5 (diff)
downloaddrakx-backup-do-not-use-9097327dc1c667fc51b8e05cc7c0626fac96665d.tar
drakx-backup-do-not-use-9097327dc1c667fc51b8e05cc7c0626fac96665d.tar.gz
drakx-backup-do-not-use-9097327dc1c667fc51b8e05cc7c0626fac96665d.tar.bz2
drakx-backup-do-not-use-9097327dc1c667fc51b8e05cc7c0626fac96665d.tar.xz
drakx-backup-do-not-use-9097327dc1c667fc51b8e05cc7c0626fac96665d.zip
Initial revision
Diffstat (limited to 'mdk-stage1/dietlibc/libpthread/pthread_atfork.c')
-rw-r--r--mdk-stage1/dietlibc/libpthread/pthread_atfork.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/mdk-stage1/dietlibc/libpthread/pthread_atfork.c b/mdk-stage1/dietlibc/libpthread/pthread_atfork.c
new file mode 100644
index 000000000..2dc612dcc
--- /dev/null
+++ b/mdk-stage1/dietlibc/libpthread/pthread_atfork.c
@@ -0,0 +1,77 @@
+#include <unistd.h>
+#include <errno.h>
+
+#include <pthread.h>
+#include "thread_internal.h"
+
+#define PTHREAD_NUM_ATFORK 4
+
+static struct _pthread_fastlock __atfork_struct_lock;
+static struct {
+ int used;
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+} pthread_atfork_buf[PTHREAD_NUM_ATFORK];
+
+int pthread_atfork(void (*prepare)(void),
+ void (*parent)(void),
+ void (*child)(void))
+{
+ int i,ret=-1;
+
+ __NO_ASYNC_CANCEL_BEGIN;
+ __pthread_lock(&__atfork_struct_lock);
+
+ for (i=0;i<PTHREAD_NUM_ATFORK; ++i) {
+ if (!(pthread_atfork_buf[i].used)) {
+ pthread_atfork_buf[i].prepare=prepare;
+ pthread_atfork_buf[i].parent=parent;
+ pthread_atfork_buf[i].child=child;
+ ret=0;
+ break;
+ }
+ }
+
+ __pthread_unlock(&__atfork_struct_lock);
+ __NO_ASYNC_CANCEL_END;
+
+ if (ret) (*(__errno_location()))=ENOMEM;
+ return ret;
+}
+
+pid_t fork(void)
+{
+ pid_t pid;
+ int i=PTHREAD_NUM_ATFORK;
+
+ __NO_ASYNC_CANCEL_BEGIN;
+
+ __TEST_CANCEL();
+ __pthread_lock(&__atfork_struct_lock);
+
+ while (i) {
+ if (pthread_atfork_buf[--i].used)
+ if (pthread_atfork_buf[i].prepare)
+ pthread_atfork_buf[i].prepare();
+ }
+
+ pid=__libc_fork();
+
+ if (pid>0) { /* parent */
+ for (i=0;i<PTHREAD_NUM_ATFORK;++i)
+ if (pthread_atfork_buf[i].used)
+ if (pthread_atfork_buf[i].parent)
+ pthread_atfork_buf[i].parent();
+ } else if (pid==0) { /* child */
+ for (i=0;i<PTHREAD_NUM_ATFORK;++i)
+ if (pthread_atfork_buf[i].used)
+ if (pthread_atfork_buf[i].child)
+ pthread_atfork_buf[i].child();
+ }
+
+ __pthread_unlock(&__atfork_struct_lock);
+ __NO_ASYNC_CANCEL_END;
+
+ return pid;
+}