330
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
341
|
3 |
All Rights Reserved
|
330
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
|
|
14 |
Object subclass:#RecursionLock
|
776
|
15 |
instanceVariableNames:'process sema'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Processes'
|
330
|
19 |
!
|
|
20 |
|
|
21 |
!RecursionLock class methodsFor:'documentation'!
|
|
22 |
|
699
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
|
|
36 |
!
|
|
37 |
|
330
|
38 |
documentation
|
|
39 |
"
|
|
40 |
like a Semaphore for mutual exclusion, but avoids the deadlock
|
|
41 |
if a critical region is reentered by the same process again.
|
341
|
42 |
I.e. allows reentering the critical region if the current process
|
|
43 |
is the one which did the original locking.
|
330
|
44 |
|
|
45 |
example:
|
|
46 |
|
776
|
47 |
|lock|
|
|
48 |
|
|
49 |
lock := RecursionLock new.
|
|
50 |
lock critical:[
|
|
51 |
Transcript showCr:'in lock ...'.
|
|
52 |
lock critical:[
|
|
53 |
Transcript showCr:'again ...'
|
|
54 |
]
|
|
55 |
]
|
|
56 |
|
|
57 |
in contrast to (wrong example):
|
330
|
58 |
|
776
|
59 |
|lock|
|
|
60 |
|
|
61 |
lock := Semaphore forMutualExclusion.
|
|
62 |
lock critical:[
|
|
63 |
Transcript showCr:'in lock ...'.
|
|
64 |
lock critical:[
|
|
65 |
'*** never reached - deadlock because sema is already locked ***'.
|
|
66 |
' (press CTRL-c and abort in the debugger)'.
|
|
67 |
Transcript showCr:'again ...'
|
|
68 |
]
|
|
69 |
]
|
330
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
|
73 |
!RecursionLock class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
new
|
|
76 |
^ self basicNew initialize
|
|
77 |
|
|
78 |
! !
|
|
79 |
|
699
|
80 |
!RecursionLock methodsFor:'private initialization'!
|
|
81 |
|
|
82 |
initialize
|
|
83 |
sema := Semaphore forMutualExclusion
|
|
84 |
! !
|
|
85 |
|
330
|
86 |
!RecursionLock methodsFor:'wait & signal'!
|
|
87 |
|
|
88 |
critical:aBlock
|
|
89 |
"evaluate aBlock as a critical region, but do not block,
|
|
90 |
if this lock is already held by the current process."
|
|
91 |
|
|
92 |
|active|
|
|
93 |
|
|
94 |
active := Processor activeProcess.
|
|
95 |
process == active ifTrue:[
|
341
|
96 |
aBlock value
|
330
|
97 |
] ifFalse:[
|
341
|
98 |
[
|
|
99 |
sema critical:[
|
|
100 |
process := active.
|
|
101 |
aBlock value
|
|
102 |
]
|
|
103 |
] valueNowOrOnUnwindDo:[
|
|
104 |
process := nil
|
|
105 |
]
|
330
|
106 |
].
|
|
107 |
! !
|
|
108 |
|
699
|
109 |
!RecursionLock class methodsFor:'documentation'!
|
330
|
110 |
|
699
|
111 |
version
|
776
|
112 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.7 1995-12-16 13:01:20 cg Exp $'
|
330
|
113 |
! !
|