Tcl Source Code

Check-in [8157b4c216]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:merge trunk
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dkf-notifier-poll
Files: files | file ages | folders
SHA1: 8157b4c21625fe80ce1a1540c5aa0700b26adb4d
User & Date: dkf 2011-11-11 13:11:08
Context
2011-11-26
16:06
merge trunk check-in: e54193b3cb user: dkf tags: dkf-notifier-poll
2011-11-11
13:11
merge trunk check-in: 8157b4c216 user: dkf tags: dkf-notifier-poll
13:09
win/tclWinConsole.c: Refactor common thread handling patterns. check-in: 042936d44f user: dkf tags: trunk
2011-10-27
18:13
merge trunk check-in: 0af44e08cb user: dkf tags: dkf-notifier-poll
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ChangeLog.




















1
2
3
4
5
6
7



















2011-10-20  Don Porter  <[email protected]>

	* library/http/http.tcl:        Bump to version 2.8.3
	* library/http/pkgIndex.tcl:
	* unix/Makefile.in:
	* win/Makefile.in:

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2011-11-11  Donal K. Fellows  <[email protected]>

	* win/tclWinConsole.c: Refactor common thread handling patterns.

2011-11-11  Alexandre Ferrieux  <[email protected]>

	* tests/zlib.test: [Bug 3428756]: Use nonblocking writes in
	single-threaded IO tests to avoid deadlocks when going beyond OS
	buffers.  Tidy up [chan configure] flags across zlib.test.

2011-11-03  Donal K. Fellows  <[email protected]>

	* unix/tclUnixCompat.c (TclpGetPwNam, TclpGetPwUid, TclpGetGrNam)
	(TclpGetGrGid): Use the elaborate memory management scheme outlined on
	http://www.opengroup.org/austin/docs/austin_328.txt to handle Tcl's
	use of standard reentrant versions of the passwd/group access
	functions so that everything can work on all BSDs. Problem identified
	by Stuart Cassoff.

2011-10-20  Don Porter  <[email protected]>

	* library/http/http.tcl:        Bump to version 2.8.3
	* library/http/pkgIndex.tcl:
	* unix/Makefile.in:
	* win/Makefile.in:

Changes to generic/tclPathObj.c.

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374


1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397

1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
    Tcl_Obj *head,
    Tcl_Obj *tail)
{
    int numBytes;
    const char *bytes;
    Tcl_Obj *copy = Tcl_DuplicateObj(head);

    bytes = Tcl_GetStringFromObj(copy, &numBytes);

    /*


     * Should we perhaps use 'Tcl_FSPathSeparator'? But then what about the
     * Windows special case? Perhaps we should just check if cwd is a root
     * volume. We should never get numBytes == 0 in this code path.
     */

    switch (tclPlatform) {
    case TCL_PLATFORM_UNIX:
	if (bytes[numBytes-1] != '/') {
	    Tcl_AppendToObj(copy, "/", 1);
	}
	break;

    case TCL_PLATFORM_WINDOWS:
	/*
	 * We need the extra 'numBytes != 2', and ':' checks because a volume
	 * relative path doesn't get a '/'. For example 'glob C:*cat*.exe'
	 * will return 'C:cat32.exe'
	 */

	if (bytes[numBytes-1] != '/' && bytes[numBytes-1] != '\\') {
	    if (numBytes!= 2 || bytes[1] != ':') {
		Tcl_AppendToObj(copy, "/", 1);
	    }

	}
	break;
    }

    Tcl_AppendObjToObj(copy, tail);
    return copy;
}

/*
 *---------------------------------------------------------------------------
 *
 * TclFSMakePathRelative --







<
<

>
>
|
<
<
<
|
<
<
<
<
<
<
|
<
<
|
<
<
|
|
<
|
|
|
>
|
<
<
<
<







1365
1366
1367
1368
1369
1370
1371


1372
1373
1374
1375



1376






1377


1378


1379
1380

1381
1382
1383
1384
1385




1386
1387
1388
1389
1390
1391
1392
    Tcl_Obj *head,
    Tcl_Obj *tail)
{
    int numBytes;
    const char *bytes;
    Tcl_Obj *copy = Tcl_DuplicateObj(head);



    /*
     * This is likely buggy when dealing with virtual filesystem drivers
     * that use some character other than "/" as a path separator.  I know
     * of no evidence that such a foolish thing exists.  This solution was



     * chosen so that "JoinPath" operations that pass through either path






     * intrep produce the same results; that is, bugward compatibility.  If


     * we need to fix that bug here, it needs fixing in Tcl_FSJoinPath() too.


     */
    bytes = Tcl_GetStringFromObj(tail, &numBytes);

    if (numBytes == 0) {
	Tcl_AppendToObj(copy, "/", 1);
    } else {
	TclpNativeJoinPath(copy, bytes);
    }




    return copy;
}

/*
 *---------------------------------------------------------------------------
 *
 * TclFSMakePathRelative --

Changes to tests/encoding.test.

27
28
29
30
31
32
33

34
35
36
37
38
39
40
proc runtests {} {
    variable x

# Some tests require the testencoding command
testConstraint testencoding [llength [info commands testencoding]]
testConstraint exec [llength [info commands exec]]
testConstraint testgetdefenc [llength [info commands testgetdefenc]]


# TclInitEncodingSubsystem is tested by the rest of this file
# TclFinalizeEncodingSubsystem is not currently tested

test encoding-1.1 {Tcl_GetEncoding: system encoding} -setup {
    set old [encoding system]
} -constraints {testencoding} -body {







>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
proc runtests {} {
    variable x

# Some tests require the testencoding command
testConstraint testencoding [llength [info commands testencoding]]
testConstraint exec [llength [info commands exec]]
testConstraint testgetdefenc [llength [info commands testgetdefenc]]
testConstraint testfinexit [llength [info commands testfinexit]]

# TclInitEncodingSubsystem is tested by the rest of this file
# TclFinalizeEncodingSubsystem is not currently tested

test encoding-1.1 {Tcl_GetEncoding: system encoding} -setup {
    set old [encoding system]
} -constraints {testencoding} -body {
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
test encoding-24.1 {EscapeFreeProc on open channels} exec {
    runInSubprocess {
	set f [open [file join [file dirname [info script]] iso2022.txt]]
	fconfigure $f -encoding iso2022-jp
	gets $f
    }
} {}
test encoding-24.2 {EscapeFreeProc on open channels} exec {
    # Bug #524674 output
    viewable [runInSubprocess {
	encoding system cp1252;	# Bug #2891556 crash revelator
	fconfigure stdout -encoding iso2022-jp
	puts ab\u4e4e\u68d9g
	testfinexit
    }]







|







414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
test encoding-24.1 {EscapeFreeProc on open channels} exec {
    runInSubprocess {
	set f [open [file join [file dirname [info script]] iso2022.txt]]
	fconfigure $f -encoding iso2022-jp
	gets $f
    }
} {}
test encoding-24.2 {EscapeFreeProc on open channels} {exec testfinexit} {
    # Bug #524674 output
    viewable [runInSubprocess {
	encoding system cp1252;	# Bug #2891556 crash revelator
	fconfigure stdout -encoding iso2022-jp
	puts ab\u4e4e\u68d9g
	testfinexit
    }]

Changes to tests/fileSystem.test.

915
916
917
918
919
920
921




922
923
924
925
926
927
928
} -cleanup {
    cd [tcltest::temporaryDirectory]
    file delete -force tilde
    cd $origdir
} -result {0 0 0 0 1}

# ----------------------------------------------------------------------





cleanupTests
unset -nocomplain drive drives
}
namespace delete ::tcl::test::fileSystem
return








>
>
>
>







915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
} -cleanup {
    cd [tcltest::temporaryDirectory]
    file delete -force tilde
    cd $origdir
} -result {0 0 0 0 1}

# ----------------------------------------------------------------------

test filesystem-10.1 {Bug 3414754} {
    string match */ [file join [pwd] foo/]
} 0

cleanupTests
unset -nocomplain drive drives
}
namespace delete ::tcl::test::fileSystem
return

Changes to tests/safe.test.

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    set token2 [safe::interpAddToAccessPath $i "/dummy/unixlike/test/path"]
    # an error shall occur (http is not anymore in the secure 0-level
    # provided deep path)
    list $token1 $token2 \
	    [catch {interp eval $i {package require http 1}} msg] $msg \
	    [safe::interpConfigure $i]\
	    [safe::interpDelete $i]
} -match glob -result "{\$p(:0:)} {\$p(:[expr 1+[llength [tcl::tm::list]]]:)} 1 {can't find package http 1} {-accessPath {[list $tcl_library */dummy/unixlike/test/path]} -statics 0 -nested 1 -deleteHook {}} {}"

# test source control on file name
test safe-8.1 {safe source control on file} -setup {
    set i "a"
    catch {safe::interpDelete $i}
} -body {
    safe::interpCreate $i







|







201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    set token2 [safe::interpAddToAccessPath $i "/dummy/unixlike/test/path"]
    # an error shall occur (http is not anymore in the secure 0-level
    # provided deep path)
    list $token1 $token2 \
	    [catch {interp eval $i {package require http 1}} msg] $msg \
	    [safe::interpConfigure $i]\
	    [safe::interpDelete $i]
} -match glob -result "{\$p(:0:)} {\$p(:*:)} 1 {can't find package http 1} {-accessPath {[list $tcl_library */dummy/unixlike/test/path]} -statics 0 -nested 1 -deleteHook {}} {}"

# test source control on file name
test safe-8.1 {safe source control on file} -setup {
    set i "a"
    catch {safe::interpDelete $i}
} -body {
    safe::interpCreate $i

Changes to tests/stringObj.test.

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    teststringobj getunicode 1
    teststringobj append 1 bar -1
    teststringobj setlength 1 0
    teststringobj append 1 bar -1
    teststringobj get 1
} {bar}

test stringObj-15.1 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself 1 0
} foofoo
test stringObj-15.2 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself 1 1
} foooo
test stringObj-15.3 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself 1 2
} fooo
test stringObj-15.4 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself 1 3
} foo
test stringObj-15.5 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 0
} foofoo
test stringObj-15.6 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 1
} foooo
test stringObj-15.7 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 2
} fooo
test stringObj-15.8 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 3
} foo


if {[testConstraint testobj]} {
    testobj freeallvars







|



|



|



|



|



|



|



|







440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    teststringobj getunicode 1
    teststringobj append 1 bar -1
    teststringobj setlength 1 0
    teststringobj append 1 bar -1
    teststringobj get 1
} {bar}

test stringObj-15.1 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself 1 0
} foofoo
test stringObj-15.2 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself 1 1
} foooo
test stringObj-15.3 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself 1 2
} fooo
test stringObj-15.4 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself 1 3
} foo
test stringObj-15.5 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself2 1 0
} foofoo
test stringObj-15.6 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself2 1 1
} foooo
test stringObj-15.7 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself2 1 2
} fooo
test stringObj-15.8 {Tcl_Append*ToObj: self appends} testobj {
    teststringobj set 1 foo
    teststringobj appendself2 1 3
} foo


if {[testConstraint testobj]} {
    testobj freeallvars

Changes to tests/thread.test.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Copyright (c) 1998-1999 by Scriptics Corporation.
# Copyright (c) 2006-2008 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

# Some tests require the testthread command

testConstraint testthread [expr {[info commands testthread] != {}}]
testConstraint thread [expr {0 == [catch {package require Thread 2.6}]}]

testConstraint notValgrind [expr {![testConstraint valgrind]}]

proc ThreadError {id info} {
    global threadId threadError
    set threadId $id
    set threadError $info







|






|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Copyright (c) 1998-1999 by Scriptics Corporation.
# Copyright (c) 2006-2008 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2.2
    namespace import -force ::tcltest::*
}

# Some tests require the testthread command

testConstraint testthread [expr {[info commands testthread] != {}}]
testConstraint thread [expr {0 == [catch {package require Thread 2.7}]}]

testConstraint notValgrind [expr {![testConstraint valgrind]}]

proc ThreadError {id info} {
    global threadId threadError
    set threadId $id
    set threadError $info
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	    after 1
	}
	testthread errorproc ThreadError
	return [llength [testthread names]]
    }
}

test thread-1.1 {Tcl_ThreadObjCmd: no args} {testthread} {
    list [catch {testthread} msg] $msg
} {1 {wrong # args: should be "testthread option ?arg ...?"}}
test thread-1.2 {Tcl_ThreadObjCmd: bad option} {testthread} {
    list [catch {testthread foo} msg] $msg
} {1 {bad option "foo": must be cancel, create, event, exit, id, join, names, send, wait, or errorproc}}
test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {thread} {
    llength [thread::names]
} 1
test thread-1.4 {Tcl_ThreadObjCmd: thread create } {thread} {
    set serverthread [thread::create -preserved]
    set numthreads [llength [thread::names]]
    thread::release $serverthread







<
<
<
<
<
<







55
56
57
58
59
60
61






62
63
64
65
66
67
68
	    after 1
	}
	testthread errorproc ThreadError
	return [llength [testthread names]]
    }
}







test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {thread} {
    llength [thread::names]
} 1
test thread-1.4 {Tcl_ThreadObjCmd: thread create } {thread} {
    set serverthread [thread::create -preserved]
    set numthreads [llength [thread::names]]
    thread::release $serverthread
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
} {1}
test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {thread} {
    thread::create {{*}{}}
    update
    after 10
    llength [thread::names]
} {1}
test thread-1.7 {Tcl_ThreadObjCmd: thread id args} {testthread} {
    set x [catch {testthread id x} msg]
    list $x $msg
} {1 {wrong # args: should be "testthread id"}}
test thread-1.8 {Tcl_ThreadObjCmd: thread id} {testthread} {
    string compare [testthread id] $mainThread
} {0}
test thread-1.9 {Tcl_ThreadObjCmd: thread names args} {testthread} {
    set x [catch {testthread names x} msg]
    list $x $msg
} {1 {wrong # args: should be "testthread names"}}
test thread-1.10 {Tcl_ThreadObjCmd: thread id} {testthread} {
    string compare [testthread names] $mainThread
} {0}
test thread-1.11 {Tcl_ThreadObjCmd: send args} {testthread} {
    set x [catch {testthread send} msg]
    list $x $msg
} {1 {wrong # args: should be "testthread send ?-async? id script"}}
test thread-1.12 {Tcl_ThreadObjCmd: send nonint} {testthread} {
    set x [catch {testthread send abc command} msg]
    list $x $msg
} {1 {expected integer but got "abc"}}
test thread-1.13 {Tcl_ThreadObjCmd: send args} {thread} {
    set serverthread [thread::create -preserved]
    set five [thread::send $serverthread {set x 5}]
    thread::release $serverthread
    set five
} 5
test thread-1.14 {Tcl_ThreadObjCmd: send bad id} {testthread} {
    set tid [expr $mainThread + 10]
    set x [catch {testthread send $tid {set x 5}} msg]
    list $x $msg
} {1 {invalid thread id}}
test thread-1.15 {Tcl_ThreadObjCmd: wait} {thread} {
    set serverthread [thread::create -preserved {set z 5 ; thread::wait}]
    set five [thread::send $serverthread {set z}]
    thread::release $serverthread
    set five
} 5
test thread-1.16 {Tcl_ThreadObjCmd: errorproc args} {testthread} {
    set x [catch {testthread errorproc foo bar} msg]
    list $x $msg
} {1 {wrong # args: should be "testthread errorproc proc"}}
test thread-1.17 {Tcl_ThreadObjCmd: errorproc change} {testthread} {
    testthread errorproc foo
    testthread errorproc ThreadError
} {}

# The tests above also cover:
# TclCreateThread, except when pthread_create fails
# NewThread, safe and regular
# ThreadErrorProc, except for printing to standard error

test thread-2.1 {ListUpdateInner and ListRemove} {thread} {







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






<
<
<
<
<






<
<
<
<
<
<
<
<







83
84
85
86
87
88
89






















90
91
92
93
94
95





96
97
98
99
100
101








102
103
104
105
106
107
108
} {1}
test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {thread} {
    thread::create {{*}{}}
    update
    after 10
    llength [thread::names]
} {1}






















test thread-1.13 {Tcl_ThreadObjCmd: send args} {thread} {
    set serverthread [thread::create -preserved]
    set five [thread::send $serverthread {set x 5}]
    thread::release $serverthread
    set five
} 5





test thread-1.15 {Tcl_ThreadObjCmd: wait} {thread} {
    set serverthread [thread::create -preserved {set z 5 ; thread::wait}]
    set five [thread::send $serverthread {set z}]
    thread::release $serverthread
    set five
} 5









# The tests above also cover:
# TclCreateThread, except when pthread_create fails
# NewThread, safe and regular
# ThreadErrorProc, except for printing to standard error

test thread-2.1 {ListUpdateInner and ListRemove} {thread} {
253
254
255
256
257
258
259
260






261


262









263
264
265
266




267
268


269



270


271









272

273
274




275




276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302


303
304
305
306

307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334


335
336
337
338

339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365


366
367
368

369

370

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397


398
399
400

401

402

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

495

496

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523


524
525
526
527

528

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549


550
551
552
553

554

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575


576
577
578
579

580

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601


602
603
604
605

606

607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622



623
624
625
626
627


628
629
630
631

632

633
634
635
636
637
638
639
640
641
642
643
644


645
646
647
648
649
650
651
652
653
654
655
656
657


658
659
660
661

662

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687


688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

760

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785


786
787
788
789

790

791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815


816
817
818
819

820

821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845


846
847
848
849

850

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875


876
877
878
879
880
881
882
883
	}
	unset x
    }
    thread::release -wait $serverthread
} 0

# TIP #285: Script cancellation support
test thread-7.1 {cancel: args} {testthread} {






    set x [catch {testthread cancel} msg]


    list $x $msg









} {1 {wrong # args: should be "testthread cancel ?-unwind? id ?result?"}}
test thread-7.2 {cancel: nonint} {testthread} {
    set x [catch {testthread cancel abc} msg]
    list $x $msg




} {1 {expected integer but got "abc"}}
test thread-7.3 {cancel: bad id} {testthread} {


    set tid [expr $mainThread + 10]



    set x [catch {testthread cancel $tid} msg]


    list $x $msg









} {1 {invalid thread id}}

test thread-7.4 {cancel: pure bytecode loop} {testthread} {
    threadReap




    unset -nocomplain ::threadError ::threadId ::threadIdStarted




    set serverthread [testthread create -joinable {

	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.5 {cancel: pure inside-command loop} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.6 {cancel: pure bytecode loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.7 {cancel: pure inside-command loop -unwind} {testthread} {
    threadReap

    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

    	  proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.8 {cancel: pure bytecode loop custom result} {testthread} {
    threadReap

    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread "the eval was canceled"]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 1 {the eval was canceled}}
test thread-7.9 {cancel: pure inside-command loop custom result} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
    	  proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread "the eval was canceled"]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 1 {the eval was canceled}}
test thread-7.10 {cancel: pure bytecode loop custom result -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread "the eval was unwound"]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 1 {the eval was unwound}}
test thread-7.11 {cancel: pure inside-command loop custom result -unwind} {testthread} {
    threadReap

    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

    	  proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread "the eval was unwound"]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {the eval was unwound}}
test thread-7.12 {cancel: after} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).
	    testthread send [testthread id -main] \
		    [list set ::threadIdStarted [testthread id]]
	    set foo 1
	}
	after 30000
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.13 {cancel: after -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).
	    testthread send [testthread id -main] \
		    [list set ::threadIdStarted [testthread id]]
	    set foo 1
	}
	after 30000
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.14 {cancel: vwait} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).
	    testthread send [testthread id -main] \
		    [list set ::threadIdStarted [testthread id]]
	    set foo 1
	}
	vwait forever
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.15 {cancel: vwait -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).
	    testthread send [testthread id -main] \
		    [list set ::threadIdStarted [testthread id]]
	    set foo 1
	}
	vwait forever
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \



		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.16 {cancel: expr} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    expr {[while {1} {incr x}]}
	}


    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.17 {cancel: expr -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    expr {[while {1} {incr x}]}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.18 {cancel: expr bignum} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    #
      # TODO: This will not cancel because libtommath
      #       does not check Tcl_Canceled.
      #
	    expr {2**99999}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.19 {cancel: expr bignum -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    #
      # TODO: This will not cancel because libtommath
      #       does not check Tcl_Canceled.
      #
	    expr {2**99999}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.20 {cancel: subst} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    subst {[while {1} {incr x}]}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.21 {cancel: subst -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    subst {[while {1} {incr x}]}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.22 {cancel: slave interp} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    while {1} {}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.23 {cancel: slave interp -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	set i [interp create]
	interp alias $i testthread {} testthread
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		testthread send [testthread id -main] \
			[list set ::threadIdStarted [testthread id]]
		set foo 1
	    }
	    set while while; $while {1} {}
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.24 {cancel: nested catch inside pure bytecode loop} {notValgrind testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {







|
>
>
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
|
|
|
|
>
>
>
>
|
|
>
>
|
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
|
>
|
|
>
>
>
>

>
>
>
>
|
>




<
|







|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>




<
|








|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>




<
|







|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
|
>

>
|
>
|



<
|








|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
|
>

>
|
>




<
|







|

|
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
|
<
|
<
|
<
<
|

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
>

>
|
>
|



<
|








|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>



<
|



|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>



<
|



|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>



<
|



|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>



<
|



|

<
<
<
<
<
|
>
>
>
|
<
|
<
|
>
>
|
|
<

>
|
>

|




|
|

<
<
|
>
>
|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>

|




<
|




|

|
|
|
<
|
<
|
<
|
<
|
>
>
|




|
|
|
|
|
|
|
|
|
|
|



|
|








|
|
|
|
|





|
|
|
|
|
|
|
|
|
|
|



|
|








|
|
|
|
|

|
<

>
|
>

|




<
|




|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>

|




<
|




|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>

|




<
|




|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>

|




<
|




|

|
|
|
<
|
<
|
<
|
<
|
>
>
|







212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

287
288
289
290
291
292
293
294
295
296
297
298
299

300

301

302

303
304
305
306
307

308
309
310
311
312
313
314
315

316
317
318
319
320
321
322
323
324
325
326
327
328
329

330

331

332

333
334
335
336
337

338
339
340
341
342
343
344
345

346
347
348
349
350
351
352
353
354
355
356
357
358

359

360

361

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

377
378
379
380
381
382
383
384
385
386
387
388
389
390

391

392

393

394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

409
410
411
412
413
414
415
416
417
418
419
420


421





























422



423

424

425


426
427



























428
429
430
431
432
433
434
435
436
437
438
439

440
441
442
443
444
445
446
447
448
449
450
451
452
453

454

455

456

457
458
459
460
461

462
463
464
465
466
467
468

469
470
471
472
473
474
475
476
477

478

479

480

481
482
483
484
485

486
487
488
489
490
491
492

493
494
495
496
497
498
499
500
501

502

503

504

505
506
507
508
509

510
511
512
513
514
515
516

517
518
519
520
521
522
523
524
525

526

527

528

529
530
531
532
533

534
535
536
537
538
539
540

541
542
543
544
545
546





547
548
549
550
551

552

553
554
555
556
557

558
559
560
561
562
563
564
565
566
567
568
569
570


571
572
573
574
575
576
577
578

579

580

581

582
583
584
585
586

587
588
589
590
591
592
593
594
595
596

597
598
599
600
601
602
603
604
605
606

607

608

609

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682

683
684
685
686
687
688
689
690
691
692

693
694
695
696
697
698
699
700
701
702

703

704

705

706
707
708
709
710

711
712
713
714
715
716
717
718
719
720

721
722
723
724
725
726
727
728
729
730

731

732

733

734
735
736
737
738

739
740
741
742
743
744
745
746
747
748

749
750
751
752
753
754
755
756
757
758

759

760

761

762
763
764
765
766

767
768
769
770
771
772
773
774
775
776

777
778
779
780
781
782
783
784
785
786

787

788

789

790
791
792
793
794
795
796
797
798
799
800
	}
	unset x
    }
    thread::release -wait $serverthread
} 0

# TIP #285: Script cancellation support
test thread-7.4 {cancel: pure bytecode loop} -constraints {thread} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel $serverthread]
    vwait ::threadId
    thread::join $serverthread
    list $res [expr {$::threadIdStarted == $serverthread}] \
	      [expr {$::threadId == $serverthread}] \
	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.5 {cancel: pure inside-command loop} -constraints {thread} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).
		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread]
    vwait threadId
    thread::join $serverthread
    list $res [expr {$::threadIdStarted == $serverthread}] \
	      [expr {$::threadId == $serverthread}] \
	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.6 {cancel: pure bytecode loop -unwind} -constraints {thread} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.7 {cancel: pure inside-command loop -unwind} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.8 {cancel: pure bytecode loop custom result} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread "the eval was canceled"]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {the eval was canceled}}
test thread-7.9 {cancel: pure inside-command loop custom result} -constraints {
    thread
} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread "the eval was canceled"]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {the eval was canceled}}
test thread-7.10 {cancel: pure bytecode loop custom result -unwind} -constraints {
    thread
} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread "the eval was unwound"]


    vwait threadId





























    thread::join $serverthread



    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]


} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted



























} -result {{} 1 1 {the eval was unwound}}
test thread-7.11 {cancel: pure inside-command loop custom result -unwind} -constraints {
    thread
} -setup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    set while while
	    $while {1} {
		# No bytecode at all here...
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread "the eval was unwound"]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {the eval was unwound}}
test thread-7.12 {cancel: after} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).

	    thread::send %ID% [list set ::threadIdStarted [thread::id]]
	    set foo 1
	}
	after 30000
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.13 {cancel: after -unwind} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).

	    thread::send %ID% [list set ::threadIdStarted [thread::id]]
	    set foo 1
	}
	after 30000
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.14 {cancel: vwait} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID [thread::id]] {
	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).

	    thread::send %ID% [list set ::threadIdStarted [thread::id]]
	    set foo 1
	}
	vwait forever
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.15 {cancel: vwait -unwind} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	if {![info exists foo]} then {
	    # signal the primary thread that we are ready
	    # to be canceled now (we are running).

	    thread::send %ID% [list set ::threadIdStarted [thread::id]]
	    set foo 1
	}
	vwait forever
    }]]
    # wait for other thread to signal "ready to cancel"





    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait threadId
    thread::join $serverthread
    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.16 {cancel: expr} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1


	    }
	    expr {[while {1} {incr x}]}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.17 {cancel: expr -unwind} -constraints thread -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    expr {[while {1} {incr x}]}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.18 {cancel: expr bignum} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
        set i [interp create]
        interp alias $i testthread {} testthread
        $i eval {
            if {![info exists foo]} then {
                # signal the primary thread that we are ready
                # to be canceled now (we are running).
                testthread send [testthread id -main] \
                        [list set ::threadIdStarted [testthread id]]
                set foo 1
            }
            #
      # TODO: This will not cancel because libtommath
      #       does not check Tcl_Canceled.
      #
            expr {2**99999}
        }
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
                  $::threadIdStarted == $serverthread : 0}] \
              [expr {[info exists ::threadId] ? \
                  $::threadId == $serverthread : 0}] \
              [expr {[info exists ::threadError] ? \
                  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.19 {cancel: expr bignum -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
        set i [interp create]
        interp alias $i testthread {} testthread
        $i eval {
            if {![info exists foo]} then {
                # signal the primary thread that we are ready
                # to be canceled now (we are running).
                testthread send [testthread id -main] \
                        [list set ::threadIdStarted [testthread id]]
                set foo 1
            }
            #
      # TODO: This will not cancel because libtommath
      #       does not check Tcl_Canceled.
      #
            expr {2**99999}
        }
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
                  $::threadIdStarted == $serverthread : 0}] \
              [expr {[info exists ::threadId] ? \
                  $::threadId == $serverthread : 0}] \
              [expr {[info exists ::threadError] ? \
                  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.20 {cancel: subst} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    subst {[while {1} {incr x}]}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.21 {cancel: subst -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    subst {[while {1} {incr x}]}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.22 {cancel: slave interp} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    while {1} {}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.23 {cancel: slave interp -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    if {![info exists foo]} then {
		# signal the primary thread that we are ready
		# to be canceled now (we are running).

		thread::send %ID% [list set ::threadIdStarted [thread::id]]
		set foo 1
	    }
	    set while while; $while {1} {}
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.24 {cancel: nested catch inside pure bytecode loop} {notValgrind testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992

993

994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023


1024
1025
1026
1027
1028
1029
1030
1031
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.26 {cancel: send async cancel bad interp path} {thread} {
    unset -nocomplain ::threadIdStarted
    set serverthread [thread::create -preserved \
	[string map [list MAIN [thread::id]] {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    thread::send MAIN \
			    [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		update
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    catch {thread::send $serverthread {interp cancel -- bad}} msg
    thread::send -async $serverthread {interp cancel -unwind}
    thread::release -wait $serverthread
    list [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
		  $msg
} {1 {could not find interpreter "bad"}}
test thread-7.27 {cancel: send async cancel -- switch} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	interp create -- -unwind
	interp alias -unwind testthread {} testthread
	interp eval -unwind {
	    proc foobar {} {
		while {1} {
		    if {![info exists foo]} then {
			# signal the primary thread that we are ready
			# to be canceled now (we are running).
			testthread send [testthread id -main] \
				[list set ::threadIdStarted [testthread id]]
			set foo 1
		    }
		    update
		}
	    }
	    foobar
	}
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread send -async $serverthread {interp cancel -- -unwind}]
    after 1000; # wait for ThreadErrorProc to be called.
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval canceled}}
test thread-7.28 {cancel: send async cancel nested catch inside pure bytecode loop} {notValgrind testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {







|





<
|








|



<
|
<

|
<

>
|
>
|
|
|





<
|







|

|
|
<
|
|
<
|
<
|
<
|
>
>
|







876
877
878
879
880
881
882
883
884
885
886
887
888

889
890
891
892
893
894
895
896
897
898
899
900
901

902

903
904

905
906
907
908
909
910
911
912
913
914
915
916

917
918
919
920
921
922
923
924
925
926
927
928

929
930

931

932

933
934
935
936
937
938
939
940
941
942
943
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.26 {cancel: send async cancel bad interp path} {thread} {
    unset -nocomplain ::threadIdStarted
    set serverthread [thread::create -preserved \
	[string map [list %ID% [thread::id]] {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		update
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    catch {thread::send $serverthread {interp cancel -- bad}} msg
    thread::send -async $serverthread {interp cancel -unwind}
    thread::release -wait $serverthread

    list [expr {$::threadIdStarted == $serverthread}] $msg

} {1 {could not find interpreter "bad"}}
test thread-7.27 {cancel: send async cancel -- switch} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	set i [interp create -- -unwind]
	$i eval "package require -exact Thread [package present Thread]"
	$i eval {
	    proc foobar {} {
		while {1} {
		    if {![info exists foo]} then {
			# signal the primary thread that we are ready
			# to be canceled now (we are running).

			thread::send %ID% [list set ::threadIdStarted [thread::id]]
			set foo 1
		    }
		    update
		}
	    }
	    foobar
	}
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::send -async $serverthread {interp cancel -- -unwind}]

    vwait ::threadId
    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval canceled}}
test thread-7.28 {cancel: send async cancel nested catch inside pure bytecode loop} {notValgrind testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
    set serverthread [testthread create -joinable {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199

1200

1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234


1235
1236
1237
1238

1239

1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275


1276
1277
1278
1279

1280

1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316


1317
1318
1319
1320

1321

1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359


1360
1361
1362
1363

1364

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400


1401
1402
1403
1404

1405

1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443


1444
1445
1446
1447
1448
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.32 {cancel: nested catch inside pure bytecode loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# No bytecode at all here...
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.33 {cancel: nested catch inside pure inside-command loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# No bytecode at all here...
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread cancel -unwind $serverthread]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.34 {cancel: send async cancel nested catch inside pure bytecode loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread send -async $serverthread {interp cancel -unwind}]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.35 {cancel: send async cancel nested catch inside pure inside-command loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread send -async $serverthread {interp cancel -unwind}]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.36 {cancel: send async testthread cancel nested catch inside pure bytecode loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread send -async $serverthread {testthread cancel -unwind [testthread id]}]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}
test thread-7.37 {cancel: send async testthread cancel nested catch inside pure inside-command loop -unwind} {testthread} {
    threadReap
    unset -nocomplain ::threadError ::threadId ::threadIdStarted

    set serverthread [testthread create -joinable {

	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).
		    testthread send [testthread id -main] \
			    [list set ::threadIdStarted [testthread id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [testthread send -async $serverthread {testthread cancel -unwind [testthread id]}]
    testthread join $serverthread
    while {[testthread event]} {}; # force events to service
    threadReap
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]


} {{} 1 1 {eval unwound}}

# cleanup
::tcltest::cleanupTests
return







|
<

>
|
>





<
|














|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>







<
|














|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>





<
|
















|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>







<
|
















|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>





<
|
















|

|
|
|
<
|
<
|
<
|
<
|
>
>
|
|
<

>
|
>







<
|
















|

|
|
|
<
|
<
|
<
|
<
|
>
>
|




1102
1103
1104
1105
1106
1107
1108
1109

1110
1111
1112
1113
1114
1115
1116
1117
1118

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138

1139

1140

1141

1142
1143
1144
1145
1146

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157

1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177

1178

1179

1180

1181
1182
1183
1184
1185

1186
1187
1188
1189
1190
1191
1192
1193
1194

1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216

1217

1218

1219

1220
1221
1222
1223
1224

1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257

1258

1259

1260

1261
1262
1263
1264
1265

1266
1267
1268
1269
1270
1271
1272
1273
1274

1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296

1297

1298

1299

1300
1301
1302
1303
1304

1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315

1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337

1338

1339

1340

1341
1342
1343
1344
1345
1346
1347
1348
    list $res [expr {[info exists ::threadIdStarted] ? \
		  $::threadIdStarted == $serverthread : 0}] \
	      [expr {[info exists ::threadId] ? \
		  $::threadId == $serverthread : 0}] \
	      [expr {[info exists ::threadError] ? \
		  [lindex [split $::threadError \n] 0] : "" }]
} {{} 1 0 {}}
test thread-7.32 {cancel: nested catch inside pure bytecode loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# No bytecode at all here...
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.33 {cancel: nested catch inside pure inside-command loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# No bytecode at all here...
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::cancel -unwind $serverthread]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.34 {cancel: send async cancel nested catch inside pure bytecode loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::send -async $serverthread {interp cancel -unwind}]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.35 {cancel: send async cancel nested catch inside pure inside-command loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::send -async $serverthread {interp cancel -unwind}]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.36 {cancel: send async thread cancel nested catch inside pure bytecode loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID% [thread::id]] {
	proc foobar {} {
	    while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		catch {
		    while {1} {
			catch {
			    while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::send -async $serverthread {thread::cancel -unwind [thread::id]}]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}
test thread-7.37 {cancel: send async thread cancel nested catch inside pure inside-command loop -unwind} -constraints {thread} -setup {

    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -body {
    set serverthread [thread::create -joinable \
	    [string map [list %ID [thread::id]] {
	proc foobar {} {
	    set catch catch
	    set while while
	    $while {1} {
		if {![info exists foo]} then {
		    # signal the primary thread that we are ready
		    # to be canceled now (we are running).

		    thread::send %ID% [list set ::threadIdStarted [thread::id]]
		    set foo 1
		}
		$catch {
		    $while {1} {
			$catch {
			    $while {1} {
				# we must call update here because otherwise
				# the thread cannot even be forced to exit.
				update
			    }
			}
		    }
		}
	    }
	}
	foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted
    set res [thread::send -async $serverthread {thread::cancel -unwind [thread::id]}]
    vwait ::threadId

    thread::join $serverthread

    list $res [expr {$::threadIdStarted == $serverthread}] \

	      [expr {$::threadId == $serverthread}] \

	      [lindex [split $::threadError \n] 0]
} -cleanup {
    unset -nocomplain ::threadError ::threadId ::threadIdStarted
} -result {{} 1 1 {eval unwound}}

# cleanup
::tcltest::cleanupTests
return

Changes to tests/unixNotfy.test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# This file contains tests for tclUnixNotfy.c.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1997 by 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.

# The tests should not be run if you have a notifier which is unable to
# detect infinite vwaits, as the tests below will hang. The presence of
# the "testthread" command indicates that this is the case.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

# When run in a Tk shell, these tests hang.
testConstraint noTk [expr {0 != [catch {package present Tk}]}]
testConstraint thread [expr {0 == [catch {package require Thread 2.6}]}]
testConstraint testthread [expr {[info commands testthread] != {}}]
# Darwin always uses a threaded notifier
testConstraint unthreaded [expr {
    (![info exist tcl_platform(threaded)] || !$tcl_platform(threaded))
    && $tcl_platform(os) ne "Darwin"
}]

# The next two tests will hang if threads are enabled because the notifier












<
<
<
<








<







1
2
3
4
5
6
7
8
9
10
11
12




13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
# This file contains tests for tclUnixNotfy.c.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1997 by 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.





if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2
    namespace import -force ::tcltest::*
}

# When run in a Tk shell, these tests hang.
testConstraint noTk [expr {0 != [catch {package present Tk}]}]
testConstraint thread [expr {0 == [catch {package require Thread 2.6}]}]

# Darwin always uses a threaded notifier
testConstraint unthreaded [expr {
    (![info exist tcl_platform(threaded)] || !$tcl_platform(threaded))
    && $tcl_platform(os) ne "Darwin"
}]

# The next two tests will hang if threads are enabled because the notifier

Changes to tests/util.test.

11
12
13
14
15
16
17

18
19
20
21
22
23
24
    package require tcltest
    namespace import -force ::tcltest::*
}

testConstraint controversialNaN 1
testConstraint testdstring [llength [info commands testdstring]]
testConstraint testconcatobj [llength [info commands testconcatobj]]


# Big test for correct ordering of data in [expr]

proc testIEEE {} {
    variable ieeeValues
    binary scan [binary format dd -1.0 1.0] c* c
    switch -exact -- $c {







>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    package require tcltest
    namespace import -force ::tcltest::*
}

testConstraint controversialNaN 1
testConstraint testdstring [llength [info commands testdstring]]
testConstraint testconcatobj [llength [info commands testconcatobj]]
testConstraint testdoubledigits [llength [info commands testdoubledigits]]

# Big test for correct ordering of data in [expr]

proc testIEEE {} {
    variable ieeeValues
    binary scan [binary format dd -1.0 1.0] c* c
    switch -exact -- $c {
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171

1172
1173
1174
1175
1176
1177

1178
1179
1180
1181
1182
1183

1184
1185
1186
1187
1188
1189

1190
1191
1192
1193
1194
1195

1196
1197
1198
1199
1200
1201

1202
1203
1204
1205
1206
1207

1208
1209
1210
1211
1212
1213

1214
1215
1216
1217
1218
1219

1220
1221
1222
1223
1224
1225

1226
1227
1228
1229
1230
1231

1232
1233
1234
1235
1236
1237

1238
1239
1240
1241
1242
1243

1244
1245
1246
1247
1248
1249

1250
1251
1252
1253
1254
1255

1256
1257
1258
1259
1260
1261

1262
1263
1264
1265
1266
1267

1268
1269
1270
1271
1272
1273

1274
1275
1276
1277
1278
1279

1280
1281
1282
1283
1284
1285

1286
1287
1288
1289
1290
1291

1292
1293
1294
1295
1296
1297

1298
1299
1300
1301
1302
1303

1304
1305
1306
1307
1308
1309

1310
1311
1312
1313
1314
1315

1316
1317
1318
1319
1320
1321

1322
1323
1324
1325
1326
1327

1328
1329
1330
1331
1332
1333

1334
1335
1336
1337
1338
1339

1340
1341
1342
1343
1344
1345

1346
1347
1348
1349
1350
1351

1352
1353
1354
1355
1356
1357

1358
1359
1360
1361
1362
1363

1364
1365
1366
1367
1368
1369

1370
1371
1372
1373
1374
1375

1376
1377
1378
1379
1380
1381

1382
1383
1384
1385
1386
1387

1388
1389
1390
1391
1392
1393

1394
1395
1396
1397
1398
1399

1400
1401
1402
1403
1404
1405

1406
1407
1408
1409
1410
1411

1412
1413
1414
1415
1416
1417

1418
1419
1420
1421
1422
1423

1424
1425
1426
1427
1428
1429

1430
1431
1432
1433
1434
1435

1436
1437
1438
1439
1440
1441

1442
1443
1444
1445
1446
1447

1448
1449
1450
1451
1452
1453

1454
1455
1456
1457
1458
1459

1460
1461
1462
1463
1464
1465

1466
1467
1468
1469
1470
1471

1472
1473
1474
1475
1476
1477

1478
1479
1480
1481
1482
1483

1484
1485
1486
1487
1488
1489

1490
1491
1492
1493
1494
1495

1496
1497
1498
1499
1500
1501

1502
1503
1504
1505
1506
1507

1508
1509
1510
1511
1512
1513

1514
1515
1516
1517
1518
1519

1520
1521
1522
1523
1524
1525

1526
1527
1528
1529
1530
1531

1532
1533
1534
1535
1536
1537

1538
1539
1540
1541
1542
1543

1544
1545
1546
1547
1548
1549

1550
1551
1552
1553
1554
1555

1556
1557
1558
1559
1560
1561

1562
1563
1564
1565
1566
1567

1568
1569
1570
1571
1572
1573

1574
1575
1576
1577
1578
1579

1580
1581
1582
1583
1584
1585

1586
1587
1588
1589
1590
1591

1592
1593
1594
1595
1596
1597

1598
1599
1600
1601
1602
1603

1604
1605
1606
1607
1608
1609

1610
1611
1612
1613
1614
1615

1616
1617
1618
1619
1620
1621

1622
1623
1624
1625
1626
1627

1628
1629
1630
1631
1632
1633

1634
1635
1636
1637
1638
1639

1640
1641
1642
1643
1644
1645

1646
1647
1648
1649
1650
1651

1652
1653
1654
1655
1656
1657

1658
1659
1660
1661
1662
1663

1664
1665
1666
1667
1668
1669

1670
1671
1672
1673
1674
1675

1676
1677
1678
1679
1680
1681

1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695

1696
1697
1698
1699
1700
1701

1702
1703
1704
1705
1706
1707

1708
1709
1710
1711
1712
1713

1714
1715
1716
1717
1718
1719

1720
1721
1722
1723
1724
1725

1726
1727
1728
1729
1730
1731

1732
1733
1734
1735
1736
1737

1738
1739
1740
1741
1742
1743

1744
1745
1746
1747
1748
1749

1750
1751
1752
1753
1754
1755

1756
1757
1758
1759
1760
1761

1762
1763
1764
1765
1766
1767

1768
1769
1770
1771
1772
1773

1774
1775
1776
1777
1778
1779

1780
1781
1782
1783
1784
1785

1786
1787
1788
1789
1790
1791

1792
1793
1794
1795
1796
1797

1798
1799
1800
1801
1802
1803

1804
1805
1806
1807
1808
1809

1810
1811
1812
1813
1814
1815

1816
1817
1818
1819
1820
1821

1822
1823
1824
1825
1826
1827

1828
1829
1830
1831
1832
1833

1834
1835
1836
1837
1838
1839

1840
1841
1842
1843
1844
1845

1846
1847
1848
1849
1850
1851

1852
1853
1854
1855
1856
1857

1858
1859
1860
1861
1862
1863

1864
1865
1866
1867
1868
1869

1870
1871
1872
1873
1874
1875

1876
1877
1878
1879
1880
1881

1882
1883
1884
1885
1886
1887

1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
test util-11.22 {Tcl_PrintDouble - scaling} {
    expr 1.1e16
} {11000000000000000.0}
test util-11.23 {Tcl_PrintDouble - scaling} {
    expr 1.1e17
} {1.1e+17}

test util-12.1 {TclDoubleDigits - Inf} ieeeFloatingPoint {
     testdoubledigits Inf -1 shortest
} {Infinity 9999 +}
test util-12.2 {TclDoubleDigits - -Inf} ieeeFloatingPoint {
     testdoubledigits -Inf -1 shortest
} {Infinity 9999 -}
test util-12.3 {TclDoubleDigits - NaN} ieeeFloatingPoint {
     testdoubledigits $ieeeValues(NaN) -1 shortest
} {NaN 9999 +}
test util-12.4 {TclDoubleDigits - NaN} {*}{
     -constraints {ieeeFloatingPoint && controversialNaN}
     -body {
	 testdoubledigits -NaN -1 shortest
     }
    -result {NaN 9999 -}
}
test util-12.5 {TclDoubleDigits - 0} {
     testdoubledigits 0.0 -1 shortest
} {0 0 +}
test util-12.6 {TclDoubleDigits - -0} {
     testdoubledigits -0.0 -1 shortest
} {0 0 -}

# Verdonk test vectors

test util-13.1 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 1754e31cd072da E+1008 +4_000000000000000000& E+303
    }
    -result {}
}
test util-13.2 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -1afcef51f0fb5f E+265 -1_000000000000000000& E+80
    }
    -result {}
}
test util-13.3 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 1754e31cd072da E+1006 +1_000000000000000000& E+303
    }
    -result {}
}
test util-13.4 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -1754e31cd072da E+1007 -2_000000000000000000& E+303
    }
    -result {}
}
test util-13.5 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 1e07b27dd78b14 E-848 +1_00000000000000000& E-255
    }
    -result {}
}
test util-13.6 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -1e29e9c56687fe E-709 -7_00000000000000000& E-214
    }
    -result {}
}
test util-13.7 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 1be03d0bf225c7 E-137 +1_00000000000000000& E-41
    }
    -result {}
}
test util-13.8 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -1a2fe76a3f9475 E-499 -1_00000000000000000& E-150
    }
    -result {}
}
test util-13.9 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 19a2028368022e E+1019 +8_999999999999999999& E+306
    }
    -result {}
}
test util-13.10 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -1317e5ef3ab327 E+509 -1_999999999999999999& E+153
    }
    -result {}
}
test util-13.11 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 1317e5ef3ab327 E+510 +3_99999999999999999& E+153
    }
    -result {}
}
test util-13.12 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -1317e5ef3ab327 E+511 -7_99999999999999999& E+153
    }
    -result {}
}
test util-13.13 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 1eb8e84fa0b278 E-1008 +6_999999999999999999& E-304
    }
    -result {}
}
test util-13.14 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -13339131c46f8b E-1004 -6_999999999999999999& E-303
    }
    -result {}
}
test util-13.15 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 1c0f92a6276c9d E-162 +2_999999999999999999& E-49
    }
    -result {}
}
test util-13.16 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -15ce1f143d7ad2 E-443 -5_99999999999999999& E-134
    }
    -result {}
}
test util-13.17 {just over exact - 2 digits} {*}{

    -body {
        verdonk_test 1c0794d9d40e96 E-301 +43_000000000000000000& E-92
    }
    -result {}
}
test util-13.18 {just over exact - 2 digits} {*}{

    -body {
        verdonk_test -1c0794d9d40e96 E-300 -86_000000000000000000& E-92
    }
    -result {}
}
test util-13.19 {just over exact - 2 digits} {*}{

    -body {
        verdonk_test 1cd5bee57763e6 E-241 +51_000000000000000000& E-74
    }
    -result {}
}
test util-13.20 {just under exact - 2 digits} {*}{

    -body {
        verdonk_test 1d1c26db7d0dae E+651 +16_999999999999999999& E+195
    }
    -result {}
}
test util-13.21 {just under exact - 2 digits} {*}{

    -body {
        verdonk_test -13f7ced916872b E-5 -38_999999999999999999& E-3
    }
    -result {}
}
test util-13.22 {just over exact - 3 digits} {*}{

    -body {
        verdonk_test 17d93193f78fc6 E+588 +151_0000000000000000000& E+175
    }
    -result {}
}
test util-13.23 {just over exact - 3 digits} {*}{

    -body {
        verdonk_test -1a82a1631eeb30 E-625 -119_000000000000000000& E-190
    }
    -result {}
}
test util-13.24 {just under exact - 3 digits} {*}{

    -body {
        verdonk_test -16c309024bab4b E+290 -282_999999999999999999& E+85
    }
    -result {}
}
test util-13.25 {just over exact - 8 digits} {*}{

    -body {
        verdonk_test 1dbbac6f83a821 E-800 +27869147_0000000000000000000& E-248
    }
    -result {}
}
test util-13.26 {just under exact - 9 digits} {*}{

    -body {
        verdonk_test -1c569e968e0944 E+430 -491080653_9999999999999999999& E+121
    }
    -result {}
}
test util-13.27 {just under exact - 9 digits} {*}{

    -body {
        verdonk_test 1c569e968e0944 E+429 +245540326_9999999999999999999& E+121
    }
    -result {}
}
test util-13.28 {just over exact - 10 digits} {*}{

    -body {
        verdonk_test -1fc575867314ee E-330 -9078555839_0000000000000000000& E-109
    }
    -result {}
}
test util-13.29 {just under exact - 10 digits} {*}{

    -body {
        verdonk_test -1c569e968e0944 E+428 -1227701634_9999999999999999999& E+120
    }
    -result {}
}
test util-13.30 {just over exact - 11 digits} {*}{

    -body {
        verdonk_test 1fc575867314ee E-329 +18157111678_0000000000000000000& E-109
    }
    -result {}
}
test util-13.31 {just over exact - 14 digits} {*}{

    -body {
        verdonk_test -18bf7e7fa6f02a E-196 -15400733123779_0000000000000000000& E-72
    }
    -result {}
}
test util-13.32 {just over exact - 17 digits} {*}{

    -body {
        verdonk_test -13de005bd620df E+217 -26153245263757307_0000000000000000000& E+49
    }
    -result {}
}
test util-13.33 {just over exact - 18 digits} {*}{

    -body {
        verdonk_test 1f92bacb3cb40c E+718 +272104041512242479_0000000000000000000& E+199
    }
    -result {}
}
test util-13.34 {just over exact - 18 digits} {*}{

    -body {
        verdonk_test -1f92bacb3cb40c E+719 -544208083024484958_0000000000000000000& E+199
    }
    -result {}
}
test util-13.35 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 142dbf25096cf5 E+148 +4_500000000000000000& E+44
    }
    -result {}
}
test util-13.36 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1afcef51f0fb5f E+263 -2_500000000000000000& E+79
    }
    -result {}
}
test util-13.37 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 102498ea6df0c4 E+145 +4_500000000000000000& E+43
    }
    -result {}
}
test util-13.38 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1754e31cd072da E+1004 -2_500000000000000000& E+302
    }
    -result {}
}
test util-13.39 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 12deac01e2b4f7 E-557 +2_50000000000000000& E-168
    }
    -result {}
}
test util-13.40 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1b1df536c13eee E-307 -6_50000000000000000& E-93
    }
    -result {}
}
test util-13.41 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 10711fed5b19a4 E-154 +4_50000000000000000& E-47
    }
    -result {}
}
test util-13.42 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -148d67e8b1e00d E-151 -4_50000000000000000& E-46
    }
    -result {}
}
test util-13.43 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 1c8c574c0c6be7 E+187 +3_49999999999999999& E+56
    }
    -result {}
}
test util-13.44 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1756183c147514 E+206 -1_49999999999999999& E+62
    }
    -result {}
}
test util-13.45 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 12ab469676c410 E+203 +1_49999999999999999& E+61
    }
    -result {}
}
test util-13.46 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1539684e774b48 E+246 -1_49999999999999999& E+74
    }
    -result {}
}
test util-13.47 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 12e5f5dfa4fe9d E-286 +9_499999999999999999& E-87
    }
    -result {}
}
test util-13.48 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1bdc2417bf7787 E-838 -9_499999999999999999& E-253
    }
    -result {}
}
test util-13.49 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 1eb8e84fa0b278 E-1009 +3_499999999999999999& E-304
    }
    -result {}
}
test util-13.50 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1e3cbc9907fdc8 E-290 -9_499999999999999999& E-88
    }
    -result {}
}
test util-13.51 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test 10ad836f269a17 E-324 +30_500000000000000000& E-99
    }
    -result {}
}
test util-13.52 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test -1b39ae1909c31b E-687 -26_500000000000000000& E-208
    }
    -result {}
}
test util-13.53 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test 1b2ab18615fcc6 E-576 +686_500000000000000000& E-176
    }
    -result {}
}
test util-13.54 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test -13e1f90a573064 E-624 -178_500000000000000000& E-190
    }
    -result {}
}
test util-13.55 {just under half ulp - 3 digits} {*}{

    -body {
        verdonk_test 16c309024bab4b E+289 +141_499999999999999999& E+85
    }
    -result {}
}
test util-13.56 {just under half ulp - 4 digits} {*}{

    -body {
        verdonk_test -159bd3ad46e346 E+193 -1695_499999999999999999& E+55
    }
    -result {}
}
test util-13.57 {just under half ulp - 4 digits} {*}{

    -body {
        verdonk_test 1df4170f0fdecc E+124 +3981_499999999999999999& E+34
    }
    -result {}
}
test util-13.58 {just over half ulp - 6 digits} {*}{

    -body {
        verdonk_test 17e1e0f1c7a4ac E+415 +126300_5000000000000000000& E+120
    }
    -result {}
}
test util-13.59 {just over half ulp - 6 digits} {*}{

    -body {
        verdonk_test -1dda592e398dd7 E+418 -126300_5000000000000000000& E+121
    }
    -result {}
}
test util-13.60 {just under half ulp - 7 digits} {*}{

    -body {
        verdonk_test -1e597c0b94b7ae E+453 -4411845_499999999999999999& E+130
    }
    -result {}
}
test util-13.61 {just under half ulp - 9 digits} {*}{

    -body {
        verdonk_test 1c569e968e0944 E+427 +613850817_4999999999999999999& E+120
    }
    -result {}
}
test util-13.62 {just under half ulp - 9 digits} {*}{

    -body {
        verdonk_test -1c569e968e0944 E+428 -122770163_49999999999999999999& E+121
    }
    -result {}
}
test util-13.63 {just over half ulp - 18 digits} {*}{

    -body {
        verdonk_test 17ae0c186d8709 E+719 +408156062268363718_5000000000000000000& E+199
    }
    -result {}
}
test util-13.64 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 152d02c7e14af7 E+76 +1_0000000000000000& E+23
    }
    -result {}
}
test util-13.65 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -19d971e4fe8402 E+89 -1_0000000000000000& E+27
    }
    -result {}
}
test util-13.66 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 19d971e4fe8402 E+90 +2_0000000000000000& E+27
    }
    -result {}
}
test util-13.67 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -19d971e4fe8402 E+91 -4_0000000000000000& E+27
    }
    -result {}
}
test util-13.68 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 15798ee2308c3a E-27 +1_0000000000000000& E-8
    }
    -result {}
}
test util-13.69 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -15798ee2308c3a E-26 -2_0000000000000000& E-8
    }
    -result {}
}
test util-13.70 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test 15798ee2308c3a E-25 +4_0000000000000000& E-8
    }
    -result {}
}
test util-13.71 {just over exact - 1 digits} {*}{

    -body {
        verdonk_test -1ef2d0f5da7dd9 E-84 -1_0000000000000000& E-25
    }
    -result {}
}
test util-13.72 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 1a784379d99db4 E+78 +4_9999999999999999& E+23
    }
    -result {}
}
test util-13.73 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -1a784379d99db4 E+80 -1_9999999999999999& E+24
    }
    -result {}
}
test util-13.74 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 13da329b633647 E+81 +2_9999999999999999& E+24
    }
    -result {}
}
test util-13.75 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -1cf389cd46047d E+85 -6_9999999999999999& E+25
    }
    -result {}
}
test util-13.76 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 19999999999999 E-3 +1_99999999999999999& E-1
    }
    -result {}
}
test util-13.77 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -13333333333333 E-2 -2_99999999999999999& E-1
    }
    -result {}
}
test util-13.78 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test 16849b86a12b9b E-48 +4_99999999999999999& E-15
    }
    -result {}
}
test util-13.79 {just under exact - 1 digits} {*}{

    -body {
        verdonk_test -16849b86a12b9b E-46 -1_99999999999999999& E-14
    }
    -result {}
}
test util-13.80 {just over exact - 2 digits} {*}{

    -body {
        verdonk_test 17ccfc73126788 E-71 +63_00000000000000000& E-23
    }
    -result {}
}
test util-13.81 {just over exact - 2 digits} {*}{

    -body {
        verdonk_test -1dc03b8fd7016a E-68 -63_00000000000000000& E-22
    }
    -result {}
}
test util-13.82 {just under exact - 2 digits} {*}{

    -body {
        verdonk_test 13f7ced916872b E-5 +38_999999999999999999& E-3
    }
    -result {}
}
test util-13.83 {just over exact - 3 digits} {*}{

    -body {
        verdonk_test 1b297cad9f70b6 E+97 +269_000000000000000000& E+27
    }
    -result {}
}
test util-13.84 {just over exact - 3 digits} {*}{

    -body {
        verdonk_test -1b297cad9f70b6 E+98 -538_00000000000000000& E+27
    }
    -result {}
}
test util-13.85 {just over exact - 3 digits} {*}{

    -body {
        verdonk_test 1cdc06b20ef183 E-82 +373_00000000000000000& E-27
    }
    -result {}
}
test util-13.86 {just over exact - 4 digits} {*}{

    -body {
        verdonk_test 1b297cad9f70b6 E+96 +1345_00000000000000000& E+26
    }
    -result {}
}
# this one is not 4 digits, it is 3, and it is covered above.
test util-13.87 {just over exact - 4 digits} {*}{
    -constraints knownBadTest
    -body {
        verdonk_test -1b297cad9f70b6 E+97 -2690_00000000000000000& E+26
    }
    -result {}
}
test util-13.88 {just over exact - 5 digits} {*}{

    -body {
        verdonk_test -150a246ecd44f3 E-63 -14257_00000000000000000& E-23
    }
    -result {}
}
test util-13.89 {just under exact - 6 digits} {*}{

    -body {
        verdonk_test -119b96f36ec68b E-19 -209900_999999999999999999& E-11
    }
    -result {}
}
test util-13.90 {just over exact - 11 digits} {*}{

    -body {
        verdonk_test 1c06d366394441 E-35 +50980203373_000000000000000000& E-21
    }
    -result {}
}
test util-13.91 {just under exact - 12 digits} {*}{

    -body {
        verdonk_test -1f58ac4db68c90 E+122 -104166211810_99999999999999999& E+26
    }
    -result {}
}
test util-13.92 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 19d971e4fe8402 E+87 +2_5000000000000000& E+26
    }
    -result {}
}
test util-13.93 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1dc74be914d16b E+81 -4_500000000000000& E+24
    }
    -result {}
}
test util-13.94 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 14adf4b7320335 E+84 +2_500000000000000& E+25
    }
    -result {}
}
test util-13.95 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1ae22487c1042b E+85 -6_5000000000000000& E+25
    }
    -result {}
}
test util-13.96 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 187fe49aab41e0 E-54 +8_5000000000000000& E-17
    }
    -result {}
}
test util-13.97 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1f5c05e4b23fd7 E-61 -8_5000000000000000& E-19
    }
    -result {}
}
test util-13.98 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test 1faa7ab552a552 E-42 +4_5000000000000000& E-13
    }
    -result {}
}
test util-13.99 {just over half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1b7cdfd9d7bdbb E-36 -2_5000000000000000& E-11
    }
    -result {}
}
test util-13.100 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 13da329b633647 E+80 +1_4999999999999999& E+24
    }
    -result {}
}
test util-13.101 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1cf389cd46047d E+84 -3_49999999999999999& E+25
    }
    -result {}
}
test util-13.102 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 1f04ef12cb04cf E+85 +7_4999999999999999& E+25
    }
    -result {}
}
test util-13.103 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -1f04ef12cb04cf E+86 -1_4999999999999999& E+26
    }
    -result {}
}
test util-13.104 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 13333333333333 E-3 +1_49999999999999999& E-1
    }
    -result {}
}
test util-13.105 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -107e1fe91b0b70 E-36 -1_49999999999999999& E-11
    }
    -result {}
}
test util-13.106 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test 149da7e361ce4c E-33 +1_49999999999999999& E-10
    }
    -result {}
}
test util-13.107 {just under half ulp - 1 digits} {*}{

    -body {
        verdonk_test -19c511dc3a41df E-30 -1_49999999999999999& E-9
    }
    -result {}
}
test util-13.108 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test -1aa83d74267822 E+93 -16_5000000000000000& E+27
    }
    -result {}
}
test util-13.109 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test 18f1d5969453de E+89 +96_5000000000000000& E+25
    }
    -result {}
}
test util-13.110 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test 11d9bd564dcda6 E-70 +94_50000000000000000& E-23
    }
    -result {}
}
test util-13.111 {just over half ulp - 2 digits} {*}{

    -body {
        verdonk_test -1a58973ecbede6 E-48 -58_50000000000000000& E-16
    }
    -result {}
}
test util-13.112 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test 1b297cad9f70b6 E+95 +672_50000000000000000& E+26
    }
    -result {}
}
test util-13.113 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test -1b297cad9f70b6 E+96 -134_500000000000000000& E+27
    }
    -result {}
}
test util-13.114 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test 1cdc06b20ef183 E-83 +186_50000000000000000& E-27
    }
    -result {}
}
test util-13.115 {just over half ulp - 3 digits} {*}{

    -body {
        verdonk_test -136071dcae4565 E-47 -860_50000000000000000& E-17
    }
    -result {}
}
test util-13.116 {just over half ulp - 6 digits} {*}{

    -body {
        verdonk_test 1cb968d297dde8 E+99 +113788_50000000000000000& E+25
    }
    -result {}
}
test util-13.117 {just over half ulp - 6 digits} {*}{

    -body {
        verdonk_test -11f3e1839eeab1 E+103 -113788_50000000000000000& E+26
    }
    -result {}
}
test util-13.118 {just under half ulp - 9 digits} {*}{

    -body {
        verdonk_test 1e9cec176c96f8 E+117 +317903333_49999999999999999& E+27
    }
    -result {}
}
test util-13.119 {just over half ulp - 11 digits} {*}{

    -body {
        verdonk_test 1c06d366394441 E-36 +25490101686_500000000000000000& E-21
    }
    -result {}
}
test util-13.120 {just under half ulp - 11 digits} {*}{

    -body {
        verdonk_test 1f58ac4db68c90 E+121 +52083105905_49999999999999999& E+26
    }
    -result {}
}

test util-14.1 {funky NaN} {*}{
    -constraints {ieeeFloatingPoint && controversialNaN}
    -body {
	set ieeeValues(-NaN)
    }
    -result -NaN
}

test util-14.2 {funky NaN} {*}{
    -constraints {ieeeFloatingPoint && controversialNaN}
    -body {
	set ieeeValues(-NaN(3456789abcdef))
    }
    -result -NaN(3456789abcdef)
}

test util-15.1 {largest subnormal} {*}{







|


|


|



|





|


|






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>







|






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>






>







|







|







1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
test util-11.22 {Tcl_PrintDouble - scaling} {
    expr 1.1e16
} {11000000000000000.0}
test util-11.23 {Tcl_PrintDouble - scaling} {
    expr 1.1e17
} {1.1e+17}

test util-12.1 {TclDoubleDigits - Inf} {testdoubledigits ieeeFloatingPoint} {
     testdoubledigits Inf -1 shortest
} {Infinity 9999 +}
test util-12.2 {TclDoubleDigits - -Inf} {testdoubledigits ieeeFloatingPoint} {
     testdoubledigits -Inf -1 shortest
} {Infinity 9999 -}
test util-12.3 {TclDoubleDigits - NaN} {testdoubledigits ieeeFloatingPoint} {
     testdoubledigits $ieeeValues(NaN) -1 shortest
} {NaN 9999 +}
test util-12.4 {TclDoubleDigits - NaN} {*}{
     -constraints {testdoubledigits ieeeFloatingPoint controversialNaN}
     -body {
	 testdoubledigits -NaN -1 shortest
     }
    -result {NaN 9999 -}
}
test util-12.5 {TclDoubleDigits - 0} testdoubledigits {
     testdoubledigits 0.0 -1 shortest
} {0 0 +}
test util-12.6 {TclDoubleDigits - -0} testdoubledigits {
     testdoubledigits -0.0 -1 shortest
} {0 0 -}

# Verdonk test vectors

test util-13.1 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1754e31cd072da E+1008 +4_000000000000000000& E+303
    }
    -result {}
}
test util-13.2 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1afcef51f0fb5f E+265 -1_000000000000000000& E+80
    }
    -result {}
}
test util-13.3 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1754e31cd072da E+1006 +1_000000000000000000& E+303
    }
    -result {}
}
test util-13.4 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1754e31cd072da E+1007 -2_000000000000000000& E+303
    }
    -result {}
}
test util-13.5 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1e07b27dd78b14 E-848 +1_00000000000000000& E-255
    }
    -result {}
}
test util-13.6 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1e29e9c56687fe E-709 -7_00000000000000000& E-214
    }
    -result {}
}
test util-13.7 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1be03d0bf225c7 E-137 +1_00000000000000000& E-41
    }
    -result {}
}
test util-13.8 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1a2fe76a3f9475 E-499 -1_00000000000000000& E-150
    }
    -result {}
}
test util-13.9 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 19a2028368022e E+1019 +8_999999999999999999& E+306
    }
    -result {}
}
test util-13.10 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1317e5ef3ab327 E+509 -1_999999999999999999& E+153
    }
    -result {}
}
test util-13.11 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1317e5ef3ab327 E+510 +3_99999999999999999& E+153
    }
    -result {}
}
test util-13.12 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1317e5ef3ab327 E+511 -7_99999999999999999& E+153
    }
    -result {}
}
test util-13.13 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1eb8e84fa0b278 E-1008 +6_999999999999999999& E-304
    }
    -result {}
}
test util-13.14 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -13339131c46f8b E-1004 -6_999999999999999999& E-303
    }
    -result {}
}
test util-13.15 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c0f92a6276c9d E-162 +2_999999999999999999& E-49
    }
    -result {}
}
test util-13.16 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -15ce1f143d7ad2 E-443 -5_99999999999999999& E-134
    }
    -result {}
}
test util-13.17 {just over exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c0794d9d40e96 E-301 +43_000000000000000000& E-92
    }
    -result {}
}
test util-13.18 {just over exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1c0794d9d40e96 E-300 -86_000000000000000000& E-92
    }
    -result {}
}
test util-13.19 {just over exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1cd5bee57763e6 E-241 +51_000000000000000000& E-74
    }
    -result {}
}
test util-13.20 {just under exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1d1c26db7d0dae E+651 +16_999999999999999999& E+195
    }
    -result {}
}
test util-13.21 {just under exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -13f7ced916872b E-5 -38_999999999999999999& E-3
    }
    -result {}
}
test util-13.22 {just over exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 17d93193f78fc6 E+588 +151_0000000000000000000& E+175
    }
    -result {}
}
test util-13.23 {just over exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1a82a1631eeb30 E-625 -119_000000000000000000& E-190
    }
    -result {}
}
test util-13.24 {just under exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -16c309024bab4b E+290 -282_999999999999999999& E+85
    }
    -result {}
}
test util-13.25 {just over exact - 8 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1dbbac6f83a821 E-800 +27869147_0000000000000000000& E-248
    }
    -result {}
}
test util-13.26 {just under exact - 9 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1c569e968e0944 E+430 -491080653_9999999999999999999& E+121
    }
    -result {}
}
test util-13.27 {just under exact - 9 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c569e968e0944 E+429 +245540326_9999999999999999999& E+121
    }
    -result {}
}
test util-13.28 {just over exact - 10 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1fc575867314ee E-330 -9078555839_0000000000000000000& E-109
    }
    -result {}
}
test util-13.29 {just under exact - 10 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1c569e968e0944 E+428 -1227701634_9999999999999999999& E+120
    }
    -result {}
}
test util-13.30 {just over exact - 11 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1fc575867314ee E-329 +18157111678_0000000000000000000& E-109
    }
    -result {}
}
test util-13.31 {just over exact - 14 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -18bf7e7fa6f02a E-196 -15400733123779_0000000000000000000& E-72
    }
    -result {}
}
test util-13.32 {just over exact - 17 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -13de005bd620df E+217 -26153245263757307_0000000000000000000& E+49
    }
    -result {}
}
test util-13.33 {just over exact - 18 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1f92bacb3cb40c E+718 +272104041512242479_0000000000000000000& E+199
    }
    -result {}
}
test util-13.34 {just over exact - 18 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1f92bacb3cb40c E+719 -544208083024484958_0000000000000000000& E+199
    }
    -result {}
}
test util-13.35 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 142dbf25096cf5 E+148 +4_500000000000000000& E+44
    }
    -result {}
}
test util-13.36 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1afcef51f0fb5f E+263 -2_500000000000000000& E+79
    }
    -result {}
}
test util-13.37 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 102498ea6df0c4 E+145 +4_500000000000000000& E+43
    }
    -result {}
}
test util-13.38 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1754e31cd072da E+1004 -2_500000000000000000& E+302
    }
    -result {}
}
test util-13.39 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 12deac01e2b4f7 E-557 +2_50000000000000000& E-168
    }
    -result {}
}
test util-13.40 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1b1df536c13eee E-307 -6_50000000000000000& E-93
    }
    -result {}
}
test util-13.41 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 10711fed5b19a4 E-154 +4_50000000000000000& E-47
    }
    -result {}
}
test util-13.42 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -148d67e8b1e00d E-151 -4_50000000000000000& E-46
    }
    -result {}
}
test util-13.43 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c8c574c0c6be7 E+187 +3_49999999999999999& E+56
    }
    -result {}
}
test util-13.44 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1756183c147514 E+206 -1_49999999999999999& E+62
    }
    -result {}
}
test util-13.45 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 12ab469676c410 E+203 +1_49999999999999999& E+61
    }
    -result {}
}
test util-13.46 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1539684e774b48 E+246 -1_49999999999999999& E+74
    }
    -result {}
}
test util-13.47 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 12e5f5dfa4fe9d E-286 +9_499999999999999999& E-87
    }
    -result {}
}
test util-13.48 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1bdc2417bf7787 E-838 -9_499999999999999999& E-253
    }
    -result {}
}
test util-13.49 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1eb8e84fa0b278 E-1009 +3_499999999999999999& E-304
    }
    -result {}
}
test util-13.50 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1e3cbc9907fdc8 E-290 -9_499999999999999999& E-88
    }
    -result {}
}
test util-13.51 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 10ad836f269a17 E-324 +30_500000000000000000& E-99
    }
    -result {}
}
test util-13.52 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1b39ae1909c31b E-687 -26_500000000000000000& E-208
    }
    -result {}
}
test util-13.53 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1b2ab18615fcc6 E-576 +686_500000000000000000& E-176
    }
    -result {}
}
test util-13.54 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -13e1f90a573064 E-624 -178_500000000000000000& E-190
    }
    -result {}
}
test util-13.55 {just under half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 16c309024bab4b E+289 +141_499999999999999999& E+85
    }
    -result {}
}
test util-13.56 {just under half ulp - 4 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -159bd3ad46e346 E+193 -1695_499999999999999999& E+55
    }
    -result {}
}
test util-13.57 {just under half ulp - 4 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1df4170f0fdecc E+124 +3981_499999999999999999& E+34
    }
    -result {}
}
test util-13.58 {just over half ulp - 6 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 17e1e0f1c7a4ac E+415 +126300_5000000000000000000& E+120
    }
    -result {}
}
test util-13.59 {just over half ulp - 6 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1dda592e398dd7 E+418 -126300_5000000000000000000& E+121
    }
    -result {}
}
test util-13.60 {just under half ulp - 7 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1e597c0b94b7ae E+453 -4411845_499999999999999999& E+130
    }
    -result {}
}
test util-13.61 {just under half ulp - 9 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c569e968e0944 E+427 +613850817_4999999999999999999& E+120
    }
    -result {}
}
test util-13.62 {just under half ulp - 9 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1c569e968e0944 E+428 -122770163_49999999999999999999& E+121
    }
    -result {}
}
test util-13.63 {just over half ulp - 18 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 17ae0c186d8709 E+719 +408156062268363718_5000000000000000000& E+199
    }
    -result {}
}
test util-13.64 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 152d02c7e14af7 E+76 +1_0000000000000000& E+23
    }
    -result {}
}
test util-13.65 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -19d971e4fe8402 E+89 -1_0000000000000000& E+27
    }
    -result {}
}
test util-13.66 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 19d971e4fe8402 E+90 +2_0000000000000000& E+27
    }
    -result {}
}
test util-13.67 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -19d971e4fe8402 E+91 -4_0000000000000000& E+27
    }
    -result {}
}
test util-13.68 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 15798ee2308c3a E-27 +1_0000000000000000& E-8
    }
    -result {}
}
test util-13.69 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -15798ee2308c3a E-26 -2_0000000000000000& E-8
    }
    -result {}
}
test util-13.70 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 15798ee2308c3a E-25 +4_0000000000000000& E-8
    }
    -result {}
}
test util-13.71 {just over exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1ef2d0f5da7dd9 E-84 -1_0000000000000000& E-25
    }
    -result {}
}
test util-13.72 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1a784379d99db4 E+78 +4_9999999999999999& E+23
    }
    -result {}
}
test util-13.73 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1a784379d99db4 E+80 -1_9999999999999999& E+24
    }
    -result {}
}
test util-13.74 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 13da329b633647 E+81 +2_9999999999999999& E+24
    }
    -result {}
}
test util-13.75 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1cf389cd46047d E+85 -6_9999999999999999& E+25
    }
    -result {}
}
test util-13.76 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 19999999999999 E-3 +1_99999999999999999& E-1
    }
    -result {}
}
test util-13.77 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -13333333333333 E-2 -2_99999999999999999& E-1
    }
    -result {}
}
test util-13.78 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 16849b86a12b9b E-48 +4_99999999999999999& E-15
    }
    -result {}
}
test util-13.79 {just under exact - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -16849b86a12b9b E-46 -1_99999999999999999& E-14
    }
    -result {}
}
test util-13.80 {just over exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 17ccfc73126788 E-71 +63_00000000000000000& E-23
    }
    -result {}
}
test util-13.81 {just over exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1dc03b8fd7016a E-68 -63_00000000000000000& E-22
    }
    -result {}
}
test util-13.82 {just under exact - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 13f7ced916872b E-5 +38_999999999999999999& E-3
    }
    -result {}
}
test util-13.83 {just over exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1b297cad9f70b6 E+97 +269_000000000000000000& E+27
    }
    -result {}
}
test util-13.84 {just over exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1b297cad9f70b6 E+98 -538_00000000000000000& E+27
    }
    -result {}
}
test util-13.85 {just over exact - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1cdc06b20ef183 E-82 +373_00000000000000000& E-27
    }
    -result {}
}
test util-13.86 {just over exact - 4 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1b297cad9f70b6 E+96 +1345_00000000000000000& E+26
    }
    -result {}
}
# this one is not 4 digits, it is 3, and it is covered above.
test util-13.87 {just over exact - 4 digits} {*}{
    -constraints {testdoubledigits knownBadTest}
    -body {
        verdonk_test -1b297cad9f70b6 E+97 -2690_00000000000000000& E+26
    }
    -result {}
}
test util-13.88 {just over exact - 5 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -150a246ecd44f3 E-63 -14257_00000000000000000& E-23
    }
    -result {}
}
test util-13.89 {just under exact - 6 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -119b96f36ec68b E-19 -209900_999999999999999999& E-11
    }
    -result {}
}
test util-13.90 {just over exact - 11 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c06d366394441 E-35 +50980203373_000000000000000000& E-21
    }
    -result {}
}
test util-13.91 {just under exact - 12 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1f58ac4db68c90 E+122 -104166211810_99999999999999999& E+26
    }
    -result {}
}
test util-13.92 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 19d971e4fe8402 E+87 +2_5000000000000000& E+26
    }
    -result {}
}
test util-13.93 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1dc74be914d16b E+81 -4_500000000000000& E+24
    }
    -result {}
}
test util-13.94 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 14adf4b7320335 E+84 +2_500000000000000& E+25
    }
    -result {}
}
test util-13.95 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1ae22487c1042b E+85 -6_5000000000000000& E+25
    }
    -result {}
}
test util-13.96 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 187fe49aab41e0 E-54 +8_5000000000000000& E-17
    }
    -result {}
}
test util-13.97 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1f5c05e4b23fd7 E-61 -8_5000000000000000& E-19
    }
    -result {}
}
test util-13.98 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1faa7ab552a552 E-42 +4_5000000000000000& E-13
    }
    -result {}
}
test util-13.99 {just over half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1b7cdfd9d7bdbb E-36 -2_5000000000000000& E-11
    }
    -result {}
}
test util-13.100 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 13da329b633647 E+80 +1_4999999999999999& E+24
    }
    -result {}
}
test util-13.101 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1cf389cd46047d E+84 -3_49999999999999999& E+25
    }
    -result {}
}
test util-13.102 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1f04ef12cb04cf E+85 +7_4999999999999999& E+25
    }
    -result {}
}
test util-13.103 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1f04ef12cb04cf E+86 -1_4999999999999999& E+26
    }
    -result {}
}
test util-13.104 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 13333333333333 E-3 +1_49999999999999999& E-1
    }
    -result {}
}
test util-13.105 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -107e1fe91b0b70 E-36 -1_49999999999999999& E-11
    }
    -result {}
}
test util-13.106 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 149da7e361ce4c E-33 +1_49999999999999999& E-10
    }
    -result {}
}
test util-13.107 {just under half ulp - 1 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -19c511dc3a41df E-30 -1_49999999999999999& E-9
    }
    -result {}
}
test util-13.108 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1aa83d74267822 E+93 -16_5000000000000000& E+27
    }
    -result {}
}
test util-13.109 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 18f1d5969453de E+89 +96_5000000000000000& E+25
    }
    -result {}
}
test util-13.110 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 11d9bd564dcda6 E-70 +94_50000000000000000& E-23
    }
    -result {}
}
test util-13.111 {just over half ulp - 2 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1a58973ecbede6 E-48 -58_50000000000000000& E-16
    }
    -result {}
}
test util-13.112 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1b297cad9f70b6 E+95 +672_50000000000000000& E+26
    }
    -result {}
}
test util-13.113 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -1b297cad9f70b6 E+96 -134_500000000000000000& E+27
    }
    -result {}
}
test util-13.114 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1cdc06b20ef183 E-83 +186_50000000000000000& E-27
    }
    -result {}
}
test util-13.115 {just over half ulp - 3 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -136071dcae4565 E-47 -860_50000000000000000& E-17
    }
    -result {}
}
test util-13.116 {just over half ulp - 6 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1cb968d297dde8 E+99 +113788_50000000000000000& E+25
    }
    -result {}
}
test util-13.117 {just over half ulp - 6 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test -11f3e1839eeab1 E+103 -113788_50000000000000000& E+26
    }
    -result {}
}
test util-13.118 {just under half ulp - 9 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1e9cec176c96f8 E+117 +317903333_49999999999999999& E+27
    }
    -result {}
}
test util-13.119 {just over half ulp - 11 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1c06d366394441 E-36 +25490101686_500000000000000000& E-21
    }
    -result {}
}
test util-13.120 {just under half ulp - 11 digits} {*}{
    -constraints testdoubledigits
    -body {
        verdonk_test 1f58ac4db68c90 E+121 +52083105905_49999999999999999& E+26
    }
    -result {}
}

test util-14.1 {funky NaN} {*}{
    -constraints {ieeeFloatingPoint controversialNaN}
    -body {
	set ieeeValues(-NaN)
    }
    -result -NaN
}

test util-14.2 {funky NaN} {*}{
    -constraints {ieeeFloatingPoint controversialNaN}
    -body {
	set ieeeValues(-NaN(3456789abcdef))
    }
    -result -NaN(3456789abcdef)
}

test util-15.1 {largest subnormal} {*}{

Changes to tests/zlib.test.

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    gets $f
} -cleanup {
    close $f
    removeFile $file
} -result ok
test zlib-8.3 {zlib transformation and fileevent} -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        fconfigure $c -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set port [lindex [fconfigure $srv -sockname] 2]
    set file [makeFile {} test.gz]
    set fout [open $file wb]
} -body {







|







126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    gets $f
} -cleanup {
    close $f
    removeFile $file
} -result ok
test zlib-8.3 {zlib transformation and fileevent} -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        fconfigure $c -translation binary -buffering none -blocking 0
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set port [lindex [fconfigure $srv -sockname] 2]
    set file [makeFile {} test.gz]
    set fout [open $file wb]
} -body {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    list copied $total size [file size $file]
} -cleanup {
    removeFile $file
    removeFile $sfile
} -result {copied 81920 size 81920}
test zlib-9.2 "socket fcopy with push" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]







|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    list copied $total size [file size $file]
} -cleanup {
    removeFile $file
    removeFile $sfile
} -result {copied 81920 size 81920}
test zlib-9.2 "socket fcopy with push" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
} -cleanup {
    close $srv
    removeFile $file
} -result {read 81920 size 81920}
test zlib-9.3 "socket fcopy bg (identity)" -constraints {tempNotWin zlib} -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        #puts "connection from $a:$p on $c"
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [string repeat a 81920]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    #puts "listening for connections on $addr $port"







|







193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
} -cleanup {
    close $srv
    removeFile $file
} -result {read 81920 size 81920}
test zlib-9.3 "socket fcopy bg (identity)" -constraints {tempNotWin zlib} -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        #puts "connection from $a:$p on $c"
        chan configure $c -translation binary -buffering none -blocking 0
        puts -nonewline $c [string repeat a 81920]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    #puts "listening for connections on $addr $port"
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    list read $::total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -returnCodes {ok error} -result {read 81920 size 81920}
test zlib-9.4 "socket fcopy bg (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]







|







218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    list read $::total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -returnCodes {ok error} -result {read 81920 size 81920}
test zlib-9.4 "socket fcopy bg (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    list read $::total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -result {read 81920 size 81920}
test zlib-9.5 "socket fcopy incremental (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    proc zlib95copy {i o t c {e {}}} {
        incr t $c
        if {$e ne {}} {
            set ::total [list error $e]







|







243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    list read $::total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -result {read 81920 size 81920}
test zlib-9.5 "socket fcopy incremental (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    proc zlib95copy {i o t c {e {}}} {
        incr t $c
        if {$e ne {}} {
            set ::total [list error $e]
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
} -cleanup {
    close $srv
    rename zlib95copy {}
    removeFile $file
} -result {{eof 81920} size 81920}
test zlib-9.6 "bug #2818131 (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push gzip $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push gunzip $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    close $srv
    unset -nocomplain total
} -result {eof 500}
test zlib-9.7 "bug #2818131 (compress)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push compress $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push decompress $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    close $srv
    unset -nocomplain total
} -result {eof 500}
test zlib-9.8 "bug #2818131 (deflate)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push deflate $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push inflate $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    unset -nocomplain total
    close $srv
} -result {eof 500}
test zlib-9.9 "bug #2818131 (gzip mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push gzip $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary -buffering none
        zlib push inflate $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }







|








|

















|








|

















|








|


















|









|







277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
} -cleanup {
    close $srv
    rename zlib95copy {}
    removeFile $file
} -result {{eof 81920} size 81920}
test zlib-9.6 "bug #2818131 (gzip)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push gzip $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary
    zlib push gunzip $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    close $srv
    unset -nocomplain total
} -result {eof 500}
test zlib-9.7 "bug #2818131 (compress)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push compress $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary
    zlib push decompress $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    close $srv
    unset -nocomplain total
} -result {eof 500}
test zlib-9.8 "bug #2818131 (deflate)" -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push deflate $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary
    zlib push inflate $s
    chan event $s readable [list apply {{s} {
        set d [read $s]
        if {[eof $s]} {
            chan event $s readable {}
            set ::total [list eof [string length $d]]
        }
    }} $s]
    vwait ::total
    close $s
    set ::total
} -cleanup {
    unset -nocomplain total
    close $srv
} -result {eof 500}
test zlib-9.9 "bug #2818131 (gzip mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push gzip $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary
        zlib push inflate $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    unset -nocomplain total
    close $srv
    rename bgerror {}
} -result {error {invalid block type}}
test zlib-9.10 "bug #2818131 (compress mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push compress $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary -buffering none
        zlib push inflate $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }







|









|







391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    unset -nocomplain total
    close $srv
    rename bgerror {}
} -result {error {invalid block type}}
test zlib-9.10 "bug #2818131 (compress mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push compress $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary
        zlib push inflate $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    unset -nocomplain total
    close $srv
    rename bgerror {}
} -result {error {invalid stored block lengths}}
test zlib-9.11 "bug #2818131 (deflate mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push deflate $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary -buffering none
        zlib push gunzip $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }







|









|







423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    unset -nocomplain total
    close $srv
    rename bgerror {}
} -result {error {invalid stored block lengths}}
test zlib-9.11 "bug #2818131 (deflate mismatch)" -constraints zlib -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none -blocking 0
        zlib push deflate $c
        puts -nonewline $c [string repeat hello 100]
        close $c
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    try {
        chan configure $s -translation binary
        zlib push gunzip $s
        chan event $s readable [list apply {{s} {
            set d [read $s]
            if {[eof $s]} {
                chan event $s readable {}
                set ::total [list eof [string length $d]]
            }
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
} -result {error {incorrect header check}}

test zlib-10.0 "bug #2818131 (close with null interp)" -constraints {
    zlib
} -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push inflate $c
        chan event $c readable [list apply {{c} {
            set d [read $c]
            if {[eof $c]} {
                chan event $c readable {}
                close $c
                set ::total [list eof [string length $d]]
            }
        }} $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push gzip $s
    chan event $s xyzzy [list apply {{s} {
        if {[gets $s line] < 0} {
            chan close $s
        }
    }} $s]
    after idle [list apply {{s} {







|














|







458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
} -result {error {incorrect header check}}

test zlib-10.0 "bug #2818131 (close with null interp)" -constraints {
    zlib
} -setup {
    proc bgerror {s} {set ::total [list error $s]}
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary
        zlib push inflate $c
        chan event $c readable [list apply {{c} {
            set d [read $c]
            if {[eof $c]} {
                chan event $c readable {}
                close $c
                set ::total [list eof [string length $d]]
            }
        }} $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none -blocking 0
    zlib push gzip $s
    chan event $s xyzzy [list apply {{s} {
        if {[gets $s line] < 0} {
            chan close $s
        }
    }} $s]
    after idle [list apply {{s} {
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
        if {[eof $c]} {
            chan event $c readable {}
            close $c
            set ::total [list eof [string length $d]]
        }
    }
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push inflate $c
        chan event $c readable [list zlibRead $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push gzip $s
    chan event $s readable [list zlibRead $s]
    after idle [list apply {{s} {
        puts $s test
        chan close $s
        after 100 {set ::total done}
    }} $s]







|







|







505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
        if {[eof $c]} {
            chan event $c readable {}
            close $c
            set ::total [list eof [string length $d]]
        }
    }
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary
        zlib push inflate $c
        chan event $c readable [list zlibRead $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none -blocking 0
    zlib push gzip $s
    chan event $s readable [list zlibRead $s]
    after idle [list apply {{s} {
        puts $s test
        chan close $s
        after 100 {set ::total done}
    }} $s]
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
        } elseif {[eof $c]} {
            chan event $c readable {}
            close $c
            set ::total [list eof 0]
        }
    }
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary -buffering none
        zlib push inflate $c
        chan event $c readable [list zlibRead $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none
    zlib push gzip $s
    chan event $s readable [list zlibRead $s]
    after idle [list apply {{s} {
        puts $s test
        chan close $s
        after 100 {set ::total done}
    }} $s]







|







|







543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
        } elseif {[eof $c]} {
            chan event $c readable {}
            close $c
            set ::total [list eof 0]
        }
    }
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        chan configure $c -translation binary
        zlib push inflate $c
        chan event $c readable [list zlibRead $c]
    }}} 0]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    after 1000 {set ::total timeout}
    set s [socket $addr $port]
    chan configure $s -translation binary -buffering none -blocking 0
    zlib push gzip $s
    chan event $s readable [list zlibRead $s]
    after idle [list apply {{s} {
        puts $s test
        chan close $s
        after 100 {set ::total done}
    }} $s]

Changes to unix/tclUnixCompat.c.

9
10
11
12
13
14
15

16
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include "tclInt.h"
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <string.h>


/* See also: SC_BLOCKING_STYLE in unix/tcl.m4
 */

#ifdef	USE_FIONBIO
#   ifdef HAVE_SYS_FILIO_H
#	include	<sys/filio.h>	/* For FIONBIO. */
#   endif
#   ifdef HAVE_SYS_IOCTL_H
#	include	<sys/ioctl.h>
#   endif
#endif	/* USE_FIONBIO */

/*
 *---------------------------------------------------------------------------
 *
 * TclUnixSetBlockingMode --
 *
 *	Set the blocking mode of a file descriptor.
 *
 * Results:
 *
 *	0 on success, -1 (with errno set) on error.
 *
 *---------------------------------------------------------------------------
 */
int
TclUnixSetBlockingMode(
    int fd,		/* File descriptor */
    int mode)		/* TCL_MODE_BLOCKING or TCL_MODE_NONBLOCKING */
{
#ifndef USE_FIONBIO
    int flags = fcntl(fd, F_GETFL);

    if (mode == TCL_MODE_BLOCKING) {
	flags &= ~O_NONBLOCK;
    } else {
	flags |= O_NONBLOCK;
    }
    return fcntl(fd, F_SETFL, flags);
#else /* USE_FIONBIO */
    int state = (mode == TCL_MODE_NONBLOCKING);
    return ioctl(fd, FIONBIO, &state);
#endif /* !USE_FIONBIO */
}

/*
 * Used to pad structures at size'd boundaries
 *
 * This macro assumes that the pointer 'buffer' was created from an aligned
 * pointer by adding the 'length'. If this 'length' was not a multiple of the
 * 'size' the result is unaligned and PadBuffer corrects both the pointer,







>
|

>








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

































28
29
30
31
32
33
34

#include "tclInt.h"
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <string.h>

/*
 * See also: SC_BLOCKING_STYLE in unix/tcl.m4
 */

#ifdef	USE_FIONBIO
#   ifdef HAVE_SYS_FILIO_H
#	include	<sys/filio.h>	/* For FIONBIO. */
#   endif
#   ifdef HAVE_SYS_IOCTL_H
#	include	<sys/ioctl.h>
#   endif
#endif	/* USE_FIONBIO */


































/*
 * Used to pad structures at size'd boundaries
 *
 * This macro assumes that the pointer 'buffer' was created from an aligned
 * pointer by adding the 'length'. If this 'length' was not a multiple of the
 * 'size' the result is unaligned and PadBuffer corrects both the pointer,
78
79
80
81
82
83
84





85

86
87





88

89
90
91
92
93
94
95
 * library calls.
 */

#ifdef TCL_THREADS

typedef struct ThreadSpecificData {
    struct passwd pwd;





    char pbuf[2048];


    struct group grp;





    char gbuf[2048];


#if !defined(HAVE_MTSAFE_GETHOSTBYNAME) || !defined(HAVE_MTSAFE_GETHOSTBYADDR)
    struct hostent hent;
    char hbuf[2048];
#endif
}  ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;







>
>
>
>
>

>


>
>
>
>
>

>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 * library calls.
 */

#ifdef TCL_THREADS

typedef struct ThreadSpecificData {
    struct passwd pwd;
#if defined(HAVE_GETPWNAM_R_5) || defined(HAVE_GETPWUID_R_5)
#define NEED_PW_CLEANER 1
    char *pbuf;
    int pbuflen;
#else
    char pbuf[2048];
#endif

    struct group grp;
#if defined(HAVE_GETGRNAM_R_5) || defined(HAVE_GETGRGID_R_5)
#define NEED_GR_CLEANER 1
    char *gbuf;
    int gbuflen;
#else
    char gbuf[2048];
#endif

#if !defined(HAVE_MTSAFE_GETHOSTBYNAME) || !defined(HAVE_MTSAFE_GETHOSTBYADDR)
    struct hostent hent;
    char hbuf[2048];
#endif
}  ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
123
124
125
126
127
128
129







130




































131
132
133
134
135
136
137
static int		CopyGrp(struct group *tgtPtr, char *buf, int buflen);
static int		CopyHostent(struct hostent *tgtPtr, char *buf,
			    int buflen);
static int		CopyPwd(struct passwd *tgtPtr, char *buf, int buflen);
static int		CopyString(const char *src, char *buf, int buflen);

#endif







#endif /* TCL_THREADS */





































/*
 *---------------------------------------------------------------------------
 *
 * TclpGetPwNam --
 *
 *      Thread-safe wrappers for getpwnam(). See "man getpwnam" for more







>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
static int		CopyGrp(struct group *tgtPtr, char *buf, int buflen);
static int		CopyHostent(struct hostent *tgtPtr, char *buf,
			    int buflen);
static int		CopyPwd(struct passwd *tgtPtr, char *buf, int buflen);
static int		CopyString(const char *src, char *buf, int buflen);

#endif

#ifdef NEED_PW_CLEANER
static void		FreePwBuf(ClientData ignored);
#endif
#ifdef NEED_GR_CLEANER
static void		FreeGrBuf(ClientData ignored);
#endif
#endif /* TCL_THREADS */

/*
 *---------------------------------------------------------------------------
 *
 * TclUnixSetBlockingMode --
 *
 *	Set the blocking mode of a file descriptor.
 *
 * Results:
 *
 *	0 on success, -1 (with errno set) on error.
 *
 *---------------------------------------------------------------------------
 */

int
TclUnixSetBlockingMode(
    int fd,			/* File descriptor */
    int mode)			/* Either TCL_MODE_BLOCKING or
				 * TCL_MODE_NONBLOCKING. */
{
#ifndef USE_FIONBIO
    int flags = fcntl(fd, F_GETFL);

    if (mode == TCL_MODE_BLOCKING) {
	flags &= ~O_NONBLOCK;
    } else {
	flags |= O_NONBLOCK;
    }
    return fcntl(fd, F_SETFL, flags);
#else /* USE_FIONBIO */
    int state = (mode == TCL_MODE_NONBLOCKING);

    return ioctl(fd, FIONBIO, &state);
#endif /* !USE_FIONBIO */
}

/*
 *---------------------------------------------------------------------------
 *
 * TclpGetPwNam --
 *
 *      Thread-safe wrappers for getpwnam(). See "man getpwnam" for more
154
155
156
157
158
159
160















161










162
163
164
165
166
167
168
169
    return getpwnam(name);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETPWNAM_R_5)
    struct passwd *pwPtr = NULL;
















    return (getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf),










		       &pwPtr) == 0 && pwPtr != NULL) ? &tsdPtr->pwd : NULL;

#elif defined(HAVE_GETPWNAM_R_4)
    return getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));

#else
#define NEED_COPYPWD 1
    struct passwd *pwPtr;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|







178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    return getpwnam(name);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETPWNAM_R_5)
    struct passwd *pwPtr = NULL;

    /*
     * How to allocate a buffer of the right initial size. If you want the
     * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
     * and weep.
     */

    if (tsdPtr->pbuf == NULL) {
	tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
	if (tsdPtr->pbuflen < 1) {
	    tsdPtr->pbuflen = 1024;
	}
	tsdPtr->pbuf = ckalloc(tsdPtr->pbuflen);
	Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
    }
    while (1) {
	int e = getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, tsdPtr->pbuflen,
		&pwPtr);

	if (e == 0) {
	    break;
	} else if (e != ERANGE) {
	    return NULL;
	}
	tsdPtr->pbuflen *= 2;
	tsdPtr->pbuf = ckrealloc(tsdPtr->pbuf, tsdPtr->pbuflen);
    }
    return (pwPtr != NULL ? &tsdPtr->pwd : NULL);

#elif defined(HAVE_GETPWNAM_R_4)
    return getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));

#else
#define NEED_COPYPWD 1
    struct passwd *pwPtr;
210
211
212
213
214
215
216















217










218
219
220
221
222
223
224
225
    return getpwuid(uid);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETPWUID_R_5)
    struct passwd *pwPtr = NULL;
















    return (getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf),










		       &pwPtr) == 0 && pwPtr != NULL) ? &tsdPtr->pwd : NULL;

#elif defined(HAVE_GETPWUID_R_4)
    return getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));

#else
#define NEED_COPYPWD 1
    struct passwd *pwPtr;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|







259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    return getpwuid(uid);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETPWUID_R_5)
    struct passwd *pwPtr = NULL;

    /*
     * How to allocate a buffer of the right initial size. If you want the
     * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
     * and weep.
     */

    if (tsdPtr->pbuf == NULL) {
	tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
	if (tsdPtr->pbuflen < 1) {
	    tsdPtr->pbuflen = 1024;
	}
	tsdPtr->pbuf = ckalloc(tsdPtr->pbuflen);
	Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
    }
    while (1) {
	int e = getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, tsdPtr->pbuflen,
		&pwPtr);

	if (e == 0) {
	    break;
	} else if (e != ERANGE) {
	    return NULL;
	}
	tsdPtr->pbuflen *= 2;
	tsdPtr->pbuf = ckrealloc(tsdPtr->pbuf, tsdPtr->pbuflen);
    }
    return (pwPtr != NULL ? &tsdPtr->pwd : NULL);

#elif defined(HAVE_GETPWUID_R_4)
    return getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));

#else
#define NEED_COPYPWD 1
    struct passwd *pwPtr;
240
241
242
243
244
245
246























247
248
249
250
251
252
253
    return NULL;		/* Not reached. */
#endif /* TCL_THREADS */
}

/*
 *---------------------------------------------------------------------------
 *























 * TclpGetGrNam --
 *
 *      Thread-safe wrappers for getgrnam(). See "man getgrnam" for more
 *      details.
 *
 * Results:
 *      Pointer to struct group on success or NULL on error.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    return NULL;		/* Not reached. */
#endif /* TCL_THREADS */
}

/*
 *---------------------------------------------------------------------------
 *
 * FreePwBuf --
 *
 *	Helper that is used to dispose of space allocated and referenced from
 *	the ThreadSpecificData for user entries. (Darn that baroque POSIX
 *	reentrant interface.)
 *
 *---------------------------------------------------------------------------
 */

#ifdef NEED_PW_CLEANER
static void
FreePwBuf(
    ClientData ignored)
{
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    ckfree(tsdPtr->pbuf);
}
#endif /* NEED_PW_CLEANER */

/*
 *---------------------------------------------------------------------------
 *
 * TclpGetGrNam --
 *
 *      Thread-safe wrappers for getgrnam(). See "man getgrnam" for more
 *      details.
 *
 * Results:
 *      Pointer to struct group on success or NULL on error.
263
264
265
266
267
268
269
270
271
272















273










274
275
276
277
278
279
280
281
    const char *name)
{
#if !defined(TCL_THREADS)
    return getgrnam(name);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETGRNAM_R_5)
    struct group *grPtr = NULL;
















    return (getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf),










		       &grPtr) == 0 && grPtr != NULL) ? &tsdPtr->grp : NULL;

#elif defined(HAVE_GETGRNAM_R_4)
    return getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));

#else
#define NEED_COPYGRP 1
    struct group *grPtr;







|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|







360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    const char *name)
{
#if !defined(TCL_THREADS)
    return getgrnam(name);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#ifdef HAVE_GETGRNAM_R_5
    struct group *grPtr = NULL;

    /*
     * How to allocate a buffer of the right initial size. If you want the
     * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
     * and weep.
     */

    if (tsdPtr->gbuf == NULL) {
	tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
	if (tsdPtr->gbuflen < 1) {
	    tsdPtr->gbuflen = 1024;
	}
	tsdPtr->gbuf = ckalloc(tsdPtr->gbuflen);
	Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
    }
    while (1) {
	int e = getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, tsdPtr->gbuflen,
		&grPtr);

	if (e == 0) {
	    break;
	} else if (e != ERANGE) {
	    return NULL;
	}
	tsdPtr->gbuflen *= 2;
	tsdPtr->gbuf = ckrealloc(tsdPtr->gbuf, tsdPtr->gbuflen);
    }
    return (grPtr != NULL ? &tsdPtr->grp : NULL);

#elif defined(HAVE_GETGRNAM_R_4)
    return getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));

#else
#define NEED_COPYGRP 1
    struct group *grPtr;
322
323
324
325
326
327
328















329










330
331
332
333
334
335
336
337
    return getgrgid(gid);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETGRGID_R_5)
    struct group *grPtr = NULL;
















    return (getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf),










		       &grPtr) == 0 && grPtr != NULL) ? &tsdPtr->grp : NULL;

#elif defined(HAVE_GETGRGID_R_4)
    return getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));

#else
#define NEED_COPYGRP 1
    struct group *grPtr;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|







444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
    return getgrgid(gid);
#else
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

#if defined(HAVE_GETGRGID_R_5)
    struct group *grPtr = NULL;

    /*
     * How to allocate a buffer of the right initial size. If you want the
     * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
     * and weep.
     */

    if (tsdPtr->gbuf == NULL) {
	tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
	if (tsdPtr->gbuflen < 1) {
	    tsdPtr->gbuflen = 1024;
	}
	tsdPtr->gbuf = ckalloc(tsdPtr->gbuflen);
	Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
    }
    while (1) {
	int e = getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, tsdPtr->gbuflen,
		&grPtr);

	if (e == 0) {
	    break;
	} else if (e != ERANGE) {
	    return NULL;
	}
	tsdPtr->gbuflen *= 2;
	tsdPtr->gbuf = ckrealloc(tsdPtr->gbuf, tsdPtr->gbuflen);
    }
    return (grPtr != NULL ? &tsdPtr->grp : NULL);

#elif defined(HAVE_GETGRGID_R_4)
    return getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));

#else
#define NEED_COPYGRP 1
    struct group *grPtr;
349
350
351
352
353
354
355























356
357
358
359
360
361
362
    return grPtr;
#endif

    return NULL;		/* Not reached. */
#endif /* TCL_THREADS */
}
























/*
 *---------------------------------------------------------------------------
 *
 * TclpGetHostByName --
 *
 *      Thread-safe wrappers for gethostbyname(). See "man gethostbyname" for
 *      more details.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    return grPtr;
#endif

    return NULL;		/* Not reached. */
#endif /* TCL_THREADS */
}

/*
 *---------------------------------------------------------------------------
 *
 * FreeGrBuf --
 *
 *	Helper that is used to dispose of space allocated and referenced from
 *	the ThreadSpecificData for group entries. (Darn that baroque POSIX
 *	reentrant interface.)
 *
 *---------------------------------------------------------------------------
 */

#ifdef NEED_GR_CLEANER
static void
FreeGrBuf(
    ClientData ignored)
{
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    ckfree(tsdPtr->gbuf);
}
#endif /* NEED_GR_CLEANER */

/*
 *---------------------------------------------------------------------------
 *
 * TclpGetHostByName --
 *
 *      Thread-safe wrappers for gethostbyname(). See "man gethostbyname" for
 *      more details.
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
 *
 *---------------------------------------------------------------------------
 */

#ifdef NEED_COPYSTRING
static int
CopyString(
    const char *src,	/* String to copy. */
    char *buf,			/* Buffer to copy into. */
    int buflen)			/* Size of buffer. */
{
    int len = 0;

    if (src != NULL) {
	len = strlen(src) + 1;







|







935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
 *
 *---------------------------------------------------------------------------
 */

#ifdef NEED_COPYSTRING
static int
CopyString(
    const char *src,		/* String to copy. */
    char *buf,			/* Buffer to copy into. */
    int buflen)			/* Size of buffer. */
{
    int len = 0;

    if (src != NULL) {
	len = strlen(src) + 1;

Changes to win/tclWinConsole.c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * tclWinConsole.c --
 *
 *	This file implements the Windows-specific console functions, and the
 *	"console" channel driver.
 *
 * Copyright (c) 1999 by Scriptics Corp.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclWinInt.h"

#include <sys/stat.h>

/*
 * The following variable is used to tell whether this module has been
 * initialized.
 */














<







1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
/*
 * tclWinConsole.c --
 *
 *	This file implements the Windows-specific console functions, and the
 *	"console" channel driver.
 *
 * Copyright (c) 1999 by Scriptics Corp.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclWinInt.h"

#include <sys/stat.h>

/*
 * The following variable is used to tell whether this module has been
 * initialized.
 */

42
43
44
45
46
47
48

















49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

84
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#define CONSOLE_EOF	  (1<<2)  /* Console has reached EOF. */
#define CONSOLE_BUFFERED  (1<<3)  /* Data was read into a buffer by the reader
				   * thread. */

#define CONSOLE_BUFFER_SIZE (8*1024)


















/*
 * This structure describes per-instance data for a console based channel.
 */

typedef struct ConsoleInfo {
    HANDLE handle;
    int type;
    struct ConsoleInfo *nextPtr;/* Pointer to next registered console. */
    Tcl_Channel channel;	/* Pointer to channel structure. */
    int validMask;		/* OR'ed combination of TCL_READABLE,
				 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
				 * which operations are valid on the file. */
    int watchMask;		/* OR'ed combination of TCL_READABLE,
				 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
				 * which events should be reported. */
    int flags;			/* State flags, see above for a list. */
    Tcl_ThreadId threadId;	/* Thread to which events should be reported.
				 * This value is used by the reader/writer
				 * threads. */
    HANDLE writeThread;		/* Handle to writer thread. */
    HANDLE readThread;		/* Handle to reader thread. */
    HANDLE writable;		/* Manual-reset event to signal when the
				 * writer thread has finished waiting for the
				 * current buffer to be written. */
    HANDLE readable;		/* Manual-reset event to signal when the
				 * reader thread has finished waiting for
				 * input. */
    HANDLE startWriter;		/* Auto-reset event used by the main thread to
				 * signal when the writer thread should
				 * attempt to write to the console. */
    HANDLE stopWriter;		/* Auto-reset event used by the main thread to
				 * signal when the writer thread should exit */
    HANDLE startReader;		/* Auto-reset event used by the main thread to
				 * signal when the reader thread should
				 * attempt to read from the console. */

    HANDLE stopReader;		/* Auto-reset event used by the main thread to
				 * signal when the reader thread should exit */

    DWORD writeError;		/* An error caused by the last background
				 * write. Set to 0 if no error has been
				 * detected. This word is shared with the
				 * writer thread so access must be
				 * synchronized with the writable object. */
    char *writeBuf;		/* Current background output buffer. Access is
				 * synchronized with the writable object. */
    int writeBufLen;		/* Size of write buffer. Access is
				 * synchronized with the writable object. */
    int toWrite;		/* Current amount to be written. Access is
				 * synchronized with the writable object. */
    int readFlags;		/* Flags that are shared with the reader
				 * thread. Access is synchronized with the
				 * readable object. */
    int bytesRead;		/* number of bytes in the buffer */
    int offset;			/* number of bytes read out of the buffer */
    char buffer[CONSOLE_BUFFER_SIZE];
				/* Data consumed by reader thread. */
} ConsoleInfo;

typedef struct ThreadSpecificData {
    /*
     * The following pointer refers to the head of the list of consoles that







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



















|
<
<
|
<
<
|
<
|
|
|
<
<
<
|
|
>
|
|
>














|
|







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


85


86

87
88
89



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

#define CONSOLE_EOF	  (1<<2)  /* Console has reached EOF. */
#define CONSOLE_BUFFERED  (1<<3)  /* Data was read into a buffer by the reader
				   * thread. */

#define CONSOLE_BUFFER_SIZE (8*1024)

/*
 * Structure containing handles associated with one of the special console
 * threads.
 */

typedef struct ConsoleThreadInfo {
    HANDLE thread;		/* Handle to reader or writer thread. */
    HANDLE readyEvent;		/* Manual-reset event to signal _to_ the main
				 * thread when the worker thread has finished
				 * waiting for its normal work to happen. */
    HANDLE startEvent;		/* Auto-reset event used by the main thread to
				 * signal when the thread should attempt to do
				 * its normal work. */
    HANDLE stopEvent;		/* Auto-reset event used by the main thread to
				 * signal when the thread should exit. */
} ConsoleThreadInfo;

/*
 * This structure describes per-instance data for a console based channel.
 */

typedef struct ConsoleInfo {
    HANDLE handle;
    int type;
    struct ConsoleInfo *nextPtr;/* Pointer to next registered console. */
    Tcl_Channel channel;	/* Pointer to channel structure. */
    int validMask;		/* OR'ed combination of TCL_READABLE,
				 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
				 * which operations are valid on the file. */
    int watchMask;		/* OR'ed combination of TCL_READABLE,
				 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
				 * which events should be reported. */
    int flags;			/* State flags, see above for a list. */
    Tcl_ThreadId threadId;	/* Thread to which events should be reported.
				 * This value is used by the reader/writer
				 * threads. */
    ConsoleThreadInfo writer;	/* A specialized thread for handling


				 * asynchronous writes to the console; the


				 * waiting starts when a start event is sent,

				 * and a reset event is sent back to the main
				 * thread when the write is done. A stop event
				 * is used to terminate the thread. */



    ConsoleThreadInfo reader;	/* A specialized thread for handling
				 * asynchronous reads from the console; the
				 * waiting starts when a start event is sent,
				 * and a reset event is sent back to the main
				 * thread when input is available. A stop
				 * event is used to terminate the thread. */
    DWORD writeError;		/* An error caused by the last background
				 * write. Set to 0 if no error has been
				 * detected. This word is shared with the
				 * writer thread so access must be
				 * synchronized with the writable object. */
    char *writeBuf;		/* Current background output buffer. Access is
				 * synchronized with the writable object. */
    int writeBufLen;		/* Size of write buffer. Access is
				 * synchronized with the writable object. */
    int toWrite;		/* Current amount to be written. Access is
				 * synchronized with the writable object. */
    int readFlags;		/* Flags that are shared with the reader
				 * thread. Access is synchronized with the
				 * readable object. */
    int bytesRead;		/* Number of bytes in the buffer. */
    int offset;			/* Number of bytes read out of the buffer. */
    char buffer[CONSOLE_BUFFER_SIZE];
				/* Data consumed by reader thread. */
} ConsoleInfo;

typedef struct ThreadSpecificData {
    /*
     * The following pointer refers to the head of the list of consoles that
128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155









156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

186
187


188

189
190
191
192
193
194
195
196
197
198
199
200

201
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

218
219

220
221
222
223
224
225
226
				 * pointer. */
} ConsoleEvent;

/*
 * Declarations for functions used only in this file.
 */

static int		ConsoleBlockModeProc(ClientData instanceData,int mode);

static void		ConsoleCheckProc(ClientData clientData, int flags);
static int		ConsoleCloseProc(ClientData instanceData,
			    Tcl_Interp *interp);
static int		ConsoleEventProc(Tcl_Event *evPtr, int flags);
static void		ConsoleExitHandler(ClientData clientData);
static int		ConsoleGetHandleProc(ClientData instanceData,
			    int direction, ClientData *handlePtr);
static void		ConsoleInit(void);
static int		ConsoleInputProc(ClientData instanceData, char *buf,
			    int toRead, int *errorCode);
static int		ConsoleOutputProc(ClientData instanceData,
			    const char *buf, int toWrite, int *errorCode);
static DWORD WINAPI	ConsoleReaderThread(LPVOID arg);
static void		ConsoleSetupProc(ClientData clientData, int flags);
static void		ConsoleWatchProc(ClientData instanceData, int mask);
static DWORD WINAPI	ConsoleWriterThread(LPVOID arg);
static void		ProcExitHandler(ClientData clientData);
static int		WaitForRead(ConsoleInfo *infoPtr, int blocking);
static void		ConsoleThreadActionProc(ClientData instanceData,
			    int action);










/*
 * This structure describes the channel type structure for command console
 * based IO.
 */

static const Tcl_ChannelType consoleChannelType = {
    "console",			/* Type name. */
    TCL_CHANNEL_VERSION_5,	/* v5 channel */
    ConsoleCloseProc,		/* Close proc. */
    ConsoleInputProc,		/* Input proc. */
    ConsoleOutputProc,		/* Output proc. */
    NULL,			/* Seek proc. */
    NULL,			/* Set option proc. */
    NULL,			/* Get option proc. */
    ConsoleWatchProc,		/* Set up notifier to watch the channel. */
    ConsoleGetHandleProc,	/* Get an OS handle from channel. */
    NULL,			/* close2proc. */
    ConsoleBlockModeProc,	/* Set blocking or non-blocking mode.*/
    NULL,			/* flush proc. */
    NULL,			/* handler proc. */
    NULL,			/* wide seek proc */
    ConsoleThreadActionProc,    /* thread action proc */
    NULL                       /* truncation */
};

/*
 *----------------------------------------------------------------------
 *
 * readConsoleBytes, writeConsoleBytes --

 * Wrapper for ReadConsole{A,W}, that takes and returns number of bytes
 * instead of number of TCHARS


 */

static BOOL
readConsoleBytes(
    HANDLE hConsole,
    LPVOID lpBuffer,
    DWORD nbytes,
    LPDWORD nbytesread)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize = sizeof(TCHAR);
    result = ReadConsole(
	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);

    if (nbytesread)
	*nbytesread = (ntchars*tcharsize);

    return result;
}

static BOOL
writeConsoleBytes(
    HANDLE hConsole,
    const void *lpBuffer,
    DWORD nbytes,
    LPDWORD nbyteswritten)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize = sizeof(TCHAR);
    result = WriteConsole(
	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);

    if (nbyteswritten)
	*nbyteswritten = (ntchars*tcharsize);

    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleInit --







|
>




















>
>
>
>
>
>
>
>
>


















|
|
|
|
|
|





|
>
|
|
>
>

>

|








|
|
>
|
|
>




|








|
|
>
|
|
>







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
				 * pointer. */
} ConsoleEvent;

/*
 * Declarations for functions used only in this file.
 */

static int		ConsoleBlockModeProc(ClientData instanceData,
			    int mode);
static void		ConsoleCheckProc(ClientData clientData, int flags);
static int		ConsoleCloseProc(ClientData instanceData,
			    Tcl_Interp *interp);
static int		ConsoleEventProc(Tcl_Event *evPtr, int flags);
static void		ConsoleExitHandler(ClientData clientData);
static int		ConsoleGetHandleProc(ClientData instanceData,
			    int direction, ClientData *handlePtr);
static void		ConsoleInit(void);
static int		ConsoleInputProc(ClientData instanceData, char *buf,
			    int toRead, int *errorCode);
static int		ConsoleOutputProc(ClientData instanceData,
			    const char *buf, int toWrite, int *errorCode);
static DWORD WINAPI	ConsoleReaderThread(LPVOID arg);
static void		ConsoleSetupProc(ClientData clientData, int flags);
static void		ConsoleWatchProc(ClientData instanceData, int mask);
static DWORD WINAPI	ConsoleWriterThread(LPVOID arg);
static void		ProcExitHandler(ClientData clientData);
static int		WaitForRead(ConsoleInfo *infoPtr, int blocking);
static void		ConsoleThreadActionProc(ClientData instanceData,
			    int action);
static BOOL		ReadConsoleBytes(HANDLE hConsole, LPVOID lpBuffer,
			    DWORD nbytes, LPDWORD nbytesread);
static BOOL		WriteConsoleBytes(HANDLE hConsole,
			    const void *lpBuffer, DWORD nbytes,
			    LPDWORD nbyteswritten);
static void		StartChannelThread(ConsoleInfo *infoPtr,
			    ConsoleThreadInfo *threadInfoPtr,
			    LPTHREAD_START_ROUTINE threadProc);
static void		StopChannelThread(ConsoleThreadInfo *threadInfoPtr);

/*
 * This structure describes the channel type structure for command console
 * based IO.
 */

static const Tcl_ChannelType consoleChannelType = {
    "console",			/* Type name. */
    TCL_CHANNEL_VERSION_5,	/* v5 channel */
    ConsoleCloseProc,		/* Close proc. */
    ConsoleInputProc,		/* Input proc. */
    ConsoleOutputProc,		/* Output proc. */
    NULL,			/* Seek proc. */
    NULL,			/* Set option proc. */
    NULL,			/* Get option proc. */
    ConsoleWatchProc,		/* Set up notifier to watch the channel. */
    ConsoleGetHandleProc,	/* Get an OS handle from channel. */
    NULL,			/* close2proc. */
    ConsoleBlockModeProc,	/* Set blocking or non-blocking mode. */
    NULL,			/* Flush proc. */
    NULL,			/* Handler proc. */
    NULL,			/* Wide seek proc. */
    ConsoleThreadActionProc,	/* Thread action proc. */
    NULL			/* Truncation proc. */
};

/*
 *----------------------------------------------------------------------
 *
 * ReadConsoleBytes, WriteConsoleBytes --
 *
 *	Wrapper for ReadConsole{A,W}, that takes and returns number of bytes
 *	instead of number of TCHARS.
 *
 *----------------------------------------------------------------------
 */

static BOOL
ReadConsoleBytes(
    HANDLE hConsole,
    LPVOID lpBuffer,
    DWORD nbytes,
    LPDWORD nbytesread)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize = sizeof(TCHAR);

    result = ReadConsole(hConsole, lpBuffer, nbytes / tcharsize, &ntchars,
	    NULL);
    if (nbytesread != NULL) {
	*nbytesread = ntchars * tcharsize;
    }
    return result;
}

static BOOL
WriteConsoleBytes(
    HANDLE hConsole,
    const void *lpBuffer,
    DWORD nbytes,
    LPDWORD nbyteswritten)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize = sizeof(TCHAR);

    result = WriteConsole(hConsole, lpBuffer, nbytes / tcharsize, &ntchars,
	    NULL);
    if (nbyteswritten != NULL) {
	*nbyteswritten = ntchars * tcharsize;
    }
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleInit --
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

261
262
263
264
265
266
267
 *
 *----------------------------------------------------------------------
 */

static void
ConsoleInit(void)
{
    ThreadSpecificData *tsdPtr;

    /*
     * Check the initialized flag first, then check again in the mutex. This
     * is a speed enhancement.
     */

    if (!initialized) {
	Tcl_MutexLock(&consoleMutex);
	if (!initialized) {
	    initialized = 1;
	    Tcl_CreateExitHandler(ProcExitHandler, NULL);
	}
	Tcl_MutexUnlock(&consoleMutex);
    }

    tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
    if (tsdPtr == NULL) {
	tsdPtr = TCL_TSD_INIT(&dataKey);

	tsdPtr->firstConsolePtr = NULL;
	Tcl_CreateEventSource(ConsoleSetupProc, ConsoleCheckProc, NULL);
	Tcl_CreateThreadExitHandler(ConsoleExitHandler, NULL);
    }
}

/*







<
<














|
<
|
>







263
264
265
266
267
268
269


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
292
293
 *
 *----------------------------------------------------------------------
 */

static void
ConsoleInit(void)
{


    /*
     * Check the initialized flag first, then check again in the mutex. This
     * is a speed enhancement.
     */

    if (!initialized) {
	Tcl_MutexLock(&consoleMutex);
	if (!initialized) {
	    initialized = 1;
	    Tcl_CreateExitHandler(ProcExitHandler, NULL);
	}
	Tcl_MutexUnlock(&consoleMutex);
    }

    if (TclThreadDataKeyGet(&dataKey) == NULL) {

	ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

	tsdPtr->firstConsolePtr = NULL;
	Tcl_CreateEventSource(ConsoleSetupProc, ConsoleCheckProc, NULL);
	Tcl_CreateThreadExitHandler(ConsoleExitHandler, NULL);
    }
}

/*
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
 *	Removes the console event source.
 *
 *----------------------------------------------------------------------
 */

static void
ConsoleExitHandler(
    ClientData clientData)	/* Old window proc */
{
    Tcl_DeleteEventSource(ConsoleSetupProc, ConsoleCheckProc, NULL);
}

/*
 *----------------------------------------------------------------------
 *







|







305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 *	Removes the console event source.
 *
 *----------------------------------------------------------------------
 */

static void
ConsoleExitHandler(
    ClientData clientData)	/* Old window proc. */
{
    Tcl_DeleteEventSource(ConsoleSetupProc, ConsoleCheckProc, NULL);
}

/*
 *----------------------------------------------------------------------
 *
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
 *	Resets the process list.
 *
 *----------------------------------------------------------------------
 */

static void
ProcExitHandler(
    ClientData clientData)	/* Old window proc */
{
    Tcl_MutexLock(&consoleMutex);
    initialized = 0;
    Tcl_MutexUnlock(&consoleMutex);
}

/*







|







329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
 *	Resets the process list.
 *
 *----------------------------------------------------------------------
 */

static void
ProcExitHandler(
    ClientData clientData)	/* Old window proc. */
{
    Tcl_MutexLock(&consoleMutex);
    initialized = 0;
    Tcl_MutexUnlock(&consoleMutex);
}

/*
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
    /*
     * Look to see if any events are already pending. If they are, poll.
     */

    for (infoPtr = tsdPtr->firstConsolePtr; infoPtr != NULL;
	    infoPtr = infoPtr->nextPtr) {
	if (infoPtr->watchMask & TCL_WRITABLE) {
	    if (WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT) {

		block = 0;
	    }
	}
	if (infoPtr->watchMask & TCL_READABLE) {
	    if (WaitForRead(infoPtr, 0) >= 0) {
		block = 0;
	    }







|
>







374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    /*
     * Look to see if any events are already pending. If they are, poll.
     */

    for (infoPtr = tsdPtr->firstConsolePtr; infoPtr != NULL;
	    infoPtr = infoPtr->nextPtr) {
	if (infoPtr->watchMask & TCL_WRITABLE) {
	    if (WaitForSingleObject(infoPtr->writer.readyEvent,
		    0) != WAIT_TIMEOUT) {
		block = 0;
	    }
	}
	if (infoPtr->watchMask & TCL_READABLE) {
	    if (WaitForRead(infoPtr, 0) >= 0) {
		block = 0;
	    }
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

static void
ConsoleCheckProc(
    ClientData data,		/* Not used. */
    int flags)			/* Event flags as passed to Tcl_DoOneEvent. */
{
    ConsoleInfo *infoPtr;
    ConsoleEvent *evPtr;
    int needEvent;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    if (!(flags & TCL_FILE_EVENTS)) {
	return;
    }








<







413
414
415
416
417
418
419

420
421
422
423
424
425
426

static void
ConsoleCheckProc(
    ClientData data,		/* Not used. */
    int flags)			/* Event flags as passed to Tcl_DoOneEvent. */
{
    ConsoleInfo *infoPtr;

    int needEvent;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    if (!(flags & TCL_FILE_EVENTS)) {
	return;
    }

411
412
413
414
415
416
417
418

419
420
421
422
423
424
425
426
427
428
429


430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

	/*
	 * Queue an event if the console is signaled for reading or writing.
	 */

	needEvent = 0;
	if (infoPtr->watchMask & TCL_WRITABLE) {
	    if (WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT) {

		needEvent = 1;
	    }
	}

	if (infoPtr->watchMask & TCL_READABLE) {
	    if (WaitForRead(infoPtr, 0) >= 0) {
		needEvent = 1;
	    }
	}

	if (needEvent) {


	    infoPtr->flags |= CONSOLE_PENDING;
	    evPtr = ckalloc(sizeof(ConsoleEvent));
	    evPtr->header.proc = ConsoleEventProc;
	    evPtr->infoPtr = infoPtr;
	    Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
	}
    }
}


/*
 *----------------------------------------------------------------------
 *
 * ConsoleBlockModeProc --
 *
 *	Set blocking or non-blocking mode on channel.







|
>











>
>

<






<







437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

460
461
462
463
464
465

466
467
468
469
470
471
472

	/*
	 * Queue an event if the console is signaled for reading or writing.
	 */

	needEvent = 0;
	if (infoPtr->watchMask & TCL_WRITABLE) {
	    if (WaitForSingleObject(infoPtr->writer.readyEvent,
		    0) != WAIT_TIMEOUT) {
		needEvent = 1;
	    }
	}

	if (infoPtr->watchMask & TCL_READABLE) {
	    if (WaitForRead(infoPtr, 0) >= 0) {
		needEvent = 1;
	    }
	}

	if (needEvent) {
	    ConsoleEvent *evPtr = ckalloc(sizeof(ConsoleEvent));

	    infoPtr->flags |= CONSOLE_PENDING;

	    evPtr->header.proc = ConsoleEventProc;
	    evPtr->infoPtr = infoPtr;
	    Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
	}
    }
}


/*
 *----------------------------------------------------------------------
 *
 * ConsoleBlockModeProc --
 *
 *	Set blocking or non-blocking mode on channel.
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478














































































479
480
481
482
483
484
485

static int
ConsoleBlockModeProc(
    ClientData instanceData,	/* Instance data for channel. */
    int mode)			/* TCL_MODE_BLOCKING or
				 * TCL_MODE_NONBLOCKING. */
{
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;

    /*
     * Consoles on Windows can not be switched between blocking and
     * nonblocking, hence we have to emulate the behavior. This is done in the
     * input function by checking against a bit in the state. We set or unset
     * the bit here to cause the input function to emulate the correct
     * behavior.
     */

    if (mode == TCL_MODE_NONBLOCKING) {
	infoPtr->flags |= CONSOLE_ASYNC;
    } else {
	infoPtr->flags &= ~(CONSOLE_ASYNC);
    }
    return 0;
}















































































/*
 *----------------------------------------------------------------------
 *
 * ConsoleCloseProc --
 *
 *	Closes a console based IO channel.







|












|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590

static int
ConsoleBlockModeProc(
    ClientData instanceData,	/* Instance data for channel. */
    int mode)			/* TCL_MODE_BLOCKING or
				 * TCL_MODE_NONBLOCKING. */
{
    ConsoleInfo *infoPtr = instanceData;

    /*
     * Consoles on Windows can not be switched between blocking and
     * nonblocking, hence we have to emulate the behavior. This is done in the
     * input function by checking against a bit in the state. We set or unset
     * the bit here to cause the input function to emulate the correct
     * behavior.
     */

    if (mode == TCL_MODE_NONBLOCKING) {
	infoPtr->flags |= CONSOLE_ASYNC;
    } else {
	infoPtr->flags &= ~CONSOLE_ASYNC;
    }
    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * StartChannelThread, StopChannelThread --
 *
 *	Helpers that codify how to ask one of the console service threads to
 *	start and stop.
 *
 *----------------------------------------------------------------------
 */

static void
StartChannelThread(
    ConsoleInfo *infoPtr,
    ConsoleThreadInfo *threadInfoPtr,
    LPTHREAD_START_ROUTINE threadProc)
{
    DWORD id;

    threadInfoPtr->readyEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
    threadInfoPtr->startEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    threadInfoPtr->stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    threadInfoPtr->thread = CreateThread(NULL, 256, threadProc, infoPtr, 0,
	    &id);
    SetThreadPriority(threadInfoPtr->thread, THREAD_PRIORITY_HIGHEST);
}

static void
StopChannelThread(
    ConsoleThreadInfo *threadInfoPtr)
{
    DWORD exitCode = 0;

    /*
     * The thread may already have closed on it's own. Check it's exit
     * code.
     */

    GetExitCodeThread(threadInfoPtr->thread, &exitCode);
    if (exitCode == STILL_ACTIVE) {
	/*
	 * Set the stop event so that if the reader thread is blocked in
	 * ConsoleReaderThread on WaitForMultipleEvents, it will exit cleanly.
	 */

	SetEvent(threadInfoPtr->stopEvent);

	/*
	 * Wait at most 20 milliseconds for the reader thread to close.
	 */

	if (WaitForSingleObject(threadInfoPtr->thread, 20) == WAIT_TIMEOUT) {
	    /*
	     * Forcibly terminate the background thread as a last resort.
	     * Note that we need to guard against terminating the thread while
	     * it is in the middle of Tcl_ThreadAlert because it won't be able
	     * to release the notifier lock.
	     */

	    Tcl_MutexLock(&consoleMutex);
	    /* BUG: this leaks memory. */
	    TerminateThread(threadInfoPtr->thread, 0);
	    Tcl_MutexUnlock(&consoleMutex);
	}
    }

    /*
     * Close all the handles associated with the thread, and set the thread
     * handle field to NULL to mark that the thread has been cleaned up.
     */

    CloseHandle(threadInfoPtr->thread);
    CloseHandle(threadInfoPtr->readyEvent);
    CloseHandle(threadInfoPtr->startEvent);
    CloseHandle(threadInfoPtr->stopEvent);
    threadInfoPtr->thread = NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleCloseProc --
 *
 *	Closes a console based IO channel.
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
 */

static int
ConsoleCloseProc(
    ClientData instanceData,	/* Pointer to ConsoleInfo structure. */
    Tcl_Interp *interp)		/* For error reporting. */
{
    ConsoleInfo *consolePtr = (ConsoleInfo *) instanceData;
    int errorCode;
    ConsoleInfo *infoPtr, **nextPtrPtr;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
    DWORD exitCode;

    errorCode = 0;

    /*
     * Clean up the background thread if necessary. Note that this must be
     * done before we can close the file, since the thread may be blocking
     * trying to read from the console.
     */

    if (consolePtr->readThread) {
	/*
	 * The thread may already have closed on it's own. Check it's exit
	 * code.
	 */

	GetExitCodeThread(consolePtr->readThread, &exitCode);

	if (exitCode == STILL_ACTIVE) {
	    /*
	     * Set the stop event so that if the reader thread is blocked in
	     * ConsoleReaderThread on WaitForMultipleEvents, it will exit
	     * cleanly.
	     */

	    SetEvent(consolePtr->stopReader);

	    /*
	     * Wait at most 20 milliseconds for the reader thread to close.
	     */

	    if (WaitForSingleObject(consolePtr->readThread, 20)
		    == WAIT_TIMEOUT) {
		/*
		 * Forcibly terminate the background thread as a last resort.
		 * Note that we need to guard against terminating the thread
		 * while it is in the middle of Tcl_ThreadAlert because it
		 * won't be able to release the notifier lock.
		 */

		Tcl_MutexLock(&consoleMutex);

		/* BUG: this leaks memory. */
		TerminateThread(consolePtr->readThread, 0);
		Tcl_MutexUnlock(&consoleMutex);
	    }
	}

	CloseHandle(consolePtr->readThread);
	CloseHandle(consolePtr->readable);
	CloseHandle(consolePtr->startReader);
	CloseHandle(consolePtr->stopReader);
	consolePtr->readThread = NULL;
    }
    consolePtr->validMask &= ~TCL_READABLE;

    /*
     * Wait for the writer thread to finish the current buffer, then terminate
     * the thread and close the handles. If the channel is nonblocking, there
     * should be no pending write operations.
     */

    if (consolePtr->writeThread) {
	if (consolePtr->toWrite) {
	    /*
	     * We only need to wait if there is something to write. This may
	     * prevent infinite wait on exit. [python bug 216289]
	     */

	    WaitForSingleObject(consolePtr->writable, INFINITE);
	}

	/*
	 * The thread may already have closed on it's own. Check it's exit
	 * code.
	 */

	GetExitCodeThread(consolePtr->writeThread, &exitCode);

	if (exitCode == STILL_ACTIVE) {
	    /*
	     * Set the stop event so that if the reader thread is blocked in
	     * ConsoleWriterThread on WaitForMultipleEvents, it will exit
	     * cleanly.
	     */

	    SetEvent(consolePtr->stopWriter);

	    /*
	     * Wait at most 20 milliseconds for the writer thread to close.
	     */

	    if (WaitForSingleObject(consolePtr->writeThread, 20)
		    == WAIT_TIMEOUT) {
		/*
		 * Forcibly terminate the background thread as a last resort.
		 * Note that we need to guard against terminating the thread
		 * while it is in the middle of Tcl_ThreadAlert because it
		 * won't be able to release the notifier lock.
		 */

		Tcl_MutexLock(&consoleMutex);

		/* BUG: this leaks memory. */
		TerminateThread(consolePtr->writeThread, 0);
		Tcl_MutexUnlock(&consoleMutex);
	    }
	}

	CloseHandle(consolePtr->writeThread);
	CloseHandle(consolePtr->writable);
	CloseHandle(consolePtr->startWriter);
	CloseHandle(consolePtr->stopWriter);
	consolePtr->writeThread = NULL;
    }
    consolePtr->validMask &= ~TCL_WRITABLE;


    /*
     * Don't close the Win32 handle if the handle is a standard channel during
     * the thread exit process. Otherwise, one thread may kill the stdio of
     * another.
     */








|
|


<
<
<







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|









|



|


|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


<







599
600
601
602
603
604
605
606
607
608
609



610
611
612
613
614
615
616
617









































618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637









































638
639
640

641
642
643
644
645
646
647
 */

static int
ConsoleCloseProc(
    ClientData instanceData,	/* Pointer to ConsoleInfo structure. */
    Tcl_Interp *interp)		/* For error reporting. */
{
    ConsoleInfo *consolePtr = instanceData;
    int errorCode = 0;
    ConsoleInfo *infoPtr, **nextPtrPtr;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);




    /*
     * Clean up the background thread if necessary. Note that this must be
     * done before we can close the file, since the thread may be blocking
     * trying to read from the console.
     */

    if (consolePtr->reader.thread) {









































	StopChannelThread(&consolePtr->reader);
    }
    consolePtr->validMask &= ~TCL_READABLE;

    /*
     * Wait for the writer thread to finish the current buffer, then terminate
     * the thread and close the handles. If the channel is nonblocking, there
     * should be no pending write operations.
     */

    if (consolePtr->writer.thread) {
	if (consolePtr->toWrite) {
	    /*
	     * We only need to wait if there is something to write. This may
	     * prevent infinite wait on exit. [Python Bug 216289]
	     */

	    WaitForSingleObject(consolePtr->writer.readyEvent, INFINITE);
	}










































	StopChannelThread(&consolePtr->writer);
    }
    consolePtr->validMask &= ~TCL_WRITABLE;


    /*
     * Don't close the Win32 handle if the handle is a standard channel during
     * the thread exit process. Otherwise, one thread may kill the stdio of
     * another.
     */

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    /*
     * Remove the file from the list of watched files.
     */

    for (nextPtrPtr = &(tsdPtr->firstConsolePtr), infoPtr = *nextPtrPtr;
	    infoPtr != NULL;
	    nextPtrPtr = &infoPtr->nextPtr, infoPtr = *nextPtrPtr) {
	if (infoPtr == (ConsoleInfo *)consolePtr) {
	    *nextPtrPtr = infoPtr->nextPtr;
	    break;
	}
    }
    if (consolePtr->writeBuf != NULL) {
	ckfree(consolePtr->writeBuf);
	consolePtr->writeBuf = 0;







|







660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
    /*
     * Remove the file from the list of watched files.
     */

    for (nextPtrPtr = &(tsdPtr->firstConsolePtr), infoPtr = *nextPtrPtr;
	    infoPtr != NULL;
	    nextPtrPtr = &infoPtr->nextPtr, infoPtr = *nextPtrPtr) {
	if (infoPtr == (ConsoleInfo *) consolePtr) {
	    *nextPtrPtr = infoPtr->nextPtr;
	    break;
	}
    }
    if (consolePtr->writeBuf != NULL) {
	ckfree(consolePtr->writeBuf);
	consolePtr->writeBuf = 0;
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
ConsoleInputProc(
    ClientData instanceData,	/* Console state. */
    char *buf,			/* Where to store data read. */
    int bufSize,		/* How much space is available in the
				 * buffer? */
    int *errorCode)		/* Where to store error code. */
{
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;
    DWORD count, bytesRead = 0;
    int result;

    *errorCode = 0;

    /*
     * Synchronize with the reader thread.







|







700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
ConsoleInputProc(
    ClientData instanceData,	/* Console state. */
    char *buf,			/* Where to store data read. */
    int bufSize,		/* How much space is available in the
				 * buffer? */
    int *errorCode)		/* Where to store error code. */
{
    ConsoleInfo *infoPtr = instanceData;
    DWORD count, bytesRead = 0;
    int result;

    *errorCode = 0;

    /*
     * Synchronize with the reader thread.
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
	    bytesRead = bufSize;
	    infoPtr->offset += bufSize;
	} else {
	    memcpy(buf, &infoPtr->buffer[infoPtr->offset], (size_t) bufSize);
	    bytesRead = infoPtr->bytesRead - infoPtr->offset;

	    /*
	     * Reset the buffer
	     */

	    infoPtr->readFlags &= ~CONSOLE_BUFFERED;
	    infoPtr->offset = 0;
	}

	return bytesRead;
    }

    /*
     * Attempt to read bufSize bytes. The read will return immediately if
     * there is any data available. Otherwise it will block until at least one
     * byte is available or an EOF occurs.
     */

    if (readConsoleBytes(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &count)
	    == TRUE) {
	buf[count] = '\0';
	return count;
    }

    return -1;
}








|















|
|







735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
	    bytesRead = bufSize;
	    infoPtr->offset += bufSize;
	} else {
	    memcpy(buf, &infoPtr->buffer[infoPtr->offset], (size_t) bufSize);
	    bytesRead = infoPtr->bytesRead - infoPtr->offset;

	    /*
	     * Reset the buffer.
	     */

	    infoPtr->readFlags &= ~CONSOLE_BUFFERED;
	    infoPtr->offset = 0;
	}

	return bytesRead;
    }

    /*
     * Attempt to read bufSize bytes. The read will return immediately if
     * there is any data available. Otherwise it will block until at least one
     * byte is available or an EOF occurs.
     */

    if (ReadConsoleBytes(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize,
	    &count) == TRUE) {
	buf[count] = '\0';
	return count;
    }

    return -1;
}

766
767
768
769
770
771
772
773

774
775
776
777
778
779
780
781
782
783
784
785
static int
ConsoleOutputProc(
    ClientData instanceData,	/* Console state. */
    const char *buf,		/* The data buffer. */
    int toWrite,		/* How many bytes to write? */
    int *errorCode)		/* Where to store error code. */
{
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;

    DWORD bytesWritten, timeout;

    *errorCode = 0;
    timeout = (infoPtr->flags & CONSOLE_ASYNC) ? 0 : INFINITE;
    if (WaitForSingleObject(infoPtr->writable, timeout) == WAIT_TIMEOUT) {
	/*
	 * The writer thread is blocked waiting for a write to complete and
	 * the channel is in non-blocking mode.
	 */

	errno = EAGAIN;
	goto error;







|
>




|







785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
static int
ConsoleOutputProc(
    ClientData instanceData,	/* Console state. */
    const char *buf,		/* The data buffer. */
    int toWrite,		/* How many bytes to write? */
    int *errorCode)		/* Where to store error code. */
{
    ConsoleInfo *infoPtr = instanceData;
    ConsoleThreadInfo *threadInfo = &infoPtr->reader;
    DWORD bytesWritten, timeout;

    *errorCode = 0;
    timeout = (infoPtr->flags & CONSOLE_ASYNC) ? 0 : INFINITE;
    if (WaitForSingleObject(threadInfo->readyEvent,timeout) == WAIT_TIMEOUT) {
	/*
	 * The writer thread is blocked waiting for a write to complete and
	 * the channel is in non-blocking mode.
	 */

	errno = EAGAIN;
	goto error;
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

	    if (infoPtr->writeBuf) {
		ckfree(infoPtr->writeBuf);
	    }
	    infoPtr->writeBufLen = toWrite;
	    infoPtr->writeBuf = ckalloc(toWrite);
	}
	memcpy(infoPtr->writeBuf, buf, (size_t)toWrite);
	infoPtr->toWrite = toWrite;
	ResetEvent(infoPtr->writable);
	SetEvent(infoPtr->startWriter);
	bytesWritten = toWrite;
    } else {
	/*
	 * In the blocking case, just try to write the buffer directly. This
	 * avoids an unnecessary copy.
	 */

	if (writeConsoleBytes(infoPtr->handle, buf, (DWORD)toWrite,
			      &bytesWritten)
		== FALSE) {
	    TclWinConvertError(GetLastError());
	    goto error;
	}
    }
    return bytesWritten;

  error:







|

|
|







|
|
<







828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

848
849
850
851
852
853
854

	    if (infoPtr->writeBuf) {
		ckfree(infoPtr->writeBuf);
	    }
	    infoPtr->writeBufLen = toWrite;
	    infoPtr->writeBuf = ckalloc(toWrite);
	}
	memcpy(infoPtr->writeBuf, buf, (size_t) toWrite);
	infoPtr->toWrite = toWrite;
	ResetEvent(threadInfo->readyEvent);
	SetEvent(threadInfo->startEvent);
	bytesWritten = toWrite;
    } else {
	/*
	 * In the blocking case, just try to write the buffer directly. This
	 * avoids an unnecessary copy.
	 */

	if (WriteConsoleBytes(infoPtr->handle, buf, (DWORD) toWrite,
		&bytesWritten) == FALSE) {

	    TclWinConvertError(GetLastError());
	    goto error;
	}
    }
    return bytesWritten;

  error:
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907

908
909
910
911
912
913
914

static int
ConsoleEventProc(
    Tcl_Event *evPtr,		/* Event to service. */
    int flags)			/* Flags that indicate what events to handle,
				 * such as TCL_FILE_EVENTS. */
{
    ConsoleEvent *consoleEvPtr = (ConsoleEvent *)evPtr;
    ConsoleInfo *infoPtr;
    int mask;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    if (!(flags & TCL_FILE_EVENTS)) {
	return 0;
    }

    /*
     * Search through the list of watched consoles for the one whose handle
     * matches the event. We do this rather than simply dereferencing the
     * handle in the event so that consoles can be deleted while the event is
     * in the queue.
     */

    for (infoPtr = tsdPtr->firstConsolePtr; infoPtr != NULL;
	    infoPtr = infoPtr->nextPtr) {
	if (consoleEvPtr->infoPtr == infoPtr) {
	    infoPtr->flags &= ~(CONSOLE_PENDING);
	    break;
	}
    }

    /*
     * Remove stale events.
     */

    if (!infoPtr) {
	return 1;
    }

    /*
     * Check to see if the console is readable. Note that we can't tell if a
     * console is writable, so we always report it as being writable unless we
     * have detected EOF.
     */

    mask = 0;
    if (infoPtr->watchMask & TCL_WRITABLE) {
	if (WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT) {

	    mask = TCL_WRITABLE;
	}
    }

    if (infoPtr->watchMask & TCL_READABLE) {
	if (WaitForRead(infoPtr, 0) >= 0) {
	    if (infoPtr->readFlags & CONSOLE_EOF) {







|


















|




















|
>







879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934

static int
ConsoleEventProc(
    Tcl_Event *evPtr,		/* Event to service. */
    int flags)			/* Flags that indicate what events to handle,
				 * such as TCL_FILE_EVENTS. */
{
    ConsoleEvent *consoleEvPtr = (ConsoleEvent *) evPtr;
    ConsoleInfo *infoPtr;
    int mask;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    if (!(flags & TCL_FILE_EVENTS)) {
	return 0;
    }

    /*
     * Search through the list of watched consoles for the one whose handle
     * matches the event. We do this rather than simply dereferencing the
     * handle in the event so that consoles can be deleted while the event is
     * in the queue.
     */

    for (infoPtr = tsdPtr->firstConsolePtr; infoPtr != NULL;
	    infoPtr = infoPtr->nextPtr) {
	if (consoleEvPtr->infoPtr == infoPtr) {
	    infoPtr->flags &= ~CONSOLE_PENDING;
	    break;
	}
    }

    /*
     * Remove stale events.
     */

    if (!infoPtr) {
	return 1;
    }

    /*
     * Check to see if the console is readable. Note that we can't tell if a
     * console is writable, so we always report it as being writable unless we
     * have detected EOF.
     */

    mask = 0;
    if (infoPtr->watchMask & TCL_WRITABLE) {
	if (WaitForSingleObject(infoPtr->writer.readyEvent,
		0) != WAIT_TIMEOUT) {
	    mask = TCL_WRITABLE;
	}
    }

    if (infoPtr->watchMask & TCL_READABLE) {
	if (WaitForRead(infoPtr, 0) >= 0) {
	    if (infoPtr->readFlags & CONSOLE_EOF) {
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

966
967
968
969
970
971
972
ConsoleWatchProc(
    ClientData instanceData,	/* Console state. */
    int mask)			/* What events to watch for, OR-ed combination
				 * of TCL_READABLE, TCL_WRITABLE and
				 * TCL_EXCEPTION. */
{
    ConsoleInfo **nextPtrPtr, *ptr;
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;
    int oldMask = infoPtr->watchMask;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    /*
     * Since most of the work is handled by the background threads, we just
     * need to update the watchMask and then force the notifier to poll once.
     */

    infoPtr->watchMask = mask & infoPtr->validMask;
    if (infoPtr->watchMask) {
	Tcl_Time blockTime = { 0, 0 };

	if (!oldMask) {
	    infoPtr->nextPtr = tsdPtr->firstConsolePtr;
	    tsdPtr->firstConsolePtr = infoPtr;
	}
	Tcl_SetMaxBlockTime(&blockTime);
    } else if (oldMask) {
	/*







|











>







967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
ConsoleWatchProc(
    ClientData instanceData,	/* Console state. */
    int mask)			/* What events to watch for, OR-ed combination
				 * of TCL_READABLE, TCL_WRITABLE and
				 * TCL_EXCEPTION. */
{
    ConsoleInfo **nextPtrPtr, *ptr;
    ConsoleInfo *infoPtr = instanceData;
    int oldMask = infoPtr->watchMask;
    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);

    /*
     * Since most of the work is handled by the background threads, we just
     * need to update the watchMask and then force the notifier to poll once.
     */

    infoPtr->watchMask = mask & infoPtr->validMask;
    if (infoPtr->watchMask) {
	Tcl_Time blockTime = { 0, 0 };

	if (!oldMask) {
	    infoPtr->nextPtr = tsdPtr->firstConsolePtr;
	    tsdPtr->firstConsolePtr = infoPtr;
	}
	Tcl_SetMaxBlockTime(&blockTime);
    } else if (oldMask) {
	/*
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
 *
 *----------------------------------------------------------------------
 */

static int
ConsoleGetHandleProc(
    ClientData instanceData,	/* The console state. */
    int direction,		/* TCL_READABLE or TCL_WRITABLE */
    ClientData *handlePtr)	/* Where to store the handle. */
{
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;

    *handlePtr = (ClientData) infoPtr->handle;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * WaitForRead --







|


|

|







1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
 *
 *----------------------------------------------------------------------
 */

static int
ConsoleGetHandleProc(
    ClientData instanceData,	/* The console state. */
    int direction,		/* TCL_READABLE or TCL_WRITABLE. */
    ClientData *handlePtr)	/* Where to store the handle. */
{
    ConsoleInfo *infoPtr = instanceData;

    *handlePtr = infoPtr->handle;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * WaitForRead --
1039
1040
1041
1042
1043
1044
1045

1046
1047
1048
1049
1050
1051
1052
1053
1054

1055
1056
1057
1058
1059
1060
1061
WaitForRead(
    ConsoleInfo *infoPtr,	/* Console state. */
    int blocking)		/* Indicates whether call should be blocking
				 * or not. */
{
    DWORD timeout, count;
    HANDLE *handle = infoPtr->handle;

    INPUT_RECORD input;

    while (1) {
	/*
	 * Synchronize with the reader thread.
	 */

	timeout = blocking ? INFINITE : 0;
	if (WaitForSingleObject(infoPtr->readable, timeout) == WAIT_TIMEOUT) {

	    /*
	     * The reader thread is blocked waiting for data and the channel
	     * is in non-blocking mode.
	     */

	    errno = EAGAIN;
	    return -1;







>








|
>







1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
WaitForRead(
    ConsoleInfo *infoPtr,	/* Console state. */
    int blocking)		/* Indicates whether call should be blocking
				 * or not. */
{
    DWORD timeout, count;
    HANDLE *handle = infoPtr->handle;
    ConsoleThreadInfo *threadInfo = &infoPtr->reader;
    INPUT_RECORD input;

    while (1) {
	/*
	 * Synchronize with the reader thread.
	 */

	timeout = blocking ? INFINITE : 0;
	if (WaitForSingleObject(threadInfo->readyEvent,
		timeout) == WAIT_TIMEOUT) {
	    /*
	     * The reader thread is blocked waiting for data and the channel
	     * is in non-blocking mode.
	     */

	    errno = EAGAIN;
	    return -1;
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
	    return 1;
	}

	/*
	 * There wasn't any data available, so reset the thread and try again.
	 */

	ResetEvent(infoPtr->readable);
	SetEvent(infoPtr->startReader);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleReaderThread --







|
|







1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
	    return 1;
	}

	/*
	 * There wasn't any data available, so reset the thread and try again.
	 */

	ResetEvent(threadInfo->readyEvent);
	SetEvent(threadInfo->startEvent);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleReaderThread --
1134
1135
1136
1137
1138
1139
1140
1141
1142

1143
1144
1145

1146


1147
1148
1149
1150
1151
1152
1153
1154
1155
 *----------------------------------------------------------------------
 */

static DWORD WINAPI
ConsoleReaderThread(
    LPVOID arg)
{
    ConsoleInfo *infoPtr = (ConsoleInfo *)arg;
    HANDLE *handle = infoPtr->handle;

    DWORD waitResult;
    HANDLE wEvents[2];


    /* The first event takes precedence. */


    wEvents[0] = infoPtr->stopReader;
    wEvents[1] = infoPtr->startReader;

    for (;;) {
	/*
	 * Wait for the main thread to signal before attempting to wait.
	 */

	waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);







|

>



>
|
>
>
|
|







1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
 *----------------------------------------------------------------------
 */

static DWORD WINAPI
ConsoleReaderThread(
    LPVOID arg)
{
    ConsoleInfo *infoPtr = arg;
    HANDLE *handle = infoPtr->handle;
    ConsoleThreadInfo *threadInfo = &infoPtr->reader;
    DWORD waitResult;
    HANDLE wEvents[2];

    /*
     * The first event takes precedence.
     */

    wEvents[0] = threadInfo->stopEvent;
    wEvents[1] = threadInfo->startEvent;

    for (;;) {
	/*
	 * Wait for the main thread to signal before attempting to wait.
	 */

	waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205

1206
1207
1208
1209
1210
1211
1212
	}

	/*
	 * Look for data on the console, but first ignore any events that are
	 * not KEY_EVENTs.
	 */

	if (readConsoleBytes(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
		(LPDWORD) &infoPtr->bytesRead) != FALSE) {
	    /*
	     * Data was stored in the buffer.
	     */

	    infoPtr->readFlags |= CONSOLE_BUFFERED;
	} else {
	    DWORD err;
	    err = GetLastError();

	    if (err == (DWORD)EOF) {
		infoPtr->readFlags = CONSOLE_EOF;
	    }
	}

	/*
	 * Signal the main thread by signalling the readable event and then
	 * waking up the notifier thread.
	 */

	SetEvent(infoPtr->readable);

	/*
	 * Alert the foreground thread. Note that we need to treat this like a
	 * critical section so the foreground thread does not terminate this
	 * thread while we are holding a mutex in the notifier code.
	 */

	Tcl_MutexLock(&consoleMutex);
	if (infoPtr->threadId != NULL) {
	    /*
	     * TIP #218. When in flight ignore the event, no one will receive
	     * it anyway.
	     */

	    Tcl_ThreadAlert(infoPtr->threadId);
	}
	Tcl_MutexUnlock(&consoleMutex);
    }

    return 0;
}







|







<
|

|









|













>







1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205

1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
	}

	/*
	 * Look for data on the console, but first ignore any events that are
	 * not KEY_EVENTs.
	 */

	if (ReadConsoleBytes(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
		(LPDWORD) &infoPtr->bytesRead) != FALSE) {
	    /*
	     * Data was stored in the buffer.
	     */

	    infoPtr->readFlags |= CONSOLE_BUFFERED;
	} else {

	    DWORD err = GetLastError();

	    if (err == (DWORD) EOF) {
		infoPtr->readFlags = CONSOLE_EOF;
	    }
	}

	/*
	 * Signal the main thread by signalling the readable event and then
	 * waking up the notifier thread.
	 */

	SetEvent(threadInfo->readyEvent);

	/*
	 * Alert the foreground thread. Note that we need to treat this like a
	 * critical section so the foreground thread does not terminate this
	 * thread while we are holding a mutex in the notifier code.
	 */

	Tcl_MutexLock(&consoleMutex);
	if (infoPtr->threadId != NULL) {
	    /*
	     * TIP #218. When in flight ignore the event, no one will receive
	     * it anyway.
	     */

	    Tcl_ThreadAlert(infoPtr->threadId);
	}
	Tcl_MutexUnlock(&consoleMutex);
    }

    return 0;
}
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239

1240
1241
1242
1243

1244


1245
1246
1247
1248
1249
1250
1251
1252
1253
 *----------------------------------------------------------------------
 */

static DWORD WINAPI
ConsoleWriterThread(
    LPVOID arg)
{

    ConsoleInfo *infoPtr = (ConsoleInfo *)arg;
    HANDLE *handle = infoPtr->handle;

    DWORD count, toWrite, waitResult;
    char *buf;
    HANDLE wEvents[2];


    /* The first event takes precedence. */


    wEvents[0] = infoPtr->stopWriter;
    wEvents[1] = infoPtr->startWriter;

    for (;;) {
	/*
	 * Wait for the main thread to signal before attempting to write.
	 */

	waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);







<
|

>




>
|
>
>
|
|







1257
1258
1259
1260
1261
1262
1263

1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
 *----------------------------------------------------------------------
 */

static DWORD WINAPI
ConsoleWriterThread(
    LPVOID arg)
{

    ConsoleInfo *infoPtr = arg;
    HANDLE *handle = infoPtr->handle;
    ConsoleThreadInfo *threadInfo = &infoPtr->writer;
    DWORD count, toWrite, waitResult;
    char *buf;
    HANDLE wEvents[2];

    /*
     * The first event takes precedence.
     */

    wEvents[0] = threadInfo->stopEvent;
    wEvents[1] = threadInfo->startEvent;

    for (;;) {
	/*
	 * Wait for the main thread to signal before attempting to write.
	 */

	waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
	toWrite = infoPtr->toWrite;

	/*
	 * Loop until all of the bytes are written or an error occurs.
	 */

	while (toWrite > 0) {
	    if (writeConsoleBytes(handle, buf, (DWORD)toWrite,
				  &count) == FALSE) {
		infoPtr->writeError = GetLastError();
		break;
	    } else {

		toWrite -= count;
		buf += count;
	    }
	}

	/*
	 * Signal the main thread by signalling the writable event and then
	 * waking up the notifier thread.
	 */

	SetEvent(infoPtr->writable);

	/*
	 * Alert the foreground thread. Note that we need to treat this like a
	 * critical section so the foreground thread does not terminate this
	 * thread while we are holding a mutex in the notifier code.
	 */








|
|


<
>
|
|
<







|







1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305

1306
1307
1308

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
	toWrite = infoPtr->toWrite;

	/*
	 * Loop until all of the bytes are written or an error occurs.
	 */

	while (toWrite > 0) {
	    if (WriteConsoleBytes(handle, buf, (DWORD) toWrite,
		    &count) == FALSE) {
		infoPtr->writeError = GetLastError();
		break;

	    }
	    toWrite -= count;
	    buf += count;

	}

	/*
	 * Signal the main thread by signalling the writable event and then
	 * waking up the notifier thread.
	 */

	SetEvent(threadInfo->readyEvent);

	/*
	 * Alert the foreground thread. Note that we need to treat this like a
	 * critical section so the foreground thread does not terminate this
	 * thread while we are holding a mutex in the notifier code.
	 */

1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
 *	This is a helper function to break up the construction of channels
 *	into File, Console, or Serial.
 *
 * Results:
 *	Returns the new channel, or NULL.
 *
 * Side effects:
 *	May open the channel
 *
 *----------------------------------------------------------------------
 */

Tcl_Channel
TclWinOpenConsoleChannel(
    HANDLE handle,
    char *channelName,
    int permissions)
{
    char encoding[4 + TCL_INTEGER_SPACE];
    ConsoleInfo *infoPtr;
    DWORD id, modes;

    ConsoleInit();

    /*
     * See if a channel with this handle already exists.
     */








|












|







1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
 *	This is a helper function to break up the construction of channels
 *	into File, Console, or Serial.
 *
 * Results:
 *	Returns the new channel, or NULL.
 *
 * Side effects:
 *	May open the channel.
 *
 *----------------------------------------------------------------------
 */

Tcl_Channel
TclWinOpenConsoleChannel(
    HANDLE handle,
    char *channelName,
    int permissions)
{
    char encoding[4 + TCL_INTEGER_SPACE];
    ConsoleInfo *infoPtr;
    DWORD modes;

    ConsoleInit();

    /*
     * See if a channel with this handle already exists.
     */

1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
	 * we only want to catch when complete lines are ready for reading.
	 */

	GetConsoleMode(infoPtr->handle, &modes);
	modes &= ~(ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
	modes |= ENABLE_LINE_INPUT;
	SetConsoleMode(infoPtr->handle, modes);

	infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL);
	infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL);
	infoPtr->stopReader = CreateEvent(NULL, FALSE, FALSE, NULL);
	infoPtr->readThread = CreateThread(NULL, 256, ConsoleReaderThread,
		infoPtr, 0, &id);
	SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
    }

    if (permissions & TCL_WRITABLE) {
	infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL);
	infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL);
	infoPtr->stopWriter = CreateEvent(NULL, FALSE, FALSE, NULL);
	infoPtr->writeThread = CreateThread(NULL, 256, ConsoleWriterThread,
		infoPtr, 0, &id);
	SetThreadPriority(infoPtr->writeThread, THREAD_PRIORITY_HIGHEST);
    }

    /*
     * Files have default translation of AUTO and ^Z eof char, which means
     * that a ^Z will be accepted as EOF when reading.
     */








|
<
<
<
<
<
<



<
<
<
|
<
<







1399
1400
1401
1402
1403
1404
1405
1406






1407
1408
1409



1410


1411
1412
1413
1414
1415
1416
1417
	 * we only want to catch when complete lines are ready for reading.
	 */

	GetConsoleMode(infoPtr->handle, &modes);
	modes &= ~(ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
	modes |= ENABLE_LINE_INPUT;
	SetConsoleMode(infoPtr->handle, modes);
	StartChannelThread(infoPtr, &infoPtr->reader, ConsoleReaderThread);






    }

    if (permissions & TCL_WRITABLE) {



	StartChannelThread(infoPtr, &infoPtr->writer, ConsoleWriterThread);


    }

    /*
     * Files have default translation of AUTO and ^Z eof char, which means
     * that a ^Z will be accepted as EOF when reading.
     */

1424
1425
1426
1427
1428
1429
1430
1431
1432

1433
1434
1435
1436
1437
1438
1439
1440
 */

static void
ConsoleThreadActionProc(
    ClientData instanceData,
    int action)
{
    ConsoleInfo *infoPtr = (ConsoleInfo *) instanceData;


    /* We do not access firstConsolePtr in the thread structures. This is not
     * for all serials managed by the thread, but only those we are watching.
     * Removal of the filevent handlers before transfer thus takes care of
     * this structure.
     */

    Tcl_MutexLock(&consoleMutex);
    if (action == TCL_CHANNEL_THREAD_INSERT) {







|

>
|







1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
 */

static void
ConsoleThreadActionProc(
    ClientData instanceData,
    int action)
{
    ConsoleInfo *infoPtr = instanceData;

    /*
     * We do not access firstConsolePtr in the thread structures. This is not
     * for all serials managed by the thread, but only those we are watching.
     * Removal of the filevent handlers before transfer thus takes care of
     * this structure.
     */

    Tcl_MutexLock(&consoleMutex);
    if (action == TCL_CHANNEL_THREAD_INSERT) {