|
Class: Monitor
Object
|
+--Monitor
- Package:
- stx:libbasic2
- Category:
- Kernel-Processes
- Version:
- rev:
1.23
date: 2017/08/09 12:13:35
- user: cg
- file: Monitor.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
Monitors (as used in Java) provide a functionality much like RecursionLocks,
but are not block based.
Therefore, monitors are not unwind-save (i.e. a return or unwind while a
monitor is locked, will lead to a deadlock situation).
You have to care for unwind protection yourself.
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.
RecursionLock
Semaphore
Delay
SharedQueue
Block
instance creation
-
new
-
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 monitors owner; or nil, if it's free
|mon p1 p2 p3|
mon := Monitor new.
p1 := [
10 timesRepeat:[
Delay waitForSeconds:0.3.
mon enter.
'p1 got it' printNL.
Delay waitForSeconds:0.3.
'p1 leaves' printNL.
mon exit
]
] fork.
p2 := [
20 timesRepeat:[
Delay waitForSeconds:0.2.
mon enter.
'p2 got it' printNL.
Delay waitForSeconds:0.2.
'p2 leaves' printNL.
mon exit
]
] fork.
p3 := [
30 timesRepeat:[
Delay waitForSeconds:0.1.
mon enter.
'p3 got it' printNL.
Delay waitForSeconds:0.1.
'p3 leaves' printNL.
mon exit
]
] fork.
|
|