Debian Codegraph @

tests/thread.test

raw
1 # Commands covered: (test)thread
2 #
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1996 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
9 # Copyright (c) 2006-2008 by Joe Mistachkin. All rights reserved.
10 #
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14
15 # when thread::release is used, -wait is passed in order allow the thread to
16 # be fully finalized, which avoids valgrind "still reachable" reports.
17
18 package require tcltest 2.5
19 source [file join [file dirname [info script]] tcltests.tcl]
20
21 ::tcltest::loadTestedCommands
22 catch [list package require -exact Tcltest [info patchlevel]]
23
24 # Some tests require the testthread command
25
26 testConstraint testthread [expr {[info commands testthread] ne {}}]
27
28
29 set threadSuperKillScript {
30 rename catch ""
31 rename while ""
32 rename unknown ""
33 rename update ""
34 thread::release
35 }
36
37 proc getThreadErrorFromInfo { info } {
38 set list [split $info \n]
39 set idx [lsearch -glob $list "*eval*unwound*"]
40 if {$idx >= 0} then {
41 return [lindex $list $idx]
42 }
43 set idx [lsearch -glob $list "*eval*canceled*"]
44 if {$idx >= 0} then {
45 return [lindex $list $idx]
46 }
47 return ""; # some other error we do not care about.
48 }
49
50 proc findThreadError { info } {
51 foreach error [lreverse $info] {
52 set error [getThreadErrorFromInfo $error]
53 if {[string length $error] > 0} then {
54 return $error
55 }
56 }
57 return ""; # some other error we do not care about.
58 }
59
60 proc ThreadError {id info} {
61 global threadSawError
62 if {[string length [getThreadErrorFromInfo $info]] > 0} then {
63 global threadId threadError
64 set threadId $id
65 lappend threadError($id) $info
66 }
67 set threadSawError($id) true; # signal main thread to exit [vwait].
68 }
69
70 proc threadSuperKill id {
71 variable threadSuperKillScript
72 try {
73 thread::send $id $::threadSuperKillScript
74 } on error {tres topts} {
75 if {$tres ne {target thread died}} {
76 return -options $topts $tres
77 }
78 }
79 }
80
81 if {[testConstraint thread]} {
82 thread::errorproc ThreadError
83 }
84
85 if {[testConstraint testthread]} {
86 proc drainEventQueue {} {
87 while {[set x [testthread event]]} {
88 #puts "WARNING: drained $x event(s) on main thread"
89 }
90 }
91
92 testthread errorproc ThreadError
93 }
94
95 # Some tests require manual draining of the event queue
96
97 testConstraint drainEventQueue [expr {[info commands drainEventQueue] != {}}]
98
99 test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {thread} {
100 llength [thread::names]
101 } 1
102 test thread-1.4 {Tcl_ThreadObjCmd: thread create } {thread} {
103 set serverthread [thread::create -preserved]
104 set numthreads [llength [thread::names]]
105 thread::release -wait $serverthread
106 set numthreads
107 } 2
108 test thread-1.5 {Tcl_ThreadObjCmd: thread create one shot} {thread} {
109 thread::create {set x 5}
110 foreach try {0 1 2 4 5 6} {
111 # Try various ways to yield
112 update
113 after 10
114 set l [llength [thread::names]]
115 if {$l == 1} {
116 break
117 }
118 }
119 set l
120 } 1
121 test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {thread} {
122 thread::create {{*}{}}
123 update
124 after 10
125 llength [thread::names]
126 } {1}
127 test thread-1.13 {Tcl_ThreadObjCmd: send args} {thread} {
128 set serverthread [thread::create -preserved]
129 set five [thread::send $serverthread {set x 5}]
130 thread::release -wait $serverthread
131 set five
132 } 5
133 test thread-1.15 {Tcl_ThreadObjCmd: wait} {thread} {
134 set serverthread [thread::create -preserved {set z 5 ; thread::wait}]
135 set five [thread::send $serverthread {set z}]
136 thread::release -wait $serverthread
137 set five
138 } 5
139
140 # The tests above also cover:
141 # TclCreateThread, except when pthread_create fails
142 # NewThread, safe and regular
143 # ThreadErrorProc, except for printing to standard error
144
145 test thread-2.1 {ListUpdateInner and ListRemove} {thread} {
146 catch {unset tid}
147 foreach t {0 1 2} {
148 upvar #0 t$t tid
149 set tid [thread::create -preserved]
150 }
151 foreach t {0 1 2} {
152 upvar #0 t$t tid
153 thread::release $tid
154 }
155 llength [thread::names]
156 } 1
157
158 test thread-3.1 {TclThreadList} {thread} {
159 catch {unset tid}
160 set len [llength [thread::names]]
161 set l1 {}
162 foreach t {0 1 2} {
163 lappend l1 [thread::create -preserved]
164 }
165 set l2 [thread::names]
166 set c [string compare [lsort [concat [thread::id] $l1]] [lsort $l2]]
167 foreach t $l1 {
168 thread::release -wait $t
169 }
170 list $len $c
171 } {1 0}
172
173 test thread-4.1 {TclThreadSend to self} {thread} {
174 catch {unset x}
175 thread::send [thread::id] {
176 set x 4
177 }
178 set x
179 } {4}
180 test thread-4.2 {TclThreadSend -async} {thread} {
181 set len [llength [thread::names]]
182 set serverthread [thread::create -preserved]
183 thread::send -async $serverthread {
184 after 1 {thread::release}
185 }
186 set two [llength [thread::names]]
187 after 100 {set done 1}
188 vwait done
189 list $len [llength [thread::names]] $two
190 } {1 1 2}
191 test thread-4.3 {TclThreadSend preserve errorInfo} {thread} {
192 set len [llength [thread::names]]
193 set serverthread [thread::create -preserved]
194 set x [catch {thread::send $serverthread {set undef}} msg]
195 set savedErrorInfo $::errorInfo
196 thread::release $serverthread
197 list $len $x $msg $savedErrorInfo
198 } {1 1 {can't read "undef": no such variable} {can't read "undef": no such variable
199 while executing
200 "set undef"
201 invoked from within
202 "thread::send $serverthread {set undef}"}}
203 test thread-4.4 {TclThreadSend preserve code} {thread} {
204 set len [llength [thread::names]]
205 set serverthread [thread::create -preserved]
206 set ::errorInfo {}
207 set x [catch {thread::send $serverthread {set ::errorInfo {}; break}} msg]
208 set savedErrorInfo $::errorInfo
209 thread::release $serverthread
210 list $len $x $msg $savedErrorInfo
211 } {1 3 {} {}}
212 test thread-4.5 {TclThreadSend preserve errorCode} {thread} {
213 set serverthread [thread::create]
214 set x [catch {thread::send $serverthread {error ERR INFO CODE}} msg]
215 set savedErrorCode $::errorCode
216 thread::release $serverthread
217 list $x $msg $savedErrorCode
218 } {1 ERR CODE}
219
220
221 test thread-5.0 {Joining threads} {thread} {
222 set serverthread [thread::create -joinable -preserved]
223 thread::send -async $serverthread {after 1000 ; thread::release}
224 thread::join $serverthread
225 } {0}
226 test thread-5.1 {Joining threads after the fact} {thread} {
227 set serverthread [thread::create -joinable -preserved]
228 thread::send -async $serverthread {thread::release}
229 after 2000
230 thread::join $serverthread
231 } {0}
232 test thread-5.2 {Try to join a detached thread} {thread} {
233 set serverthread [thread::create -preserved]
234 thread::send -async $serverthread {after 1000 ; thread::release}
235 catch {set res [thread::join $serverthread]} msg
236 while {[llength [thread::names]] > 1} {
237 after 20
238 }
239 lrange $msg 0 2
240 } {cannot join thread}
241
242 test thread-6.1 {freeing very large object trees in a thread} thread {
243 # conceptual duplicate of obj-32.1
244 set serverthread [thread::create -preserved]
245 thread::send -async $serverthread {
246 set x {}
247 for {set i 0} {$i<100000} {incr i} {
248 set x [list $x {}]
249 }
250 unset x
251 }
252 thread::release -wait $serverthread
253 } 0
254
255 # TIP #285: Script cancellation support
256 test thread-7.4 {cancel: pure bytecode loop} -constraints {thread drainEventQueue} -setup {
257 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
258 } -body {
259 set serverthread [thread::create -joinable \
260 [string map [list %ID% [thread::id]] {
261 proc foobar {} {
262 if {![info exists foo]} then {
263 # signal the primary thread that we are ready
264 # to be canceled now (we are running).
265 thread::send %ID% [list set ::threadIdStarted [thread::id]]
266 set foo 1
267 }
268 while {1} {
269 # No bytecode at all here...
270 }
271 }
272 foobar
273 }]]
274 # wait for other thread to signal "ready to cancel"
275 vwait ::threadIdStarted
276 set res [thread::cancel $serverthread]
277 vwait ::threadSawError($serverthread)
278 thread::join $serverthread; drainEventQueue
279 list $res [expr {$::threadIdStarted == $serverthread}] \
280 [expr {[info exists ::threadId] ? \
281 $::threadId == $serverthread : 0}] \
282 [expr {[info exists ::threadError($serverthread)] ? \
283 [findThreadError $::threadError($serverthread)] : ""}]
284 } -cleanup {
285 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
286 } -result {{} 1 1 {eval canceled}}
287 test thread-7.5 {cancel: pure inside-command loop} -constraints {thread drainEventQueue} -setup {
288 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
289 } -body {
290 set serverthread [thread::create -joinable \
291 [string map [list %ID% [thread::id]] {
292 proc foobar {} {
293 if {![info exists foo]} then {
294 # signal the primary thread that we are ready
295 # to be canceled now (we are running).
296 thread::send %ID% [list set ::threadIdStarted [thread::id]]
297 set foo 1
298 }
299 set while while
300 $while {1} {
301 # No bytecode at all here...
302 }
303 }
304 foobar
305 }]]
306 # wait for other thread to signal "ready to cancel"
307 vwait ::threadIdStarted
308 set res [thread::cancel $serverthread]
309 vwait ::threadSawError($serverthread)
310 thread::join $serverthread; drainEventQueue
311 list $res [expr {$::threadIdStarted == $serverthread}] \
312 [expr {[info exists ::threadId] ? \
313 $::threadId == $serverthread : 0}] \
314 [expr {[info exists ::threadError($serverthread)] ? \
315 [findThreadError $::threadError($serverthread)] : ""}]
316 } -cleanup {
317 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
318 } -result {{} 1 1 {eval canceled}}
319 test thread-7.6 {cancel: pure bytecode loop -unwind} -constraints {thread drainEventQueue} -setup {
320 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
321 } -body {
322 set serverthread [thread::create -joinable \
323 [string map [list %ID% [thread::id]] {
324 proc foobar {} {
325 if {![info exists foo]} then {
326 # signal the primary thread that we are ready
327 # to be canceled now (we are running).
328 thread::send %ID% [list set ::threadIdStarted [thread::id]]
329 set foo 1
330 }
331 while {1} {
332 # No bytecode at all here...
333 }
334 }
335 foobar
336 }]]
337 # wait for other thread to signal "ready to cancel"
338 vwait ::threadIdStarted
339 set res [thread::cancel -unwind $serverthread]
340 vwait ::threadSawError($serverthread)
341 thread::join $serverthread; drainEventQueue
342 list $res [expr {$::threadIdStarted == $serverthread}] \
343 [expr {[info exists ::threadId] ? \
344 $::threadId == $serverthread : 0}] \
345 [expr {[info exists ::threadError($serverthread)] ? \
346 [findThreadError $::threadError($serverthread)] : ""}]
347 } -cleanup {
348 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
349 } -result {{} 1 1 {eval unwound}}
350 test thread-7.7 {cancel: pure inside-command loop -unwind} -constraints {thread drainEventQueue} -setup {
351 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
352 } -body {
353 set serverthread [thread::create -joinable \
354 [string map [list %ID% [thread::id]] {
355 proc foobar {} {
356 if {![info exists foo]} then {
357 # signal the primary thread that we are ready
358 # to be canceled now (we are running).
359 thread::send %ID% [list set ::threadIdStarted [thread::id]]
360 set foo 1
361 }
362 set while while
363 $while {1} {
364 # No bytecode at all here...
365 }
366 }
367 foobar
368 }]]
369 # wait for other thread to signal "ready to cancel"
370 vwait ::threadIdStarted
371 set res [thread::cancel -unwind $serverthread]
372 vwait ::threadSawError($serverthread)
373 thread::join $serverthread; drainEventQueue
374 list $res [expr {$::threadIdStarted == $serverthread}] \
375 [expr {[info exists ::threadId] ? \
376 $::threadId == $serverthread : 0}] \
377 [expr {[info exists ::threadError($serverthread)] ? \
378 [findThreadError $::threadError($serverthread)] : ""}]
379 } -cleanup {
380 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
381 } -result {{} 1 1 {eval unwound}}
382 test thread-7.8 {cancel: pure bytecode loop custom result} -constraints {thread drainEventQueue} -setup {
383 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
384 } -body {
385 set serverthread [thread::create -joinable \
386 [string map [list %ID% [thread::id]] {
387 proc foobar {} {
388 if {![info exists foo]} then {
389 # signal the primary thread that we are ready
390 # to be canceled now (we are running).
391 thread::send %ID% [list set ::threadIdStarted [thread::id]]
392 set foo 1
393 }
394 while {1} {
395 # No bytecode at all here...
396 }
397 }
398 foobar
399 }]]
400 # wait for other thread to signal "ready to cancel"
401 vwait ::threadIdStarted
402 set res [thread::cancel $serverthread "the eval was canceled"]
403 vwait ::threadSawError($serverthread)
404 thread::join $serverthread; drainEventQueue
405 list $res [expr {$::threadIdStarted == $serverthread}] \
406 [expr {[info exists ::threadId] ? \
407 $::threadId == $serverthread : 0}] \
408 [expr {[info exists ::threadError($serverthread)] ? \
409 [findThreadError $::threadError($serverthread)] : ""}]
410 } -cleanup {
411 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
412 } -result {{} 1 1 {the eval was canceled}}
413 test thread-7.9 {cancel: pure inside-command loop custom result} -constraints {
414 thread
415 drainEventQueue
416 } -setup {
417 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
418 } -body {
419 set serverthread [thread::create -joinable \
420 [string map [list %ID% [thread::id]] {
421 proc foobar {} {
422 if {![info exists foo]} then {
423 # signal the primary thread that we are ready
424 # to be canceled now (we are running).
425 thread::send %ID% [list set ::threadIdStarted [thread::id]]
426 set foo 1
427 }
428 set while while
429 $while {1} {
430 # No bytecode at all here...
431 }
432 }
433 foobar
434 }]]
435 # wait for other thread to signal "ready to cancel"
436 vwait ::threadIdStarted
437 set res [thread::cancel $serverthread "the eval was canceled"]
438 vwait ::threadSawError($serverthread)
439 thread::join $serverthread; drainEventQueue
440 list $res [expr {$::threadIdStarted == $serverthread}] \
441 [expr {[info exists ::threadId] ? \
442 $::threadId == $serverthread : 0}] \
443 [expr {[info exists ::threadError($serverthread)] ? \
444 [findThreadError $::threadError($serverthread)] : ""}]
445 } -cleanup {
446 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
447 } -result {{} 1 1 {the eval was canceled}}
448 test thread-7.10 {cancel: pure bytecode loop custom result -unwind} -constraints {
449 thread
450 drainEventQueue
451 } -setup {
452 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
453 } -body {
454 set serverthread [thread::create -joinable \
455 [string map [list %ID% [thread::id]] {
456 proc foobar {} {
457 if {![info exists foo]} then {
458 # signal the primary thread that we are ready
459 # to be canceled now (we are running).
460 thread::send %ID% [list set ::threadIdStarted [thread::id]]
461 set foo 1
462 }
463 while {1} {
464 # No bytecode at all here...
465 }
466 }
467 foobar
468 }]]
469 # wait for other thread to signal "ready to cancel"
470 vwait ::threadIdStarted
471 set res [thread::cancel -unwind $serverthread "the eval was unwound"]
472 vwait ::threadSawError($serverthread)
473 thread::join $serverthread; drainEventQueue
474 list $res [expr {$::threadIdStarted == $serverthread}] \
475 [expr {[info exists ::threadId] ? \
476 $::threadId == $serverthread : 0}] \
477 [expr {[info exists ::threadError($serverthread)] ? \
478 [findThreadError $::threadError($serverthread)] : ""}]
479 } -cleanup {
480 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
481 } -result {{} 1 1 {the eval was unwound}}
482 test thread-7.11 {cancel: pure inside-command loop custom result -unwind} -constraints {
483 thread
484 drainEventQueue
485 } -setup {
486 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
487 } -body {
488 set serverthread [thread::create -joinable \
489 [string map [list %ID% [thread::id]] {
490 proc foobar {} {
491 if {![info exists foo]} then {
492 # signal the primary thread that we are ready
493 # to be canceled now (we are running).
494 thread::send %ID% [list set ::threadIdStarted [thread::id]]
495 set foo 1
496 }
497 set while while
498 $while {1} {
499 # No bytecode at all here...
500 }
501 }
502 foobar
503 }]]
504 # wait for other thread to signal "ready to cancel"
505 vwait ::threadIdStarted
506 set res [thread::cancel -unwind $serverthread "the eval was unwound"]
507 vwait ::threadSawError($serverthread)
508 thread::join $serverthread; drainEventQueue
509 list $res [expr {$::threadIdStarted == $serverthread}] \
510 [expr {[info exists ::threadId] ? \
511 $::threadId == $serverthread : 0}] \
512 [expr {[info exists ::threadError($serverthread)] ? \
513 [findThreadError $::threadError($serverthread)] : ""}]
514 } -cleanup {
515 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
516 } -result {{} 1 1 {the eval was unwound}}
517 test thread-7.12 {cancel: after} -constraints {thread drainEventQueue} -setup {
518 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
519 } -body {
520 set serverthread [thread::create -joinable \
521 [string map [list %ID% [thread::id]] {
522 if {![info exists foo]} then {
523 # signal the primary thread that we are ready
524 # to be canceled now (we are running).
525 thread::send %ID% [list set ::threadIdStarted [thread::id]]
526 set foo 1
527 }
528 after 30000
529 }]]
530 # wait for other thread to signal "ready to cancel"
531 vwait ::threadIdStarted
532 set res [thread::cancel $serverthread]
533 vwait ::threadSawError($serverthread)
534 thread::join $serverthread; drainEventQueue
535 list $res [expr {$::threadIdStarted == $serverthread}] \
536 [expr {[info exists ::threadId] ? \
537 $::threadId == $serverthread : 0}] \
538 [expr {[info exists ::threadError($serverthread)] ? \
539 [findThreadError $::threadError($serverthread)] : ""}]
540 } -cleanup {
541 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
542 } -result {{} 1 1 {eval canceled}}
543 test thread-7.13 {cancel: after -unwind} -constraints {thread drainEventQueue} -setup {
544 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
545 } -body {
546 set serverthread [thread::create -joinable \
547 [string map [list %ID% [thread::id]] {
548 if {![info exists foo]} then {
549 # signal the primary thread that we are ready
550 # to be canceled now (we are running).
551 thread::send %ID% [list set ::threadIdStarted [thread::id]]
552 set foo 1
553 }
554 after 30000
555 }]]
556 # wait for other thread to signal "ready to cancel"
557 vwait ::threadIdStarted
558 set res [thread::cancel -unwind $serverthread]
559 vwait ::threadSawError($serverthread)
560 thread::join $serverthread; drainEventQueue
561 list $res [expr {$::threadIdStarted == $serverthread}] \
562 [expr {[info exists ::threadId] ? \
563 $::threadId == $serverthread : 0}] \
564 [expr {[info exists ::threadError($serverthread)] ? \
565 [findThreadError $::threadError($serverthread)] : ""}]
566 } -cleanup {
567 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
568 } -result {{} 1 1 {eval unwound}}
569 test thread-7.14 {cancel: vwait} -constraints {thread drainEventQueue} -setup {
570 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
571 } -body {
572 set serverthread [thread::create -joinable \
573 [string map [list %ID% [thread::id]] {
574 if {![info exists foo]} then {
575 # signal the primary thread that we are ready
576 # to be canceled now (we are running).
577 thread::send %ID% [list set ::threadIdStarted [thread::id]]
578 set foo 1
579 }
580 vwait forever
581 }]]
582 # wait for other thread to signal "ready to cancel"
583 vwait ::threadIdStarted
584 set res [thread::cancel $serverthread]
585 vwait ::threadSawError($serverthread)
586 thread::join $serverthread; drainEventQueue
587 list $res [expr {$::threadIdStarted == $serverthread}] \
588 [expr {[info exists ::threadId] ? \
589 $::threadId == $serverthread : 0}] \
590 [expr {[info exists ::threadError($serverthread)] ? \
591 [findThreadError $::threadError($serverthread)] : ""}]
592 } -cleanup {
593 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
594 } -result {{} 1 1 {eval canceled}}
595 test thread-7.15 {cancel: vwait -unwind} -constraints {thread drainEventQueue} -setup {
596 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
597 } -body {
598 set serverthread [thread::create -joinable \
599 [string map [list %ID% [thread::id]] {
600 if {![info exists foo]} then {
601 # signal the primary thread that we are ready
602 # to be canceled now (we are running).
603 thread::send %ID% [list set ::threadIdStarted [thread::id]]
604 set foo 1
605 }
606 vwait forever
607 }]]
608 # wait for other thread to signal "ready to cancel"
609 vwait ::threadIdStarted
610 set res [thread::cancel -unwind $serverthread]
611 vwait ::threadSawError($serverthread)
612 thread::join $serverthread; drainEventQueue
613 list $res [expr {$::threadIdStarted == $serverthread}] \
614 [expr {[info exists ::threadId] ? \
615 $::threadId == $serverthread : 0}] \
616 [expr {[info exists ::threadError($serverthread)] ? \
617 [findThreadError $::threadError($serverthread)] : ""}]
618 } -cleanup {
619 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
620 } -result {{} 1 1 {eval unwound}}
621 test thread-7.16 {cancel: expr} -constraints {thread drainEventQueue} -setup {
622 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
623 } -body {
624 set serverthread [thread::create -joinable \
625 [string map [list %ID% [thread::id]] {
626 set i [interp create]
627 $i eval "package require -exact Thread [package present Thread]"
628 $i eval {
629 if {![info exists foo]} then {
630 # signal the primary thread that we are ready
631 # to be canceled now (we are running).
632
633 thread::send %ID% [list set ::threadIdStarted [thread::id]]
634 set foo 1
635 }
636 expr {[while {1} {incr x}]}
637 }
638 }]]
639 # wait for other thread to signal "ready to cancel"
640 vwait ::threadIdStarted
641 set res [thread::cancel $serverthread]
642 vwait ::threadSawError($serverthread)
643 thread::join $serverthread; drainEventQueue
644 list $res [expr {$::threadIdStarted == $serverthread}] \
645 [expr {[info exists ::threadId] ? \
646 $::threadId == $serverthread : 0}] \
647 [expr {[info exists ::threadError($serverthread)] ? \
648 [findThreadError $::threadError($serverthread)] : ""}]
649 } -cleanup {
650 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
651 } -result {{} 1 1 {eval canceled}}
652 test thread-7.17 {cancel: expr -unwind} -constraints {thread drainEventQueue} -setup {
653 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
654 } -body {
655 set serverthread [thread::create -joinable \
656 [string map [list %ID% [thread::id]] {
657 set i [interp create]
658 $i eval "package require -exact Thread [package present Thread]"
659 $i eval {
660 if {![info exists foo]} then {
661 # signal the primary thread that we are ready
662 # to be canceled now (we are running).
663 thread::send %ID% [list set ::threadIdStarted [thread::id]]
664 set foo 1
665 }
666 expr {[while {1} {incr x}]}
667 }
668 }]]
669 # wait for other thread to signal "ready to cancel"
670 vwait ::threadIdStarted
671 set res [thread::cancel -unwind $serverthread]
672 vwait ::threadSawError($serverthread)
673 thread::join $serverthread; drainEventQueue
674 list $res [expr {$::threadIdStarted == $serverthread}] \
675 [expr {[info exists ::threadId] ? \
676 $::threadId == $serverthread : 0}] \
677 [expr {[info exists ::threadError($serverthread)] ? \
678 [findThreadError $::threadError($serverthread)] : ""}]
679 } -cleanup {
680 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
681 } -result {{} 1 1 {eval unwound}}
682 test thread-7.18 {cancel: expr bignum} {thread drainEventQueue knownBug} {
683 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
684 set serverthread [thread::create -joinable \
685 [string map [list %ID% [thread::id]] {
686 set i [interp create]
687 $i eval "package require -exact Thread [package present Thread]"
688 $i eval {
689 if {![info exists foo]} then {
690 # signal the primary thread that we are ready
691 # to be canceled now (we are running).
692 thread::send %ID% [list set ::threadIdStarted [thread::id]]
693 set foo 1
694 }
695 #
696 # BUGBUG: This will not cancel because libtommath
697 # does not check Tcl_Canceled.
698 #
699 expr {2**99999}
700 }
701 }]]
702 # wait for other thread to signal "ready to cancel"
703 vwait ::threadIdStarted; after 1000
704 set res [thread::cancel $serverthread]
705 vwait ::threadSawError($serverthread); # WARNING: Never returns (see above).
706 thread::join $serverthread; drainEventQueue; # WARNING: Never returns (see above).
707 list $res [expr {[info exists ::threadIdStarted] ? \
708 $::threadIdStarted == $serverthread : 0}] \
709 [expr {[info exists ::threadId] ? \
710 $::threadId == $serverthread : 0}] \
711 [expr {[info exists ::threadError($serverthread)] ? \
712 [findThreadError $::threadError($serverthread)] : ""}]
713 } {{} 1 0 {}}
714 test thread-7.19 {cancel: expr bignum -unwind} {thread drainEventQueue knownBug} {
715 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
716 set serverthread [thread::create -joinable \
717 [string map [list %ID% [thread::id]] {
718 set i [interp create]
719 $i eval "package require -exact Thread [package present Thread]"
720 $i eval {
721 if {![info exists foo]} then {
722 # signal the primary thread that we are ready
723 # to be canceled now (we are running).
724 thread::send %ID% [list set ::threadIdStarted [thread::id]]
725 set foo 1
726 }
727 #
728 # BUGBUG: This will not cancel because libtommath
729 # does not check Tcl_Canceled.
730 #
731 expr {2**99999}
732 }
733 }]]
734 # wait for other thread to signal "ready to cancel"
735 vwait ::threadIdStarted; after 1000
736 set res [thread::cancel -unwind $serverthread]
737 vwait ::threadSawError($serverthread); # WARNING: Never returns (see above).
738 thread::join $serverthread; drainEventQueue; # WARNING: Never returns (see above).
739 list $res [expr {[info exists ::threadIdStarted] ? \
740 $::threadIdStarted == $serverthread : 0}] \
741 [expr {[info exists ::threadId] ? \
742 $::threadId == $serverthread : 0}] \
743 [expr {[info exists ::threadError($serverthread)] ? \
744 [findThreadError $::threadError($serverthread)] : ""}]
745 } {{} 1 0 {}}
746 test thread-7.20 {cancel: subst} -constraints {thread drainEventQueue} -setup {
747 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
748 } -body {
749 set serverthread [thread::create -joinable \
750 [string map [list %ID% [thread::id]] {
751 set i [interp create]
752 $i eval "package require -exact Thread [package present Thread]"
753 $i eval {
754 if {![info exists foo]} then {
755 # signal the primary thread that we are ready
756 # to be canceled now (we are running).
757 thread::send %ID% [list set ::threadIdStarted [thread::id]]
758 set foo 1
759 }
760 subst {[while {1} {incr x}]}
761 }
762 }]]
763 # wait for other thread to signal "ready to cancel"
764 vwait ::threadIdStarted
765 set res [thread::cancel $serverthread]
766 vwait ::threadSawError($serverthread)
767 thread::join $serverthread; drainEventQueue
768 list $res [expr {$::threadIdStarted == $serverthread}] \
769 [expr {[info exists ::threadId] ? \
770 $::threadId == $serverthread : 0}] \
771 [expr {[info exists ::threadError($serverthread)] ? \
772 [findThreadError $::threadError($serverthread)] : ""}]
773 } -cleanup {
774 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
775 } -result {{} 1 1 {eval canceled}}
776 test thread-7.21 {cancel: subst -unwind} -constraints {thread drainEventQueue} -setup {
777 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
778 } -body {
779 set serverthread [thread::create -joinable \
780 [string map [list %ID% [thread::id]] {
781 set i [interp create]
782 $i eval "package require -exact Thread [package present Thread]"
783 $i eval {
784 if {![info exists foo]} then {
785 # signal the primary thread that we are ready
786 # to be canceled now (we are running).
787 thread::send %ID% [list set ::threadIdStarted [thread::id]]
788 set foo 1
789 }
790 subst {[while {1} {incr x}]}
791 }
792 }]]
793 # wait for other thread to signal "ready to cancel"
794 vwait ::threadIdStarted
795 set res [thread::cancel -unwind $serverthread]
796 vwait ::threadSawError($serverthread)
797 thread::join $serverthread; drainEventQueue
798 list $res [expr {$::threadIdStarted == $serverthread}] \
799 [expr {[info exists ::threadId] ? \
800 $::threadId == $serverthread : 0}] \
801 [expr {[info exists ::threadError($serverthread)] ? \
802 [findThreadError $::threadError($serverthread)] : ""}]
803 } -cleanup {
804 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
805 } -result {{} 1 1 {eval unwound}}
806 test thread-7.22 {cancel: child interp} -constraints {thread drainEventQueue} -setup {
807 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
808 } -body {
809 set serverthread [thread::create -joinable \
810 [string map [list %ID% [thread::id]] {
811 set i [interp create]
812 $i eval "package require -exact Thread [package present Thread]"
813 $i eval {
814 if {![info exists foo]} then {
815 # signal the primary thread that we are ready
816 # to be canceled now (we are running).
817 thread::send %ID% [list set ::threadIdStarted [thread::id]]
818 set foo 1
819 }
820 while {1} {}
821 }
822 }]]
823 # wait for other thread to signal "ready to cancel"
824 vwait ::threadIdStarted
825 set res [thread::cancel $serverthread]
826 vwait ::threadSawError($serverthread)
827 thread::join $serverthread; drainEventQueue
828 list $res [expr {$::threadIdStarted == $serverthread}] \
829 [expr {[info exists ::threadId] ? \
830 $::threadId == $serverthread : 0}] \
831 [expr {[info exists ::threadError($serverthread)] ? \
832 [findThreadError $::threadError($serverthread)] : ""}]
833 } -cleanup {
834 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
835 } -result {{} 1 1 {eval canceled}}
836 test thread-7.23 {cancel: child interp -unwind} -constraints {thread drainEventQueue} -setup {
837 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
838 } -body {
839 set serverthread [thread::create -joinable \
840 [string map [list %ID% [thread::id]] {
841 set i [interp create]
842 $i eval "package require -exact Thread [package present Thread]"
843 $i eval {
844 if {![info exists foo]} then {
845 # signal the primary thread that we are ready
846 # to be canceled now (we are running).
847 thread::send %ID% [list set ::threadIdStarted [thread::id]]
848 set foo 1
849 }
850 set while while; $while {1} {}
851 }
852 }]]
853 # wait for other thread to signal "ready to cancel"
854 vwait ::threadIdStarted
855 set res [thread::cancel -unwind $serverthread]
856 vwait ::threadSawError($serverthread)
857 thread::join $serverthread; drainEventQueue
858 list $res [expr {$::threadIdStarted == $serverthread}] \
859 [expr {[info exists ::threadId] ? \
860 $::threadId == $serverthread : 0}] \
861 [expr {[info exists ::threadError($serverthread)] ? \
862 [findThreadError $::threadError($serverthread)] : ""}]
863 } -cleanup {
864 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
865 } -result {{} 1 1 {eval unwound}}
866 test thread-7.24 {cancel: nested catch inside pure bytecode loop} {thread drainEventQueue} {
867 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
868 set serverthread [thread::create -joinable \
869 [string map [list %ID% [thread::id]] {
870 proc foobar {} {
871 while {1} {
872 if {![info exists foo]} then {
873 # signal the primary thread that we are ready
874 # to be canceled now (we are running).
875 thread::send %ID% [list set ::threadIdStarted [thread::id]]
876 set foo 1
877 }
878 catch {
879 while {1} {
880 catch {
881 while {1} {
882 # we must call update here because otherwise
883 # the thread cannot even be forced to exit.
884 update
885 }
886 }
887 }
888 }
889 }
890 }
891 foobar
892 }]]
893 # wait for other thread to signal "ready to cancel"
894 vwait ::threadIdStarted; after 1000
895 set res [thread::cancel $serverthread]
896 threadSuperKill $serverthread
897 vwait ::threadSawError($serverthread)
898 thread::join $serverthread; drainEventQueue
899 list $res [expr {[info exists ::threadIdStarted] ? \
900 $::threadIdStarted == $serverthread : 0}] \
901 [expr {[info exists ::threadId] ? \
902 $::threadId == $serverthread : 0}] \
903 [expr {[info exists ::threadError($serverthread)] ? \
904 [findThreadError $::threadError($serverthread)] : ""}]
905 } {{} 1 0 {}}
906 test thread-7.25 {cancel: nested catch inside pure inside-command loop} {thread drainEventQueue} {
907 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
908 set serverthread [thread::create -joinable \
909 [string map [list %ID% [thread::id]] {
910 proc foobar {} {
911 set catch catch
912 set while while
913 $while {1} {
914 if {![info exists foo]} then {
915 # signal the primary thread that we are ready
916 # to be canceled now (we are running).
917 thread::send %ID% [list set ::threadIdStarted [thread::id]]
918 set foo 1
919 }
920 $catch {
921 $while {1} {
922 $catch {
923 $while {1} {
924 # we must call update here because otherwise
925 # the thread cannot even be forced to exit.
926 update
927 }
928 }
929 }
930 }
931 }
932 }
933 foobar
934 }]]
935 # wait for other thread to signal "ready to cancel"
936 vwait ::threadIdStarted; after 1000
937 set res [thread::cancel $serverthread]
938 threadSuperKill $serverthread
939 vwait ::threadSawError($serverthread)
940 thread::join $serverthread; drainEventQueue
941 list $res [expr {[info exists ::threadIdStarted] ? \
942 $::threadIdStarted == $serverthread : 0}] \
943 [expr {[info exists ::threadId] ? \
944 $::threadId == $serverthread : 0}] \
945 [expr {[info exists ::threadError($serverthread)] ? \
946 [findThreadError $::threadError($serverthread)] : ""}]
947 } {{} 1 0 {}}
948 test thread-7.26 {cancel: send async cancel bad interp path} {thread drainEventQueue} {
949 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
950 set serverthread [thread::create -joinable \
951 [string map [list %ID% [thread::id]] {
952 proc foobar {} {
953 while {1} {
954 if {![info exists foo]} then {
955 # signal the primary thread that we are ready
956 # to be canceled now (we are running).
957 thread::send %ID% [list set ::threadIdStarted [thread::id]]
958 set foo 1
959 }
960 update
961 }
962 }
963 foobar
964 }]]
965 # wait for other thread to signal "ready to cancel"
966 vwait ::threadIdStarted
967 catch {thread::send $serverthread {interp cancel -- bad}} msg
968 thread::send -async $serverthread {interp cancel -unwind}
969 vwait ::threadSawError($serverthread)
970 thread::join $serverthread; drainEventQueue
971 list [expr {$::threadIdStarted == $serverthread}] $msg
972 } {1 {could not find interpreter "bad"}}
973 test thread-7.27 {cancel: send async cancel -- switch} -constraints {thread drainEventQueue} -setup {
974 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
975 } -body {
976 set serverthread [thread::create -joinable \
977 [string map [list %ID% [thread::id]] {
978 set i [interp create -- -unwind]
979 $i eval "package require -exact Thread [package present Thread]"
980 $i eval {
981 proc foobar {} {
982 while {1} {
983 if {![info exists foo]} then {
984 # signal the primary thread that we are ready
985 # to be canceled now (we are running).
986 thread::send %ID% [list set ::threadIdStarted [thread::id]]
987 set foo 1
988 }
989 update
990 }
991 }
992 foobar
993 }
994 }]]
995 # wait for other thread to signal "ready to cancel"
996 vwait ::threadIdStarted
997 set res [thread::send -async $serverthread {interp cancel -- -unwind}]
998 vwait ::threadSawError($serverthread)
999 thread::join $serverthread; drainEventQueue
1000 list $res [expr {$::threadIdStarted == $serverthread}] \
1001 [expr {[info exists ::threadId] ? \
1002 $::threadId == $serverthread : 0}] \
1003 [expr {[info exists ::threadError($serverthread)] ? \
1004 [findThreadError $::threadError($serverthread)] : ""}]
1005 } -cleanup {
1006 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1007 } -result {{} 1 1 {eval canceled}}
1008 test thread-7.28 {cancel: send async cancel nested catch inside pure bytecode loop} {thread drainEventQueue} {
1009 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1010 set serverthread [thread::create -joinable \
1011 [string map [list %ID% [thread::id]] {
1012 proc foobar {} {
1013 while {1} {
1014 if {![info exists foo]} then {
1015 # signal the primary thread that we are ready
1016 # to be canceled now (we are running).
1017 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1018 set foo 1
1019 }
1020 catch {
1021 while {1} {
1022 catch {
1023 while {1} {
1024 # we must call update here because otherwise
1025 # the thread cannot even be forced to exit.
1026 update
1027 }
1028 }
1029 }
1030 }
1031 }
1032 }
1033 foobar
1034 }]]
1035 # wait for other thread to signal "ready to cancel"
1036 vwait ::threadIdStarted; after 1000
1037 set res [thread::send -async $serverthread {interp cancel}]
1038 threadSuperKill $serverthread
1039 vwait ::threadSawError($serverthread)
1040 thread::join $serverthread; drainEventQueue
1041 list $res [expr {[info exists ::threadIdStarted] ? \
1042 $::threadIdStarted == $serverthread : 0}] \
1043 [expr {[info exists ::threadId] ? \
1044 $::threadId == $serverthread : 0}] \
1045 [expr {[info exists ::threadError($serverthread)] ? \
1046 [findThreadError $::threadError($serverthread)] : ""}]
1047 } {{} 1 1 {eval canceled}}
1048 test thread-7.29 {cancel: send async cancel nested catch pure inside-command loop} {thread drainEventQueue} {
1049 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1050 set serverthread [thread::create -joinable \
1051 [string map [list %ID% [thread::id]] {
1052 proc foobar {} {
1053 set catch catch
1054 set while while
1055 $while {1} {
1056 if {![info exists foo]} then {
1057 # signal the primary thread that we are ready
1058 # to be canceled now (we are running).
1059 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1060 set foo 1
1061 }
1062 $catch {
1063 $while {1} {
1064 $catch {
1065 $while {1} {
1066 # we must call update here because otherwise
1067 # the thread cannot even be forced to exit.
1068 update
1069 }
1070 }
1071 }
1072 }
1073 }
1074 }
1075 foobar
1076 }]]
1077 # wait for other thread to signal "ready to cancel"
1078 vwait ::threadIdStarted; after 1000
1079 set res [thread::send -async $serverthread {interp cancel}]
1080 threadSuperKill $serverthread
1081 vwait ::threadSawError($serverthread)
1082 thread::join $serverthread; drainEventQueue
1083 list $res [expr {[info exists ::threadIdStarted] ? \
1084 $::threadIdStarted == $serverthread : 0}] \
1085 [expr {[info exists ::threadId] ? \
1086 $::threadId == $serverthread : 0}] \
1087 [expr {[info exists ::threadError($serverthread)] ? \
1088 [findThreadError $::threadError($serverthread)] : ""}]
1089 } {{} 1 1 {eval canceled}}
1090 test thread-7.30 {cancel: send async thread cancel nested catch inside pure bytecode loop} {thread drainEventQueue} {
1091 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1092 set serverthread [thread::create -joinable \
1093 [string map [list %ID% [thread::id]] {
1094 proc foobar {} {
1095 while {1} {
1096 if {![info exists foo]} then {
1097 # signal the primary thread that we are ready
1098 # to be canceled now (we are running).
1099 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1100 set foo 1
1101 }
1102 catch {
1103 while {1} {
1104 catch {
1105 while {1} {
1106 # we must call update here because otherwise
1107 # the thread cannot even be forced to exit.
1108 update
1109 }
1110 }
1111 }
1112 }
1113 }
1114 }
1115 foobar
1116 }]]
1117 # wait for other thread to signal "ready to cancel"
1118 vwait ::threadIdStarted; after 1000
1119 set res [thread::send -async $serverthread {thread::cancel [thread::id]}]
1120 threadSuperKill $serverthread
1121 vwait ::threadSawError($serverthread)
1122 thread::join $serverthread; drainEventQueue
1123 list $res [expr {[info exists ::threadIdStarted] ? \
1124 $::threadIdStarted == $serverthread : 0}] \
1125 [expr {[info exists ::threadId] ? \
1126 $::threadId == $serverthread : 0}] \
1127 [expr {[info exists ::threadError($serverthread)] ? \
1128 [findThreadError $::threadError($serverthread)] : ""}]
1129 } {{} 1 1 {eval canceled}}
1130 test thread-7.31 {cancel: send async thread cancel nested catch pure inside-command loop} {thread drainEventQueue} {
1131 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1132 set serverthread [thread::create -joinable \
1133 [string map [list %ID% [thread::id]] {
1134 proc foobar {} {
1135 set catch catch
1136 set while while
1137 $while {1} {
1138 if {![info exists foo]} then {
1139 # signal the primary thread that we are ready
1140 # to be canceled now (we are running).
1141 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1142 set foo 1
1143 }
1144 $catch {
1145 $while {1} {
1146 $catch {
1147 $while {1} {
1148 # we must call update here because otherwise
1149 # the thread cannot even be forced to exit.
1150 update
1151 }
1152 }
1153 }
1154 }
1155 }
1156 }
1157 foobar
1158 }]]
1159 # wait for other thread to signal "ready to cancel"
1160 vwait ::threadIdStarted; after 1000
1161 set res [thread::send -async $serverthread {thread::cancel [thread::id]}]
1162 threadSuperKill $serverthread
1163 vwait ::threadSawError($serverthread)
1164 thread::join $serverthread; drainEventQueue
1165 list $res [expr {[info exists ::threadIdStarted] ? \
1166 $::threadIdStarted == $serverthread : 0}] \
1167 [expr {[info exists ::threadId] ? \
1168 $::threadId == $serverthread : 0}] \
1169 [expr {[info exists ::threadError($serverthread)] ? \
1170 [findThreadError $::threadError($serverthread)] : ""}]
1171 } {{} 1 1 {eval canceled}}
1172 test thread-7.32 {cancel: nested catch inside pure bytecode loop -unwind} -constraints {thread drainEventQueue} -setup {
1173 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1174 } -body {
1175 set serverthread [thread::create -joinable \
1176 [string map [list %ID% [thread::id]] {
1177 proc foobar {} {
1178 while {1} {
1179 if {![info exists foo]} then {
1180 # signal the primary thread that we are ready
1181 # to be canceled now (we are running).
1182 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1183 set foo 1
1184 }
1185 catch {
1186 while {1} {
1187 catch {
1188 while {1} {
1189 # No bytecode at all here...
1190 }
1191 }
1192 }
1193 }
1194 }
1195 }
1196 foobar
1197 }]]
1198 # wait for other thread to signal "ready to cancel"
1199 vwait ::threadIdStarted
1200 set res [thread::cancel -unwind $serverthread]
1201 vwait ::threadSawError($serverthread)
1202 thread::join $serverthread; drainEventQueue
1203 list $res [expr {$::threadIdStarted == $serverthread}] \
1204 [expr {[info exists ::threadId] ? \
1205 $::threadId == $serverthread : 0}] \
1206 [expr {[info exists ::threadError($serverthread)] ? \
1207 [findThreadError $::threadError($serverthread)] : ""}]
1208 } -cleanup {
1209 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1210 } -result {{} 1 1 {eval unwound}}
1211 test thread-7.33 {cancel: nested catch inside pure inside-command loop -unwind} -constraints {thread drainEventQueue} -setup {
1212 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1213 } -body {
1214 set serverthread [thread::create -joinable \
1215 [string map [list %ID% [thread::id]] {
1216 proc foobar {} {
1217 set catch catch
1218 set while while
1219 $while {1} {
1220 if {![info exists foo]} then {
1221 # signal the primary thread that we are ready
1222 # to be canceled now (we are running).
1223 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1224 set foo 1
1225 }
1226 $catch {
1227 $while {1} {
1228 $catch {
1229 $while {1} {
1230 # No bytecode at all here...
1231 }
1232 }
1233 }
1234 }
1235 }
1236 }
1237 foobar
1238 }]]
1239 # wait for other thread to signal "ready to cancel"
1240 vwait ::threadIdStarted
1241 set res [thread::cancel -unwind $serverthread]
1242 vwait ::threadSawError($serverthread)
1243 thread::join $serverthread; drainEventQueue
1244 list $res [expr {$::threadIdStarted == $serverthread}] \
1245 [expr {[info exists ::threadId] ? \
1246 $::threadId == $serverthread : 0}] \
1247 [expr {[info exists ::threadError($serverthread)] ? \
1248 [findThreadError $::threadError($serverthread)] : ""}]
1249 } -cleanup {
1250 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1251 } -result {{} 1 1 {eval unwound}}
1252 test thread-7.34 {cancel: send async cancel nested catch inside pure bytecode loop -unwind} -constraints {thread drainEventQueue} -setup {
1253 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1254 } -body {
1255 set serverthread [thread::create -joinable \
1256 [string map [list %ID% [thread::id]] {
1257 proc foobar {} {
1258 while {1} {
1259 if {![info exists foo]} then {
1260 # signal the primary thread that we are ready
1261 # to be canceled now (we are running).
1262 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1263 set foo 1
1264 }
1265 catch {
1266 while {1} {
1267 catch {
1268 while {1} {
1269 # we must call update here because otherwise
1270 # the thread cannot even be forced to exit.
1271 update
1272 }
1273 }
1274 }
1275 }
1276 }
1277 }
1278 foobar
1279 }]]
1280 # wait for other thread to signal "ready to cancel"
1281 vwait ::threadIdStarted
1282 set res [thread::send -async $serverthread {interp cancel -unwind}]
1283 vwait ::threadSawError($serverthread)
1284 thread::join $serverthread; drainEventQueue
1285 list $res [expr {$::threadIdStarted == $serverthread}] \
1286 [expr {[info exists ::threadId] ? \
1287 $::threadId == $serverthread : 0}] \
1288 [expr {[info exists ::threadError($serverthread)] ? \
1289 [findThreadError $::threadError($serverthread)] : ""}]
1290 } -cleanup {
1291 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1292 } -result {{} 1 1 {eval unwound}}
1293 test thread-7.35 {cancel: send async cancel nested catch inside pure inside-command loop -unwind} -constraints {thread drainEventQueue} -setup {
1294 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1295 } -body {
1296 set serverthread [thread::create -joinable \
1297 [string map [list %ID% [thread::id]] {
1298 proc foobar {} {
1299 set catch catch
1300 set while while
1301 $while {1} {
1302 if {![info exists foo]} then {
1303 # signal the primary thread that we are ready
1304 # to be canceled now (we are running).
1305 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1306 set foo 1
1307 }
1308 $catch {
1309 $while {1} {
1310 $catch {
1311 $while {1} {
1312 # we must call update here because otherwise
1313 # the thread cannot even be forced to exit.
1314 update
1315 }
1316 }
1317 }
1318 }
1319 }
1320 }
1321 foobar
1322 }]]
1323 # wait for other thread to signal "ready to cancel"
1324 vwait ::threadIdStarted
1325 set res [thread::send -async $serverthread {interp cancel -unwind}]
1326 vwait ::threadSawError($serverthread)
1327 thread::join $serverthread; drainEventQueue
1328 list $res [expr {$::threadIdStarted == $serverthread}] \
1329 [expr {[info exists ::threadId] ? \
1330 $::threadId == $serverthread : 0}] \
1331 [expr {[info exists ::threadError($serverthread)] ? \
1332 [findThreadError $::threadError($serverthread)] : ""}]
1333 } -cleanup {
1334 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1335 } -result {{} 1 1 {eval unwound}}
1336 test thread-7.36 {cancel: send async thread cancel nested catch inside pure bytecode loop -unwind} -constraints {thread drainEventQueue} -setup {
1337 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1338 } -body {
1339 set serverthread [thread::create -joinable \
1340 [string map [list %ID% [thread::id]] {
1341 proc foobar {} {
1342 while {1} {
1343 if {![info exists foo]} then {
1344 # signal the primary thread that we are ready
1345 # to be canceled now (we are running).
1346 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1347 set foo 1
1348 }
1349 catch {
1350 while {1} {
1351 catch {
1352 while {1} {
1353 # we must call update here because otherwise
1354 # the thread cannot even be forced to exit.
1355 update
1356 }
1357 }
1358 }
1359 }
1360 }
1361 }
1362 foobar
1363 }]]
1364 # wait for other thread to signal "ready to cancel"
1365 vwait ::threadIdStarted
1366 set res [thread::send -async $serverthread {thread::cancel -unwind [thread::id]}]
1367 vwait ::threadSawError($serverthread)
1368 thread::join $serverthread; drainEventQueue
1369 list $res [expr {$::threadIdStarted == $serverthread}] \
1370 [expr {[info exists ::threadId] ? \
1371 $::threadId == $serverthread : 0}] \
1372 [expr {[info exists ::threadError($serverthread)] ? \
1373 [findThreadError $::threadError($serverthread)] : ""}]
1374 } -cleanup {
1375 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1376 } -result {{} 1 1 {eval unwound}}
1377 test thread-7.37 {cancel: send async thread cancel nested catch inside pure inside-command loop -unwind} -constraints {thread drainEventQueue} -setup {
1378 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1379 } -body {
1380 set serverthread [thread::create -joinable \
1381 [string map [list %ID% [thread::id]] {
1382 proc foobar {} {
1383 set catch catch
1384 set while while
1385 $while {1} {
1386 if {![info exists foo]} then {
1387 # signal the primary thread that we are ready
1388 # to be canceled now (we are running).
1389 thread::send %ID% [list set ::threadIdStarted [thread::id]]
1390 set foo 1
1391 }
1392 $catch {
1393 $while {1} {
1394 $catch {
1395 $while {1} {
1396 # we must call update here because otherwise
1397 # the thread cannot even be forced to exit.
1398 update
1399 }
1400 }
1401 }
1402 }
1403 }
1404 }
1405 foobar
1406 }]]
1407 # wait for other thread to signal "ready to cancel"
1408 vwait ::threadIdStarted
1409 set res [thread::send -async $serverthread {thread::cancel -unwind [thread::id]}]
1410 vwait ::threadSawError($serverthread)
1411 thread::join $serverthread; drainEventQueue
1412 list $res [expr {$::threadIdStarted == $serverthread}] \
1413 [expr {[info exists ::threadId] ? \
1414 $::threadId == $serverthread : 0}] \
1415 [expr {[info exists ::threadError($serverthread)] ? \
1416 [findThreadError $::threadError($serverthread)] : ""}]
1417 } -cleanup {
1418 unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
1419 } -result {{} 1 1 {eval unwound}}
1420
1421 test thread-8.1 {threaded fork stress} -constraints {thread} -setup {
1422 unset -nocomplain ::threadCount ::execCount ::threads ::thread
1423 set ::threadCount 10
1424 set ::execCount 10
1425 } -body {
1426 set ::threads [list]
1427 for {set i 0} {$i < $::threadCount} {incr i} {
1428 lappend ::threads [thread::create -joinable [string map \
1429 [list %execCount% $::execCount] {
1430 proc execLs {} {
1431 if {$::tcl_platform(platform) eq "windows"} then {
1432 return [exec $::env(COMSPEC) /c DIR]
1433 } else {
1434 return [exec /bin/ls]
1435 }
1436 }
1437 set j {%execCount%}; while {[incr j -1]} {execLs}
1438 }]]
1439 }
1440 foreach ::thread $::threads {
1441 thread::join $::thread
1442 }
1443 } -cleanup {
1444 unset -nocomplain ::threadCount ::execCount ::threads ::thread
1445 } -result {}
1446
1447 # cleanup
1448 ::tcltest::cleanupTests
1449 return