added the equality(=) method to Array.
fixed the equality primitive handler to accept the subclasses of Semaphore/SemaphoreGroup
This commit is contained in:
@ -76,6 +76,29 @@ class(#pointer) Array(Collection)
|
||||
{
|
||||
0 priorTo: (anArray basicSize) do: [:i | self at: i put: (anArray at: i) ].
|
||||
}
|
||||
|
||||
method = anArray
|
||||
{
|
||||
| size i |
|
||||
if (self class ~~ anArray class) { ^false }.
|
||||
|
||||
size := self size.
|
||||
if (size ~~ anArray size) { ^false }.
|
||||
|
||||
i := 0.
|
||||
while (i < size)
|
||||
{
|
||||
if ((self at: i) ~= (anArray at: i)) { ^false }.
|
||||
i := i + 1.
|
||||
}.
|
||||
|
||||
^true.
|
||||
}
|
||||
|
||||
method ~= anArray
|
||||
{
|
||||
^(self = anArray) not
|
||||
}
|
||||
}
|
||||
|
||||
## -------------------------------------------------------------------------------
|
||||
|
@ -62,46 +62,11 @@ class Semaphore(Object)
|
||||
|
||||
var(#get,#set) _group := nil.
|
||||
|
||||
method(#class) forMutualExclusion
|
||||
{
|
||||
| sem |
|
||||
sem := self new.
|
||||
sem signal.
|
||||
^sem
|
||||
}
|
||||
|
||||
## ==================================================================
|
||||
|
||||
method(#primitive) signal.
|
||||
method(#primitive) wait.
|
||||
|
||||
(*
|
||||
TODO: timed wait...
|
||||
method waitWithTimeout: seconds
|
||||
{
|
||||
| s |
|
||||
s := Semaphore new.
|
||||
Processor signal: s after: seconds.
|
||||
self waitWithTimedSemaphore: s.
|
||||
|
||||
if (self.
|
||||
}
|
||||
|
||||
method waitWithTimeout: seconds and: nanoSeconds
|
||||
{
|
||||
<primitive: #_semaphore_wait>
|
||||
self primitiveFailed
|
||||
}
|
||||
*)
|
||||
|
||||
(* TODO: MIGRATE TO MUTEX...
|
||||
method critical: aBlock
|
||||
{
|
||||
self wait.
|
||||
^aBlock ensure: [ self signal ]
|
||||
}*)
|
||||
|
||||
|
||||
## ==================================================================
|
||||
|
||||
method heapIndex
|
||||
@ -145,8 +110,14 @@ class Mutex(Semaphore)
|
||||
^s.
|
||||
}
|
||||
|
||||
method lock { ^self wait }
|
||||
method unlock { ^self signal }
|
||||
(*
|
||||
TODO: how to prohibit wait and signal???
|
||||
method(#prohibited) wait.
|
||||
method(#prohibited) signal.
|
||||
*)
|
||||
|
||||
method lock { ^super wait }
|
||||
method unlock { ^super signal }
|
||||
|
||||
method critical: block
|
||||
{
|
||||
|
@ -49,6 +49,42 @@ class MyObject(Object)
|
||||
^true
|
||||
}
|
||||
|
||||
method(#class) test_mutex
|
||||
{
|
||||
| mtx sem v p q |
|
||||
|
||||
mtx := Mutex new.
|
||||
sem := Semaphore new.
|
||||
|
||||
p := 0.
|
||||
|
||||
[ mtx lock.
|
||||
v := 0.
|
||||
2000 timesRepeat: [v := v + 1. p := p + 1. ].
|
||||
q := v.
|
||||
mtx unlock.
|
||||
sem signal.
|
||||
] fork.
|
||||
|
||||
[ mtx critical: [
|
||||
v := 0.
|
||||
2000 timesRepeat: [v := v + 1. p := p + 1. ].
|
||||
q := v.
|
||||
].
|
||||
sem signal.
|
||||
] fork.
|
||||
|
||||
mtx lock.
|
||||
v := 0.
|
||||
2000 timesRepeat: [v := v + 1. p := p + 1. ].
|
||||
mtx unlock.
|
||||
|
||||
sem wait.
|
||||
sem wait.
|
||||
|
||||
^%( v, p ) ## v must be 2000, p must be 6000
|
||||
}
|
||||
|
||||
method(#class) main
|
||||
{
|
||||
| tc limit |
|
||||
@ -57,7 +93,8 @@ class MyObject(Object)
|
||||
## 0 - 4
|
||||
[ self proc1 == 100 ],
|
||||
[ Processor sleepFor: 2. self proc1 == 200 ],
|
||||
[ self test_semaphore_heap == true ]
|
||||
[ self test_semaphore_heap == true ],
|
||||
[ self test_mutex = #(2000 6000) ]
|
||||
).
|
||||
|
||||
limit := tc size.
|
||||
|
@ -67,16 +67,19 @@ class MyObject(TestObject)
|
||||
[(Apex isKindOf: Class) == true],
|
||||
[(Apex isKindOf: Apex) == true],
|
||||
[(SmallInteger isKindOf: Integer) == false],
|
||||
[(10 isKindOf: Integer) == true],
|
||||
[(10 isKindOf: 20) == false],
|
||||
[(SmallInteger isKindOf: SmallInteger) == false],
|
||||
[(Object isKindOf: SmallInteger) == false],
|
||||
|
||||
## 10-14
|
||||
[(10 isKindOf: Integer) == true],
|
||||
[(10 isKindOf: 20) == false],
|
||||
[([] isKindOf: BlockContext) == true],
|
||||
[([] isKindOf: MethodContext) == false],
|
||||
[([] isKindOf: Context) == true],
|
||||
|
||||
## 15-20
|
||||
[('string' isKindOf: String) == true],
|
||||
[(#symbol isKindOf: String) == true],
|
||||
|
||||
[('string' isKindOf: Symbol) == false],
|
||||
[(#symbol isKindOf: Symbol) == true]
|
||||
).
|
||||
|
Reference in New Issue
Block a user