summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libpthread/pthread_atfork.c
diff options
context:
space:
mode:
authorMystery Man <unknown@mandriva.org>2001-05-14 14:17:54 +0000
committerMystery Man <unknown@mandriva.org>2001-05-14 14:17:54 +0000
commit16984701aa5165b77f229cc318e8025ee8cf81b5 (patch)
tree923ed2780582f751b2be106641be35cf71fedd66 /mdk-stage1/dietlibc/libpthread/pthread_atfork.c
parentc5050eb3ba91cf0eab748b773dcbb34b88ae88bc (diff)
downloaddrakx-backup-do-not-use-16984701aa5165b77f229cc318e8025ee8cf81b5.tar
drakx-backup-do-not-use-16984701aa5165b77f229cc318e8025ee8cf81b5.tar.gz
drakx-backup-do-not-use-16984701aa5165b77f229cc318e8025ee8cf81b5.tar.bz2
drakx-backup-do-not-use-16984701aa5165b77f229cc318e8025ee8cf81b5.tar.xz
drakx-backup-do-not-use-16984701aa5165b77f229cc318e8025ee8cf81b5.zip
This commit was manufactured by cvs2svn to create branch 'dietlibc'.topic/dietlibc
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;
+}