|
Class: Monitor
Object
|
+--Monitor
- Package:
- stx:libbasic2
- Category:
- Kernel-Processes
- Version:
- rev:
1.28
date: 2024/04/24 08:47:44
- user: cg
- file: Monitor.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
Monitors (as used in Java) provide a functionality much like RecursionLocks,
but are not block based. This means that the monitor can be left at some
completely different and unrelated place from where it was acquired.
Therefore, monitors are not unwind-save
(i.e. a return or unwind while a monitor is locked,
can lead to a deadlock situation).
You have to care for unwind protection / cleanup yourself.
However, when waiting for a monitor, a check is made if the owning process
is still alive, and the monitor will be acquired eventually, if the owner is dead.
Notice:
This is an unused demo class - there is no WARRANTY.
It is not used by the system itself.
Smalltalkers should use Semaphores and RecursionLocks, which
are unwind-save.
copyrightCOPYRIGHT (c) 1996 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
accessing
-
deadProcessPollTime
-
when trying to acquire a locked monitor,
check if the owning process is dead (and forgot to release)
in this cycle time
-
deadProcessPollTime: milliSecondsOrNilForDefault
-
when trying to acquire a locked monitor,
check if the owning process is dead (and forgot to release)
in this cycle time
instance creation
-
new
-
return a new unowned monitor
enter & leave
-
critical: aBlock
-
a critical section.
Executes a block as a critical section, secured by the receiver.
-
enter
-
enter the monitor
-
exit
-
exit the monitor
-
fakeEnter: aProcess count: additionalCount
-
(fake-)enter the monitor, without blocking.
Raises an error, if the monitor is not free and owned by another process
initialization
-
initialize
-
(comment from inherited method)
just to ignore initialize to objects which do not need it
queries
-
count
-
-
isFree
-
return true, if the monitor is free
(i.e. noone waits and count is zero)
-
owningProcess
-
return the monitor's owner; or nil, if it's free
|mon p1 p2 p3|
mon := Monitor new.
p1 := [
10 timesRepeat:[
Delay waitForSeconds:0.3.
mon enter.
Transcript showCR:'p1 got it'.
Delay waitForSeconds:0.3.
Transcript showCR:'p1 leaves'.
mon exit
]
] fork.
p2 := [
20 timesRepeat:[
Delay waitForSeconds:0.2.
mon enter.
Transcript showCR:'p2 got it'.
Delay waitForSeconds:0.2.
Transcript showCR:'p2 leaves'.
mon exit
]
] fork.
p3 := [
30 timesRepeat:[
Delay waitForSeconds:0.1.
mon enter.
Transcript showCR:'p3 got it'.
Delay waitForSeconds:0.1.
Transcript showCR:'p3 leaves'.
mon exit
]
] fork.
| same using critical blocks
|mon p1 p2 p3|
mon := Monitor new.
p1 := [
10 timesRepeat:[
Delay waitForSeconds:0.3.
mon critical:[
Transcript showCR:'p1 got it'.
Delay waitForSeconds:0.3.
Transcript showCR:'p1 leaves'.
]
]
] fork.
p2 := [
20 timesRepeat:[
Delay waitForSeconds:0.2.
mon critical:[
Transcript showCR:'p2 got it'.
Delay waitForSeconds:0.2.
Transcript showCR:'p2 leaves'.
].
]
] fork.
p3 := [
30 timesRepeat:[
Delay waitForSeconds:0.1.
mon critical:[
Transcript showCR:'p3 got it'.
Delay waitForSeconds:0.1.
Transcript showCR:'p3 leaves'.
]
]
] fork.
|
|