added Semaphore>>forMutualExclusion

This commit is contained in:
hyunghwan.chung 2016-05-18 15:10:00 +00:00
parent 04ccebe9b4
commit 50776d945f
2 changed files with 18 additions and 14 deletions

View File

@ -68,6 +68,14 @@
{
#dcl count waiting_head waiting_tail heapIndex fireTimeSec fireTimeNsec.
#method(#class) forMutualExclusion
{
| sem |
sem := self new.
sem signal.
^sem
}
#method initialize
{
self.count := 0.
@ -104,11 +112,8 @@
#method critical: aBlock
{
## TODO: implement this
"
self wait.
^aBlock ensure: [ self signal ]
"
}
## ==================================================================

View File

@ -48,26 +48,25 @@
#method(#class) main
{
## THIS CRASHES VM. PLEASE CHECK. CRASHES WHEN REPETITION IS 1000
|t1 t2 s1 s2 s3|
'START OF MAIN' dump.
s1 := Semaphore new.
s2 := Semaphore new.
s2 := Semaphore forMutualExclusion.
t1 := [
100 timesRepeat: ['BLOCK #1' dump].
##s2 critical: [
## 10 timesRepeat: ['BLOCK #1' dump ]
##]
##1000 timesRepeat: ['BLOCK #1' dump].
s2 critical: [
10000 timesRepeat: ['BLOCK #1' dump ].
Exception signal: 'Raised Exception at process t1'.
]
] newProcess.
t2 := [
100 timesRepeat: ['BLOCK #2' dump].
##s2 critical: [
## 10 timesRepeat: ['BLOCK #2' dump. ]
##].
##1000 timesRepeat: ['BLOCK #2' dump].
s2 critical: [
10000 timesRepeat: ['BLOCK #2' dump. ]
].
s1 signal.
] newProcess.