-
Notifications
You must be signed in to change notification settings - Fork 65
/
MultiProcessor.class.st
76 lines (71 loc) · 2.64 KB
/
MultiProcessor.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
Class {
#name : #MultiProcessor,
#superclass : #Object,
#instVars : [
'mutex',
'processor',
'guardedProcessorProtocol',
'unguardedProcessorProtocol',
'owner',
'registerState',
'coInterpreter',
'threadIndex'
],
#category : #'Cog-Processors'
}
{ #category : #'instance creation' }
MultiProcessor class >> for: aProcessorAlien coInterpreter: coInterpreter [
^self new
processor: aProcessorAlien;
coInterpreter: coInterpreter;
yourself
]
{ #category : #'initialize-release' }
MultiProcessor >> coInterpreter: aCoInterpreter [
coInterpreter := aCoInterpreter
]
{ #category : #'message forwarding' }
MultiProcessor >> doesNotUnderstand: aMessage [
"Forward a message to the actual processor, managing a thread-switch if necessary.
Catch ProcessorSimulationTraps and raise them outside of the critical section to
avoid deadlock when reentering the VM from a trap and switching threads in the run-time."
| selector result trap |
selector := aMessage selector.
(guardedProcessorProtocol includes: selector) ifFalse:
[^(unguardedProcessorProtocol includes: selector)
ifTrue: [processor perform: selector withArguments: aMessage arguments]
ifFalse: [super doesNotUnderstand: aMessage]].
result := [mutex critical:
[owner ~~ mutex owningProcess ifTrue:
[owner ifNotNil:
[registerState at: owner put: processor registerState].
(registerState at: (owner := mutex owningProcess) ifAbsent: nil)
ifNil: [coInterpreter initializeProcessorForThreadIndex: (threadIndex := threadIndex + 1)]
ifNotNil: [:newState| processor setRegisterState: newState]].
processor perform: selector withArguments: aMessage arguments]]
on: ProcessorSimulationTrap, Error
do: [:ex| trap := ex].
^trap ifNil: [result] ifNotNil: [trap signal]
]
{ #category : #'initialize-release' }
MultiProcessor >> initialize [
registerState := WeakIdentityKeyDictionary new.
mutex := Mutex new.
threadIndex := 0.
guardedProcessorProtocol := unguardedProcessorProtocol := Set new
]
{ #category : #'initialize-release' }
MultiProcessor >> processor: aProcessor [
processor := aProcessor.
guardedProcessorProtocol := aProcessor class selectors asSet
addAll: aProcessor class superclass selectors;
yourself.
unguardedProcessorProtocol := #(#'Cog API' #opcodes #disassembly #printing)
inject: Set new
into: [ :protocol :category|
protocol
addAll: (aProcessor class organization listAtCategoryNamed: category);
addAll: (aProcessor class superclass organization listAtCategoryNamed: category);
yourself].
guardedProcessorProtocol removeAll: unguardedProcessorProtocol
]