2016-02-18 17:49:56 +00:00
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#class(#pointer) Process(Object)
|
|
|
|
{
|
2016-02-29 17:26:40 +00:00
|
|
|
#dcl initial_context current_context state sp prev next sem.
|
2015-10-19 06:16:43 +00:00
|
|
|
|
|
|
|
#method new
|
|
|
|
{
|
|
|
|
"instantiation is not allowed"
|
|
|
|
^nil. "TODO: raise an exception"
|
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method prev
|
|
|
|
{
|
|
|
|
^self.prev.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method next
|
|
|
|
{
|
|
|
|
^self.next.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method next: aProcess
|
|
|
|
{
|
|
|
|
self.next := aProcess.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method prev: aProcess
|
|
|
|
{
|
|
|
|
self.prev := aProcess.
|
|
|
|
}
|
2015-10-18 15:06:17 +00:00
|
|
|
|
|
|
|
#method resume
|
|
|
|
{
|
2016-02-19 15:52:56 +00:00
|
|
|
<primitive: #_process_resume>
|
|
|
|
self primitiveFailed
|
|
|
|
|
|
|
|
##^Processor resume: self.
|
2015-10-18 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 06:35:18 +00:00
|
|
|
#method terminate
|
|
|
|
{
|
|
|
|
<primitive: #_process_terminate>
|
|
|
|
self primitiveFailed
|
|
|
|
}
|
|
|
|
|
2016-02-19 15:52:56 +00:00
|
|
|
#method yield
|
|
|
|
{
|
|
|
|
<primitive: #_process_yield>
|
|
|
|
self primitiveFailed
|
|
|
|
}
|
|
|
|
|
2016-02-12 16:23:26 +00:00
|
|
|
#method sp
|
|
|
|
{
|
|
|
|
^sp.
|
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 17:49:56 +00:00
|
|
|
#class Semaphore(Object)
|
|
|
|
{
|
2016-03-16 02:27:18 +00:00
|
|
|
#dcl count waiting_head waiting_tail heapIndex fireTime.
|
2016-02-18 17:49:56 +00:00
|
|
|
|
|
|
|
#method initialize
|
|
|
|
{
|
2016-03-16 02:27:18 +00:00
|
|
|
self.count := 0.
|
|
|
|
self.heapIndex := 0.
|
|
|
|
self.fireTime := 0.
|
2016-02-18 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method signal
|
|
|
|
{
|
|
|
|
<primitive: #_semaphore_signal>
|
|
|
|
self primitiveFailed.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method wait
|
|
|
|
{
|
|
|
|
<primitive: #_semaphore_wait>
|
|
|
|
self primitiveFailed.
|
|
|
|
}
|
2016-03-16 02:27:18 +00:00
|
|
|
|
|
|
|
#method heapIndex
|
|
|
|
{
|
|
|
|
^heapIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
#method heapIndex: anIndex
|
|
|
|
{
|
|
|
|
heapIndex := anIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
#method fireTime
|
|
|
|
{
|
|
|
|
^fireTime
|
|
|
|
}
|
|
|
|
|
|
|
|
#method fireTime: anInteger
|
|
|
|
{
|
|
|
|
self.fireTime := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method youngerThan: aSemaphore
|
|
|
|
{
|
|
|
|
^self.fireTime < (aSemaphore fireTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#class SemaphoreHeap(Object)
|
|
|
|
{
|
|
|
|
#dcl arr size.
|
|
|
|
|
|
|
|
#method initialize
|
|
|
|
{
|
|
|
|
self.size := 0.
|
|
|
|
self.arr := Array new: 100.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method size
|
|
|
|
{
|
|
|
|
^self.size
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
#method at: anIndex
|
|
|
|
{
|
|
|
|
^self.arr at: anIndex.
|
|
|
|
}
|
|
|
|
|
2016-03-16 02:27:18 +00:00
|
|
|
#method insert: aSemaphore
|
|
|
|
{
|
|
|
|
self.size >= (self.arr size) ifTrue: [
|
|
|
|
| newarr newsize |
|
|
|
|
newsize := (self.arr size) * 2.
|
|
|
|
newarr := Array new: newsize.
|
|
|
|
newarr copy: self.arr.
|
|
|
|
self.arr := newarr.
|
|
|
|
].
|
|
|
|
|
|
|
|
self.size := self.size + 1.
|
|
|
|
self.arr at: self.size put: aSemaphore.
|
|
|
|
aSemaphore heapIndex: self.size.
|
|
|
|
|
|
|
|
^self siftUp: self.size.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method popTop
|
|
|
|
{
|
|
|
|
| top |
|
|
|
|
|
|
|
|
top := self.arr at: 1.
|
|
|
|
self deleteAt: 1.
|
|
|
|
^top
|
|
|
|
}
|
|
|
|
|
|
|
|
#method updateAt: anIndex
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#method deleteAt: anIndex
|
|
|
|
{
|
|
|
|
| item |
|
|
|
|
|
|
|
|
item := self.arr at: anIndex.
|
|
|
|
item heapIndex: -1.
|
|
|
|
|
|
|
|
(anIndex == self.size)
|
|
|
|
ifTrue: [
|
|
|
|
"the last item"
|
|
|
|
self.arr at: self.size put: nil.
|
|
|
|
self.size := self.size - 1
|
|
|
|
]
|
|
|
|
ifFalse: [
|
|
|
|
| xitem |
|
|
|
|
|
|
|
|
xitem := self.arr at: self.size.
|
|
|
|
self.arr at: anIndex put: xitem.
|
|
|
|
xitem heapIndex: anIndex.
|
|
|
|
self.arr at: self.size put: nil.
|
|
|
|
self.size := self.size - 1.
|
|
|
|
|
|
|
|
(xitem youngerThan: item)
|
|
|
|
ifTrue: [self siftUp: anIndex ]
|
|
|
|
ifFalse: [self siftDown: anIndex ]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#method parentIndex: anIndex
|
|
|
|
{
|
|
|
|
## ^(anIndex - 1) quo: 2
|
|
|
|
^anIndex quo: 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#method leftChildIndex: anIndex
|
|
|
|
{
|
|
|
|
## ^(anIndex * 2) + 1.
|
|
|
|
^(anIndex * 2).
|
|
|
|
}
|
|
|
|
|
|
|
|
#method rightChildIndex: anIndex
|
|
|
|
{
|
|
|
|
## ^(anIndex * 2) + 2.
|
|
|
|
^(anIndex * 2) + 1.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method siftUp: anIndex
|
|
|
|
{
|
2016-03-16 10:13:03 +00:00
|
|
|
| pindex cindex par item stop |
|
2016-03-16 02:27:18 +00:00
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
(anIndex <= 1) ifTrue: [ ^anIndex ].
|
2016-03-16 02:27:18 +00:00
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
pindex := anIndex.
|
|
|
|
item := self.arr at: anIndex.
|
2016-03-16 02:27:18 +00:00
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
stop := false.
|
|
|
|
[ stop ] whileFalse: [
|
2016-03-16 02:27:18 +00:00
|
|
|
|
|
|
|
cindex := pindex.
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
(cindex > 1)
|
|
|
|
ifTrue: [
|
|
|
|
pindex := self parentIndex: cindex.
|
|
|
|
par := self.arr at: pindex.
|
|
|
|
|
|
|
|
(item youngerThan: par)
|
|
|
|
ifTrue: [
|
|
|
|
## move the parent down
|
|
|
|
self.arr at: cindex put: par.
|
|
|
|
par heapIndex: cindex.
|
|
|
|
]
|
|
|
|
ifFalse: [ stop := true ].
|
|
|
|
]
|
|
|
|
ifFalse: [ stop := true ].
|
2016-03-16 02:27:18 +00:00
|
|
|
].
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
self.arr at: cindex put: item.
|
|
|
|
item heapIndex: cindex.
|
2016-03-16 02:27:18 +00:00
|
|
|
|
|
|
|
^cindex
|
|
|
|
}
|
|
|
|
|
|
|
|
#method siftDown: anIndex
|
|
|
|
{
|
|
|
|
| base capa cindex item |
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
base := (self.size quo: 2) + 1.
|
2016-03-16 02:27:18 +00:00
|
|
|
(anIndex > base) ifTrue: [^anIndex].
|
|
|
|
|
|
|
|
cindex := anIndex.
|
|
|
|
item := self.arr at: cindex.
|
|
|
|
|
|
|
|
[ cindex < base ] whileTrue: [
|
|
|
|
| left right younger xitem |
|
|
|
|
|
|
|
|
left := self leftChildIndex: cindex.
|
|
|
|
right := self rightChildIndex: cindex.
|
|
|
|
|
|
|
|
((right <= self.size) and: [(self.arr at: right) youngerThan: (self.arr at: left)])
|
|
|
|
ifTrue: [ younger := right ]
|
|
|
|
ifFalse: [ younger := left ].
|
|
|
|
|
|
|
|
xitem := self.arr at: younger.
|
|
|
|
(item youngerThan: xitem)
|
|
|
|
ifTrue: [
|
|
|
|
"break the loop"
|
|
|
|
base := anIndex
|
|
|
|
]
|
|
|
|
ifFalse: [
|
|
|
|
self.arr at: cindex put: xitem.
|
|
|
|
xitem heapIndex: cindex.
|
|
|
|
cindex := younger.
|
|
|
|
]
|
|
|
|
].
|
|
|
|
|
|
|
|
self.arr at: cindex put: item.
|
|
|
|
item heapIndex: cindex.
|
|
|
|
|
|
|
|
^cindex
|
|
|
|
}
|
2016-02-18 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#class ProcessScheduler(Object)
|
|
|
|
{
|
2016-03-16 02:27:18 +00:00
|
|
|
#dcl tally active runnable_head runnable_tail sem_heap.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method new
|
|
|
|
{
|
|
|
|
"instantiation is not allowed"
|
|
|
|
^nil. "TODO: raise an exception"
|
|
|
|
}
|
|
|
|
|
|
|
|
#method activeProcess
|
|
|
|
{
|
|
|
|
^self.active.
|
|
|
|
}
|
|
|
|
|
2015-10-22 02:47:25 +00:00
|
|
|
#method sleep: anInteger
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2015-10-22 02:47:25 +00:00
|
|
|
<primitive: #_processor_sleep>
|
2015-10-19 06:16:43 +00:00
|
|
|
self primitiveFailed.
|
2015-10-22 02:47:25 +00:00
|
|
|
}
|
2015-10-19 06:16:43 +00:00
|
|
|
|
2015-10-22 02:47:25 +00:00
|
|
|
#method resume: aProcess
|
|
|
|
{
|
|
|
|
<primitive: #_processor_schedule>
|
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
2015-10-22 02:47:25 +00:00
|
|
|
"The primitive does something like the following in principle:
|
2015-10-15 14:40:08 +00:00
|
|
|
(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.
|
2015-10-22 02:47:25 +00:00
|
|
|
].
|
|
|
|
"
|
2015-10-18 15:06:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method remove: aProcess
|
|
|
|
{
|
2015-10-22 02:47:25 +00:00
|
|
|
"<primitive: #_processor_remove>"
|
2015-10-15 14:40:08 +00:00
|
|
|
"TODO: "
|
2015-10-18 15:06:17 +00:00
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
"
|
|
|
|
#method yield
|
|
|
|
{
|
2015-10-22 02:47:25 +00:00
|
|
|
<primitive: #_processor_yield>
|
2015-10-15 14:40:08 +00:00
|
|
|
self primitiveFailed
|
|
|
|
}
|
|
|
|
"
|
2016-03-16 02:27:18 +00:00
|
|
|
|
|
|
|
#method signal: aSemaphore after: anInteger
|
|
|
|
{
|
|
|
|
self.sem_heap add: aSemaphore withTimeout: anInteger.
|
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|