/
CoInterpreterMT.class.st
1578 lines (1428 loc) · 64.3 KB
/
CoInterpreterMT.class.st
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
Class {
#name : #CoInterpreterMT,
#superclass : #CoInterpreterPrimitives,
#instVars : [
'cogThreadManager',
'checkThreadActivation',
'maxWaitingPriority',
'foreignCallbackPriority',
'deferThreadSwitch',
'disowningVMThread',
'disownCount',
'foreignCallbackProcessSlot',
'willNotThreadWarnCount',
'activeProcessAffined',
'relinquishing',
'processHasThreadId',
'noThreadingOfGUIThread'
],
#classVars : [
'DisownFlagsShift',
'LockGUIThreadFlag',
'LockGUIThreadShift',
'OwnVMForeignThreadFlag',
'ProcessUnaffinedOnDisown',
'ReturnToThreadSchedulingLoop',
'VMAlreadyOwnedHenceDoNotDisown'
],
#pools : [
'VMThreadingConstants'
],
#category : #'VMMaker-Multithreading'
}
{ #category : #translation }
CoInterpreterMT class >> ancilliaryClasses [
"Answer any extra classes to be included in the translation."
^super ancilliaryClasses, { CogThreadManager. CogVMThread }
]
{ #category : #translation }
CoInterpreterMT class >> apiExportHeaderName [
^'cointerp.h'
]
{ #category : #translation }
CoInterpreterMT class >> declareCVarsIn: aCCodeGenerator [
aCCodeGenerator
addHeaderFile:'"sqAtomicOps.h"'. "For THRLOG"
aCCodeGenerator vmClass
declareInterpreterVersionIn: aCCodeGenerator
defaultName: 'Cog MT'.
aCCodeGenerator
var: #disowningVMThread type: #'CogVMThread *'
]
{ #category : #initialization }
CoInterpreterMT class >> initializeMiscConstants [
super initializeMiscConstants.
"N.B. some of these DisownFlags are replicated in platforms/Cross/vm/sqVirtualMachine.h.
Hence they should always be initialized."
DisownVMForProcessorRelinquish := 64.
(InitializationOptions at: #COGMTVM ifAbsent: [false]) == false ifTrue:
[^self].
COGMTVM := true.
ReturnToThreadSchedulingLoop := 2 "setjmp/longjmp code."
]
{ #category : #initialization }
CoInterpreterMT class >> initializePrimitiveTable [
super initializePrimitiveTable.
COGMTVM ifTrue:
[(227 to: 229) do:
[:pidx| self assert: (PrimitiveTable at: pidx + 1) = #primitiveFail].
PrimitiveTable
at: 227 + 1 put: #primitiveVMCurrentThreadId;
at: 228 + 1 put: #primitiveProcessBoundThreadId;
at: 229 + 1 put: #primitiveProcessBindToThreadId]
]
{ #category : #initialization }
CoInterpreterMT class >> initializeSchedulerIndices [
super initializeSchedulerIndices.
"Class Process"
ThreadIdIndex := 4.
"disown result/own argument flags & max number of threads"
LockGUIThreadShift := 16.
LockGUIThreadFlag := 1 << LockGUIThreadShift.
OwnVMForeignThreadFlag := 1 << (LockGUIThreadShift + 1).
VMAlreadyOwnedHenceDoNotDisown := 1 << (LockGUIThreadShift + 2).
ProcessUnaffinedOnDisown := 1 << (LockGUIThreadShift + 3).
DisownFlagsShift := LockGUIThreadShift + 4.
ThreadIdMask := (1 << LockGUIThreadShift) - 1 "results in 64k thread indices"
]
{ #category : #accessing }
CoInterpreterMT class >> interpreterVersion [
^ 'Cog MT'
]
{ #category : #translation }
CoInterpreterMT class >> isNonArgumentImplicitReceiverVariableName: aString [
^'cogThreadManager' = aString
or: [super isNonArgumentImplicitReceiverVariableName: aString]
]
{ #category : #testing }
CoInterpreterMT class >> isThreadedVM [
^true
]
{ #category : #translation }
CoInterpreterMT class >> mustBeGlobal: var [
"Make disownCount global so that debugging/instrumentation code can use it to check
if a threaded FFI call is in progress (i.e. this isn't necessary for production)"
^(super mustBeGlobal: var)
or: [ #('disownCount' 'willNotThreadWarnCount') includes: var]
]
{ #category : #'accessing class hierarchy' }
CoInterpreterMT class >> primitivesClass [
^CoInterpreterMT
]
{ #category : #translation }
CoInterpreterMT class >> sourceFileName [
"Answer the filename for the core interpreter"
^'cointerp.c'
]
{ #category : #'process primitive support' }
CoInterpreterMT >> activateProcess: activeProc [
objectMemory
storePointerUnchecked: MyListIndex
ofObject: activeProc
withValue: objectMemory nilObject.
objectMemory
storePointer: ActiveProcessIndex
ofObject: self schedulerPointer
withValue: activeProc
]
{ #category : #simulation }
CoInterpreterMT >> assertCStackPointersBelongToCurrentThread [
<doNotGenerate>
| range |
range := self cStackRangeForCurrentThread.
self assert: ((range includes: cogit getCStackPointer)
and: [range includes: cogit getCFramePointer])
]
{ #category : #simulation }
CoInterpreterMT >> assertCStackPointersBelongToCurrentVMOwner [
<doNotGenerate>
| range |
range := self cStackRangeForCurrentVMOwner.
self assert: (range notNil "VM is owned"
and: [(range includes: cogit getCStackPointer)
and: [range includes: cogit getCFramePointer]])
]
{ #category : #'debug support' }
CoInterpreterMT >> assertSaneThreadAndProcess [
<inline: true>
self assert: cogThreadManager getVMOwner > 0.
self assert: cogThreadManager currentVMThread state = CTMAssignableOrInVM.
self assert: (objectMemory fetchPointer: MyListIndex ofObject: self activeProcess) = objectMemory nilObject
]
{ #category : #'process primitive support' }
CoInterpreterMT >> bindProcess: aProcess toId: newId [
"Change a Process's thread binding and answer 0, otherwise answer a suitable error code.
Cases:
process is unbound & unaffined
id 0 nothing to do
id non-zero ensure thread and bind
process is affined (temporarily bound to a thread for the duration of a surrender of ownership)
id = affined index nothing to do
id = 0 nothing to do
id ~= 0 && id ~= affined index fail
process is bound (permanently bound to a thread)
id = bound index nothing to do
id ~= bound index set bound index"
| threadId ownerIndex |
processHasThreadId ifFalse:
[^PrimErrUnsupported].
threadId := self threadIdFieldOf: aProcess.
ownerIndex := self ownerIndexOfThreadId: threadId.
(self isAffinedThreadId: threadId) ifTrue:
[(ownerIndex = newId
or: [newId = 0]) ifTrue:
[^0].
^PrimErrInappropriate].
(cogThreadManager growThreadInfosToAtLeast: newId)
ifFalse: [ ^ PrimErrLimitExceeded ].
"newId > 0 ifTrue:
[(cogThreadManager startThreadForThreadIndex: newId) ifFalse:
[^PrimErrLimitExceeded]]."
self setOwnerIndexOfProcess: aProcess to: newId bind: true.
^0
]
{ #category : #simulation }
CoInterpreterMT >> cStackRangeForCurrentThread [
<doNotGenerate>
^self cStackRangeForThreadIndex: cogThreadManager ioGetThreadLocalThreadIndex
]
{ #category : #simulation }
CoInterpreterMT >> cStackRangeForCurrentVMOwner [
<doNotGenerate>
^self cStackRangeForThreadIndex: cogThreadManager getVMOwner
]
{ #category : #simulation }
CoInterpreterMT >> cStackRangeForThreadIndex: threadIndex [
"Each simulated processor thread gets 4k of the rump C stack.
The top-most section is reserved for in-memory variables such as vmOwnerLock."
<doNotGenerate>
| top |
^(threadIndex between: 1 and: cogThreadManager maxNumThreads) ifTrue:
[top := self rumpCStackAddress - (threadIndex * RumpCStackSize).
top - RumpCStackSize + 1 to: top]
]
{ #category : #'process primitive support' }
CoInterpreterMT >> cedeToHigherPriorityThreads [
"Invoked from checkForEventsMayContextSwitch: to switch threads if a thread
wanting to acquire the VM has higher priority than the active process."
| activeProc ownerIndex activeContext activePriority activeThread vmThread |
<var: #activeThread type: #'CogVMThread *'>
<var: #vmThread type: #'CogVMThread *'>
<inline: false>
activeProc := self activeProcess.
activePriority := self quickFetchInteger: PriorityIndex ofObject: activeProc.
ownerIndex := self ownerIndexOfProcess: activeProc.
ownerIndex = 0
ifTrue: [activeThread := cogThreadManager currentVMThread]
ifFalse: [activeThread := cogThreadManager vmThreadAt: ownerIndex].
activeThread priority: activePriority.
vmThread := cogThreadManager
highestPriorityThreadIfHigherThan: activePriority
expectedMax: maxWaitingPriority.
(vmThread isNil "no waiting thread of sufficiently high priority. Do not switch."
or: [vmThread = activeThread]) "The activeProcess needs to run on a different thread. Leave this to
threadSwitchIfNecessary:from: in checkForEventsMayContextSwitch:"
ifTrue:
[maxWaitingPriority > activePriority ifTrue:
[maxWaitingPriority := activePriority].
^self].
self assert: vmThread priority > activePriority.
self assert: vmThread ~= cogThreadManager currentVMThread.
self assertValidExecutionPointe: instructionPointer r: framePointer s: stackPointer.
maxWaitingPriority > vmThread priority ifTrue:
[maxWaitingPriority := vmThread priority].
statProcessSwitch := statProcessSwitch + 1.
activeContext := self ensureFrameIsMarried: framePointer SP: stackPointer.
objectMemory storePointer: SuspendedContextIndex ofObject: activeProc withValue: activeContext.
self ensurePushedInstructionPointer.
self externalWriteBackHeadFramePointers.
self putToSleep: activeProc yieldingIf: preemptionYields.
"Transcript cr; print: #cedeToHighestPriorityThreadIfHigherThan:; cr.
self printExternalHeadFrame.
self print: 'ip: '; printHex: self instructionPointer. Transcript cr; flush."
self returnToSchedulingLoopAndReleaseVMOrWakeThread: vmThread source: CSCheckEvents
]
{ #category : #'process primitive support' }
CoInterpreterMT >> checkForEventsMayContextSwitch: mayContextSwitch [
"Check for possible interrupts and handle one if necessary.
Answer if a context switch has occurred."
| switched sema now |
<inline: false>
<var: #now type: #usqLong>
self assertSaneThreadAndProcess.
cogit assertCStackWellAligned.
statCheckForEvents := statCheckForEvents + 1.
"restore the stackLimit if it has been smashed."
self restoreStackLimit.
self externalWriteBackHeadFramePointers.
self assert: stackPage = stackPages mostRecentlyUsedPage.
"Allow the platform to do anything it needs to do synchronously."
self ioSynchronousCheckForEvents.
self checkCogCompiledCodeCompactionCalledFor.
objectMemory needGCFlag ifTrue:
["sufficientSpaceAfterGC: runs the incremental GC and
then, if not enough space is available, the fullGC."
(objectMemory sufficientSpaceAfterGC: 0) ifFalse:
[self setSignalLowSpaceFlagAndSaveProcess]].
mayContextSwitch ifFalse: [^false].
switched := false.
self assert: deferThreadSwitch not.
deferThreadSwitch := true.
(profileProcess ~= objectMemory nilObject
or: [nextProfileTick > 0 and:[self ioHighResClock >= nextProfileTick]]) ifTrue:
[nextProfileTick := 0.
"Take a sample (if not already done so) for the profiler if it is active. This
must be done before any of the synchronousSignals below or else we will
attribute a pause in ioRelinquishProcessor to the newly activated process."
profileProcess = objectMemory nilObject ifTrue:
[profileProcess := self activeProcess.
profileMethod := objectMemory nilObject].
"and signal the profiler semaphore if it is present"
(profileSemaphore ~= objectMemory nilObject
and: [self synchronousSignal: profileSemaphore]) ifTrue:
[switched := true]].
self checkDeliveryOfLongRunningPrimitiveSignal ifTrue:
[switched := true].
objectMemory signalLowSpace ifTrue:
[objectMemory signalLowSpace: false. "reset flag"
sema := objectMemory splObj: TheLowSpaceSemaphore.
(sema ~= objectMemory nilObject
and: [self synchronousSignal: sema]) ifTrue:
[switched := true]].
"inIOProcessEvents prevents reentrancy into ioProcessEvents and allows disabling
ioProcessEvents e.g. for native GUIs. We would like to manage that here but can't
since the platform code may choose to call ioProcessEvents itself in various places."
false
ifTrue:
[((now := self ioUTCMicroseconds) >= nextPollUsecs
and: [inIOProcessEvents = 0]) ifTrue:
[statIOProcessEvents := statIOProcessEvents + 1.
inIOProcessEvents := inIOProcessEvents + 1.
self ioProcessEvents. "sets interruptPending if interrupt key pressed; may callback"
inIOProcessEvents > 0 ifTrue:
[inIOProcessEvents := inIOProcessEvents - 1].
nextPollUsecs := now + 20000
"msecs to wait before next call to ioProcessEvents. Note that strictly
speaking we might need to update 'now' at this point since
ioProcessEvents could take a very long time on some platforms"]]
ifFalse:
[((now := self ioUTCMicroseconds) >= nextPollUsecs and: [ self inGUIThread ]) ifTrue:
[statIOProcessEvents := statIOProcessEvents + 1.
self ioProcessEvents. "sets interruptPending if interrupt key pressed; may callback"
nextPollUsecs := now + 20000
"msecs to wait before next call to ioProcessEvents. Note that strictly
speaking we might need to update 'now' at this point since
ioProcessEvents could take a very long time on some platforms"]].
interruptPending ifTrue:
[interruptPending := false.
"reset interrupt flag"
sema := objectMemory splObj: TheInterruptSemaphore.
(sema ~= objectMemory nilObject
and: [self synchronousSignal: sema]) ifTrue:
[switched := true]].
nextWakeupUsecs ~= 0 ifTrue:
[now >= nextWakeupUsecs ifTrue:
[nextWakeupUsecs := 0.
"set timer interrupt to 0 for 'no timer'"
sema := objectMemory splObj: TheTimerSemaphore.
(sema ~= objectMemory nilObject
and: [self synchronousSignal: sema]) ifTrue:
[switched := true]]].
"signal any pending finalizations"
pendingFinalizationSignals > 0 ifTrue:
[pendingFinalizationSignals := 0.
sema := objectMemory splObj: TheFinalizationSemaphore.
(sema ~= objectMemory nilObject
and: [self synchronousSignal: sema]) ifTrue:
[switched := true]].
"signal all semaphores in semaphoresToSignal"
self signalExternalSemaphores ifTrue:
[switched := true].
deferThreadSwitch := false.
checkThreadActivation ifTrue:
[checkThreadActivation := false.
self cedeToHigherPriorityThreads]. "N.B. This may not return if we do switch."
self threadSwitchIfNecessary: self activeProcess from: CSCheckEvents.
^switched
]
{ #category : #'process primitive support' }
CoInterpreterMT >> checkVMOwnershipFromHeartbeat [
"Check whether the VM is unowned and needs to set a thread running to try and own it.
Do not attempt this if the image doesn't have a threadId inst var in Process; the VM
can't thread these images."
<inline: false>
self sqLowLevelMFence.
"("processHasThreadId
"and: [cogThreadManager getVMOwner = 0]) "ifTrue:
[cogThreadManager ensureRunningVMThread: relinquishing]
]
{ #category : #'process primitive support' }
CoInterpreterMT >> deferStackLimitSmashAround: functionSymbol [
"Defer smashes of the stackLimit around the call of functionSymbol (for assert checks).
N.B. SYNCHRONIZE WITH forceInterruptCheckFromHeartbeat"
<var: #functionSymbol declareC: 'void (*functionSymbol)(void)'>
deferSmash := true.
self perform: functionSymbol.
deferSmash := false.
deferredSmash ifTrue:
[deferredSmash := false.
self forceInterruptCheck.
self checkVMOwnershipFromHeartbeat].
^true "called from assert"
]
{ #category : #'process primitive support' }
CoInterpreterMT >> deferStackLimitSmashAround: functionSymbol with: arg [
"Defer smashes of the stackLimit around the call of functionSymbol (for assert checks).
N.B. SYNCHRONIZE WITH forceInterruptCheckFromHeartbeat"
<var: #functionSymbol declareC: 'void (*functionSymbol)(sqInt)'>
deferSmash := true.
self sqLowLevelMFence.
self perform: functionSymbol with: arg.
deferSmash := false.
self sqLowLevelMFence.
deferredSmash ifTrue:
[deferredSmash := false.
self sqLowLevelMFence.
self forceInterruptCheck.
self checkVMOwnershipFromHeartbeat].
^true "called from assert"
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> disownVM: flags [
"Release the VM to other threads and answer the current thread's index.
Currently valid flags:
DisownVMLockOutFullGC - prevent fullGCs while this thread disowns the VM
DisownVMForFFICall - informs the VM that it is entering an FFI call
DisownVMForThreading - informs the VM that it is entering an FFI call etc during which threading should be permitted
OwnVMForeignThreadFlag - indicates lowest-level entry from a foreign thread
- not to be used explicitly by clients
- only set by ownVMFromUnidentifiedThread
VMAlreadyOwnedHenceDoNotDisown
- indicates an ownVM from a callback was made when
the vm was still owned.
- not to be used explicitly by clients
- only set by ownVMFromUnidentifiedThread
This is the entry-point for plugins and primitives that wish to release the VM while
performing some operation that may potentially block, and for callbacks returning
back to some blocking operation. If this thread does not reclaim the VM before-
hand then when the next heartbeat occurs the thread manager will schedule a
thread to acquire the VM which may start running the VM in place of this thread.
N.B. Most of the state needed to resume after preemption is set in preemptDisowningThread."
<api>
<inline: false>
^ self disownVM: flags fromVMThread: cogThreadManager currentVMThread.
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> disownVM: flags fromVMThread: vmThread [
<inline: #always>
"Release the VM to other threads and answer the current thread's index.
Currently valid flags:
DisownVMLockOutFullGC - prevent fullGCs while this thread disowns the VM
DisownVMForFFICall - informs the VM that it is entering an FFI call
DisownVMForThreading - informs the VM that it is entering an FFI call etc during which threading should be permitted
OwnVMForeignThreadFlag - indicates lowest-level entry from a foreign thread
- not to be used explicitly by clients
- only set by ownVMFromUnidentifiedThread
VMAlreadyOwnedHenceDoNotDisown
- indicates an ownVM from a callback was made when
the vm was still owned.
- not to be used explicitly by clients
- only set by ownVMFromUnidentifiedThread
This is the entry-point for plugins and primitives that wish to release the VM while
performing some operation that may potentially block, and for callbacks returning
back to some blocking operation. If this thread does not reclaim the VM before-
hand then when the next heartbeat occurs the thread manager will schedule a
thread to acquire the VM which may start running the VM in place of this thread.
N.B. Most of the state needed to resume after preemption is set in preemptDisowningThread."
<api>
<inline: false>
| result |
<var: #vmThread type: #'CogVMThread *'>
self assert: self successful.
cogit recordEventTrace ifTrue:
[self recordTrace: TraceDisownVM thing: (objectMemory integerObjectOf: flags) source: 0].
processHasThreadId ifFalse:
[willNotThreadWarnCount < 10 ifTrue:
[self print: 'warning: VM parameter 48 indicates Process doesn''t have threadId; VM will not thread'; cr.
willNotThreadWarnCount := willNotThreadWarnCount + 1]].
(flags anyMask: VMAlreadyOwnedHenceDoNotDisown) ifTrue:
[disowningVMThread := vmThread.
vmThread state: CTMUnavailable.
^0].
self cCode: ''
inSmalltalk:
[| range |
range := self cStackRangeForThreadIndex: vmThread index.
self assert: (range includes: cogit getCStackPointer).
self assert: (range includes: cogit getCFramePointer)].
(flags anyMask: DisownVMForProcessorRelinquish) ifTrue:
[| proc |
(proc := objectMemory splObj: foreignCallbackProcessSlot) ~= objectMemory nilObject ifTrue:
[foreignCallbackPriority := self quickFetchInteger: PriorityIndex ofObject: proc].
relinquishing := true.
self sqLowLevelMFence].
(flags anyMask: DisownVMLockOutFullGC) ifTrue:
[objectMemory incrementFullGCLock].
(noThreadingOfGUIThread and: [self inGUIThread]) ifTrue:
[^vmThread index
+ LockGUIThreadFlag
+ (activeProcessAffined ifTrue: [0] ifFalse: [ProcessUnaffinedOnDisown])
+ (flags << DisownFlagsShift)].
disownCount := disownCount + 1.
disowningVMThread := vmThread.
"self cr; cr; print: 'disownVM Csp: '; printHex: vmThread cStackPointer; cr.
(0 to: 16 by: 4) do:
[:offset|
self print: ' *(esp+'; printNum: offset; print: ': '; printHex: (stackPages longAt: cogit processor sp + offset); cr].
cogit processor printIntegerRegistersOn: Transcript."
"OwnVMForeignThreadFlag indicates lowest-level of entry by a foreign
thread. If that's where we are then release the vmThread. Otherwise
indicate the vmThread is off doing something outside of the VM."
(flags anyMask: OwnVMForeignThreadFlag)
ifTrue:
["I don't think this is quite right. Josh's use case is creating some foreign thread and then registering
it with the VM. That's not the same as binding a process to a foreign thread given that the foreign
callback process is about to terminate anyway (it is returning from a callback here). So do we need
an additional concept, that of a vmThread being either of the set known to the VM or floating?"
self flag: 'issue with registering foreign threads with the VM'.
(self isBoundProcess: self activeProcess) ifFalse:
[cogThreadManager unregisterVMThread: vmThread]]
ifFalse: [vmThread state: CTMUnavailable].
result := vmThread index
+ (activeProcessAffined ifTrue: [0] ifFalse: [ProcessUnaffinedOnDisown])
+ (flags << DisownFlagsShift).
cogThreadManager releaseVM.
^result
]
{ #category : #initialization }
CoInterpreterMT >> enterSmalltalkExecutive [
"Main entry-point into the interpreter at each execution level, where an
execution level is either the start of execution or reentry for a callback."
<cmacro: '() enterSmalltalkExecutiveImplementation()'>
"Simulation of the setjmp in enterSmalltalkExecutiveImplementation for reentry
into interpreter. Simulation of the register state switch on thread switch."
| vmo tlti thisActivationsSP thisActivationsFP retVal |
[vmo := cogThreadManager getVMOwner.
tlti := cogThreadManager ioGetThreadLocalThreadIndex.
self assert: vmo = tlti.
thisActivationsSP := cogit processor sp.
thisActivationsFP := cogit processor fp.
retVal := [self enterSmalltalkExecutiveImplementation]
on: ReenterInterpreter
do: [:ex|
vmo := cogThreadManager getVMOwner.
tlti := cogThreadManager ioGetThreadLocalThreadIndex.
self assert: (ex returnValue = ReturnToThreadSchedulingLoop
or: [vmo = tlti]).
ex return: ex returnValue].
"We must cut back the stack pointers on return to mimic the longjmp."
cogit processor
setFramePointer: thisActivationsFP
stackPointer: thisActivationsSP.
retVal = ReturnToInterpreter] whileTrue
]
{ #category : #'callback support' }
CoInterpreterMT >> enterSmalltalkExecutiveFromCallback [
<inline: true>
self threadSchedulingLoop: cogThreadManager currentVMThread
]
{ #category : #initialization }
CoInterpreterMT >> enterSmalltalkExecutiveImplementation [
"Main entry-point into the interpreter at each execution level, where an execution
level is either the start of execution or reentry for a callback. Capture the C stack
pointers so that calls from machine-code into the C run-time occur at this level.
This is the actual implementation, separated from enterSmalltalkExecutive so the
simulator can wrap it in an exception handler and hence simulate the setjmp/longjmp.
Override to return if a longjmp to reenterInterpreter passes a parameter greater than 1.
This causes a return to threadSchedulingLoop:startingVM: and is used to surrender
control to another thread."
<inline: false>
self assertSaneThreadAndProcess.
cogit assertCStackWellAligned.
cogit ceCaptureCStackPointers.
"Setjmp for reentry into interpreter from elsewhere, e.g. machine-code trampolines."
(self sigset: reenterInterpreter jmp: 0) > 1 ifTrue:
[^0].
(self isMachineCodeFrame: framePointer) ifTrue:
[self returnToExecutive: false postContextSwitch: true
"NOTREACHED"].
self setMethod: (self iframeMethod: framePointer).
instructionPointer = cogit ceReturnToInterpreterPC ifTrue:
[instructionPointer := self iframeSavedIP: framePointer].
self assertValidExecutionPointe: instructionPointer r: framePointer s: stackPointer imbar: true line: #'__LINE__'.
self interpret.
"NOTREACHED"
^0
]
{ #category : #'process primitive support' }
CoInterpreterMT >> forceInterruptCheckFromHeartbeat [
"Force an interrupt check ASAP. This version is the
entry-point to forceInterruptCheck for the heartbeat
timer to allow for repeatable debugging.
N.B. SYNCHRONIZE WITH deferStackLimitSmashAround:"
suppressHeartbeatFlag ifFalse:
[self checkForLongRunningPrimitive.
self sqLowLevelMFence.
deferSmash
ifTrue:
[deferredSmash := true.
self sqLowLevelMFence]
ifFalse:
[self forceInterruptCheck.
self checkVMOwnershipFromHeartbeat]]
]
{ #category : #'internal interpreter access' }
CoInterpreterMT >> getCogVMFlags [
"Answer an array of flags indicating various properties of the Cog VM.
These are the same as the image header flags shifted right two bits (excluding float order and full screen flags).
Bit 0: implies the image's Process class has threadId as its 3rd inst var (zero relative)
Bit 1: if set, methods that are interpreted will have the flag bit set in their header
Bit 2: if set, implies preempting a process does not put it to the back of its run queue
Bit 3: if set, implies the GUI will run on the first thread and event queues will not be accessed from other threads
Bit 4: if set, implies the new finalization scheme where WeakArrays are queued
Bit 5: if set, implies wheel events will be delivered as such and not mapped to arrow key events"
^objectMemory integerObjectOf: (processHasThreadId ifTrue: [1] ifFalse: [0])
+ (flagInterpretedMethods ifTrue: [2] ifFalse: [0])
+ (preemptionYields ifTrue: [0] ifFalse: [4])
+ (noThreadingOfGUIThread ifTrue: [8] ifFalse: [0])
+ (newFinalization ifTrue: [16] ifFalse: [0])
+ (imageHeaderFlags >> 2 bitClear: 1 + 2 + 4 + 8 + 16)
]
{ #category : #'image save/restore' }
CoInterpreterMT >> getImageHeaderFlags [
"Answer the flags that are contained in the 7th long of the image header."
^fullScreenFlag "0 or 1"
+ (VMBIGENDIAN ifTrue: [0] ifFalse: [2]) "this is the imageFloatsLittleEndian flag"
+ (processHasThreadId ifTrue: [4] ifFalse: [0])
+ (flagInterpretedMethods ifTrue: [8] ifFalse: [0])
+ (preemptionYields ifTrue: [0] ifFalse: [16r10])
+ (noThreadingOfGUIThread ifTrue: [16r20] ifFalse: [0])
+ (newFinalization ifTrue: [16r40] ifFalse: [0])
+ (sendWheelEvents ifTrue: [16r80] ifFalse: [0])
+ (imageHeaderFlags bitClear: 16rFF) "these are any flags we do not recognize"
]
{ #category : #'process primitive support' }
CoInterpreterMT >> getMaxWaitingPriority [
<cmacro: '() GIV(maxWaitingPriority)'>
^maxWaitingPriority
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> inGUIThread [
"The first thread is assumed to be the GUI thread, the VM thread that expects to receive
window events, etc. This might appear to invite race conditions but it is only to be used
to decide whether to not give up the VM from the GUI thread (see disownVM:)."
^cogThreadManager getVMOwner = 1
]
{ #category : #initialization }
CoInterpreterMT >> initialEnterSmalltalkExecutive [
"Main entry-point into the interpreter at system start-up."
"Ensure that the myList of the activeProcess is nil. Needed to load
old images which don't nil myList in transferTo:{from:}"
objectMemory storePointerUnchecked: MyListIndex ofObject: self activeProcess withValue: objectMemory nilObject.
self initializeVMOwnerLock.
cogThreadManager startThreadSubsystem.
self threadSchedulingLoop: (cogThreadManager vmThreadAt: 1)
]
{ #category : #initialization }
CoInterpreterMT >> initialize [
super initialize.
relinquishing := checkThreadActivation := deferThreadSwitch := false.
foreignCallbackPriority := maxWaitingPriority := disownCount := willNotThreadWarnCount := 0
]
{ #category : #initialization }
CoInterpreterMT >> initializeInterpreter: bytesToShift [
super initializeInterpreter: bytesToShift.
foreignCallbackProcessSlot := (objectMemory lengthOf: objectMemory specialObjectsOop) > ForeignCallbackProcess
ifTrue: [ForeignCallbackProcess]
ifFalse: [NilObject]
]
{ #category : #simulation }
CoInterpreterMT >> initializeProcessorForThreadIndex: threadIndex [
"Each simulated processor thread gets 4k of the rump C stack."
<doNotGenerate>
cogit initializeProcessorStack: (self cStackRangeForThreadIndex: threadIndex) last
]
{ #category : #initialization }
CoInterpreterMT >> initializeVMOwnerLock [
"Initially it seems we do not need to call anybody.
The VMOwner lock is initialized by the cogit, which has a ifdef COGMT for it...
cogit generateVMOwnerLockFunctions"
]
{ #category : #'process primitive support' }
CoInterpreterMT >> isAffinedProcess: aProcess [
^self isAffinedThreadId: (self ownerIndexOfProcess: aProcess)
]
{ #category : #'process primitive support' }
CoInterpreterMT >> isAffinedThreadId: threadId [
^(objectMemory isIntegerObject: threadId)
and: [((objectMemory integerValueOf: threadId) bitAnd: 1) = 0]
]
{ #category : #'process primitive support' }
CoInterpreterMT >> isBoundProcess: aProcess [
^self isBoundThreadId: (self threadIdFieldOf: aProcess)
]
{ #category : #'process primitive support' }
CoInterpreterMT >> isBoundThreadId: threadId [
^(objectMemory isIntegerObject: threadId)
and: [((objectMemory integerValueOf: threadId) bitAnd: 1) = 1]
]
{ #category : #simulation }
CoInterpreterMT >> isThreadedVM [
<doNotGenerate>
^true
]
{ #category : #'process primitive support' }
CoInterpreterMT >> isUnboundThreadId: threadId [
"Answer if the threadId is neither affined nor bound. Not the same as bound not."
^threadId = objectMemory nilObject
or: [((objectMemory integerValueOf: threadId) bitAnd: (ThreadIdMask << 1) + 1) = 0]
]
{ #category : #initialization }
CoInterpreterMT >> loadInitialContext [
| activeProc |
super loadInitialContext.
activeProc := self activeProcess.
self assert: (self ownerIndexOfProcess: activeProc) = 0.
activeProcessAffined := (self ownerIndexOfProcess: activeProc) ~= 0
]
{ #category : #'object memory support' }
CoInterpreterMT >> mapInterpreterOops [
"Map all oops in the interpreter's state to their new values
during garbage collection or a become: operation."
"Assume: All traced variables contain valid oops."
<var: #vmThread type: #'CogVMThread *'>
super mapInterpreterOops.
"Per-thread state; trace each thread's own newMethod and stack of awol processes."
1 to: cogThreadManager getNumThreads do:
[:i| | vmThread |
vmThread := cogThreadManager vmThreadAt: i.
vmThread state ifNotNil:
[(vmThread newMethodOrNull notNil
and: [objectMemory shouldRemapOop: vmThread newMethodOrNull]) ifTrue:
[vmThread newMethodOrNull: (objectMemory remapObj: vmThread newMethodOrNull)].
0 to: vmThread awolProcIndex - 1 do:
[:j|
(objectMemory shouldRemapOop: (vmThread awolProcesses at: j)) ifTrue:
[vmThread awolProcesses at: j put: (objectMemory remapObj: (vmThread awolProcesses at: j))]]]]
]
{ #category : #'object memory support' }
CoInterpreterMT >> markAndTraceInterpreterOops: fullGCFlag [
"Mark and trace all oops in the interpreter's state."
<var: #vmThread type: #'CogVMThread *'>
super markAndTraceInterpreterOops: fullGCFlag.
"Per-thread state; trace each thread's own newMethod and stack of awol processes."
1 to: cogThreadManager getNumThreads do:
[:i| | vmThread |
vmThread := cogThreadManager vmThreadAt: i.
vmThread state notNil ifTrue:
[vmThread newMethodOrNull notNil ifTrue:
[objectMemory markAndTrace: vmThread newMethodOrNull].
0 to: vmThread awolProcIndex - 1 do:
[:j|
objectMemory markAndTrace: (vmThread awolProcesses at: j)]]]
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> ownVM: threadIndexAndFlags [
<api>
<inline: false>
"This is the entry-point for plugins and primitives that wish to reacquire the VM after having
released it via disownVM or callbacks that want to acquire it without knowing their ownership
status. This call will block until the VM is owned by the current thread or an error occurs.
The argument should be the value answered by disownVM, or 0 for callbacks that don't know
if they have disowned or not. This is both an optimization to avoid having to query thread-
local storage for the current thread's index (since it can easily keep it in some local variable),
and a record of when an unbound process becomes affined to a thread for the dynamic
extent of some operation.
Answer 0 if the current thread is known to the VM.
Answer 1 if the current thread is unknown to the VM and takes ownership.
Answer -1 if the current thread is unknown to the VM and fails to take ownership."
| threadIndex flags vmThread myProc activeProc sched |
<var: #vmThread type: #'CogVMThread *'>
threadIndexAndFlags = 0 ifTrue:
[^self ownVMFromUnidentifiedThread].
threadIndex := threadIndexAndFlags bitAnd: ThreadIdMask.
flags := threadIndexAndFlags >> DisownFlagsShift.
(flags anyMask: DisownVMForProcessorRelinquish) ifTrue:
[relinquishing := false.
self sqLowLevelMFence].
(threadIndexAndFlags anyMask: LockGUIThreadFlag) ifTrue:
[self assert: (noThreadingOfGUIThread and: [self inGUIThread]).
self assert: disowningVMThread = nil.
(flags anyMask: DisownVMLockOutFullGC) ifTrue:
[objectMemory decrementFullGCLock].
cogit recordEventTrace ifTrue:
[self recordTrace: TraceOwnVM thing: ConstZero source: 0].
^0].
vmThread := cogThreadManager acquireVMFor: threadIndex.
disownCount := disownCount - 1.
(flags anyMask: DisownVMLockOutFullGC) ifTrue:
[objectMemory decrementFullGCLock].
disowningVMThread notNil ifTrue:
[vmThread = disowningVMThread ifTrue:
[self cCode: ''
inSmalltalk:
[| range |
range := self cStackRangeForThreadIndex: threadIndex.
self assert: (range includes: cogit getCStackPointer).
self assert: (range includes: cogit getCFramePointer)].
self assert: self successful.
self assert: (objectMemory fetchPointer: MyListIndex ofObject: self activeProcess) = objectMemory nilObject.
disowningVMThread := nil.
cogit recordEventTrace ifTrue:
[self recordTrace: TraceOwnVM thing: ConstOne source: 0].
^0]. "if not preempted we're done."
self preemptDisowningThread].
"We've been preempted; we must restore state and update the threadId
in our process, and may have to put the active process to sleep."
sched := self schedulerPointer.
activeProc := objectMemory fetchPointer: ActiveProcessIndex ofObject: sched.
(threadIndexAndFlags anyMask: OwnVMForeignThreadFlag)
ifTrue:
[self assert: foreignCallbackProcessSlot == ForeignCallbackProcess.
myProc := objectMemory splObj: foreignCallbackProcessSlot.
self assert: myProc ~= objectMemory nilObject.
objectMemory splObj: foreignCallbackProcessSlot put: objectMemory nilObject]
ifFalse: [myProc := cogThreadManager popAWOLProcess: vmThread].
self assert: activeProc ~= myProc.
(activeProc ~= objectMemory nilObject
and: [(objectMemory fetchPointer: MyListIndex ofObject: activeProc) = objectMemory nilObject]) ifTrue:
[self putToSleep: activeProc yieldingIf: preemptionYields].
self assert: (objectMemory fetchPointer: MyListIndex ofObject: myProc) = (objectMemory splObj: ProcessInExternalCodeTag).
objectMemory
storePointer: ActiveProcessIndex ofObject: sched withValue: myProc;
storePointerUnchecked: MyListIndex ofObject: myProc withValue: objectMemory nilObject.
"Only unaffine if the process was affined at this level and did not become bound in the interim."
((threadIndexAndFlags anyMask: ProcessUnaffinedOnDisown)
and: [ (self isBoundProcess: myProc) not ])
ifTrue: [self setOwnerIndexOfProcess: myProc to: 0 bind: false].
activeProcessAffined := (self ownerIndexOfProcess: myProc) ~= 0.
self initPrimCall.
self externalSetStackPageAndPointersForSuspendedContextOfProcess: myProc.
"If this primitive is called from machine code maintain the invariant that the return pc
of an interpreter callee calling a machine code caller is ceReturnToInterpreterPC."
(vmThread inMachineCode
and: [instructionPointer >= objectMemory startOfMemory]) ifTrue:
[self iframeSavedIP: framePointer put: instructionPointer.
instructionPointer := cogit ceReturnToInterpreterPC].
newMethod := vmThread newMethodOrNull.
argumentCount := vmThread argumentCount.
self cCode:
[self memcpy: reenterInterpreter
_: vmThread reenterInterpreter
_: (self sizeof: #'jmp_buf')]
inSmalltalk:
[reenterInterpreter := vmThread reenterInterpreter].
vmThread newMethodOrNull: nil.
self cCode: ''
inSmalltalk:
[| range |
range := self cStackRangeForThreadIndex: threadIndex.
self assert: (range includes: vmThread cStackPointer).
self assert: (range includes: vmThread cFramePointer)].
cogit setCStackPointer: vmThread cStackPointer.
cogit setCFramePointer: vmThread cFramePointer.
self assert: newMethod ~~ nil.
cogit recordEventTrace ifTrue:
[self recordTrace: TraceOwnVM thing: ConstTwo source: 0].
^threadIndexAndFlags bitAnd: OwnVMForeignThreadFlag
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> ownVMFromUnidentifiedThread [
"Attempt to take ownership from a thread that as yet doesn't know its index.
This supports callbacks where the callback could originate from any thread.
Answer 0 if the owning thread is known to the VM.
Answer 1 if the owning thread is unknown to the VM and now owns the VM.
Answer -1 if the owning thread is unknown to the VM and fails to own the VM.
Answer -2 if the owning thread is unknown to the VM and there is no foreign callback process installed."
| count threadIndex vmThread |
<var: #vmThread type: #'CogVMThread *'>
<inline: false>
(threadIndex := cogThreadManager ioGetThreadLocalThreadIndex) ~= 0 ifTrue:
[ "this is a callback from a known thread"
threadIndex = cogThreadManager getVMOwner ifTrue: "the VM has not been disowned"
[self assert: (disowningVMThread isNil or: [disowningVMThread = self currentVMThread]).
disowningVMThread := nil.
self currentVMThread state: CTMAssignableOrInVM.
^VMAlreadyOwnedHenceDoNotDisown].
^self ownVM: threadIndex].
foreignCallbackPriority = 0 ifTrue:
[^-2].
count := 0.
"If the current thread doesn't have an index it's new to the vm
and we need to allocate a new threadInfo, failing if we can't.
We also need a process in the foreignCallbackProcessSlot upon
which to run the thread's eventual callback."
[[cogThreadManager tryLockVMToIndex: -1] whileFalse:
[self waitingPriorityIsAtLeast: foreignCallbackPriority.
cogThreadManager ioTransferTimeslice].
(objectMemory splObj: foreignCallbackProcessSlot) ~= objectMemory nilObject] whileFalse:
[cogThreadManager releaseVM.
(count := count + 1) > 1000 ifTrue:
[^-2].
cogThreadManager ioMilliSleep: 1].
vmThread := cogThreadManager unusedThreadInfo.
"N.B. Keep the VM locked anonymously so that we reserve the non-nil ForeignCallbackProcess
for this thread, avoiding the race between competing foreign callbacks. The acquireVMFor: in
ownVM: will set the vmOwner to the actual index. So only unlock on failure."
vmThread isNil ifTrue:
[cogThreadManager releaseVM.
^-1].
vmThread
state: CTMWantingOwnership;
priority: foreignCallbackPriority.
cogThreadManager registerVMThread: vmThread.
^self ownVM: vmThread index + OwnVMForeignThreadFlag
]
{ #category : #'process primitive support' }
CoInterpreterMT >> ownerIndexOfProcess: aProcess [
^self ownerIndexOfThreadId: (self threadIdFieldOf: aProcess)
]
{ #category : #'process primitive support' }
CoInterpreterMT >> ownerIndexOfThreadId: threadId [
^(objectMemory isIntegerObject: threadId)
ifTrue: [(objectMemory integerValueOf: threadId) >> 1 bitAnd: ThreadIdMask]
ifFalse: [0]
]
{ #category : #'vm scheduling' }
CoInterpreterMT >> preemptDisowningThread [
"Set the relevant state for disowningVMThread so that it can resume after
being preempted and set disowningVMThread to nil to indicate preemption.
N.B. This should only be sent from checkPreemptionOfDisowningThread.