summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2013-07-25 14:25:47 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2013-07-25 14:25:47 (GMT)
commit3b1898a43fd6c834aaf8afa6f35c378f64821e75 (patch)
tree3accb464bb6e57619b6e278834834856ea8a4042
parent7fb69e61c80a0f943d0cbc01e7f4ba39eb2cb737 (diff)
parent69fbd6c489ee246d40067e6a6394419ecb9a6b07 (diff)
downloadtcl-3b1898a43fd6c834aaf8afa6f35c378f64821e75.zip
tcl-3b1898a43fd6c834aaf8afa6f35c378f64821e75.tar.gz
tcl-3b1898a43fd6c834aaf8afa6f35c378f64821e75.tar.bz2
rebase
-rw-r--r--ChangeLog6
-rw-r--r--tests/unixForkEvent.test45
-rw-r--r--unix/tclUnixNotfy.c119
3 files changed, 166 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 71015ed..b85db83 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-07-17 Harald Oehlmann <oehhar@users.sf.net>
+
+ * tclUnixNotify.c Tcl_InitNotifier: Bug [a0bc856dcd]
+ Start notifier thread again if we were forked, to solve Rivet bug
+ 55153.
+
2013-07-05 Kevin B. Kenny <kennykb@acm.org>
* library/tzdata/Africa/Casablanca:
diff --git a/tests/unixForkEvent.test b/tests/unixForkEvent.test
new file mode 100644
index 0000000..a0c3f19
--- /dev/null
+++ b/tests/unixForkEvent.test
@@ -0,0 +1,45 @@
+# This file contains a collection of tests for the procedures in the file
+# tclUnixNotify.c. Sourcing this file into Tcl runs the tests and
+# generates output for errors. No output means no errors were found.
+#
+# Copyright (c) 1995-1997 Sun Microsystems, Inc.
+# Copyright (c) 1998-1999 by Scriptics Corporation.
+#
+# See the file "license.terms" for information on usage and redistribution
+# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
+
+package require tcltest 2
+namespace import -force ::tcltest::*
+
+testConstraint testfork [llength [info commands testfork]]
+
+# Test if the notifier thread is well initialized in a forked interpreter
+# by Tcl_InitNotifier
+test unixforkevent-1.1 {fork and test writeable event} \
+ -constraints testfork \
+ -body {
+ set folder [makeDirectory unixtestfork]
+ set pid [testfork]
+ if {$pid == 0} {
+ # we are the forked process
+ set result initialized
+ set h [open [file join $folder test.txt] w]
+ fileevent $h writable\
+ "set result writable;\
+ after cancel [after 1000 {set result timeout}]"
+ vwait result
+ close $h
+ makeFile $result result.txt $folder
+ exit
+ }
+ # we are the original process
+ while {![file readable [file join $folder result.txt]]} {}
+ viewFile result.txt $folder
+ } \
+ -result {writable} \
+ -cleanup {
+ catch { removeFolder $folder }
+ }
+
+::tcltest::cleanupTests
+return
diff --git a/unix/tclUnixNotfy.c b/unix/tclUnixNotfy.c
index 51f0b1f..ec721ca 100644
--- a/unix/tclUnixNotfy.c
+++ b/unix/tclUnixNotfy.c
@@ -112,7 +112,7 @@ typedef struct ThreadSpecificData {
int eventReady; /* True if an event is ready to be processed.
* Used as condition flag together with waitCV
* above. */
-#endif
+#endif /* TCL_THREADS */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
@@ -128,6 +128,15 @@ static Tcl_ThreadDataKey dataKey;
static int notifierCount = 0;
/*
+ * The following static stores the process ID of the initialized notifier
+ * thread. If it changes, we have passed a fork and we should start a new
+ * notifier thread.
+ *
+ * You must hold the notifierMutex lock before accessing this variable.
+ */
+static pid_t processIDInitialized = 0;
+
+/*
* The following variable points to the head of a doubly-linked list of
* ThreadSpecificData structures for all threads that are currently waiting on
* an event.
@@ -185,7 +194,7 @@ static Tcl_Condition notifierCV;
static Tcl_ThreadId notifierThread;
-#endif
+#endif /* TCL_THREADS */
/*
* Static routines defined in this file.
@@ -193,7 +202,13 @@ static Tcl_ThreadId notifierThread;
#ifdef TCL_THREADS
static void NotifierThreadProc(ClientData clientData);
-#endif
+#ifdef HAVE_PTHREAD_ATFORK
+static int atForkInit = 0;
+static void AtForkPrepare(void);
+static void AtForkParent(void);
+static void AtForkChild(void);
+#endif /* HAVE_PTHREAD_ATFORK */
+#endif /* TCL_THREADS */
static int FileHandlerEventProc(Tcl_Event *evPtr, int flags);
/*
@@ -267,11 +282,38 @@ Tcl_InitNotifier(void)
*/
Tcl_MutexLock(&notifierMutex);
+#ifdef HAVE_PTHREAD_ATFORK
+ /*
+ * Install pthread_atfork handlers to reinitialize the notifier in the
+ * child of a fork.
+ */
+
+ if (!atForkInit) {
+ int result = pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild);
+
+ if (result) {
+ Tcl_Panic("Tcl_InitNotifier: pthread_atfork failed");
+ }
+ atForkInit = 1;
+ }
+#endif
+ /*
+ * Check if my process id changed, e.g. I was forked
+ * In this case, restart the notifier thread and close the
+ * pipe to the original notifier thread
+ */
+ if (notifierCount > 0 && processIDInitialized != getpid()) {
+ notifierCount = 0;
+ processIDInitialized = 0;
+ close(triggerPipe);
+ triggerPipe = -1;
+ }
if (notifierCount == 0) {
if (TclpThreadCreate(&notifierThread, NotifierThreadProc, NULL,
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE) != TCL_OK) {
Tcl_Panic("Tcl_InitNotifier: unable to start notifier thread");
}
+ processIDInitialized = getpid();
}
notifierCount++;
@@ -284,7 +326,7 @@ Tcl_InitNotifier(void)
}
Tcl_MutexUnlock(&notifierMutex);
-#endif
+#endif /* TCL_THREADS */
return (ClientData) tsdPtr;
}
@@ -1230,6 +1272,75 @@ NotifierThreadProc(
TclpThreadExit (0);
}
+
+#ifdef HAVE_PTHREAD_ATFORK
+/*
+ *----------------------------------------------------------------------
+ *
+ * AtForkPrepare --
+ *
+ * Lock the notifier in preparation for a fork.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+AtForkPrepare(void)
+{
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * AtForkParent --
+ *
+ * Unlock the notifier in the parent after a fork.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+AtForkParent(void)
+{
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * AtForkChild --
+ *
+ * Unlock and reinstall the notifier in the child after a fork.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+AtForkChild(void)
+{
+ notifierMutex = NULL;
+ notifierCV = NULL;
+ Tcl_InitNotifier();
+}
+#endif /* HAVE_PTHREAD_ATFORK */
+
#endif /* TCL_THREADS */
#endif /* HAVE_COREFOUNDATION */