.
Index: generic/tclExecute.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclExecute.c,v
retrieving revision 1.114
diff -u -r1.114 tclExecute.c
--- generic/tclExecute.c 14 Nov 2003 20:44:44 -0000 1.114
+++ generic/tclExecute.c 15 Nov 2003 22:41:10 -0000
@@ -1088,6 +1088,7 @@
int traceInstructions = (tclTraceExec == 3);
char cmdNameBuf[21];
#endif
+ int instructionCount = 0;
/*
* The execution uses a unified stack: first the catch stack, immediately
@@ -1202,6 +1203,21 @@
#ifdef TCL_COMPILE_STATS
iPtr->stats.instructionCount[*pc]++;
#endif
+
+ /*
+ * Check for asynchronous handlers [Bug 746722]; we
+ * do the check every 16th instruction.
+ */
+
+ if (!(instructionCount++ & ~15) && Tcl_AsyncReady()) {
+ DECACHE_STACK_INFO();
+ result = Tcl_AsyncInvoke(interp, result);
+ CACHE_STACK_INFO();
+ if (result == TCL_ERROR) {
+ goto checkForCatch;
+ }
+ }
+
switch (*pc) {
case INST_RETURN:
if (iPtr->returnOpts != iPtr->defaultReturnOpts) {
@@ -1210,6 +1226,7 @@
Tcl_IncrRefCount(iPtr->returnOpts);
}
result = TCL_RETURN;
+
case INST_DONE:
if (tosPtr <= eePtr->stackPtr + initStackTop) {
tosPtr--;
Index: generic/tclTest.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclTest.c,v
retrieving revision 1.71
diff -u -r1.71 tclTest.c
--- generic/tclTest.c 14 Nov 2003 23:21:02 -0000 1.71
+++ generic/tclTest.c 15 Nov 2003 22:41:18 -0000
@@ -129,6 +129,9 @@
int Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
static int AsyncHandlerProc _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int code));
+#ifdef TCL_THREADS
+static Tcl_ThreadCreateType AsyncThreadProc _ANSI_ARGS_((ClientData));
+#endif
static void CleanupTestSetassocdataTests _ANSI_ARGS_((
ClientData clientData, Tcl_Interp *interp));
static void CmdDelProc1 _ANSI_ARGS_((ClientData clientData));
@@ -840,6 +843,30 @@
}
Tcl_SetResult(interp, (char *)argv[3], TCL_VOLATILE);
return code;
+#ifdef TCL_THREADS
+ } else if ( strcmp( argv[1], "marklater" ) == 0 ) {
+ if ( argc != 3 ) {
+ goto wrongNumArgs;
+ }
+ if ( Tcl_GetInt( interp, argv[2], &id ) != TCL_OK ) {
+ return TCL_ERROR;
+ }
+ for (asyncPtr = firstHandler; asyncPtr != NULL;
+ asyncPtr = asyncPtr->nextPtr) {
+ if (asyncPtr->id == id) {
+ Tcl_ThreadId threadID;
+ if ( Tcl_CreateThread( &threadID,
+ AsyncThreadProc,
+ (ClientData) asyncPtr,
+ TCL_THREAD_STACK_DEFAULT,
+ TCL_THREAD_NOFLAGS ) != TCL_OK ) {
+ Tcl_SetResult( interp, "can't create thread", TCL_STATIC );
+ return TCL_ERROR;
+ }
+ break;
+ }
+ }
+#endif
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be create, delete, int, or mark",
@@ -878,6 +905,37 @@
ckfree((char *)cmd);
return code;
}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * AsyncThreadProc --
+ *
+ * Delivers an asynchronous event to a handler in another thread.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * Invokes Tcl_AsyncMark on the handler
+ *
+ *----------------------------------------------------------------------
+ */
+
+#ifdef TCL_THREADS
+static Tcl_ThreadCreateType
+AsyncThreadProc( ClientData clientData )
+ /* Parameter is a pointer to a */
+ /* TestAsyncHandler, defined above */
+{
+
+ TestAsyncHandler* asyncPtr = clientData;
+ Tcl_Sleep( 1 );
+ Tcl_AsyncMark( asyncPtr->handler );
+ Tcl_ExitThread( TCL_OK );
+ TCL_THREAD_CREATE_RETURN;
+}
+#endif
/*
*----------------------------------------------------------------------
Index: tests/async.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/async.test,v
retrieving revision 1.6
diff -u -r1.6 async.test
--- tests/async.test 24 Jul 2003 16:05:24 -0000 1.6
+++ tests/async.test 15 Nov 2003 22:41:18 -0000
@@ -25,6 +25,10 @@
return
}
+tcltest::testConstraint threaded \
+ [expr { [info exists ::tcl_platform(threaded)]
+ && $::tcl_platform(threaded) }]
+
proc async1 {result code} {
global aresult acode
set aresult $result
@@ -146,19 +150,57 @@
list [catch {testasync mark $hm2 "foobar" 5} msg] $msg $x
} {3 del2 {0 0 0 del1 del2}}
+proc nothing {} {
+ # empty proc
+}
+proc hang1 { handle } {
+ global aresult
+ set aresult {Async event not delivered}
+ testasync marklater $handle
+ for { set i 0 } { $i < 1000000 && $aresult eq "Async event not delivered" } { incr i } {
+ nothing
+ }
+ return $aresult
+}
+proc hang2 { handle } {
+ global aresult
+ set aresult {Async event not delivered}
+ testasync marklater $handle
+ for { set i 0 } { $i < 1000000 && $aresult eq "Async event not delivered" } { incr i } {}
+ return $aresult
+}
+
+test async-4.1 {async interrupting bytecode sequence} \
+ -constraints threaded \
+ -setup {
+ set hm [testasync create async3]
+ } \
+ -body {
+ hang1 $hm
+ } \
+ -cleanup {
+ testasync delete $hm
+ } \
+ -result {test pattern}
+
+test async-4.2 {async interrupting straight bytecode sequence} \
+ -constraints threaded \
+ -setup {
+ set hm [testasync create async3]
+ } \
+ -body {
+ hang2 $hm
+ } \
+ -cleanup {
+ testasync delete $hm
+ } \
+ -result {test pattern}
+
# cleanup
testasync delete
::tcltest::cleanupTests
return
-
-
-
-
-
-
-
-
-
-
-
+# Local Variables:
+# mode: tcl
+# End: