added more code for process scheduling

This commit is contained in:
hyunghwan.chung
2015-10-15 14:40:08 +00:00
parent 5a82cdc417
commit f52356e8c8
11 changed files with 499 additions and 272 deletions

View File

@ -7,12 +7,12 @@
#method(#class) dump
{
<primitive: #dump>
<primitive: #_dump>
}
#method dump
{
<primitive: #dump>
<primitive: #_dump>
}
## -------------------------------------------------------
@ -31,16 +31,39 @@
## -------------------------------------------------------
## -------------------------------------------------------
#method(#class) basicNew
{
<primitive: #_basic_new>
self primitiveFailed.
}
#method(#class) basicNew: anInteger
{
<primitive: #_basic_new_with_size>
self primitiveFailed.
}
#method(#class) new
{
<primitive: #new>
self primitiveFailed.
| x |
x := self basicNew.
x initialize. "TODO: assess if it's good to call 'initialize' from new."
^x.
}
#method(#class) new: anInteger
{
<primitive: #newWithSize>
self primitiveFailed.
| x |
x := self basicNew: anInteger.
x initialize. "TODO: assess if it's good to call 'initialize' from new."
^x.
}
#method initialize
{
"a subclass may override this method."
^self.
}
## -------------------------------------------------------
@ -48,12 +71,12 @@
#method class
{
<primitive: #class>
<primitive: #_class>
}
#method(#class) class
{
<primitive: #class>
<primitive: #_class>
^Class
}
@ -62,37 +85,38 @@
#method basicSize
{
<primitive: #basicSize>
^0
<primitive: #_basic_size>
self primitiveFailed.
}
#method(#class) basicSize
{
<primitive: #_basic_size>
self primitiveFailed.
}
#method basicAt: anInteger
{
<primitive: #basicAt>
<primitive: #_basic_at>
self error: 'out of range'.
}
#method basicAt: anInteger put: anObject
{
<primitive: #basicAtPut>
<primitive: #_basic_at_put>
self error: 'out of range'.
}
#method(#class) basicSize
{
<primitive: #basicSize>
^0
}
#method(#class) basicAt: anInteger
{
<primitive: #basicAt>
<primitive: #_basic_at>
self error: 'out of range'.
}
#method(#class) basicAt: anInteger put: anObject
{
<primitive: #basicAtPut>
<primitive: #_basic_at_put>
self error: 'out of range'.
}
@ -103,12 +127,12 @@
{
"check if the receiver is identical to anObject.
this doesn't compare the contents"
<primitive: #identical>
<primitive: #_identical>
}
#method ~~ anObject
{
<primitive: #notIdentical>
<primitive: #_not_identical>
^(self == anObject) not.
}
@ -116,12 +140,12 @@
{
"check if the receiver is identical to anObject.
this doesn't compare the contents"
<primitive: #identical>
<primitive: #_identical>
}
#method(#class) ~~ anObject
{
<primitive: #notIdentical>
<primitive: #_not_identical>
^(self == anObject) not.
}

186
stix/kernel/Context.st Normal file
View File

@ -0,0 +1,186 @@
#class(#pointer) Context(Apex)
{
}
#class(#pointer) MethodContext(Context)
{
#dcl sender ip sp ntmprs method receiver home origin.
#method pc
{
^ip
}
#method pc: anInteger
{
ip := anInteger.
"sp := sp - 1." "whould this always work??? "
}
#method sp
{
^sp.
}
#method sp: anInteger
{
sp := anInteger.
}
#method pc: aPC sp: aSP
{
ip := aPC.
sp := aSP.
##sp := sp - 1.
}
}
#class(#pointer) BlockContext(Context)
{
#dcl caller ip sp ntmprs nargs source home origin.
#method fork
{
"crate a new process in the runnable state"
## TODO
}
#method newProcess
{
"create a new process in the suspended state"
## TODO
}
#method newProcessWith: anArray
{
"create a new process in the suspended state passing the elements
of anArray as block arguments"
## TODO
}
#method value
{
<primitive: #_block_value>
}
#method value: a
{
<primitive: #_block_value>
}
#method value: a value: b
{
<primitive: #_block_value>
}
#method value: a value: b value: c
{
<primitive: #_block_value>
}
#method ifTrue: aBlock
{
^(self value) ifTrue: aBlock.
}
#method ifFalse: aBlock
{
^(self value) ifFalse: aBlock.
}
#method ifTrue: trueBlock ifFalse: falseBlock
{
^(self value) ifTrue: trueBlock ifFalse: falseBlock
}
#method whileTrue: aBlock
{
## http://stackoverflow.com/questions/2500483/is-there-a-way-in-a-message-only-language-to-define-a-whiletrue-message-without
## ----------------------------------------------------------------------------
## ^(self value) ifTrue: [aBlock value. self whileTrue: aBlock].
## ----------------------------------------------------------------------------
## less block context before whileTrue: is recursively sent.
## whileTrue: is sent in a method context.
## (self value) ifFalse: [^nil].
## aBlock value.
## self whileTrue: aBlock.
## ----------------------------------------------------------------------------
## ----------------------------------------------------------------------------
| pc sp xsp |
sp := thisContext sp.
sp := sp - 1. "decrement sp by 1 becuase thisContext pushed above affects the sp method"
pc := thisContext pc.
self value ifFalse: [ ^nil "^self" ].
aBlock value.
##thisContext pc: pc - 3 sp: sp.
##thisContext pc: pc + 2 sp: sp.
thisContext pc: pc + 1 sp: sp.
## this +2 or - 3 above is dependent on the byte code instruction size used for 'store'
## +2 to skip STORE_INTO_TEMP(pc) and POP_STACKTOP.
## TODO: make it independent of the byte code size
## ----------------------------------------------------------------------------
## #<label>:
## thisContext pc: #<label> sp: sp.
##
## | pc |
## pc := thisContext pc.
## ^self value ifTrue: [aBlock value. thisContext pc: pc]
## ----------------------------------------------------------------------------
## self value ifTrue: [ aBlock value. thisContext restart. ].
}
#method pc
{
^ip
}
#method pc: anInteger
{
ip := anInteger.
}
#method sp
{
^sp
}
#method sp: anInteger
{
sp := anInteger.
}
#method restart
{
ip := source pc.
}
"------ TODO: -------------------------------------"
#method on: anError do: anExceptionBlock
{
"TODO: handle if anError is an ErrorSet .."
}
#method ensure: aBlock
{
}
#method ifCurtailed: aBlock
{
}
"------ TODO: -------------------------------------"
}

80
stix/kernel/Process.st Normal file
View File

@ -0,0 +1,80 @@
#class(#pointer) Process(Object)
{
#dcl sp state prev next.
#method prev
{
^self.prev.
}
#method next
{
^self.next.
}
#method next: aProcess
{
self.next := aProcess.
}
#method prev: aProcess
{
self.prev := aProcess.
}
}
#class ProcessScheduler(Object)
{
#dcl tally head tail active.
#method new
{
"instantiation is not allowed"
^nil. "TODO: raise an exception"
}
#method activeProcess
{
^self.active.
}
#method add: aProcess
{
<primitive: #_scheduler_add>
(self.tally = 0)
ifTrue: [
self.head := aProcess.
self.tail := aProcess.
self.tally := 1.
]
ifFalse: [
aProcess next: self.head.
self.head prev: aProcess.
self.head := aProcess.
self.tally := self.tally + 1.
].
}
#method remove: aProcess
{
"<primitive: #_scheduler_remove>"
"TODO: "
}
"
#method yield
{
<primitive: #processYield>
self primitiveFailed
}
#method enter: aContext
{
<primitive: #processEnter>
self primitiveFailed
}
"
}

View File

@ -29,41 +29,61 @@
#class Number(Magnitude)
{
#method add: aNumber
{
<primitive: #integerAdd>
}
#method + aNumber
{
<primitive: #integerAdd>
<primitive: #_integer_add>
self primitiveFailed.
}
#method - aNumber
{
<primitive: #integerSub>
<primitive: #_integer_sub>
self primitiveFailed.
}
#method * aNumber
{
<primitive: #integerMul>
<primitive: #_integer_mul>
self primitiveFailed.
}
#method = aNumber
{
<primitive: #integerEQ>
<primitive: #_integer_eq>
self primitiveFailed.
}
#method ~= aNumber
{
<primitive: #_integer_ne>
self primitiveFailed.
}
#method < aNumber
{
<primitive: #integerLT>
<primitive: #_integer_lt>
self primitiveFailed.
}
#method > aNumber
{
<primitive: #integerGT>
<primitive: #_integer_gt>
self primitiveFailed.
}
#method <= aNumber
{
<primitive: #_integer_le>
self primitiveFailed.
}
#method >= aNumber
{
<primitive: #_integer_ge>
self primitiveFailed.
}
#method to: end by: step do: aBlock
{
@ -100,169 +120,13 @@
#include 'Collection-Array.st'.
#include 'Collection-Set.st'.
#class(#pointer) Context(Apex)
{
}
#class(#pointer) MethodContext(Context)
{
#dcl sender ip sp ntmprs method receiver home origin.
#method pc
{
^ip
}
#method pc: anInteger
{
ip := anInteger.
"sp := sp - 1." "whould this always work??? "
}
#method sp
{
^sp.
}
#method sp: anInteger
{
sp := anInteger.
}
#method pc: aPC sp: aSP
{
ip := aPC.
sp := aSP.
##sp := sp - 1.
}
}
#class(#pointer) BlockContext(Context)
{
#dcl caller ip sp ntmprs nargs source home origin.
#method value
{
<primitive: #blockValue>
}
#method value: a
{
<primitive: #blockValue>
}
#method value: a value: b
{
<primitive: #blockValue>
}
#method value: a value: b value: c
{
<primitive: #blockValue>
}
#method whileTrue: aBlock
{
## http://stackoverflow.com/questions/2500483/is-there-a-way-in-a-message-only-language-to-define-a-whiletrue-message-without
## ----------------------------------------------------------------------------
## ^(self value) ifTrue: [aBlock value. self whileTrue: aBlock].
## ----------------------------------------------------------------------------
## less block context before whileTrue: is recursively sent.
## whileTrue: is sent in a method context.
## (self value) ifFalse: [^nil].
## aBlock value.
## self whileTrue: aBlock.
## ----------------------------------------------------------------------------
## ----------------------------------------------------------------------------
| pc sp xsp |
sp := thisContext sp.
sp := sp - 1. "decrement sp by 1 becuase thisContext pushed above affects the sp method"
pc := thisContext pc.
self value ifFalse: [ ^nil "^self" ].
aBlock value.
##thisContext pc: pc - 3 sp: sp.
##thisContext pc: pc + 2 sp: sp.
thisContext pc: pc + 1 sp: sp.
## this +2 or - 3 above is dependent on the byte code instruction size used for 'store'
## +2 to skip STORE_INTO_TEMP(pc) and POP_STACKTOP.
## TODO: make it independent of the byte code size
## ----------------------------------------------------------------------------
## #<label>:
## thisContext pc: #<label> sp: sp.
##
## | pc |
## pc := thisContext pc.
## ^self value ifTrue: [aBlock value. thisContext pc: pc]
## ----------------------------------------------------------------------------
## self value ifTrue: [ aBlock value. thisContext restart. ].
}
#method pc
{
^ip
}
#method pc: anInteger
{
ip := anInteger.
}
#method sp
{
^sp
}
#method sp: anInteger
{
sp := anInteger.
}
#method restart
{
ip := source pc.
}
"------ TODO: -------------------------------------"
#method on: anError do: anExceptionBlock
{
"TODO: handle if anError is an ErrorSet .."
}
#method ensure: aBlock
{
}
#method ifCurtailed: aBlock
{
}
"------ TODO: -------------------------------------"
}
#class(#pointer) CompiledMethod(Object)
{
#dcl owner preamble preamble_data_1 preamble_data_2 ntmprs nargs code source.
}
#class(#pointer) Process(Object)
{
#dcl state.
}
#include 'Context.st'.
#include 'Process.st'.
#class FFI(Object)
{
@ -313,23 +177,23 @@ f isNil ifTrue: [ self error: 'No such function' ].
#method privateOpen: aString
{
<primitive: #ffiOpen>
<primitive: #_ffi_open>
^nil. ## TODO: Error signal: 'can not open'
}
#method privateClose: aHandle
{
<primitive: #ffiClose>
<primitive: #_ffi_close>
}
#method privateCall: aSymbol withSig: aString withArgs: anArray
{
<primitive: #ffiCall>
<primitive: #_ffi_call>
}
#method privateGetSymbol: aString in: aHandle
{
<primitive: #ffiGetSym>
<primitive: #_ffi_getsym>
^nil.
}
}

View File

@ -240,9 +240,11 @@
## ffi call: #puts withSig: 'i|s' withArgs: #('hello world').
ffi close.
"
self abc.
self abc.
self abc.
"
FFI isNil dump.
FFI notNil dump.