File: sched.c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (1271 lines) | stat: -rw-r--r-- 28,021 bytes parent folder | download | duplicates (2)
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
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
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
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
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
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
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
944
945
946
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
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
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
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
/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	<assert.h>
#include	"nickle.h"
#include	"ref.h"

Value   running;
Value   stopped;
Bool	signalException;

extern void dumpSleep (void), dumpThreads (void);

static void
_ThreadInsert (Value thread)
{
    Value	*prev, t;

    switch (thread->thread.state) {
    case ThreadRunning:
	prev = &running;
	break;
    case ThreadSuspended:
	prev = &stopped;
	break;
    case ThreadFinished:
    default:
	return;
    }
    for (; (t = *prev); prev = &t->thread.next)
	if (t->thread.priority < thread->thread.priority)
	    break;
    thread->thread.next = t;
    *prev = thread;
}

static void
_ThreadRemove (Value thread)
{
    Value	*prev;

    switch (thread->thread.state) {
    case ThreadRunning:
	prev = &running;
	break;
    case ThreadSuspended:
	prev = &stopped;
	break;
    case ThreadFinished:
    default:
	return;
    }
    for (; *prev != thread; prev = &(*prev)->thread.next);
    *prev = thread->thread.next;
}

void
ThreadSetState (Value thread, ThreadState state)
{
    if (state != thread->thread.state)
    {
	_ThreadRemove (thread);
	thread->thread.state = state;
	_ThreadInsert (thread);
    }
}

void
ThreadSleep (Value thread, Value sleep, int priority)
{
    thread->thread.priority = priority;
    thread->thread.sleep = sleep;
    SetSignalSuspend ();
}

void
ThreadStepped (Value thread)
{
    Value   t;

    if ((t = thread->thread.next) &&
        thread->thread.priority <= t->thread.priority)
    {
        _ThreadRemove (thread);
        _ThreadInsert (thread);
    }
}

void
ThreadsWakeup (Value sleep, WakeKind kind)
{
    Value	thread, next;

    for (thread = stopped; thread; thread = next)
    {
	next = thread->thread.next;
	if ((thread->thread.state == ThreadSuspended) &&
	    thread->thread.sleep == sleep)
	{
	    thread->thread.sleep = 0;
	    ThreadSetState (thread, ThreadRunning);
	    if (kind == WakeOne)
		break;
	}
    }
}

Bool	lastThreadError;

void
ThreadFinish (Value thread, Bool error)
{
    if (thread->thread.state != ThreadFinished)
    {
	ThreadSetState (thread, ThreadFinished);
	ThreadsWakeup (thread, WakeAll);
	lastThreadError = error;
    }
}

Value
do_Thread_join (Value target)
{
    ENTER ();
    if (!ValueIsThread(target))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("join needs thread argument"),
				target, Void);
	RETURN (Void);
    }
    if (target->thread.state != ThreadFinished)
    {
	ThreadSleep (running, target, PrioritySync);
	RETURN (Void);
    }
    RETURN (target->thread.continuation.value);
}

static void
ThreadListState (Value thread)
{
    switch (thread->thread.state) {
    case ThreadRunning:
	FilePuts (FileStdout, " running");
	break;
    case ThreadSuspended:
	FilePuts (FileStdout, " suspended");
	break;
    case ThreadFinished:
        FilePuts (FileStdout, " finished");
	break;
    }
}

Value
do_Thread_list (void)
{
    ENTER ();
    Value   t;

    for (t = running; t; t = t->thread.next)
    {
	FilePrintf (FileStdout, "\t%%%d", t->thread.id);
	ThreadListState (t);
	FileOutput (FileStdout, '\n');
    }
    for (t = stopped; t; t = t->thread.next)
    {
	FilePrintf (FileStdout, "\t%%%d", t->thread.id);
	ThreadListState (t);
	if (t->thread.sleep)
	    FilePrintf (FileStdout, " %g", t->thread.sleep);
	FileOutput (FileStdout, '\n');
    }
    RETURN(Void);
}

Value
do_Thread_id_to_thread (Value id)
{
    ENTER ();
    int	i;
    Value   t;

    i = IntPart (id, "Invalid thread id");
    if (aborting)
	RETURN (Void);
    for (t = running; t; t = t->thread.next)
	if (t->thread.id == i)
	    RETURN (t);
    for (t = stopped; t; t = t->thread.next)
	if (t->thread.id == i)
	    RETURN (t);
    RETURN (Void);
}

Value
do_Thread_current (void)
{
    ENTER ();
    Value   ret;
    if (running)
	ret = running;
    else
	ret = Void;
    RETURN (ret);
}

Value
do_Thread_set_priority (Value thread, Value priority)
{
    ENTER ();
    int	    i;
    if (!ValueIsThread(thread))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("set_priority: not a thread"),
				thread, priority);
	RETURN (Void);
    }
    i = IntPart (priority, "Invalid thread priority");
    if (aborting)
	RETURN (Void);
    if (i != thread->thread.priority)
    {
	_ThreadRemove (thread);
	thread->thread.priority = i;
	_ThreadInsert (thread);
    }
    RETURN (NewInt (thread->thread.priority));
}

Value
do_Thread_get_priority (Value thread)
{
    ENTER ();
    if (!ValueIsThread(thread))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("get_priority: not a thread"),
				thread, Void);
	RETURN (Void);
    }
    RETURN (NewInt (thread->thread.priority));
}

static int
KillThread (Value thread)
{
    int	ret;

    if (!ValueIsThread(thread))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("kill: not a thread"),
				thread, Void);
	return 0;
    }
    if (thread->thread.state == ThreadFinished)
	ret = 0;
    else
	ret = 1;
    ThreadFinish (thread, False);
    return ret;
}

Value
do_Thread_kill (int n, Value *p)
{
    ENTER ();
    Value   thread;
    int	    ret = 0;

    if (n == 0)
    {
	thread = lookupVar (0, "thread");
	if (!ValueIsThread(thread))
	    RaiseStandardException (exception_invalid_argument, 3,
				    NewStrString ("kill: no default thread"),
				    thread, Void);
	else
	    ret = KillThread (thread);
    }
    else
    {
	while (n--)
	    ret += KillThread (*p++);
    }
    RETURN (NewInt (ret));
}

void
TraceFunction (Value file, FramePtr frame, CodePtr code, ExprPtr name)
{
    int		    fe;

    FilePuts (file, "    ");
    if (name)
	PrettyExpr (file, name, -1, 0, False);
    else
	FilePuts (file, "<anonymous>");
    FilePuts (file, " (");
    for (fe = 0; fe < code->base.argc; fe++)
    {
	if (fe)
	    FilePuts (file, ", ");
	FilePrintf (file, "%G", BoxValue (frame->frame, fe));
    }
    FilePuts (file, ")\n");
}

static void
TraceStatement (Value file, ExprPtr stat)
{
    FilePrintf (file, "%A:%d: ", stat->base.file, stat->base.line);
    PrettyStat (file, stat, False);
}

void
TraceFrame (Value file, FramePtr frame, ObjPtr obj, InstPtr pc, int depth)
{
    ENTER ();
    int		max;
    CodePtr	code;

    if (obj && pc)
	TraceStatement (file, ObjStatement (obj, pc));
    for (max = depth; frame && max--; frame = frame->previous)
    {
	code = frame->function->func.code;
	TraceFunction (file, frame, code, code->base.name);
	TraceStatement (file, ObjStatement (frame->saveObj, frame->savePc));
    }
    EXIT ();
}

#ifdef DEBUG_JUMP
static void
TraceIndent (int indent)
{
    while (indent--)
	FilePuts (FileStdout, "    ");
}
#endif

Value
do_Thread_trace (int n, Value *p)
{
    ENTER ();
    Value	v;
    FramePtr	frame;
    InstPtr	pc;
    ObjPtr	obj;
    int		depth = 20;

    if (n == 0)
	v = lookupVar (0, "cont");
    else
	v = p[0];
    if (n > 1)
    {
	depth = IntPart (p[1], "Invalid trace depth");
	if (aborting)
	    RETURN (Void);
    }
    switch (ValueTag(v)) {
    case rep_thread:
    case rep_continuation:
	frame = v->continuation.frame;
	pc = v->continuation.pc;
	obj = v->continuation.obj;
	break;
    default:
	if (n == 0)
	    RaiseStandardException (exception_invalid_argument, 3,
				    NewStrString ("trace: no default continuation"),
				    NewInt (0), Void);
	else
	    RaiseStandardException (exception_invalid_argument, 3,
				    NewStrString ("Thread::trace: neither continuation nor thread"),
				    NewInt (0), v);
	RETURN (Void);
    }
    TraceFrame (FileStdout, frame, obj, pc, depth);
    RETURN(Void);
}

static void
ThreadMark (void *object)
{
    ThreadPtr	thread = object;

    ContinuationMark (&thread->continuation);
    MemReference (thread->jump);
    MemReference (thread->sleep);
    MemReference (thread->next);
}

static Bool
ThreadPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
    (void) format;
    (void) base;
    (void) width;
    (void) prec;
    (void) fill;
    FilePrintf (f, "%%%d", av->thread.id);
    return True;
}

ValueRep    ThreadRep = {
    { ThreadMark, 0, "ThreadRep" },	/* base */
    rep_thread,	/* tag */
    {			/* binary */
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	ValueEqual,
	0,
	0,
    },
    {			    /* unary */
	0,
	0,
	0,
    },
    0,
    0,
    ThreadPrint,
    0,
    0,
};

static int  ThreadId;

Value
NewThread (FramePtr frame, ObjPtr code)
{
    ENTER ();
    Value ret;

    ret = ALLOCATE (&ThreadRep.data, sizeof (Thread));

    ret->thread.jump = 0;
    ret->thread.state = ThreadRunning;
    ret->thread.priority = 0;
    ret->thread.sleep = 0;
    ret->thread.id = ++ThreadId;
    ret->thread.partial = 0;
    ret->thread.next = 0;

    ContinuationInit (&ret->thread.continuation);
    ret->thread.continuation.obj = code;
    ret->thread.continuation.pc = ObjCode (code, 0);
    ret->thread.continuation.frame = frame;

    complete = True;
    if (code->error)
	ret->thread.state = ThreadFinished;
    _ThreadInsert (ret);
    RETURN (ret);
}

typedef struct _blockHandler {
    DataType		    *data;
    struct _blockHandler    *next;
    NickleBlockHandler	    handler;
    void		    *closure;
} BlockHandler;

static void
BlockHandlerMark (void *object)
{
    BlockHandler    *bh = object;

    MemReference (bh->next);
}

DataType BlockHandlerType = { BlockHandlerMark, 0, "BlockHandlerType" };

static BlockHandler	*blockHandlers;

void
ThreadsRegisterBlockHandler (NickleBlockHandler handler, void *closure)
{
    ENTER ();
    BlockHandler    *bh = ALLOCATE (&BlockHandlerType, sizeof (BlockHandler));
    bh->next = blockHandlers;
    blockHandlers = bh;
    bh->handler = handler;
    bh->closure = closure;
    EXIT ();
}

void
ThreadsUnregisterBlockHandler (NickleBlockHandler handler, void *closure)
{
    ENTER ();
    BlockHandler    **prev, *bh;

    for (prev = &blockHandlers; (bh = *prev); prev = &bh->next)
    {
	if (bh->handler == handler && bh->closure == closure)
	{
	    bh->handler = 0;
	    *prev = bh->next;
	}
    }
    EXIT ();
}

void
ThreadsBlock (void)
{
    BlockHandler    *bh, *next;

    for (bh = blockHandlers; bh; bh = next)
    {
	next = bh->next;
	if (bh->handler)
	    (*bh->handler) (bh->closure);
    }

    /* Pend in either select or sigsuspend, depending
     * on whether there are files blocked
     */
    if (!running)
	FileCheckBlocked(True);
}

ReferencePtr	RunningReference, StoppedReference;
ReferencePtr	BlockHandlerReference;

void
ThreadInit (void)
{
    ENTER ();
    RunningReference = NewReference ((void **) &running);
    MemAddRoot (RunningReference);
    StoppedReference = NewReference ((void **) &stopped);
    MemAddRoot (StoppedReference);
    BlockHandlerReference = NewReference ((void **) &blockHandlers);
    MemAddRoot (BlockHandlerReference);
    EXIT ();
}

DataType FarJumpType = { 0, 0, "FarJumpType" };

FarJumpPtr
NewFarJump (int inst, int twixt, int catch, int frame)
{
    ENTER ();
    FarJumpPtr	farJump;

    farJump = ALLOCATE (&FarJumpType, sizeof (FarJump));
    farJump->inst = inst;
    farJump->twixt = twixt;
    farJump->catch = catch;
    farJump->frame = frame;
    RETURN (farJump);
}

Value
FarJumpContinuation (ContinuationPtr continuation, FarJumpPtr farJump)
{
    ENTER ();
    Value	ret;
    CatchPtr	catch;
    TwixtPtr	twixt;
    FramePtr	frame;
    InstPtr	pc;
    ObjPtr	obj;
    int		twixts;
    int		catches;
    int		frames;

    ret = NewContinuation (continuation, 0);

    /*
     * Unwind twixts
     */
    twixts = farJump->twixt;
    twixt = ret->continuation.twixts;
    while (twixts--)
	twixt = twixt->continuation.twixts;
    ret->continuation.twixts = twixt;

    /*
     * Unwind catches
     */
#ifdef DEBUG_JUMP
    FilePrintf (FileStdout, "FarJump catches before: ");
    ThreadCatches (running);
#endif
    catches = farJump->catch;
    catch = ret->continuation.catches;
    while (catches--)
	catch = catch->continuation.catches;
    ret->continuation.catches = catch;
#ifdef DEBUG_JUMP
    FilePrintf (FileStdout, "FarJump catches after: ");
    ThreadCatches (running);
#endif

    /*
     * Unwind frames
     */
    frames = farJump->frame;
    frame = ret->continuation.frame;
    obj = continuation->obj;
    pc = continuation->pc;
    if (farJump->inst < 0)
	frames++;
    while (frames--)
    {
	pc = frame->savePc;
	obj = frame->saveObj;
	frame = frame->previous;
    }
    ret->continuation.frame = frame;
    /*
     * Set pc for non-return jumps
     */
    if (farJump->inst >= 0)
	pc = ObjCode (obj, farJump->inst);

    /*
     * Assertion here -- the stack is OK because
     * only intervening catch frames are on the stack
     * and they never have extra values on the stack
     */

    ret->continuation.pc = pc;
    ret->continuation.obj = obj;
    RETURN (ret);
}

void
ContinuationMark (void *object)
{
    ContinuationPtr	continuation = object;

    assert (!continuation->pc ||
	    (ObjCode (continuation->obj, 0) <= continuation->pc &&
	     continuation->pc <= ObjCode (continuation->obj, ObjLast(continuation->obj))));
    MemReference (continuation->obj);
    MemReference (continuation->frame);
    MemReference (continuation->stack);
    MemReference (continuation->value);
    MemReference (continuation->catches);
    MemReference (continuation->twixts);
}

static Bool
ContinuationPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
    (void) av;
    (void) format;
    (void) base;
    (void) width;
    (void) prec;
    (void) fill;
    FilePuts (f, "continutation");
    return True;
}

ValueRep    ContinuationRep = {
    { ContinuationMark, 0, "ContinuationRep" },	/* base */
    rep_continuation,		/* tag */
    {				/* binary */
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	ValueEqual,
	0,
	0,
    },
    {				/* unary */
	0,
	0,
	0,
    },
    0,
    0,
    ContinuationPrint,
    0,
    0,
};

Value
NewContinuation (ContinuationPtr continuation, InstPtr pc)
{
    ENTER ();
    Value   ret;

    ret = ALLOCATE (&ContinuationRep.data, sizeof (Continuation));
    ContinuationSet (&ret->continuation, continuation);
    ret->continuation.pc = pc;
    RETURN (ret);
}

static ContinuationPtr
EmptyContinuation (void)
{
    ENTER ();
    Value   ret;

    ret = ALLOCATE (&ContinuationRep.data, sizeof (Continuation));
    ContinuationInit (&ret->continuation);
    RETURN (&ret->continuation);
}

#ifdef DEBUG_JUMP
int dump_jump = 0;

void
ContinuationTrace (char *where, Continuation *continuation, int indent)
{
    int	    s;
    StackObject	*stack = continuation->stack;
    CatchPtr	catches = continuation->catches;
    TwixtPtr	twixts = continuation->twixts;
    ObjPtr	obj = continuation->obj;
    InstPtr	pc = continuation->pc;

    if (!dump_jump)
	return;
    TraceIndent (indent);
    FilePuts (FileStdout, "*** ");
    FilePuts (FileStdout, where);
    FilePuts (FileStdout, " ***\n");
    TraceIndent (indent);
    FilePuts (FileStdout, "stack:     ");
    for (s = 0; STACK_TOP(stack) + (s) < STACK_MAX(stack); s++)
    {
	if (s)
	    FilePuts (FileStdout, ", ");
	FilePrintf (FileStdout, "%g", STACK_ELT(stack, s));
    }
    FilePuts (FileStdout, "\n");
    TraceIndent (indent);
    FilePuts (FileStdout, "frame:\nCALLS\n");
    TraceFrame (FileStdout, continuation->frame, obj, pc, 20);
    FilePuts (FileStdout, "END CALLS\n");
    TraceIndent (indent);
    FilePuts (FileStdout, "catches:   ");
    for (s = 0; catches; catches = catches->continuation.catches, s++)
    {
	if (s)
	    FilePuts (FileStdout, ", ");
	FilePrintf (FileStdout, "%A", catches->exception->symbol.name);
    }
    FilePuts (FileStdout, "\n");
    TraceIndent (indent);
    FilePuts (FileStdout, "statement: ");
    if (obj && pc)
	PrettyStat (FileStdout, ObjStatement (obj, pc), False);
    else
	FilePuts (FileStdout, "corrupted continuation!\n");
    for (s = 0; twixts; twixts = twixts->continuation.twixts, s++)
    {
	ContinuationTrace ("twixt", &twixts->continuation, indent+1);
    }
}
#endif

InstPtr
ContinuationSet (ContinuationPtr dst, ContinuationPtr src)
{
    ENTER ();
    dst->value = src->value;
    dst->pc = 0;
    dst->obj = src->obj;
    dst->frame = src->frame;
    dst->stack = 0;
    dst->catches = src->catches;
    dst->twixts = src->twixts;
    /* last, to make sure remaining entries are initialized before any GC */
    dst->stack = StackCopy (src->stack);
    RETURN (src->pc);
}

void
ContinuationInit (ContinuationPtr dst)
{
    dst->pc = 0;
    dst->obj = 0;
    dst->frame = 0;
    dst->value = Void;
    dst->catches = 0;
    dst->twixts = 0;
    dst->stack = 0;
    dst->stack = StackCreate ();
}

static void
MarkJump (void *object)
{
    JumpPtr jump = object;

    MemReference (jump->enter);
    MemReference (jump->entering);
    MemReference (jump->leave);
    MemReference (jump->parent);
    MemReference (jump->continuation);
    MemReference (jump->ret);
}

DataType    JumpType = { MarkJump, 0, "JumpType" };

JumpPtr
NewJump (TwixtPtr leave, TwixtPtr enter, TwixtPtr parent,
	 ContinuationPtr continuation, Value ret)
{
    ENTER ();
    JumpPtr jump;

    jump = ALLOCATE (&JumpType, sizeof (Jump));
    jump->leave = leave;
    jump->enter = enter;
    jump->entering = TwixtNext (parent, enter);
    jump->parent = parent;
    jump->continuation = continuation;
    jump->ret = ret;
    RETURN (jump);
}

/*
 * An unhandled exception will attempt to jump to NULL,
 * catch that and invoke the debugger.  When the exception
 * was raised, the code carefully pushed a continuation from
 * the point of the exception to pass to the debugger
 */

static void
JumpUnhandledException (Value thread)
{
    Value   continuation = STACK_POP (thread->thread.continuation.stack);

    /* make exec loop reschedule */
    if (thread == running)
	SetSignalError ();
    DebugStart (continuation);
    ThreadFinish (thread, True);
}

/*
 * Figure out where to go next in a longjmp through twixts
 */
Value
JumpContinue (Value thread, InstPtr *next)
{
    ENTER ();
    JumpPtr	jump = thread->thread.jump;
    TwixtPtr	twixt;

    if (jump->leave)
    {
	/*
	 * Going up
	 */
	twixt = jump->leave;
	ContinuationSet (&thread->thread.continuation, &twixt->continuation);
	*next = twixt->leave;
	jump->leave = twixt->continuation.twixts;
	/*
	 * Matching element of the twixt chain, next time start
	 * back down the other side
	 */
	if (jump->leave == jump->parent)
	    jump->leave = 0;
    }
    else if (jump->entering)
    {
	/*
	 * Going down
	 */
	twixt = jump->entering;
	*next = ContinuationSet (&thread->thread.continuation, &twixt->continuation);
	jump->entering = TwixtNext (jump->entering, jump->enter);
    }
    else
    {
	/*
	 * All done, set to final continuation and drop the jump object
	 */
	*next = ContinuationSet (&thread->thread.continuation, jump->continuation);
	thread->thread.jump = 0;
    }
    if (!*next)
	JumpUnhandledException (thread);
    RETURN (jump->ret);
}

/*
 * Build a Jump that threads through all of the necessary twixt blocks
 * ending up at 'continuation' returning 'ret'
 */
InstPtr
JumpStart (Value thread, ContinuationPtr continuation, Value ret)
{
    ENTER ();
    int	diff;
    TwixtPtr	leave = thread->thread.continuation.twixts;
    TwixtPtr	enter = continuation->twixts;
    TwixtPtr	leave_parent, enter_parent, parent;
    InstPtr	next;

    /*
     * Make both lists the same length.  Note that either can be empty
     */
    leave_parent = leave;
    enter_parent = enter;
    diff = TwixtDepth (leave_parent) - TwixtDepth (enter_parent);
    if (diff >= 0)
	while (diff-- > 0)
	    leave_parent = leave_parent->continuation.twixts;
    else
	while (diff++ < 0)
	    enter_parent = enter_parent->continuation.twixts;
    /*
     * Now find the common parent
     */
    while (leave_parent != enter_parent)
    {
	leave_parent = leave_parent->continuation.twixts;
	enter_parent = enter_parent->continuation.twixts;
    }

    parent = enter_parent;
    /*
     * Build a data structure to get from leave to enter via parent
     */
    thread->thread.jump = NewJump (leave, enter, parent, continuation, ret);
    /*
     * Don't need the jump return value yet; we're off to the twixt
     * blocks; after that, the return value will get retrieved by the
     * final OpLeaveDone or OpEnterDone instruction
     */
    (void) JumpContinue (thread, &next);
    RETURN (next);
}

Value
ContinuationJump (Value thread, ContinuationPtr continuation, Value ret, InstPtr *next)
{
#ifdef DEBUG_JUMP
    ContinuationTrace ("ContinuationJump from", &thread->thread.continuation, 1);
    ContinuationTrace ("ContinuationJump to", continuation, 1);
#endif
    ENTER ();
    /*
     * If there are twixt enter or leave blocks to execute, build a Jump
     * that walks them and then resets the continuation.
     *
     * Otherwise, just jump
     */
    if (thread->thread.continuation.twixts != continuation->twixts)
	*next = JumpStart (thread, continuation, ret);
    else
	*next = ContinuationSet (&thread->thread.continuation, continuation);
    if (!*next)
	JumpUnhandledException (thread);
    RETURN (ret);
}

/*
 * It is necessary that SetJump and LongJump have the same number
 * of arguments -- the arguments pushed by SetJump will have to be
 * popped when LongJump executes.  If this is not so, the stack copy
 * created here should be adjusted to account for this difference
 */
Value
do_setjmp (Value continuation_ref, Value ret)
{
    ENTER ();
    Value	continuation;

    if (!ValueIsRef(continuation_ref))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("setjump: not a reference"),
				NewInt (0), continuation_ref);
	RETURN (Void);
    }
    continuation = NewContinuation (&running->thread.continuation,
				    running->thread.continuation.pc + 1);
    /*
     * Adjust stack for set jump return
     */
    STACK_DROP (continuation->continuation.stack, 2);
    RefValueSet (continuation_ref, continuation);
#ifdef DEBUG_JUMP
    ContinuationTrace ("do_setjmp", &continuation->continuation, 1);
#endif
    RETURN (ret);
}

Value
do_longjmp (InstPtr *next, Value continuation, Value ret)
{
    ENTER ();

    if (!running)
	RETURN (Void);
    if (!ValueIsContinuation(continuation))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("longjmp: non-continuation argument"),
				NewInt (0), continuation);
	RETURN (Void);
    }
    RETURN (ContinuationJump (running, &continuation->continuation, ret, next));
}

static void
CatchMark (void *object)
{
    CatchPtr	catch = object;

    ContinuationMark (&catch->continuation);
    MemReference (catch->exception);
}

DataType    CatchType = { CatchMark, 0, "CatchType" };

CatchPtr
NewCatch (Value thread, SymbolPtr exception)
{
    ENTER();
    CatchPtr	catch;

    catch = ALLOCATE (&CatchType, sizeof (Catch));
    catch->exception = exception;
    ContinuationSet (&catch->continuation, &thread->thread.continuation);
    catch->continuation.pc = thread->thread.continuation.pc + 1;
    RETURN (catch);
}

static void
TwixtMark (void *object)
{
    TwixtPtr	twixt = object;

    ContinuationMark (&twixt->continuation);
}

DataType    TwixtType = { TwixtMark, 0, "TwixtType" };

TwixtPtr
NewTwixt (ContinuationPtr	continuation,
	  InstPtr	enter,
	  InstPtr	leave)
{
    ENTER ();
    TwixtPtr	twixt;

    twixt = ALLOCATE (&TwixtType, sizeof (Twixt));
    twixt->leave = leave;
    if (continuation->twixts)
	twixt->depth = continuation->twixts->depth + 1;
    else
	twixt->depth = 1;
    ContinuationSet (&twixt->continuation, continuation);
    twixt->continuation.pc = enter;
    RETURN (twixt);
}

/*
 * Twixts are chained deepest first.  Walking
 * down the list is a bit of work
 */

TwixtPtr
TwixtNext (TwixtPtr twixt, TwixtPtr last)
{
    if (last == twixt)
	return 0;
    while (last->continuation.twixts != twixt)
	last = last->continuation.twixts;
    return last;
}

void
RaiseException (Value thread, SymbolPtr except, Value args, InstPtr *next)
{
    ENTER ();
    CatchPtr	    catch;
    ContinuationPtr continuation = 0;

    for (catch = thread->thread.continuation.catches;
	 catch;
	 catch = catch->continuation.catches)
    {
	if (catch->exception == except)
	{
	    continuation = &catch->continuation;
	    /*
	     * Hold a reference to this nested value because
	     * ContinuationJump is about to smash the thread
	     */
	    REFERENCE (catch);
	    break;
	}
    }
    /* unhandled exception -- build an empty continuation and jump to it */
    if (!continuation)
    {
	int	i;
	InstPtr	pc = thread->thread.continuation.pc;

	PrintError ("Unhandled exception %A (", except->symbol.name);
	if (args)
	{
	    int	    dim = ArrayLimits(&args->array)[0];
	    for (i = 0; i < dim; i++)
	    {
		PrintError ("%g", ArrayValue (&args->array, i));
		if (i < dim - 1)
		    PrintError (", ");
	    }
	}
	PrintError (")\n");
	TraceFrame (FileStderr, thread->thread.continuation.frame,
		    thread->thread.continuation.obj,
		    pc,
		    20);
	continuation = EmptyContinuation();
	STACK_PUSH (continuation->stack,
		    NewContinuation (&thread->thread.continuation, pc));
    }
    ContinuationJump (thread, continuation, args, next);
    EXIT ();
}

SymbolPtr	    standardExceptions[_num_standard_exceptions];
StandardException   standardException;
Value		    standardExceptionArgs;
ReferencePtr	    standardExceptionArgsRef;

void
RegisterStandardException (StandardException	se,
			   SymbolPtr		sym)
{
    ENTER ();
    standardExceptions[se] = sym;
    MemAddRoot (sym);
    if (!standardExceptionArgsRef)
    {
	standardExceptionArgsRef = NewReference ((void **) &standardExceptionArgs);
	MemAddRoot (standardExceptionArgsRef);
    }
    EXIT ();
}

SymbolPtr
CheckStandardException (void)
{
    SymbolPtr		except = standardExceptions[standardException];

    signalException = False;
    standardException = exception_none;
    standardExceptionArgs = 0;
    return except;
}

void
RaiseStandardException (StandardException   se,
			int		    argc,
			...)
{
    ENTER ();
    Value	args;
    int		i;
    va_list	va;

    va_start (va, argc);
    i = argc;
    args = NewArray (False, False, typePoly, 1, &i);
    for (i = 0; i < argc; i++)
	ArrayValueSet (&args->array, i, va_arg (va, Value));
    standardException = se;
    standardExceptionArgs = args;
    SetSignalException ();
    EXIT ();
}

Value
JumpStandardException (Value thread, InstPtr *next)
{
    ENTER ();
    SymbolPtr		except = standardExceptions[standardException];
    Value		args = standardExceptionArgs;

    aborting = False;
    if (except)
	RaiseException (thread, except, args, next);
    standardException = exception_none;
    standardExceptionArgs = 0;
    RETURN (args);
}

static void
SignalThread (Value thread, Value signal, Bool executing)
{
    ENTER ();
    int		i = 1;
    Value	args = NewArray (False, False, typePoly, 1, &i);
    SymbolPtr	except = standardExceptions[exception_signal];

    ArrayValueSet (&args->array, 0, signal);
    if (thread == running && executing)
    {
	standardException = exception_signal;
	standardExceptionArgs = args;
	SetSignalException ();
    }
    else if (except)
    {
	InstPtr	next;

	RaiseException (thread, except, args, &next);
	thread->thread.continuation.value = args;
	thread->thread.continuation.pc = next;
	if (thread->thread.state == ThreadSuspended) {
	    thread->thread.sleep = 0;
	    ThreadSetState (thread, ThreadRunning);
	}
    }
    EXIT ();
}

void
ThreadsSignal (Value signal)
{
    ENTER ();
    Value   thread, next;

    /* do running first -- signalling makes threads run */
    for (thread = running; thread; thread = next)
    {
	next = thread->thread.next;
	SignalThread (thread, signal, False);
    }
    for (thread = stopped; thread; thread = next)
    {
	next = thread->thread.next;
	SignalThread (thread, signal,  False);
    }
    EXIT ();
}

Value
do_Thread_signal (Value thread, Value signal)
{
    ENTER ();
    SignalThread (thread, signal, True);
    RETURN (Void);
}